xx xx|eeeee| ssss|ppppp |iiiiii|ll | cccc|eeeee xx xx |ee |ss |pp p| ii |ll |cc |ee x |eeee | s |ppppp | ii |ll |cc |eeee xx xx |ee | ss|pp | ii |ll |cc |ee xx xx|eeeee|ssss |pp |iiiiii|lllll| cccc|eeeee The Art of Changing Filesystems (chroot to change the / to a new mountpoint) - xespilce -- find me on irc.dal.net #debian oct 14, 2004 version 1.0 Linux Tutorial on chroot I am writing this tutorial because i feel that the current documentation on chroot was insufficient. This is meant as a tutorial for Beginner to Intermediate users on how to mount and chroot to a filesystem with a new / on different drives. Why chroot ??? The answer is quite simple. You need root access for whatever reason on a computer that you cannot normally gain root access on (because you have forgotten the password, you are going to install a new operating system, etc ...). Of course this means you must have physical access to the computer. You also need to have a basic shell environment such as the environment many *nix installation bootdisks provide, rescue cd's and custom cd's like knoppix. now lets get into the good stuff ;) reboot into the shell environment. The key to this trick is that you automatically have root access when you boot into a live cd. so when you change your environment those permisions are carried over to the new filesystem. we are in the environment first off we need to mount the directories that will serve as the mountpoints for the new filesystem. ****important**** the higharchy of the directories is very important and should be set up something like the following mkdir /mnt/newRoot cd /mnt/newRoot mkdir proc mkdir home mkdir boot and so forth one directory for every partition of the foreign filesystem (these directories become labels of the filesystem just like the one's listed in the /etc/fstab file) then we mount the filesystem mount /dev/hdxy /mnt/newRoot Where hdxy is the hard drive x and the partitoin y that the / of the new filesystem will be located. Continue to do this for every partition of the hard drive like so mount /dev/hda2 /mnt/newRoot mount /dev/hda1 /mnt/newRoot/boot mount /dev/hda4 /mnt/newRoot/home mount --bind /proc /mnt/newRoot/proc ****important**** the /proc directory must be bound to the new filesystem because it is used by the kernel for various tasks (see tutorial on mount for more information) Now that we have mounted the filesystem we are ready to chroot in. :) The command: chroot /mnt/newRoot /bin/bash will allow you to enter the new system now let me explain what exactly this command does. First off it changes the / directory from where ever it was to the directory that is at /mnt/newRoot so now when you type cd / you wind up wherever /mnt/newRoot is located. Pretty nifty huh? When you type cd /home you wind up wherever /mnt/newRoot/home was located ;) that is why it is so important to make the mountpoints of the other partitions in relation to the mountpoint of the newRoot mountpoint. The command: df Is very helpfull in figuring out wether or not you have sucessfully changed your / location it should output something like the following: Filesystem 1K-blocks Used Available Use% Mounted on /dev/hda2 7692908 1411604 5890524 20% / tmpfs 453780 0 453780 0% /dev/shm /dev/hda1 15022 7528 6693 53% /boot /dev/hda4 105748284 365244 100011308 1% /home This shows the hard drive locations of the labels in the filesystem, ie /boot is the first partition on the first hard drive and /home is the fourth partition on the first hard drive. This is the second indicator if you have done something incorrectly (the first being that this command didnt work ;) but I will cover that shortly). If several of the fields in a column are the same then you probably did something wrong and should type "exit" then push enter to get back to the original / mountpoint. If the df command didnt work or if you recieved an error along the lines /bin/bash file not found you probably either forgot to mount a partitoin or are not using the bourne again shell. Reference: chroot /mnt/newRoot /bin/bash The second argument /bin/bash is a command that is executed upon entering the new environment, I say new environment because you may use a shell independent of the shell that previously got you to this point. If you were using a bash shell you could change to another shell. Of course you could just have done: chroot /mnt/newRoot instead of chroot /mnt/newRoot /bin/bash But everyone knows that it is much cooler to do the latter ;) Now your probably asking yourself this is sweet and all but ... how the heck do i get back to my old mountpoints ???? piece of cake just type in: exit Then enter and your back to where you started. Problems: -- forgot to mount the /proc filesystem This breaks commands ... many commands you try will not work, they will instead say something about a problem with the /proc directory -- Many commands are broken Probably did not run a shell. You will not have access to many commands if you do not run a shell. Typical errors are of the infamous "command not found" variety -- cannot exit the new environment You didnt read the whole fucking manual go back 17 lines :p -- unexpected files (or lack there of) in the mounted partitions Simple the partition is not mounted correctly ;) go back and run the df command and make sure that the mount table doesnt have duplicate values in the volume columns and that the mountpoint of the directory that you are looking at is actually there.