Skip to content

Instantly share code, notes, and snippets.

@krha
Created February 28, 2012 06:03
Show Gist options
  • Select an option

  • Save krha/1930002 to your computer and use it in GitHub Desktop.

Select an option

Save krha/1930002 to your computer and use it in GitHub Desktop.
Rooting rootFS
#!/bin/bash
PATH=/bin:/usr/bin:/sbin:/usr/sbin
NEWTYP=ext4
NEWMNT="/mnt/new-root"
OLDMNT="/mnt/old-root"
RUNLEVEL=`runlevel | awk '{print $2}'`
if [ $# -eq 1 ]
then
NEWDEV=$1
else
echo "usage : program [device file]"
exit 2
fi
echo "EBS Block is at $NEWDEV"
echo "Remounting writable."
mount -o remount,rw /
[ ! -d $NEWMNT ] && echo "Creating directory $NEWMNT." && mkdir -p $NEWMNT
while true ; do
sleep 1
echo "Trying to mount $NEWDEV writable."
mount -t $NEWTYP -o rw $NEWDEV $NEWMNT || continue
echo "Mounted."
break;
done
[ ! -d $NEWMNT/$OLDMNT ] && echo "Creating directory $NEWMNT/$OLDMNT." && mkdir -p $NEWMNT/$OLDMNT
echo "Trying to pivot."
cd $NEWMNT
pivot_root . ./$OLDMNT
# Some linux kernel(natty) has mount point at /var/run and /varlock
# TODO: automatically umount all unnecessaty moutings
for dir in /dev /proc /sys /run /var/run /var/lock; do
echo "Moving mounted file system ${OLDMNT}${dir} to $dir."
mount --move ./${OLDMNT}${dir} ${dir}
done
echo "umount EBS from old root"
umount ${OLDMNT}${NEWMNT}
umount ${OLDMNT}/var/lock
umount ${OLDMNT}/mnt
echo "Restart sshd"
service ssh restart
echo "Restart init process"
telinit u
echo "Kill original processes"
fuser -m -k ${OLDMNT}
#exec chroot / /bin/sh -c "exec /sbin/init $*" < /dev/console > /dev/console 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment