Friday, January 15, 2010

Reorder partition numbering and recover the grub2

Before we reorder the partition numbering it is a good idea to have a USB startup disk ready which can be created in ubuntu or other windows softwares. Reordering partition number can cause the failure of the booting files of grub2.

* reorder partition numbering

If you move partitions, change their sizes and create new ones, the numbering might get mixed up (e.g. with gparted). The partitions might look like these:
/dev/sda1 ntfs /media/win
/dev/sda2 ext4 /
/dev/sda3 ext3 /boot
/dev/sda4 extended
/dev/sda8 linux-swap
/dev/sda6 fat32 /media/programs
/dev/sda7 fat32 /media/testdisk
/dev/sda5 fat32 /media/stuff

If you want to reorder the partition numbering for whatever reason, fdisk will help. Execute fdisk /dev/sda (as you can see, I want to reorder the numbering of my first SCSI disk) after becoming root.

# fdisk /dev/sda

The number of cylinders for this disk is set to 19457.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs

Command (m for help):

If m is entered at the prompt, the possible commands will be listed. Type x to activate the extra functionality (experts only). Press m again to see new expert options. Enter f (fix partition order) and w to write the changes to the disk and exit.

*Recovering of Grub 2*

As we mentioned before, the reordering of the partitions will cause failure of grub2 and the systems installed in other than the number reordered partitions can not boot anymore. In this end, we boot up the system by the USB memory key:

1.Find the partition of the target system, say, sda2

sudo fdisk -l

2. Mount the target partition.
sudo mount /dev/sda2 /mnt
sudo grub-install -–root-directory=/mnt/ /dev/sda

3. Install Grub2
sudo update-grub

Another way to do that is as follows:
$sudo fdisk -l

This will show your partition table. Here is my table to understand it better :

/dev/sda1 29 8369 66999082+ 83 Linux
/dev/sda2 * 8370 13995 45190845 7 HPFS/NTFS
/dev/sda3 13996 14593 4803435 5 Extended
/dev/sda5 13996 14593 4803403+ 82 Linux swap / Solaris

Now i will mount Linux (sda1 here)

$sudo mount /dev/sda1 /mnt
$sudo mount --bind /dev /mnt/dev
$sudo mount --bind /proc /mnt/proc

The following command is optional (it copies resolv.conf)
$sudo cp /etc/resolv.conf /mnt/etc/resolv.conf

Now chroot into the enviroment we made :
sudo chroot /mnt

Now, you will run commands as root.

You may want to edit /etc/default/grub file to fit your system (timeout options etc)

#nano -w /etc/default/grub

Play with the options if you want.(But do not forget to give grub-update command if you saved it )

Now install/recover Grub2 via :

#grub-install /dev/sda
command.However you may get errors with that code like me.If so please use this command :
#grub-install --recheck /dev/sda

Now you can exit the chroot, umount the system and reboot your box :
$sudo umount /mnt/dev
$sudo umount /mnt/proc
$sudo umount /mnt
$sudo reboot



At 5:13 AM , Anonymous Anonymous said...



Post a Comment

Subscribe to Post Comments [Atom]

<< Home