HOWTO: chroot from a LiveCD

From Sabayon Wiki
Revision as of 21:21, 11 December 2012 by Aposteeltje (Talk | contribs)

Jump to: navigation, search
i18n: en tr
Question.png
When you have LVM Volumes, or Encrypted Volumes, and want to Chroot, Please read these sections first: HOWTO: Mount LVM & HOWTO: Mount Encrypted Partition

Boot a Sabayon Linux LiveCD/DVD or other Live disc. Get to a virtual terminal (console) or open a Konsole/Terminal window on the Desktop, and log-in as the root user.

Stop.png
Make sure the LiveCD/DVD you use is for the same architecture as the architecture of the installation on the hard disk, i.e. to chroot to a 64-bit installation (e.g. amd64) you cannot use a 32-bit LiveCD/DVD (e.g. x86), and vice versa. You will see an error message "chroot: cannot run command `/bin/bash': Exec format error" if you try to chroot from a LiveCD/DVD of a different architecture.


Question.png
Whilst it is possible to mount filesystems from within the chrooted system, this is not recommended. The reason for this is that the LiveCD/DVD environment won't know about these mounted systems, so if you forget about them and leave them mounted when you exit from the chroot environment, they will not be unmounted properly when the system shuts down, which could cause damage to the filesystems on those mounts.


Mount the root partition of the installed system (and, if you have /boot on a separate partition, mount that too). If separate partitions are used for other areas of the system (for example, a separate partition for /var/log) then you will need to mount them too. Additionally, mount the /dev and /proc filesystems so that they can be used by the chrooted environment.


In the following example, /dev/hda1 is the /boot partition and /dev/hda3 is the root partition. Obviously replace those with the device names for your boot partition (if you have one) and your root partition. If your partition names are of the form /dev/sd<letter><number> rather than /dev/hd<letter><number> then obviously use that form instead. Obviously, if you do not have /boot on a separate partition to / (root) then you should omit the mount and umount commands referring to /boot.

# mkdir -p /mnt/sabayon/boot
# mount /dev/hda3 /mnt/sabayon
# mount /dev/hda1 /mnt/sabayon/boot
# mount -t proc none /mnt/sabayon/proc
# mount -o bind /dev /mnt/sabayon/dev


So now we are set to enter into our installed system:

# chroot /mnt/sabayon /bin/bash
# env-update
# source /etc/profile
# export PS1="(chroot) $PS1"


You should then end up with the following prompt:

(chroot) #


Grub2 reads the /etc/mtab file to ascertain the filesystems that are currently mounted, so /etc/mtab must be up-to-date. If you do not have a separate boot partition then update /etc/mtab using the following command:

(chroot) # grep -v rootfs /proc/mounts > /etc/mtab

or the following command if you do have a separate boot partition:

(chroot) # cp /proc/mounts /etc/mtab


From now on, you can enter commands as if you were using your installed system directly. You could, for example, install a package by using either the equo or emerge commands. The ability to act directly on the installed system like this is very useful in the event that something is broken and you cannot boot to a working system. You might want to revert to an earlier version of a video driver that you know works, or you might want to fix the kernel config and rebuild it because a change you made to the kernel earlier resulted in a kernel panic when you rebooted. And so on.

(chroot) # <enter any command you want>


Once you have finished, you can exit from the chroot environment and unmount the drives:

(chroot) # exit
# umount /mnt/sabayon/boot /mnt/sabayon/dev /mnt/sabayon/proc /mnt/sabayon


Reboot the PC. If your actions during the chroot session were successful then your installed system should boot normally.