Skip to content

Instantly share code, notes, and snippets.

@neonsea neonsea/chroot.sh
Last active Apr 19, 2019

Embed
What would you like to do?
OpenWRT chroot helper script
#!/bin/bash
# script to mount and chroot into a rootfs directory
# designed for openwrt systems, but should work for anything with minimal modifications
# unless you specify a different chroot dir,
# place script on the same level as the rootfs directory, should look something like so:
# /mnt/
# |- chroot.sh
# |- rootfs/
# |- bin/
# |- etc/
# [...]
# usage is same as regular chroot command - no args implies 'chroot ./rootfs/ /bin/bash'
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
if [ $# -ge 1 ]
then
ROOTFS="$1"
else
ROOTFS="${DIR}/rootfs"
fi
if [ $# -lt 2 ]
then
ARGS="/bin/bash"
else
ARGS="${@:2}"
fi
{
{ mount | grep -e "on ${DIR} .*noexec"; } && mount ${DIR} -o exec,remount
{ mount | grep "on ${ROOTFS}/proc"; } || mount -t proc proc ${ROOTFS}/proc/
{ mount | grep "on ${ROOTFS}/sys"; } || mount -t sysfs sys ${ROOTFS}/sys/
{ mount | grep "on ${ROOTFS}/tmp"; } || mount -o bind /tmp ${ROOTFS}/tmp/
{ mount | grep "on ${ROOTFS}/dev"; } || mount -o bind /dev ${ROOTFS}/dev/
{ mount | grep "on ${ROOTFS}/dev/pts"; } || mount -t devpts /dev/pts ${ROOTFS}/dev/pts/
cp /etc/resolv.conf ${ROOTFS}/etc/resolv.conf
} >/dev/null 2>&1
chroot ${ROOTFS} ${ARGS}
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.