6

I'm trying to copy the partition structure of a drive that died, all the info I have is that it has an MBR, uses an msdos partition table (not sure if this matters?) and has 9 partitions and looks something like this:

/dev/sda,sector 0 is MBR
/dev/sda1 is kernel (RAW) - 190MB
/dev/sda2 is swap (SWAP) - 478MB
/dev/sda4 is rescue (RAW) - 190MB
/dev/sda5 is root - ext3 - 4.66GB
/dev/sda6 is app - ext3 - 4.66GB
/dev/sda7 is config - ext3 - 6.1MB
/dev/sda8 is user - ext3 - 956MB
/dev/sda9 is maps - ext3 - rest of drive

Since sda3 is "missing" I'm guessing that's the extended partition? However when trying to do that in gparted or the Ubuntu disk tool, the next logical partition gets named sda5 instead of sda4. I'm going to be copying filesystems to the drive that have hardcoded device names in all the scripts so the numbers need to match exactly.

Is there a way to rename the sda5 to sda4? Or maybe I'm completely off on the extended partition thing?

Any help would be greatly appreciated, thanks.

goylik999
  • 63
  • 4

2 Answers2

3

Is there a way to rename the sda5 to sda4?

Yup {and that is why a long answer below this question exists}


Am taking my USB pendrive /dev/sdc as example. Modify for your case accordingy


First know your drive : {Just to check you are using the desired disk }

sudo fdisk -l /dev/sdc

Disk /dev/sdc: 14.32 GiB, 15376318464 bytes, 30031872 sectors
Disk model: Cruzer Blade    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xba104c33

Device Boot Start End Sectors Size Id Type /dev/sdc1 2048 3426303 3424256 1.6G 83 Linux /dev/sdc2 3426304 12898303 9472000 4.5G 83 Linux /dev/sdc3 12898304 20127743 7229440 3.4G b W95 FAT32 /dev/sdc4 20127744 27377663 7249920 3.5G 5 Extended /dev/sdc5 20129792 22233087 2103296 1G 83 Linux

sudo sfdisk -d /dev/sdc

label: dos
label-id: 0xba104c33
device: /dev/sdc
unit: sectors
sector-size: 512

/dev/sdc1 : start= 2048, size= 3424256, type=83 /dev/sdc2 : start= 3426304, size= 9472000, type=83 /dev/sdc3 : start= 12898304, size= 7229440, type=b /dev/sdc4 : start= 20127744, size= 7249920, type=5 /dev/sdc5 : start= 20129792, size= 2103296, type=83

sudo parted -l /dev/sdc

Model: SanDisk Cruzer Blade (scsi)
Disk /dev/sdc: 15.4GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags 1 1049kB 1754MB 1753MB primary ext4 2 1754MB 6604MB 4850MB primary btrfs 3 6604MB 10.3GB 3701MB primary fat32 4 10.3GB 14.0GB 3712MB extended 5 10.3GB 11.4GB 1077MB logical ext2


Note

  • Make sure all partitions unmounted during the partition table edit process.
  • No applications accessing the disk / modifying its contents.

Know your working tool - sfdisk

From manpage

sfdisk is a script-oriented tool for partitioning any block device.

Want more details ??

Run : man sfdisk


Try the tool on the desired drive

sfdisk -d /dev/sdc

If /dev/sdc is the target disk ;

Backup the partition table first sdc.bak

sfdisk -d /dev/sdc > sdc.bak

Get yourself a working copy - sdc.new

cp sdc.bak sdc.new

Get your hands dirty

Open sdc.new in your favorite editor

label: dos
label-id: 0xba104c33
device: /dev/sdc
unit: sectors
sector-size: 512

/dev/sdc1 : start= 2048, size= 3424256, type=83 /dev/sdc2 : start= 3426304, size= 9472000, type=83 /dev/sdc3 : start= 12898304, size= 7229440, type=b /dev/sdc4 : start= 20127744, size= 7249920, type=5 /dev/sdc5 : start= 20129792, size= 2103296, type=83

Note the partition sequence

/dev/sdc1
/dev/sdc2
/dev/sdc3
/dev/sdc4
/dev/sdc5

Now i want partition sequence to be like this

/dev/sdc3
/dev/sdc4
/dev/sdc1
/dev/sdc2
/dev/sdc5

In short my required changes :

/dev/sdc1 ------------> /dev/sdc3
/dev/sdc2 ------------> /dev/sdc4
/dev/sdc3 ------------> /dev/sdc1
/dev/sdc4 ------------> /dev/sdc2
/dev/sdc5 ------------> /dev/sdc5

Edit sdc.new the accordingly

Note-

  • Do not change any numbers except the partition numbers. I am sure you know why.

sdc.new

label: dos
label-id: 0xba104c33
device: /dev/sdc
unit: sectors
sector-size: 512

/dev/sdc3 : start= 2048, size= 3424256, type=83 /dev/sdc4 : start= 3426304, size= 9472000, type=83 /dev/sdc1 : start= 12898304, size= 7229440, type=b /dev/sdc2 : start= 20127744, size= 7249920, type=5 /dev/sdc5 : start= 20129792, size= 2103296, type=83

Write the partition table back

sfdisk /dev/sdc < sdc.new

Sample Output :

Checking that no-one is using this disk right now ... OK

Disk /dev/sdc: 14.32 GiB, 15376318464 bytes, 30031872 sectors Disk model: Cruzer Blade
Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xba104c33

Old situation:

Device Boot Start End Sectors Size Id Type /dev/sdc1 2048 3426303 3424256 1.6G 83 Linux /dev/sdc2 3426304 12898303 9472000 4.5G 83 Linux /dev/sdc3 12898304 20127743 7229440 3.4G b W95 FAT32 /dev/sdc4 20127744 27377663 7249920 3.5G 5 Extended /dev/sdc5 20129792 22233087 2103296 1G 83 Linux

>>> Script header accepted. >>> Script header accepted. >>> Script header accepted. >>> Script header accepted. >>> Script header accepted. >>> Created a new DOS disklabel with disk identifier 0xba104c33. /dev/sdc1: Created a new partition 3 of type 'Linux' and of size 1.6 GiB. Partition #3 contains a ext4 signature. /dev/sdc4: Created a new partition 4 of type 'Linux' and of size 4.5 GiB. Partition #4 contains a btrfs signature. /dev/sdc5: Created a new partition 1 of type 'W95 FAT32' and of size 3.4 GiB. Partition #1 contains a vfat signature. /dev/sdc2: Created a new partition 2 of type 'Extended' and of size 3.5 GiB. /dev/sdc3: Created a new partition 5 of type 'Linux' and of size 1 GiB. Partition #5 contains a ext2 signature. /dev/sdc6: Done.

New situation: Disklabel type: dos Disk identifier: 0xba104c33

Device Boot Start End Sectors Size Id Type /dev/sdc1 12898304 20127743 7229440 3.4G b W95 FAT32 /dev/sdc2 20127744 27377663 7249920 3.5G 5 Extended /dev/sdc3 2048 3426303 3424256 1.6G 83 Linux /dev/sdc4 3426304 12898303 9472000 4.5G 83 Linux /dev/sdc5 20129792 22233087 2103296 1G 83 Linux

Partition table entries are not in disk order.

The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.


Time to cross-check

sudo fdisk -l /dev/sdc

Disk /dev/sdc: 14.32 GiB, 15376318464 bytes, 30031872 sectors
Disk model: Cruzer Blade    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xba104c33

Device Boot Start End Sectors Size Id Type /dev/sdc1 12898304 20127743 7229440 3.4G b W95 FAT32 /dev/sdc2 20127744 27377663 7249920 3.5G 5 Extended /dev/sdc3 2048 3426303 3424256 1.6G 83 Linux /dev/sdc4 3426304 12898303 9472000 4.5G 83 Linux /dev/sdc5 20129792 22233087 2103296 1G 83 Linux

Partition table entries are not in disk order.

sudo parted -l /dev/sdc

Model: SanDisk Cruzer Blade (scsi)
Disk /dev/sdc: 15.4GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags 3 1049kB 1754MB 1753MB primary ext4 4 1754MB 6604MB 4850MB primary btrfs 1 6604MB 10.3GB 3701MB primary fat32 2 10.3GB 14.0GB 3712MB extended 5 10.3GB 11.4GB 1077MB logical ext2


In case things get messed up restore from the backup partition table sdc.bak -

sfdisk /dev/sdc < sdc.bak
Madhubala
  • 2,008
2

I can help with parts of this.

If the disk uses an MBR partition table, then it has to use an extended partition as you suspected because the MBR can only support four primary partitions. This is one of the reasons GPT partition tables are favored. They don't have that limitation. They also support larger partitions.

When Linux looks at a disk drive, it assigns devices to each partition it finds sequentially on the drive. If that drive is the first drive it sees, it will call it /dev/sda and the partitions on it will be assigned starting with /dev/sda1. So you cannot rename these.

It is good practice not to rely on device names for exactly the type of issues you are running into. Most people use the UUIDs of partitions to refer to them which is a big improvement. I go one step further and use unique, human readable/meaningful partition labels to refer to partitions. If you have multiple drives, you cannot guarantee that they will be recognized in the same order at each boot, so a device that was /dev/sdb one time might be /dev/sdc another time. It gets even worse when you have removable disks that don't even get a device file until they are physically attached to the system. UUIDs are more invariant, but will also change if you resize or move a partition. Labels are not modified, but rely on you to name them uniquely.

I don't know why /dev/sda4 is being skipped - especially if swap is already assigned to /dev/sda2. I don't know how to fix that. If you look at the new drive, there should be "something" where /dev/sda4 is supposed to be. Maybe /dev/sda3 fills up the rest of the disk, but even if it does, I would think /dev/sda4 would still be the first extended partition within it. Or, maybe the opposite, it doesn't fill the rest of the disk and so /dev/sd4 is being reserved for the space after the whole /dev/sd3 extended partition.

As a possible workaround, if you have access to another computer, you could build the new drive on it, obtain the UUIDs or labels of all the new partitions and then run all your scripts through sed to change all the device references to use UUIDs or labels. You could even switch to a GPT partitioning scheme if you wanted to.

Joe
  • 586
  • 6
  • 13