Tuesday, March 6, 2012

Chroot explained

For one reason or another you broke your system and don't know what to do? There is still one fix that you can try and that is chroot. As explained from this  howto chroot not be scared. we will try to dig a little bit more about how it works. You should know the basic of the Linux file system Hierarchy.


Example of file system hierarchy in SuSE and it's partitions











          Lets get started,  we will take the above partitions as an example.
    • boot with a dvd  and select rescue mode, the net install cd has rescue mode or you can use the livecd as well, you can even attach the disk that is in question as secondary disk in a working linux system.The idea is to have access to the filesystem that is broken.

    • Log in and check your disk partitions. If you know your partitions well, then it will not be a problem. Normally if you followed the suggestion of the disk partitioner of the installer you should only have 3 partitions namely swap, root  and a home. Now if you have  more  partitions than usual you may need to check every partition. Once booted in rescue mode you can mount them one by one if needed (preferably) in /mnt  and check  which partitions or directory it contain. You need to mount the the disk according to the file system hierarchy in accordance with  the linux tree  in which the root partition is the first to be mounted in /mnt. Are you getting confused now? let's see the example below.


    Boot the rescue system







        log in as root which does not require a password and check which directory you are in


           

          Check the partitions on the disk using the fdisk utility.

            If you have given each partition a volume label then you can just run   blkid  This will save you time and if you did not give it a volume label then you can proceed with the procedure as described below.
            • You can start mounting the partitions one a time inside /mnt and check which directory it contains. starting with sda1
                 
            In this partition it is obvious that this is a swap partition and not needed to      be mounted.


                  
                Mounting sda2 and checking its content suggest that it is the boot partition.





                   
                  Mounting sda3 and checking its content suggest that it is the "/" partition. 



                            Mounting sda4 and having trouble mounting it? if you look at the out put of fdisk -l you will see that it is the extended partition which holds the other logical partitions. Then it is not needed to be mounted. 



                        
                      Mounting sda5 and checking its content suggest that it is the usr partition. 



                              
                        Mounting sda6 and checking its content suggest that is the var partition. 



                             

                           Mounting sda7 and checking it's directory content suggest that it is the home partition.



                          • Now that it's clear about the partitions you can umount everything inside /mnt.



                          NOTE:

                          Again the above procedure is not necessary if you only have 3 partitions, which is normally the swap, / and /home. Mount / inside /mnt directly and then mount /home under /mnt/home, or if your partition's are different than the above example you can just apply the procedure, Then proceed with the chroot.
                          • Now were ready to mount them in order, starting with the /  and since it holds or contain all the directory in the Linux tree you can mount the rest accordingly.



                            • When its done you will need to do  "The bind mounts" as described in  the man pages of   mount (8)  to make sure that you have access to the tools, resources  needed which will come from the /dev, /sys and /proc directory.

                              • At last we are ready to chroot.


                                   

                                Check which directory you are in.

                                        
                                    
                                   Check which partition you are in.    

                                  The result will confirm the success of your chroot and This will let you do ( but not limited to ) the following.

                                  •  zypper  if your dealing packages and repositories.
                                     
                                  •  yast  and all its modules using ncurses bindings  meaning no gui.
                                     
                                  • Remove add edit some config files.
                                     
                                  • Finally everybodies favorite   startx   although i wouldnt do that unless you dont know how fix things using the shell then this will be your last option. You will need to <CTRL>+<ALT>+<F1> up to <F6> if you want to have a shell access during this session and <CTRL>+<ALT>+<F7> to go back to your X session. When your done log-out so X server will stop and you will be back to your default Rescue shell you can type  exit  without the quotes to exit the chroot then reboot, run    shutdown -r now  



                                  Final thoughts:

                                  Tinkering about things in your system is normal and that's how we learn from trying things new, edit files, compile some kernel modules or install some tarball and so on. Eventually you will end up with a broken system and that's a disaster waiting to happen in any Linux user or enthusiast. After all that have been said, there is still hope  to fix your system and that is by means of chroot.

                                  Good luck!!


                                  External links and source:

                                  http://en.wikipedia.org/wiki/Chroot 

                                  http://www.johnlange.ca/2009/09/22/suse-broken-dont-fear-the-chroot/ 

                                  No comments:

                                  Post a Comment