eth0s/rootfs/sbin/lfs-chroot

90 lines
1.3 KiB
Text
Raw Permalink Normal View History

2019-10-16 17:23:02 +08:00
#!/bin/bash
printhelp() {
cat << EOF
Usage:
$(basename $0) <newroot> <command>
(omit command to chroot only)
EOF
}
msgerr() {
echo -e "ERROR: $@"
}
if [ "$UID" != "0" ]; then
msgerr "$(basename $0) need root access!"
printhelp
exit 1
fi
LFS=$1
if [ -z $1 ]; then
msgerr "Please set directory for chroot!"
printhelp
exit 1
fi
if [ ! -d $LFS ]; then
msgerr "Directory '$LFS' not exist"
printhelp
exit 1
fi
shift
CMD=$@
if [ -z $2 ]; then
CMD="/bin/bash --login"
fi
if [ -e /sys/firmware/efi/systab ]; then
EFI_SYSTEM=1
fi
pushd $LFS &>/dev/null
mount --bind /dev $LFS/dev
mount -t devpts devpts $LFS/dev/pts -o gid=5,mode=620
mount -t proc proc $LFS/proc
mount -t sysfs sysfs $LFS/sys
if [ -n "$EFI_SYSTEM" ]; then
mount --bind /sys/firmware/efi/efivars $LFS/sys/firmware/efi/efivars
fi
mount -t tmpfs tmpfs $LFS/run
if [ -h $LFS/dev/shm ]; then
mkdir -p $LFS/$(readlink $LFS/dev/shm)
fi
cp -L /etc/resolv.conf $LFS/etc
2019-11-10 17:50:36 +08:00
echo ":: entering chroot to $LFS."
2019-10-16 17:23:02 +08:00
chroot "$LFS" /usr/bin/env -i \
HOME=/root \
TERM="$TERM" \
PS1='\u:\w\$ ' \
PATH=/bin:/usr/bin:/sbin:/usr/sbin $CMD
retval=$?
popd &>/dev/null
umount $LFS/dev/pts
umount $LFS/dev
umount $LFS/run
umount $LFS/proc
if [ -n "$EFI_SYSTEM" ]; then
umount $LFS/sys/firmware/efi/efivars
fi
umount $LFS/sys
2019-11-10 17:50:36 +08:00
echo ":: chroot exited."
2019-10-16 17:23:02 +08:00
exit $retval