Skip to content

Instantly share code, notes, and snippets.

@jsarenik jsarenik/busyroot.sh
Last active Mar 31, 2017

Embed
What would you like to do?
chroot hack
#!/bin/sh
CHROOT=${1:-"myroot"}
ARCH=${2:-"armv5l"}
test -n "$UNSHARE" || {
export UNSHARE="unshare --fork --pid --mount"
exec $UNSHARE $0 $CHROOT
}
mydir() {
test -d $1 || mkdir -p $1
}
mountem() {
#############################
mydir $CHROOT
export CHROOT=$(cd $CHROOT; pwd)
mydir $CHROOT/bin
./busybox-$ARCH --install $CHROOT/bin
mydir $CHROOT/proc
mount -v -t proc proc -o rw,nosuid,nodev,noexec,relatime $CHROOT/proc
#############################
}
mountem
chroot $CHROOT
echo
mount | grep $CHROOT | awk '{print $3}' | while read mntpoint;
do umount -v $mntpoint; done;
#!/bin/sh
test -n "$UNSHARE" || {
test $(id -u) -eq 0 || { echo "You have to be root!"; exit 1; }
basename=${0##*/}
#trap '' INT QUIT TERM EXIT
export CHROOT=$(cd $1; pwd); shift
test -d "$CHROOT" || { echo Directory $CHROOT does not exist.; exit 1; }
export COMMAND=${*:-'/bin/su -'}
# /etc/resolv.conf, /etc/hosts, /etc/fstab, /etc/passwd,
# /etc/group, /etc/shadow, /etc/ld.so.conf, /etc/scsi_id.config
cpfromsys() { F=$1; cp $F $CHROOT/$F; }
F=$CHROOT/etc/sysconfig; test -d $F && touch $F/network
cpfromsys /etc/hosts
cpfromsys /etc/resolv.conf
echo "$CHROOT" | grep -q "i386" && export SETARCH="setarch i386"
UNSHARE="unshare --ipc --mount --uts --fork --pid"
export UNSHARE
exec sh $0
} && {
PS1='\$ '
exec $SETARCH $UNSHARE sh -c "
mount -v -t devpts -o newinstance devpts $CHROOT/dev/pts;
mount -v -t tmpfs -o rw,nosuid,nodev tmpfs $CHROOT/dev/shm;
mount -v -t sysfs -o rw,nosuid,nodev,noexec,relatime sys $CHROOT/sys;
mount -v -t proc proc $CHROOT/proc;
ln -nsf pts/ptmx $CHROOT/dev/ptmx
chroot $CHROOT env -i \
SHELL=$SHELL \
HOME=$HOME \
TERM=$TERM \
PS1=\"chsys:${CHROOT##*/}$PS1\" \
DISPLAY=$DISPLAY \
PATH=/usr/sbin:/usr/bin:/sbin:/bin \
CHROOT=$CHROOT \
/bin/sh -c \"exec $COMMAND\";
EXIT=\$?;
mount | grep $CHROOT | awk '{print \$3}' | while read mntpoint;
do umount -v \$mntpoint; done;
exit \$EXIT;
"
}
/dev d 755 0 0 - - - - -
/dev/pts d 755 0 0 - - - - -
/dev/shm d 755 0 0 - - - - -
/tmp d 1777 0 0 - - - - -
/etc d 755 0 0 - - - - -
/root d 750 0 0 - - - -
/bin/busybox f 4755 0 0 - - - - -
/etc/shadow f 600 0 0 - - - - -
/etc/passwd f 644 0 0 - - - - -
#/etc/network/if-up.d d 755 0 0 - - - - -
#/etc/network/if-pre-up.d d 755 0 0 - - - - -
#/etc/network/if-down.d d 755 0 0 - - - - -
#/etc/network/if-post-down.d d 755 0 0 - - - - -
###/usr/share/udhcpc/default.script f 755 0 0 - - - - -
# uncomment this to allow starting x as non-root
#/usr/X11R6/bin/Xfbdev f 4755 0 0 - - - - -
# Normal system devices
# <name> <type> <mode> <uid> <gid> <major> <minor> <start> <inc> <count>
/dev/mem c 640 0 0 1 1 0 0 -
/dev/kmem c 640 0 0 1 2 0 0 -
/dev/null c 666 0 0 1 3 0 0 -
/dev/zero c 666 0 0 1 5 0 0 -
/dev/random c 666 0 0 1 8 0 0 -
/dev/urandom c 666 0 0 1 9 0 0 -
/dev/ram b 640 0 0 1 1 0 0 -
/dev/ram b 640 0 0 1 0 0 1 4
/dev/loop b 640 0 0 7 0 0 1 2
/dev/rtc c 640 0 0 10 135 - - -
/dev/console c 666 0 0 5 1 - - -
/dev/tty c 666 0 0 5 0 - - -
/dev/tty c 666 0 0 4 0 0 1 8
/dev/ttyp c 666 0 0 3 0 0 1 10
/dev/ptyp c 666 0 0 2 0 0 1 10
/dev/ptmx c 666 0 0 5 2 - - -
/dev/ttyP c 666 0 0 57 0 0 1 4
/dev/ttyS c 666 0 0 4 64 0 1 4
/dev/fb c 640 0 5 29 0 0 32 4
/dev/psaux c 666 0 0 10 1 0 0 -
# Input stuff
/dev/input d 755 0 0 - - - - -
/dev/input/mice c 640 0 0 13 63 0 0 -
/dev/input/mouse c 660 0 0 13 32 0 1 4
/dev/input/event c 660 0 0 13 64 0 1 4
@jsarenik

This comment has been minimized.

Copy link
Owner Author

commented May 9, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.