Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Why /etc/profile needs to be sourced after chroot?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Installing Gentoo
View previous topic :: View next topic  
Author Message
avidseeker
n00b
n00b


Joined: 28 May 2023
Posts: 24

PostPosted: Sun Feb 18, 2024 8:12 am    Post subject: Why /etc/profile needs to be sourced after chroot? Reply with quote

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
View user's profile Send private message
Zucca
Moderator
Moderator


Joined: 14 Jun 2007
Posts: 3348
Location: Rasi, Finland

PostPosted: Sun Feb 18, 2024 9:06 am    Post subject: Reply with quote

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
View user's profile Send private message
avidseeker
n00b
n00b


Joined: 28 May 2023
Posts: 24

PostPosted: Sun Feb 18, 2024 9:11 am    Post subject: Reply with quote

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
View user's profile Send private message
Banana
Veteran
Veteran


Joined: 21 May 2004
Posts: 1395
Location: Germany

PostPosted: Sun Feb 18, 2024 9:14 am    Post subject: Reply with quote

Then you have to check if there is a difference in the startup files: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
_________________
My personal space
My delta-labs.org snippets do expire

PFL - Portage file list - find which package a file or command belongs to.
Back to top
View user's profile Send private message
avidseeker
n00b
n00b


Joined: 28 May 2023
Posts: 24

PostPosted: Sat Feb 24, 2024 10:21 pm    Post subject: Reply with quote

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
View user's profile Send private message
grknight
Retired Dev
Retired Dev


Joined: 20 Feb 2015
Posts: 1666

PostPosted: Sun Feb 25, 2024 12:50 am    Post subject: Reply with quote

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
View user's profile Send private message
avidseeker
n00b
n00b


Joined: 28 May 2023
Posts: 24

PostPosted: Sun Feb 25, 2024 5:06 am    Post subject: Reply with quote

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
View user's profile Send private message
Zucca
Moderator
Moderator


Joined: 14 Jun 2007
Posts: 3348
Location: Rasi, Finland

PostPosted: Sun Feb 25, 2024 10:38 am    Post subject: Reply with quote

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
View user's profile Send private message
Goverp
Advocate
Advocate


Joined: 07 Mar 2007
Posts: 2011

PostPosted: Sun Feb 25, 2024 1:43 pm    Post subject: Reply with quote

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
  1. does a cd ~ so I end up in my chroot's root home directory instead of "/";
  2. alters the prompt so I know I'm in the chroot; and
  3. 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
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21651

PostPosted: Sun Feb 25, 2024 4:37 pm    Post subject: Reply with quote

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
View user's profile Send private message
avidseeker
n00b
n00b


Joined: 28 May 2023
Posts: 24

PostPosted: Mon Feb 26, 2024 9:00 pm    Post subject: Reply with quote

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
View user's profile Send private message
avidseeker
n00b
n00b


Joined: 28 May 2023
Posts: 24

PostPosted: Mon Feb 26, 2024 9:07 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Installing Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum