In the last part of this series, you have learned how to recover from problems occurring in your Master Boot Record.
It has showed you how to get it back once it has been removed. Here we continue with troubleshooting the partition table of the Master Boot Record. Without a partition table, you cannot access any of the information on your computer's hard drive, so if it gets lost, recovering it is critical.
In general, you can encounter two different kinds of problems in regard to partitions. You may have lost the complete partition table, or you may have a problem with the file system on a partition. Here, you'll learn how to recover the partition table. To do this, you need your rescue CD and
gpart to find the exact information about the beginning and end of the partitions on your server's hard disk. Once you've found that, use
fdisk to recreate the partitions. The following procedure shows how to do this.
1. Start your server from the rescue CD and make sure that you open a console where you have root permissions.
gpart /dev/sda to scan your hard drive for all partitions. This may take between five seconds and an hour. Once finished, it will show you partition information:
root@Knoppix:~# gpart /dev/sda Begin scan... Possible partition(Linux ext2), size(7789mb), offset(0mb) Possible extended partition at offset(7789mb) Possible partition(Linux swap), size(400mb), offset(7789mb) End scan.
Checking partitions... Partition(Linux ext2 filesystem): primary Partition(Linux swap or Solaris/x86): primary Ok.Guessed primary partition table: Primary partition(1) type: 141(0x83)(Linux ext2 filesystem) size: 7789mb #s(15952480) s(63-15952542) chs: (0/1/1)-(992/254/61)d (0/1/1)-(992/254/61)rPrimary partition(2) type: 140(0x82)(Linux swap or Solaris/x86) size: 400mb #s(819248) s(15952608-16771855) chs: (993/1/1)-(1023/254/63)d (993/1/1)-(1043/254/59)rPrimary partition(3) type: 000(0x00)(unused) size: 0mb #s(0) s(0-0) chs: (0/0/0)-(0/0/0)d (0/0/0)-(0/0/0)rPrimary partition(4) type: 000(0x00)(unused) size: 0mb #s(0) s(0-0) chs: (0/0/0)-(0/0/0)d (0/0/0)-(0/0/0)r
Evaluate the information that gpart provides carefully, after all, gpart stands for guess partition. It guesses, nothing more, nothing less. On the example server I have swap in a logical parition /dev/sda5. As you can see, gpart found the swap partition with its correct size, beginning and end on disk, but couldn't find that it was a logical partition. Based on this information, you would try to recreate the swap partition on /dev/sda2. Your server would boot with that, but would give errors as well. But, that doesn't really matter because once booted you can check system files like /etc/fstab to find the partition your swap originally was, and repair the partitions.
3. Now that you have found the original partition boundaries, write them down and start fdisk using
fdisk /dev/sda. This shows you a message about your disk's size, ignore that and type
n to start the interface to create a new partition. Next, type
p to create the first primary partition. It asks what partition number you want to assign, type
4. Next comes the important part: you have to specify where the partition originally started and ended. To find this information, you need the C/H/S line in the gpart output for this partition. Consider the line below:
chs: (0/1/1)-(992/254/61)d (0/1/1)-(992/254/61)r
In this line, the first number between brackets indicates the original starting cylinder, which in this example is cylinder 0. The second series of numbers between brackets tells you where the partition originally ended, in this case that is on cylinder 992. There is one catch though: in fdisk the first cylinder is cylinder 1. That means that all other cylinders as displayed with gpart need to be incremented with 1. So, you have to create a partition now that starts at cylinder 1 and ends on cylinder 993. Repeat steps 3 and 4 to recreate your other partitions as well and then close fdisk using the
w command. Fdisk probably gives a message now that the new partition table can be used only after a reboot. The example below shows what has happened so far:
root@Knoppix:~# fdisk /dev/sda
The number of cylinders for this disk is set to 1044. 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 (e.g., DOS FDISK, OS/2 FDISK)Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-1044, default 1): 1 Last cylinder or +size or +sizeM or +sizeK (1-1044, default 1044): 993Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (994-1044, default 994): Using default value 994 Last cylinder or +size or +sizeM or +sizeK (994-1044, default 1044): Using default value 1044Command (m for help): w The partition table has been altered!Calling ioctl() to re-read partition table.WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot. Syncing disks.
5. Reboot your server to activate the changes.
You now have recovered your partitions. It may work, it may not. If it doesn't work, I recommend starting by recreating the first partition first. Try to mount it from the rescue CD and if that works, continue from there, recreating all other partitions you need. Once succeeded to reconstruct the root partition, read /etc/fstab because it gives you invaluable information about the original device names that you've used.
Tip! LVM, extended and swap partitions use another partition type. In fdisk, use
lto get an overview of available partition types, and use
tto change the type of a partition. Don't forget to reboot after changing your partition table.
So now you have learned how to get back your partitions if they were lost. The next part in this series will teach you how to fix problems with LVM logical volumes.
ABOUT THE AUTHOR: Sander van Vugt is an author and independent technical trainer, specializing in Linux since 1994. Vugt is also a technical consultant for high-availability (HA) clustering and performance optimization, as well as an expert on SLED 10 administration.