Wednesday, July 30, 2014

Simple GRUB2 Tutorial for making Rescue Media

 

GRUB2 is full featured multipurpose boot loader that can be booted from virtually any storage media and even from network ( PXE ).

Required:

Any Linux distribution with GRUB2 & Gparted package installed ( I am using Ubuntu 14.04 ) or you can download grub2 & Gparted source from Internet & build it on your Linux distribution.

Steps to Install GRUB2 on Pen Drive :-

  1. Make backup of all data on pen drive
  2. start Gparted as root ( or from Terminal as “ sudo gparted “ )
  3. Select Disk & then Delete all partitions on Pen drive ( usually there is only one ). Click on Green Checked button to Start.
    12
  4.  Now create first partition for your general use. Leave some space in according to your rescue Linux distribution size ( e.g. I want to use Porteus on second drive so its size is around 150MB so I am leaving 200MB free Space to create another partition from that space ) .
  5. Choose File system according to user operating system. E.g. Windows : NTFS or FAT32   Linux: EXT2,3 or 4 or BTRFS, ZFS… 

    Create Partition as Primary Partition ( Default ). (Don’t create second partition first as Windows will only detect & show First partition on Removable storage e.g. Pen drive )3

  6. Now create second partition as EXT4 . It must be Primary Partition.45
  7. Now our Partition setup is Done. Structure looks like this.6
  8. Now note device node string in Partition Column for second partition. Its here /dev/sdb2 or if you done these steps already & don't know device node string then type in Terminal “ sudo fdisk –l” & note down second entry of device7
  9. Lets install Grub2 by typing this command on Terminal. “sudo grub-install <Disk node> –-boot-directory=<mount point of second partition>/boot”. In my case its   “ sudo grub-install /dev/sdb –boot-directory=/media/ankush/System/boot ”.

    Here Disk Node is what you see in fdisk output:  Disk /dev/sdx : some size in MB’s or GB’s, Byte Count & its not /dev/sdxY ( means we want only device node not the partition node as GRUB2 gets installed in MBR not in VBR).

    Now Mount Point is /mnt/sdxY or /media/<username>/<Volume Name> or <UUID> ( in Ubuntu ). If you don't know mount point then Google it for your Linux distribution.8

  10. Now run “ sudo grub-mkconfig –o <mount point of second partition>/boot/grub/grub.cfg “ .  This will create grub.cfg which is Grub2 configuration / menu file here –> boot/grub/grub2.cfg9
  11. Now we need to edit this grub.cfg file to add out Rescue Linux entries.

    Now we can specify partition in grub menu entry as (hdx,y) (here x is hard disk or storage device no. e.g.. if 2 hard disk attached then hd0 will be first & hd1 will be second & y is partition number.) e.g. (hd1,1) which is fine but if partition ordering changes entries will not boot as partition number has changed. ( Number will change if you delete partition or create new partition or even by BIOS ordering of devices which is different on different platforms)

    We can specify partition by searching its UUID ( for NTFS & FAT its serial number ) which is best way because even if partition ordering changes they are still bootable but UUID changes every time we format that drive. ( So change UUID in menu entry after you format drive)

    We can specify partition by its Name but if two partition having same name then first one will be chosen.

    We are using UUID searching here.

    Now to search UUID of our partition type “ sudo blkid –o full ” to know UUID.

    10

  12. Now to make grub directory editable type “ sudo chmod –R 755 <path to boot folder>/* “. Now open grub.cfg by Text Editor program e.g.. vi or nano or Gedit or KEdit. To change grub2 background image search for background_image string & copy your jpg or png image to grub directory on pen drive & specify its path. Now if its jpeg then add insmod jpeg, if png then insmod png as shown in screenshot11
  13. Now extract your Rescue Linux files to pen drive. & add its entry to grub.cfg. there is grub2 helper program to convert from syslinux/extlinux menu to grub menu entries or Google it. Now to set root variable add line “ search -–no-floppy –-fs-uuid –-set <UUID that you got from step 10> “ to set root drive variable.12
  14. Now if everything ok then you have your bootable pen drive ready. As second partition is not gets mounted on Windows, malware cant affect it directly, different filesytem  keeps it away from harm.
  15. Optionally you can protect grub entries by password. Add –-users “allowed username” in menu entry's first line before {1312.1
  16. To make grub2 bootable disk create folder on desktop & run “ grub-mkrescue <folder path> –o bootable.iso“. Add grub.cfg in grub folder specify root as set root=(cd) & remove search lines if any & also add rescue Linux files to folder BEFORE RUNNING COMMAND ( grub-mkrescue takes folder content & outputs bootable iso without grub.cfg so folder structure must have grub.cfg in boot/grub/grub.cfg to make it work )

 

 

 

 

 

 

 

 

 

 

 

 

No comments:

Post a Comment