Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Installing Gentoo
  • Search

Why /etc/profile needs to be sourced after chroot?

Having problems with the Gentoo Handbook? If you're still working your way through it, or just need some info before you start your install, this is the place. All other questions go elsewhere.
Post Reply
Advanced search
12 posts • Page 1 of 1
Author
Message
avidseeker
n00b
n00b
Posts: 25
Joined: Sun May 28, 2023 7:00 am
Contact:
Contact avidseeker
Website

Why /etc/profile needs to be sourced after chroot?

  • Quote

Post by avidseeker » Sun Feb 18, 2024 8:12 am

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?
Top
Zucca
Administrator
Administrator
User avatar
Posts: 4703
Joined: Thu Jun 14, 2007 10:31 pm
Location: Rasi, Finland
Contact:
Contact Zucca
Website

  • Quote

Post by Zucca » Sun Feb 18, 2024 9:06 am

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 :..

Code: Select all

0100100100100000011000010110110100100000
0100111001100001010011100010000100100000
0100100100100000011000010110110100100000
0110000100100000011011010110000101101110
00100001
Top
avidseeker
n00b
n00b
Posts: 25
Joined: Sun May 28, 2023 7:00 am
Contact:
Contact avidseeker
Website

  • Quote

Post by avidseeker » Sun Feb 18, 2024 9:11 am

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?
Top
Banana
Administrator
Administrator
User avatar
Posts: 2397
Joined: Fri May 21, 2004 12:02 pm
Location: Germany
Contact:
Contact Banana
Website

  • Quote

Post by Banana » Sun Feb 18, 2024 9:14 am

Then you have to check if there is a difference in the startup files: https://www.gnu.org/software/bash/manua ... Files.html
Forum Guidelines

PFL - Portage file list - find which package a file or command belongs to.
My delta-labs.org snippets do expire
Top
avidseeker
n00b
n00b
Posts: 25
Joined: Sun May 28, 2023 7:00 am
Contact:
Contact avidseeker
Website

  • Quote

Post by avidseeker » Sat Feb 24, 2024 10:21 pm

This is the path after su -:

Code: Select all

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: Select all

root ~ # echo ~/.bash*
/root/.bash_history

Code: Select all

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.
Top
grknight
Retired Dev
Retired Dev
Posts: 2565
Joined: Fri Feb 20, 2015 9:36 pm

  • Quote

Post by grknight » Sun Feb 25, 2024 12:50 am

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: Select all

# 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.
Top
avidseeker
n00b
n00b
Posts: 25
Joined: Sun May 28, 2023 7:00 am
Contact:
Contact avidseeker
Website

  • Quote

Post by avidseeker » Sun Feb 25, 2024 5:06 am

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:
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?
Top
Zucca
Administrator
Administrator
User avatar
Posts: 4703
Joined: Thu Jun 14, 2007 10:31 pm
Location: Rasi, Finland
Contact:
Contact Zucca
Website

  • Quote

Post by Zucca » Sun Feb 25, 2024 10:38 am

avidseeker wrote:The gnu manual says that:
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: Select all

chroot /mnt/gentoo /bin/bash -l
... forces sourcing of /etc/profile.
..: Zucca :..

Code: Select all

0100100100100000011000010110110100100000
0100111001100001010011100010000100100000
0100100100100000011000010110110100100000
0110000100100000011011010110000101101110
00100001
Top
Goverp
Advocate
Advocate
User avatar
Posts: 2404
Joined: Wed Mar 07, 2007 6:41 pm

  • Quote

Post by Goverp » Sun Feb 25, 2024 1:43 pm

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: Select all

# 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: Select all

#! /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: Select all

#!/bin/sh
cd ~

PS1="CHROOT ${WHENCE} \u \w #"

[ -x "$CHROOT_COMMAND" ] && $CHROOT_COMMAND
Greybeard
Top
Hu
Administrator
Administrator
Posts: 24403
Joined: Tue Mar 06, 2007 5:38 am

  • Quote

Post by Hu » Sun Feb 25, 2024 4:37 pm

Goverp wrote:

Code: Select all

# 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.
Top
avidseeker
n00b
n00b
Posts: 25
Joined: Sun May 28, 2023 7:00 am
Contact:
Contact avidseeker
Website

  • Quote

Post by avidseeker » Mon Feb 26, 2024 9:00 pm

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.
Top
avidseeker
n00b
n00b
Posts: 25
Joined: Sun May 28, 2023 7:00 am
Contact:
Contact avidseeker
Website

  • Quote

Post by avidseeker » Mon Feb 26, 2024 9:07 pm

Zucca wrote:Interesting. I've never thought of this. I wonder if

Code: Select all

chroot /mnt/gentoo /bin/bash -l
... forces sourcing of /etc/profile.
Here are my findings:

Code: Select all

[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: Select all

[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?
Top
Post Reply

12 posts • Page 1 of 1

Return to “Installing Gentoo”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic