Chroot from a livecd
From Gentoo Linux Wiki
Occasionally a linux install will end up in a situation where it won't boot successfully for whatever reason (it could be because another OS has overwritten the MBR, or there's a problem with boot scripts or kernel configuration).
The purpose of this guide is to perform a chroot from virtually any livecd (or other linux) environment allowing the problem to be diagnosed and fixed.
Contents |
[edit] Limitations
While a chrooted install from a livecd has excellent functionality, it won't be complete. For example, the chrooted system is still using the kernel of the livecd. This means that any relevent kernel driver modules not already loaded will need to be modprobe'd before entering the chroot. For the same reason, any kernel modules only available in the chrooted system (such as ndiswrapper or other third party modules) can't be loaded.
To solve the problem of drivers, either locate a livecd that does contain the drivers requried or alternatively consider creating a livecd specifically for the system.
[edit] Prerequisite knowledge
This guide assumes knowledge of the hardware of the system to be chrooted and the drivers (specifically kernel modules) required by that hardware. It also assumes commandline knowledge equivalent of someone who has completed an install using the Gentoo Handbook (since most of the steps and instructions are very similar).
[edit] Preparing the livecd environment
Before actually chrooting to the install, the livecd environment needs to be prepared using the following steps:
- Load any required kernel modules. Some are autoloaded for you.
- Set up the network if you think you may need it.
- Initialize any available swap with, for example (where /dev/hda2 is a swap partition): swapon /dev/hda2.
[edit] Mounting the installed system
First a quick note on mounting filesystems. While it's possible to mount filesystems from within the chrooted system, this is not recommended. The reason for this is that the livecd environment won't know about these mounted systems, so if they are forgotten about and left mounted, they will not be unmounted properly when the system shuts down, which could cause damage to the filesystems on those mounts.
Mount the root (and if necessary, /boot) partitions of the installed system. If separate partitions are used for other areas of the system (for example, a separate partition for /var/log) then these will also need to be mounted.
In the following example, /dev/hda1 is the /boot partition and /dev/hda3 is the root partition.
Additionally, mount the /dev and /proc filesystems so that they can be used by the chrooted environment.
[edit] Chrooting to the installed system
Before you chroot, you want to copy over /etc/resolv.conf so that your chroot environment can resolve domain names:
To actually enter the chrooted environment, the steps are the same as those used in the Gentoo Handbook:
- Chroot to the installed system.
- Ensure the environment is correct.
- Add a reminder to the command prompt.
[edit] Special Cases
[edit] Reinstalling Grub
Grub reads the /etc/mtab file to learn about the currently mounted filesystems, so this needs to be up-to-date. You can update it from /proc/mounts by running:
[edit] Exiting the chroot
When finished, ensure that all running programs have stopped and then run the following commands to exit the chroot and unmount the mounted partitions. Don't forget to unmount any other mounted partitions before unmounting /mnt/gentoo.
If you receive a message stating that a given mount is busy, this can mean one of two things:
- A program was left running inside of the chroot.
- Or more frequently: A mount point still exists on this mount. For example, /mnt/gentoo/boot is still mounted when trying to unmount /mnt/gentoo.
In the latter case, simply unmount the offending mount point first. To get a reminder of all the current mount points, run mount with no parameters.
[edit] Troubleshooting
[edit] Exec format error
If the chroot command returns with the error "chroot: cannot run command `/bin/bash': Exec format error", this usually indicates that the livecd environment is not compatible with that of the installed system.
For example, the error is most frequently seen when trying to chroot to a 64-bit system (eg. amd64) from a 32-bit livecd (eg. x86).
The solution is to use a livecd which is using the same architecture as the installed system.