CentOS / Redhat: Chroot And Mount Raid Or Actual Hard Disk From Rescue Kernel / CD

Posted on in Categories last updated June 24, 2010

Something is wrong with my RHEL networking configuration so I booted using my ISPs remote rescue kernel mode to fix my system. How do I mount and chroot into actual installation to fix the problem? How do I boot from any Live Linux CD and chroot into /dev/sda1 or /dev/md0 to fix the problem or recover the data?

Once booted into rescue kernel or using Live Linux CD you need to verify that your disks are detected. Type the following command to verify the same:
# fdisk -l
Next, create a directory to chroot and mount existing partitions:
# mkdir /chroot
# d=/chroot

Mount actual partition:
# mount /dev/sda1 $d
Mount /proc and other file system inside $d, enter:
# mount -o bind /dev $d/dev
# mount -o bind /sys $d/sys
# mount -o bind /dev/shm $d/dev/shm
# mount -o bind /proc $d/proc

Chroot and access your data:
# chroot $d
# df
# ls
# vi /etc/somefile

Exit and reboot the system:
# exit
# umount $d/proc
# umount $d/sys
# umount $d/dev/shm
# umount $d/dev
# umount /dev/sda1

A Note About Multiple /boot, /home, /tmp Partitions

Cosinder the following parition layout for /dev/sda

  • / – /dev/sda1
  • /boot – /dev/sda2
  • /home – /dev/sda3
  • /tmp – /dev/sda4

To mount all at /chroot, enter:
# d=/chroot
# mkdir $d
# mount /dev/sda1 $d
# mount -o bind /dev $d/dev
# mount -o bind /sys $d/sys
# mount -o bind /dev/shm $d/dev/shm
# mount -o bind /proc $d/proc
# mount /dev/sda2 $d/boot
# mount /dev/sda3 $d/home
# mount /dev/sda4 $d/tmp
# chroot $d

A Note About /dev/md0 (Software RAID)

Type the following command to mount /dev/md0 at /chroot/data (/dev/sda1 at /chroot):
# d=/chroot
# mkdir $d
# mount /dev/sda1 $d
# mount -o bind /dev $d/dev
# mount -o bind /sys $d/sys
# mount -o bind /dev/shm $d/dev/shm
# mount -o bind /proc $d/proc
# mount /dev/md0 $d/data
# chroot $d
# ls
# df

Now you can access software RAID too.

How Do I Recover Data?

Once /dev/md0 or /dev/sda1 mounted simply copy data using scp / rsync or external usb pen / hard disks.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Share this on (or read 3 comments/add one below):

3 comment

Leave a Comment