Mounting a Filesystem inside a Live CD

Say for some reason you can’t access your computer. Perhaps you need to reinstall GRUB, or maybe you are running a minimal linux install (say LFS) and need to work in a different environment that supports your hardware until you can configure your linux kernel. Well, one of the options is to mount your filesystem from inside a Live CD.

The process is actually quite simple once you understand what you need to do.

First, and most obvious, you need to boot into the Live CD. This typically involves setting BIOS so that your cdrom/dvdrom device or usb flash drive is higher in the boot order than your hard drive. Alternatively, you can hit the button corresponding to change your boot order and do a one time boot from the live cd/usb.

Once the live cd environment is up and running, you’ll need to open a terminal and start mounting the filesystems. The first filesystem to mount is, just like it’s name, / (root). You can mount it whev ever you want, just be sure if you change the location from what I have shown you change it all the way through the instructions.

Here I am mounting my root partition (/dev/sda5) at /mnt.

mount /dev/sda5 /mnt

If you need help finding the proper device names, a few commands of help are:

fdisk -l
blkid
cat /etc/fstab       (though this only works before you restart to the live cd enviroment)

Next, you’ll want to mount any separate partitions you have. This is not strictly required unless you are going to preform some action in that particular partition. That is, be sure to mount /boot if you are updating, fixing, or reinstalling GRUB. Mount /home if you need access to something in your home directory. Here I am mounting /home and /boot only.

mount /dev/sda6 /mnt/home
mount /dev/sda1 /mnt/boot

This next step mounts all the critical virtual filesystems. These are things like /dev, populated by udev at boot, /proc, /sys, etc. They all contain special files that point to, for example, a hard drive, or cdrom drive. Because you don’t want to actually change the location of the current system and ruin the live cd environment, you use the bind mount option so they can show up in two places at once.

for place in /dev /dev/pts /proc /sys /run; do mount --bind "$place" "/mnt$place"; done

In order to work in your “normal” filesystem, you next change the root directory. If you don’t, all the binaries, paths, etc., are in relation to the live cd root filesystem.

chroot /mnt

Congrats, you are now working inside your “normal” root filesystem. If you want to, for example, reinstall GRUB, issue the following commands:

grub-install --recheck /dev/sda
update-grub

Or perhaps you want to run a custom script you wrote for just this purpose:

cd /path/to/script
./script

Once you are done, simply exit the chroot environment

exit
-or-
[CTRL][D]

If you aren’t done you don’t have to unmount your filesystems. If you are, you also don’t need to unmount your filesystems, simply reboot and pull out your live cd/usb.

Tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

Protected by WP Anti Spam