
Code: Select all
init=/sbin/openrc-init
-systemd -logind -elogind seatdI am NaN! I am a man!


Code: Select all
root ~ # echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin:/usr/lib/llvm/17/binCode: Select all
root ~ # echo ~/.bash*
/root/.bash_historyCode: 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
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
The only way I can think of is that bash doesn't regard chrooting as login?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.
Interesting. I've never thought of this. I wonder ifavidseeker wrote:The gnu manual says that:The only way I can think of is that bash doesn't regard chrooting as login?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.
Code: Select all
chroot /mnt/gentoo /bin/bash -lCode: Select all
init=/sbin/openrc-init
-systemd -logind -elogind seatdI am NaN! I am a man!
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" $mountsCode: 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"
doneCode: Select all
#!/bin/sh
cd ~
PS1="CHROOT ${WHENCE} \u \w #"
[ -x "$CHROOT_COMMAND" ] && $CHROOT_COMMANDDepending 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.Goverp wrote:Code: Select all
# and after exiting the chroot, umount all the bind mounts

I'm still using the convenient arch-chroot wrapper script from arch-install-scripts. The script also takes care of umounting.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:

Here are my findings:Zucca wrote:Interesting. I've never thought of this. I wonder if... forces sourcing of /etc/profile.Code: Select all
chroot /mnt/gentoo /bin/bash -l
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_perlCode: 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