Change Root

In Linux OS construction / modification, change root (/ directory) is a very powerful tool to simplify a lot of works. However, you need to have some basic understanding on how to prepare a / directory before changing into it. Otherwise, catastrophic consequences can occur including not limited destruction of your data, and hard disk partition too.

Also, for obvious reason, you need root or at least, sudo access.

That being said: make sure you have a decent experience in developing Linux operating system or have a VM to gain the needed experience.


From here on out, we need to make sure our terminologies are correct:

  • host - the currently running operating system; the one being change root from
  • guest - the designated operating system; the one being change root into

Identify Guest Partitions and Mount It

The first thing to do is to mount your guest partition or setup the directory. If you're at the latter, you should be fine. Otherwise, you need to identify and mount the partition.


Find Device

The easiest way is to use lsblk command. This will list out all the partitions accordingly. Find and select the partition containing the root directory.

$ lsblk
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT          
sda              8:0    0 931.5G  0 disk                      
├─sda1           8:1    0   243M  0 part                      
├─sda2           8:2    0     1K  0 part                      
└─sda5           8:5    0 931.3G  0 part                      
  └─sda5_crypt 254:0    0 931.3G  0 crypt                     
    ├─<lvm-name>-root                              
    │          254:1    0    28G  0 lvm   /
    ├─<lvm-name>-swap_1
    │          254:2    0  11.9G  0 lvm   [SWAP]
    └─<lvm-name>-home
               254:3    0 891.4G  0 lvm   /home
sdb              8:16   1  28.7G  0 disk  
└─sdb1           8:17   1   250M  0 part  /mnt/bootloader


Decrypt and Manage Logical Volume

If the partition you're dealing with is encrypted with LUKS, you'll need to decrypt it. I have separate guides for its detailed explanations. Here however, I'll summarize into the few crucial steps.

# decrypt the partition
$ cryptsetup luksOpen /dev/<encrypted partition> <encrypted_partition>_crypt

# scan for logical volume group
$ pvscan

# list the logical volume group. Look for the one you're interested with
$ vgscan

# activate group
$ vchange -a y "<the volume group name you're interested with>"

# check the logical volumes activation status      
$ lvscan

# find you remapped device partition. From here on out, use this remapped partition instead.
$ ls /dev/mapper

Once you're done, you no longer want the partition but the mapped logical volume partitions.


Mount It

Once done, you can mount that partition to a directory. In this guide, we will make a directory called "target" and mount to it.

$ mkdir -p target
$ mount /dev/<partition of interest> ./target


Repeat for Any Separated Sub Partitions

Some installation has separated partitions like /boot, /home, /var etc. You need to mount them up accordingly into the target directory. Keep track of all the partitions you had mounted and its sequences as we need to mount them during the cleanup step. Here's an example for separated /home, and /var:

$ mount /dev/<partition with home> ./target/home
$ mount /dev/<partition with var> ./target/var

Mounting Systems

Now that the filesystem is up and ready, it's time to mount the current operational system directory like dev, sys, and proc. To do that is quite straight-forward:

$ mount -t proc proc /target/proc
$ mount -t sysfs sys /target/sys
$ mount -o bind /dev /target/dev


For established network, you just need to copy the resolv.conf file:

$ cp -L .etc.resolv.conf /target/etc/resolv.conf


Double Check

This stage is crucial so please enter the target directory and perform a double checking inside it. Example:

  • check the target/proc is the same as the /proc on your system
  • check the target/home is valid.
  • and goes on...

Don't get overconfident with what you prepared so far. Remember, one mistake is equal to data destruction. Take your time and be careful with it.

Change Root

Once you're done, we can change root now.

$ chroot ./target /bin/bash

The pattern is $ chroot <directory> <terminal in guest>

You'll notice the prompt has now changed into your guest system. Congratulations! You're now in guest operating system. However, there are a few things to keep in mind:

  • You're still using the host kernel, not the guest one.


Minor Keep Up

There are 2 things you need to keep up after change root, one is to update the mtab

$ grep -v rootfs /proc/mounts > /etc/mtab

Also, updating the terminal settings:

$ source /etc/profile
$ export PS1="(chroot) $PS1"   # to make it clear we're in guest mode

Do Your Work

At this point, you can perform your work for the operating system. Things you can do are:

  • reinstall Grub to disk MBR (or repair the grub)
  • reset forgotten password
  • perform kernel upgrade / downgrade
  • rebuild initramfs disk
  • fix /etc/fstab or /etc/crypttab
  • reinstall package with package manager
  • and so on

Also, keep in mind that not all programs are written in a way working in chroot system. Hence, be extra careful with reading the error and warning information.

Clean Up

Once you're happy with what you have, it's time to exit and cleanup.


Exit Change Root

To exit, simply type "exit". Before that, make sure you do a data sync to ensure integrity.

(chroot) $ sync
(chroot) $ exit
$ sync
$

You should notice that the prompt is returning back to host mode now.


Unmount All Directory

Now, we need to unmount all the directory in backward sequences. Based on the example above, it is from the mounted system directory, then those mounted separated partition, then the mounted root partitions. As an example, it looks something like this (observe the backward patterns):

$ umount /target/dev
$ umount /target/proc
$ umount /target/sys
$ umount /target/home
$ umount /target/var
$ umount /target


Deactivate LVM Volumes

If the guest partitions are LVM activated, it's time to deactivate them. Otherwise, skip this step.

# activate group
$ vchange -a n "<the volume group name you're interested with>"

# check the logical volumes activation status      
$ lvscan


Close the Decrypted Volume

If your guest partition is decrypted, then it's time to close it. Otherwise, skip this step.

$ cryptsetup luksClose sda3_crypt

That's all about the art of chroot. Once you master it, you have to power to alter anything within your might!