View previous topic :: View next topic |
Author |
Message |
avidseeker n00b
Joined: 28 May 2023 Posts: 24
|
Posted: Sun Feb 18, 2024 8:12 am Post subject: Why /etc/profile needs to be sourced after chroot? |
|
|
Not directly related to Gentoo, but the way I understand it is that /etc/profile is sourced on interactive shell session (bash) automatically.
This is what I'm used to (at least coming from Arch Linux). I am wondering why is the behavior different? |
|
Back to top |
|
|
Zucca Moderator
Joined: 14 Jun 2007 Posts: 3348 Location: Rasi, Finland
|
Posted: Sun Feb 18, 2024 9:06 am Post subject: |
|
|
By sourcing /etc/profile you'll set all the necessary environment variables (among other things) inside the chroot.
If you know shell scripting, then see what's inside the /etc/profile and learn. ;) _________________ ..: Zucca :..
Gentoo IRC channels reside on Libera.Chat.
--
Quote: | I am NaN! I am a man! |
|
|
Back to top |
|
|
avidseeker n00b
Joined: 28 May 2023 Posts: 24
|
Posted: Sun Feb 18, 2024 9:11 am Post subject: |
|
|
Yes I know about the environment variables it sets. My questions is that since the first command in the chroot is bash, shouldn't bash automatically source /etc/profile?
More concretely, I have two partitions one with an Arch system and one with a Gentoo system. Why is chrooting to Arch automatically sources /etc/profile, while chrooting to Gentoo doesn't? |
|
Back to top |
|
|
Banana Veteran
Joined: 21 May 2004 Posts: 1395 Location: Germany
|
|
Back to top |
|
|
avidseeker n00b
Joined: 28 May 2023 Posts: 24
|
Posted: Sat Feb 24, 2024 10:21 pm Post subject: |
|
|
This is the path after su -:
Code: | root ~ # echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin:/usr/lib/llvm/17/bin |
I'm trying to understand what file is setting the path here.
Code: | root ~ # echo ~/.bash*
/root/.bash_history |
Code: |
root ~ # cat /etc/profile
if [ -n "${BASH_VERSION-}" ] ; then
# Newer bash ebuilds include /etc/bash/bashrc which will setup PS1
# including color. We leave out color here because not all
# terminals support it.
if [ -f /etc/bash/bashrc ] ; then
# Bash login shells run only /etc/profile
# Bash non-login shells run only /etc/bash/bashrc
# Since we want to run /etc/bash/bashrc regardless, we source it
# from here. It is unfortunate that there is no way to do
# this *after* the user's .bash_profile runs (without putting
# it in the user's dot-files), but it shouldn't make any
# difference.
. /etc/bash/bashrc
else
PS1='\u@\h \w \$ '
fi
fi
|
But checking /etc/bash/bashrc, I don't see any relevant function updating PATH.
In Arch, there is append_path function in /etc/profile. |
|
Back to top |
|
|
grknight Retired Dev
Joined: 20 Feb 2015 Posts: 1666
|
Posted: Sun Feb 25, 2024 12:50 am Post subject: |
|
|
A lot of the "Gentoo magic" happens in /etc/profile.env which is created by our env-update tool. This is read in much earlier in /etc/profile:
Code: | # Load environment settings from profile.env, which is created by
# env-update from the files in /etc/env.d
if [ -e /etc/profile.env ] ; then
. /etc/profile.env
fi | env-update reads in lots of settings from /etc/env.d and compiles them into /etc/profile.env or /etc/ld.so.conf as needed. |
|
Back to top |
|
|
avidseeker n00b
Joined: 28 May 2023 Posts: 24
|
Posted: Sun Feb 25, 2024 5:06 am Post subject: |
|
|
Oh, I overlooked this. This clears some things up.
Now back to my original question. Doesn't bash source /etc/profile by default? which sources /etc/profile.env?
Then why is /etc/profile needed to be sourced manually?
The gnu manual says that:
Quote: | When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile. |
The only way I can think of is that bash doesn't regard chrooting as login? |
|
Back to top |
|
|
Zucca Moderator
Joined: 14 Jun 2007 Posts: 3348 Location: Rasi, Finland
|
Posted: Sun Feb 25, 2024 10:38 am Post subject: |
|
|
avidseeker wrote: | The gnu manual says that:
Quote: | When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile. |
The only way I can think of is that bash doesn't regard chrooting as login? | Interesting. I've never thought of this. I wonder if Code: | chroot /mnt/gentoo /bin/bash -l | ... forces sourcing of /etc/profile. _________________ ..: Zucca :..
Gentoo IRC channels reside on Libera.Chat.
--
Quote: | I am NaN! I am a man! |
|
|
Back to top |
|
|
Goverp Advocate
Joined: 07 Mar 2007 Posts: 2011
|
Posted: Sun Feb 25, 2024 1:43 pm Post subject: |
|
|
Following the above, I've tweaked an old script I use to build my chroot. It's benefit is I can write things like the following, and just edit the list of mounts:
Code: | # Mounts below must be absolute, starting with a / . They can have source=mountpoint
mounts="/proc /sys /dev /run /usr/local/bin /boot /usr/src /lib/modules /usr/local/overlay /var/cache/binpkgs /var/cache/distfiles /var/db/repos"
chroot=~packager/chroot
path="${0%/*}" # The build-chroot script is in the same directory as this script
"$path"/build-chroot "$chroot" $mounts |
build-chroot:
Code: | #! /bin/sh
# Recursively bind-mount a series of directories within a target directory
# then chroot into that target directory,
# and after exiting the chroot, umount all the bind mounts
# so as to leave the mount enviroment back in its original state
# Syntax : build-chroot <target directory> <directories to be recusively bind-mounted>
# N. B. the directories to be mounted MUST be specified as absolute paths
# goverp 7 Mar 2020
# updated 25 Feb 2024 to start bash with "_l" to cause it to execute /etc/profile - thanks Zucca
chroot="$1"
shift
mounts="$*"
# $umounts will be the list of directories mounted, but in reverse order
umounts=""
# Recursively bind-mount the directories, and make them rslaves to keep changes within the chroot
for dir in $mounts
do
target="${dir#*=}"
if [ "$target" != "$dir" ]
then dir="${dir%%=$target}"
fi
target="${chroot}/${target}"
mount --rbind "$dir" "$target"
mount --make-rslave "$target"
umounts="$target $umounts"
done
# Enter the chroot environment
WHENCE="$chroot" chroot "$chroot" /bin/bash -l
# Unmount the mounted directories in reverse order, handling submounts
for target in $umounts
do
umount -R "$target"
done |
I also have a ~/.bash_profile in the chroot that
- does a cd ~ so I end up in my chroot's root home directory instead of "/";
- alters the prompt so I know I'm in the chroot; and
- lets me set environment variable CHROOT_COMMAND if I want to execute something:
~/.bash_profile
Code: | #!/bin/sh
cd ~
PS1="CHROOT ${WHENCE} \u \w #"
[ -x "$CHROOT_COMMAND" ] && $CHROOT_COMMAND |
_________________ Greybeard |
|
Back to top |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21651
|
Posted: Sun Feb 25, 2024 4:37 pm Post subject: |
|
|
Goverp wrote: | Code: | # and after exiting the chroot, umount all the bind mounts |
| Depending on your use case, you might be able to simplify this by using a mount namespace for the chroot, so that the mounts are automatically cleaned out when the last process from the chroot exits. This also avoids them showing up in /proc/mounts for non-chroot processes, which may make the output cleaner. To use a mount namespace, run the entire chroot-building script under /usr/bin/unshare -m. See man unshare for details.
You can also rig the build-chroot script to re-invoke itself under unshare if you want to keep the caller simpler. |
|
Back to top |
|
|
avidseeker n00b
Joined: 28 May 2023 Posts: 24
|
Posted: Mon Feb 26, 2024 9:00 pm Post subject: |
|
|
Goverp wrote: | Following the above, I've tweaked an old script I use to build my chroot. It's benefit is I can write things like the following, and just edit the list of mounts:
|
I'm still using the convenient arch-chroot wrapper script from arch-install-scripts. The script also takes care of umounting.
You might find it interesting and useful for adapting it to your script. |
|
Back to top |
|
|
avidseeker n00b
Joined: 28 May 2023 Posts: 24
|
Posted: Mon Feb 26, 2024 9:07 pm Post subject: |
|
|
Zucca wrote: | Interesting. I've never thought of this. I wonder if Code: | chroot /mnt/gentoo /bin/bash -l | ... forces sourcing of /etc/profile. |
Here are my findings:
Code: |
[root@Host ~]# chroot /mnt /bin/bash
Host / # which ls
which: no ls in (/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl)
Host / # echo $PATH | tr : "\n"
/usr/local/sbin
/usr/local/bin
/usr/bin
/usr/lib/jvm/default/bin
/usr/bin/site_perl
/usr/bin/vendor_perl
/usr/bin/core_perl |
However,
Code: |
[root@Host ~]# chroot /mnt /bin/bash -l
Host / # which ls
/bin/ls
Host / # echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin:/usr/lib/llvm/17/bin
|
If /bin/bash without --login doesn't load /etc/profile, then what is setting the PATH? |
|
Back to top |
|
|
|