Search:  
Gentoo Wiki

Chroot_from_a_livecd

Contents

Introduction

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.

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.

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).

Preparing the livecd environment

Before actually chrooting to the install, the livecd environment needs to be prepared using the following steps:

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.

Code: Mount required filesystems
mount /dev/hda3 /mnt/gentoo
mount /dev/hda1 /mnt/gentoo/boot
mount -t proc none /mnt/gentoo/proc
mount -o bind /dev /mnt/gentoo/dev

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: cp -L /etc/resolv.conf /mnt/gentoo/etc/resolv.conf

To actually enter the chrooted environment, the steps are the same as those used in the Gentoo Handbook:

Code: Chroot to the installed system
# chroot /mnt/gentoo /bin/bash
# env-update
>> Regenerating /etc/ld.so.cache...
# source /etc/profile
# export PS1="(chroot) $PS1"

Special Cases

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: cp /proc/mounts /etc/mtab

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.

Code: Exit the chroot and unmount filesystems
(chroot)# exit
# umount /mnt/gentoo/boot /mnt/gentoo/dev /mnt/gentoo/proc /mnt/gentoo

If you receive a message stating that a given mount is busy, this can mean one of two things:

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.

Troubleshooting

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.

Retrieved from "http://www.gentoo-wiki.info/Chroot_from_a_livecd"

Last modified: Wed, 08 Oct 2008 20:58:00 +0000 Hits: 13,256