October 03, 2013

Recovering /boot FS in Linux

If you removed the data on /boot FS completely, here is the process how to recover the data on /boot FS.

NOTE: in my case I’m using LVM and LUKS encryption. /dev/sda1 is /boot partition /dev/sda2 is encrypted LVM partition with root VG.

1. Reinstall following packages

root@laptop ~# yum reinstall kernel -y
root@laptop ~# yum reinstall redhat-logos -y
root@laptop ~# yum reinstall grub -y

2. Refresh grub installation

root@laptop ~# grub-install /dev/sda1

3. Update MBR

root@laptop ~# grub
Probing devices to guess BIOS drives. This may take a long time.

    GNU GRUB  version 0.97  (640K lower / 3072K upper memory)

 [ Minimal BASH-like line editing is supported.  For the first word, TAB
   lists possible command completions.  Anywhere else TAB lists the possible
   completions of a device/filename.]
grub> root (hd0,0)
root (hd0,0)
 Filesystem type is ext2fs, partition type 0x83
grub> setup (hd0)
setup (hd0)
 Checking if "/boot/grub/stage1" exists... no
 Checking if "/grub/stage1" exists... yes
 Checking if "/grub/stage2" exists... yes
 Checking if "/grub/e2fs_stage1_5" exists... yes
 Running "embed /grub/e2fs_stage1_5 (hd0)"...  27 sectors are embedded.
succeeded
 Running "install /grub/stage1 (hd0) (hd0)1+27 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.
grub> quit
quit

4. Create your grub.conf file

root@laptop ~# cat /boot/grub/grub.conf
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz.redhat
hiddenmenu
title RHEL6 (2.6.32-358.11.1.el6.x86_64)
  root (hd0,0)
  kernel /vmlinuz-2.6.32-358.11.1.el6.x86_64 ro root=/dev/vg_oc/lv_root_rhel LANG=en_US.UTF-8 KEYBOARDTYPE=pc KEYTABLE=us rhgb quiet
  initrd /initramfs-2.6.32-358.11.1.el6.x86_64.img

5. Make a symlink

root@laptop ~# ln -s ./grub.conf /boot/grub/menu.lst
root@laptop ~# ls -la /boot/grub/menu.lst
lrwxrwxrwx 1 root root 11 Jul  4 19:53 /boot/grub/menu.lst -> ./grub.conf

6. Restore file(s) default SELinux security contexts on /boot

root@laptop ~# restorecon -Rv /boot/

7. FS check on /boot FS

root@laptop ~# umount /boot
root@laptop ~# fsck -fy /dev/sda1

8. Restart system

shutdown -r now

More output


root@laptop ~# find /boot/ -ls
     2    4 dr-xr-xr-x   4 root     root         3072 Jul  4 19:41 /boot/
    12 2352 -rw-r--r--   1 root     root      2407976 May 15 16:55 /boot/System.map-2.6.32-358.11.1.el6.x86_64
 65025    1 drwxr-xr-x   2 root     root         1024 Jul  4 19:53 /boot/grub
 65033   12 -rw-------   1 root     root        11956 Jul  4 19:36 /boot/grub/minix_stage1_5
 65038    1 -rw-------   1 root     root           30 Jul  4 19:36 /boot/grub/device.map
 65039    2 -rw-r--r--   1 root     root         1341 May  6  2010 /boot/grub/splash.xpm.gz.redhat
 65036   12 -rw-------   1 root     root        11364 Jul  4 19:36 /boot/grub/vstafs_stage1_5
 65041    1 -rw-------   1 root     root          254 Jul  4 19:48 /boot/grub/grub.conf
 65037   14 -rw-------   1 root     root        13964 Jul  4 19:36 /boot/grub/xfs_stage1_5
 65028   14 -rw-------   1 root     root        13380 Jul  4 19:36 /boot/grub/e2fs_stage1_5
 65034   15 -rw-------   1 root     root        14412 Jul  4 19:36 /boot/grub/reiserfs_stage1_5
 65027  124 -rw-------   1 root     root       125992 Jul  4 19:36 /boot/grub/stage2
 65031   12 -rw-------   1 root     root        11756 Jul  4 19:36 /boot/grub/iso9660_stage1_5
 65035   12 -rw-------   1 root     root        12024 Jul  4 19:36 /boot/grub/ufs2_stage1_5
 65026    1 -rw-------   1 root     root          512 Jul  4 19:36 /boot/grub/stage1
 65032   13 -rw-------   1 root     root        13268 Jul  4 19:36 /boot/grub/jfs_stage1_5
 65040    0 lrwxrwxrwx   1 root     root           11 Jul  4 19:53 /boot/grub/menu.lst -> ./grub.conf
 65029   13 -rw-------   1 root     root        12620 Jul  4 19:36 /boot/grub/fat_stage1_5
 65030   12 -rw-------   1 root     root        11748 Jul  4 19:36 /boot/grub/ffs_stage1_5
    11    1 -rw-r--r--   1 root     root          171 May 15 16:55 /boot/.vmlinuz-2.6.32-358.11.1.el6.x86_64.hmac
    14  182 -rw-r--r--   1 root     root       185867 May 15 16:55 /boot/symvers-2.6.32-358.11.1.el6.x86_64.gz
    15 3952 -rwxr-xr-x   1 root     root      4046704 May 15 16:55 /boot/vmlinuz-2.6.32-358.11.1.el6.x86_64
 32513    1 drwxr-xr-x   3 root     root         1024 Jul  4 19:41 /boot/efi
 32514    1 drwxr-xr-x   3 root     root         1024 Jul  4 19:41 /boot/efi/EFI
 32515    1 drwxr-xr-x   2 root     root         1024 Jul  4 19:41 /boot/efi/EFI/redhat
 32516  246 -rwxr-xr-x   1 root     root       251274 Dec  6  2012 /boot/efi/EFI/redhat/grub.efi
    13  102 -rw-r--r--   1 root     root       104086 May 15 16:55 /boot/config-2.6.32-358.11.1.el6.x86_64
    16 19996 -rw-r--r--   1 root     root     20474925 Jul  4 19:43 /boot/initramfs-2.6.32-358.11.1.el6.x86_64.img

root@laptop ~# df -Ph /boot/
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             485M   37M  423M   8% /boot

If system will not boot, then you can use SysRescCD or any other Live Linux distro http://www.sysresccd.org/Download

boot sysresccd in 64 bit kernel mode (should be same arch as your OS you want to recover) once booted do following

cryptsetup luksOpen /dev/sda2 rheluncrypted
vgscan
vgchange -ay
mkdir /mnt/rhel
mount /dev/mapper/vg_oc-lv_root_rhel /mnt/rhel
mount --bind /dev /mnt/rhel/dev
mount --bind /dev/pts  /mnt/rhel/dev/pts
mount --bind /proc /mnt/rhel/proc
mount --bind /sys  /mnt/rhel/sys
mount /dev/sda1 /mnt/rhel/boot
chroot /mnt/rhel /bin/bash

grub-install /dev/sda1

grub
root (hd0,0)
setup (hd0)
quit

exit

umount /mnt/rhel/boot
umount /mnt/rhel/sys
umount /mnt/rhel/proc
umount /mnt/rhel/dev/pts
umount /mnt/rhel/dev
umount /mnt/rhel
vgchange -an
cryptsetup luksClose rheluncrypted
sync
umount /boot
fsck -fy /dev/sda1
shutdown -r now