The kernel keeps the partition table in cache permanently (unless explicitly told to reload, and that can't be done if some of the partitions are in use). So you're safe until you reboot (or tell the kernel to operate on data that doesn't reflect the true disk contents; for example, if you've already activated mdraid, it might have written its metadata on the disk already).
If you have an up-to-date backup of your boot sector (the first 512 bytes), you can restore it (
cat boot-sector-backup >/dev/sda — do check that the size of the file you're restoring is exactly 512 bytes). Your bootloader installation may have created a boot sector backup, but if it's been upgraded or you've repartitioned since then, it won't be up-to-date. Do not restore a backup that may be obsolete.
The kernel's information about the partitions is accessible through
/sys/class/block/sda/sda*. In the directory for each partition (
start contains the offset of the beginning of the partition, in 512-byte sectors.
size contains the size of the partition, in 512-byte sectors (except for the extended partition).
If you have partitions numbered 5 or above, they are logical partitions (see What is the difference between "extended" partition and "logical" partition), contained inside an extended partition. There is a single extended partition (or none), and it is one of the partitions 1–4. The file
size does not contain the size of the extended partition, so you first need to determine that; it must be large enough for all logical partitions to fit, and must not encompass any primary partitions (the other partitions numbered 1–4).
fdisk /dev/sda. Use
u to switch the unit to sectors. Create the partitions (
n) with the right offset and size (as the prompt says, put
+ before the number of sectors when it comes to the size), starting with the extended partition.
p to check that the partition table looks right. If some of these partitions are not Linux data partitions, use
t to set their type (
82 for Linux swap,
c for a Windows FAT32 partition,
7 for a Windows NTFS partition). If you have a bootable DOS/Windows partition, set its bootable flag (
Double-check that the output looks good, then press
w to commit the new table to disk.
Save the contents of
/sys/class/block/sda/ in a tar archive on a USB stick. Then reboot from a removable media. **After rebooting, if the partition table you created is not correct, you risk massive data corruption**. So from the removable media, runfsck -n
(don't forget the-n
) to check the consistency of the filesystems on each partition (don't usemount`, which would only work if the offset was correct and could damage the disk (even in read-only mode, because it would write the journal) if the offset was correct but not the size).
If fsck finds no filesystem, you got the offset of a partition wrong. If it reports errors, chances are you got the size of the partition wrong. As long as you haven't written to the disk, you can still fix the partition table. When you have no partition from the disk mounted, pressing
fdisk will make the kernel re-read the partition table. Once you have your partitions right, you should be able to reboot into your normal system and continue as usual.