Difference between revisions of "HOWTO: chroot from a LiveCD"

From Sabayon Wiki
Jump to: navigation, search
Line 22: Line 22:
# mount /dev/hda1 /mnt/sabayon/boot
# mount /dev/hda1 /mnt/sabayon/boot
# mount -t proc none /mnt/sabayon/proc
# mount -t proc none /mnt/sabayon/proc
# mount -o bind /dev /mnt/sabayon/dev</pre>}}
# mount -o bind /dev /mnt/sabayon/dev
# mount -o bind /run /mnt/sabayon/run</pre>}}
''works like a charm as the resolv.conf is in run''
note:<after some experimentation this '''fixed the no network in chroot for updates etc.'''>
''prior cp /etc/resolv.conf > /mnt/sabayon/etc now quite deprecated''

Revision as of 05:03, 19 April 2014

i18n: en tr
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.

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.

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
# mount -o bind /run /mnt/sabayon/run

works like a charm as the resolv.conf is in run note:<after some experimentation this fixed the no network in chroot for updates etc.> prior cp /etc/resolv.conf > /mnt/sabayon/etc now quite deprecated

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.