Change Root
From ArchWiki
Changing Root involves the process of changing of the apparent disk root directory (and the current running process and its children) to another root directory. When you change root to another directory you cannot access files and commands outside that directory. This directory is called a chroot jail. Changing root is commonly done for system maintenance for such tasks as to reinstall GRUB or to reset a forgotten password. Changing root is often done from from a LiveCD or LiveUSB to enter the installation root partition.
Contents |
Requirements
- You'll need to boot to another working Linux environment (for example, to a LiveCD or USB flash disk). For Arch install mediums there is both the LiveCD and USB Flash disk.
- Root privleges are required before you can chroot.
- Be sure that the architectures of the Linux environment you have booted into, matches the architecture of the root directory you wish to enter (i.e. i686, x86_64).
Mounting the device
First, the device or partition with the Linux system on it will need to be mounted. To discover the kernel name of the storage device name, type:
fdisk -l
Create a directory where you would like to mount the partition, then mount the device or partition:
mkdir /mnt/arch mount /dev/<device-or-partition-name> /mnt/arch
Changing Root
Before you attempt to chroot you can test the architecture of your current Linux environment to be sure that the current environment and the environment you wish to enter match. You can find the architecture of your current environment by:
uname -m
If they do, then you can proceed to mount the temporary filesystems:
cd /mnt/arch mount -t proc none /mnt/arch/proc mount -t sysfs none /mnt/arch/sys mount -o bind /dev /mnt/arch/dev
Mount other parts of your filesystem (e.g. /var, /usr... except /boot) that reside on separate partitions but which you need access to. Generally when you're chrooting to fix something you won't need access to /home, so you don't need to bother with that:
mount /dev/<device-or-partition-name> /mnt/arch/var
It's possible to mount filesystems after you've chrooted, but it's safer to do so beforehand. The reason is that when you exit a chroot, the outside/kernel environment will know about the mounted filesystems and can be safely unmounted. Filesystems not properly unmounted could possibly cause damage to your filesystem.
If you need your static filesystems updated (if you'll be doing anything with GRUB...), you'll need to be sure your /etc/mtab is up-to-date:
grep -v rootfs /proc/mounts > /etc/mtab
Now chroot to your installed device or partition and define your shell:
chroot /mnt/arch /bin/bash
If using a separate /boot partition, you must mount this partition now within the chroot environment. This is necessary for actions like reinstalling GRUB, performing a kernel update, or rebuilding the initramfs image:
mount /dev/<device-or-partition-name> /boot
To be sure of which environment you are in, you can change your bash prompt to show it:
source /etc/profile export PS1="(chroot) $PS1"
Exiting chroot
First the chroot shell must be exited:
exit
Then unmount the temporary filesystems and mounted devices then reboot:
umount /mnt/arch/{proc,sys,dev}
Finally attempt to unmount your hard drive:
cd .. umount /mnt/arch
If you get an error saying that /mnt (or any other partition) 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/usr is still mounted when trying to unmount /mnt.
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. If you still are unable to mount a partition, use the force option:
umount -f /mnt
Finally:
reboot
Resources
- Wikipedia - for the introduction.