diff options
| author | emmett1 <emmett1.2miligrams@protonmail.com> | 2024-06-12 23:37:45 +0800 |
|---|---|---|
| committer | emmett1 <emmett1.2miligrams@protonmail.com> | 2024-06-12 23:37:45 +0800 |
| commit | dfd4934a783d991b31063171f9e454a218e431b1 (patch) | |
| tree | a43138940b7efbb5a28e9797d87b0349aaf20685 /apkg-chroot | |
| parent | b52a73a431aec49e791b1a03be38bb8d643751db (diff) | |
| download | autils-dfd4934a783d991b31063171f9e454a218e431b1.tar.gz autils-dfd4934a783d991b31063171f9e454a218e431b1.zip | |
updates and added apkg-chroot and apkg-orphan
Diffstat (limited to 'apkg-chroot')
| -rwxr-xr-x | apkg-chroot | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/apkg-chroot b/apkg-chroot new file mode 100755 index 0000000..0e44577 --- /dev/null +++ b/apkg-chroot @@ -0,0 +1,105 @@ +#!/bin/sh +# +# script to enter chroot + +printhelp() { + cat << EOF + +Usage: + $(basename $0) <chroot-dir> [command] + +If 'command' is unspecified, ${0##*/} will launch /bin/sh. + +EOF +} + +msgerr() { + echo "ERROR: $*" +} + +unmount() { + while [ "$(mount | awk '{print $3}' | grep -x $1)" ]; do + umount $1 2>/dev/null || : + done +} + +umountall() { + for i in $(mount | awk '{print $3}' | grep $TARGET | tac); do + unmount $i + done +} + +[ "$(id -u)" = "0" ] || { + msgerr "$(basename $0) need root access!" + printhelp + exit 1 +} + +TARGET=$1 +trap umountall 1 2 3 15 + +[ "$1" ] || { + msgerr "Please set directory for chroot!" + printhelp + exit 1 +} + +[ -d "$TARGET" ] || { + msgerr "Directory '$TARGET' not exist!" + printhelp + exit 1 +} + +shift + +if [ ! "$1" ]; then + CMD="/bin/sh" +else + CMD=$* +fi + +if [ -e /sys/firmware/efi/systab ]; then + EFI_SYSTEM=1 +fi + +mount --bind /dev $TARGET/dev +mount -t devpts devpts $TARGET/dev/pts -o gid=5,mode=620 +mount -t proc proc $TARGET/proc +mount -t sysfs sysfs $TARGET/sys +if [ -n "$EFI_SYSTEM" ]; then + mount --bind /sys/firmware/efi/efivars $TARGET/sys/firmware/efi/efivars +fi +mount -t tmpfs tmpfs $TARGET/run + +if [ -h $TARGET/dev/shm ]; then + mkdir -p $TARGET/$(readlink $TARGET/dev/shm) +fi + +[ -f $TARGET/etc/resolv.conf ] && { + backupresolvconf=1 + mv $TARGET/etc/resolv.conf $TARGET/etc/resolv.conf.tmp +} +cp -L /etc/resolv.conf $TARGET/etc + +chroot "$TARGET" /usr/bin/env -i \ +HOME=/root \ +TERM="$TERM" \ +PS1='\u:\w\$ ' \ +PATH=/bin:/usr/bin:/sbin:/usr/sbin $CMD + +retval=$? + +[ "$backupresolvconf" = 1 ] && { + mv $TARGET/etc/resolv.conf.tmp $TARGET/etc/resolv.conf +} + +unmount $TARGET/dev/pts +unmount $TARGET/dev +unmount $TARGET/run +unmount $TARGET/proc +if [ -n "$EFI_SYSTEM" ]; then + unmount $TARGET/sys/firmware/efi/efivars +fi +unmount $TARGET/sys + +exit $retval |