Skip to content

Instantly share code, notes, and snippets.

@pllopis
Created December 21, 2011 13:07
Show Gist options
  • Select an option

  • Save pllopis/1505981 to your computer and use it in GitHub Desktop.

Select an option

Save pllopis/1505981 to your computer and use it in GitHub Desktop.
Chroot into a device or file taking care of mounting proc, sysfs, /dev, setting mount options, umounting, etc
#!/bin/bash
function ifloop {
# If it is NOT a file in /dev, assume file loopback mount
if [[ ! $1 =~ /dev/* ]]
then
echo "-o loop"
fi
}
function usage {
echo "./inception <file/device> <mountpoint>"
echo "For undoing, use ./inception -d <mountpoint> "
}
function inception {
DEVICE=$1
MOUNTPOINT=$2
MOUNTOPT=$3
mount $MOUNTOPT $DEVICE $MOUNTPOINT
mount -t proc proc ${MOUNTPOINT}/proc
mount -t sysfs sys ${MOUNTPOINT}/sys
mount -o bind /dev ${MOUNTPOINT}/dev
chroot $MOUNTPOINT /bin/bash
}
function wake {
MOUNTPOINT=$1
umount ${MOUNTPOINT}/dev
umount ${MOUNTPOINT}/sys
umount ${MOUNTPOINT}/proc
umount $MOUNTPOINT
}
if [ "$1" == "-d" ]
then
echo "Inception level down..."
MOUNTPOINT=$2
if [ $# -ne 2 ]
then
usage
exit
fi
wake "$MOUNTPOINT"
exit
fi
DEVICE=$1
MOUNTPOINT=$2
MOUNTOPT=$(ifloop "$DEVICE")
if [ $# -ne 2 ]
then
usage
exit
fi
echo "Inception level up..."
inception "$DEVICE" "$MOUNTPOINT" "$MOUNTOPT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment