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:
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.
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.
$ lsblkNAME           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   /homesdb              8:16   1  28.7G  0 disk  └─sdb1           8:17   1   250M  0 part  /mnt/bootloaderIf 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/mapperOnce you're done, you no longer want the partition but the mapped logical volume partitions.
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> ./targetSome 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/varNow 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/devFor established network, you just need to copy the resolv.conf file:
$ cp -L .etc.resolv.conf /target/etc/resolv.confThis stage is crucial so please enter the target directory and perform a double checking inside it. Example:
target/proc is the same as the /proc on your systemtarget/home is valid.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.
Once you're done, we can change root now.
$ chroot ./target /bin/bashThe 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:
There are 2 things you need to keep up after change root, one is to update the mtab
$ grep -v rootfs /proc/mounts > /etc/mtabAlso, updating the terminal settings:
$ source /etc/profile$ export PS1="(chroot) $PS1"   # to make it clear we're in guest modeAt this point, you can perform your work for the operating system. Things you can do are:
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.
Once you're happy with what you have, it's time to exit and cleanup.
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.
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 /targetIf 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      $ lvscanIf your guest partition is decrypted, then it's time to close it. Otherwise, skip this step.
$ cryptsetup luksClose sda3_cryptThat's all about the art of chroot. Once you master it, you have to power to alter anything within your might!