0

Given a disk layout like this

                                            Disk: /dev/sdc
                                       Size: 119.9 GiB, 127865454592 bytes, 249737216 sectors
                                                 Label: dos, identifier: 0x000efb19

    Device               Boot                   Start              End          Sectors         Size        Id Type
>>  Free space                                   2048             8191             6144           3M                                 
    /dev/sdc1                                    8192          4615234          4607043         2.2G         e W95 FAT16 (LBA)
    /dev/sdc2                                 4615235         29985858         25370624        12.1G         5 Extended
    ├─Free space                              4618240          4620288             2049           1M
    ├─/dev/sdc5                               4620288          4685821            65534          32M        83 Linux
    ├─/dev/sdc6                               4685824          5210109           524286         256M         c W95 FAT32 (LBA)
    └─/dev/sdc7                               5210112         29983811         24773700        11.8G        83 Linux
    /dev/sdc3            *                   29986816         92901375         62914560          30G        83 Linux
    Free space                               92901376        249737215        156835840        74.8G

If I wanted to move sdc3 into sdc2 (thus making it sdc8), how would I do that?

Note: The end of sdc7 is the start of sdc3, so there's no need to actually MOVE any data. This question is just how to change the partition table such that sdc3 is in sdc2

tl;dr How do I change the partition table without actually editing the data in the partitions?

Max Coplan
  • 103
  • 4

1 Answers1

1

First, you should write down or take a picture of all start and end sectors, if anything goes wrong, you can restore it. To be on the safe side backup the beginning of every partition with a filesystem:

dd if=/dev/sdc3 of=sdc3.bak bs=1M count=1

Then use fdisk to:

  1. Delete partitions 2 and 3. This will delete all logical partitions too.

  2. Create a logical partition from sector 4615235 to at least sector 92901375.

  3. Recreate all other partitions taking care of keeping the start sector as it was and a couple of sectors between partitions for the EBR. A recent version of fdisk will warn you that it detected a filesystem signature. Obviously that is a good thing and the block should not be cleared.

fdisk usually tries to keep 1MiB alignment of partitions, so it might be necessary to go into expert mode (x) and use b to adjust the starting sector.

If this is not your primary disk I would go to the effort to change the partition type to GPT and get rid of all DOS'es weird behaviour.

Remark: All filesystems can reside on a partition larger than the FS. Most of them can be resized safely to match the new partition size.

Piotr P. Karwasz
  • 5,748
  • 2
  • 11
  • 21
  • What if I just replaced [original](https://gist.github.com/vegerot/c1c5d86aecc42c90e7a9f8192d0bf0da) with [modified](https://gist.github.com/vegerot/63527f3f989c50d67eb1e4436462611c)? – Max Coplan Dec 07 '19 at 21:27
  • Would I need to change more than just the name and the size of sdc2 (to something that extends it past the end of sdc8)? – Max Coplan Dec 07 '19 at 21:27
  • And when you said in step 1. to delete partitions 2 and 7, did you mean to say 2 and 3? Partition 7 is in the logical partition, and your instructions don't address partition 3, which is what I'm concerned with – Max Coplan Dec 07 '19 at 21:31
  • When you say "couple of sectors between partitions for the EBR", should I just keep the same spacing between partitions as in the original? – Max Coplan Dec 07 '19 at 21:41
  • Yes, you just need to change `sda2` size, **fdisk** should do the rest. Also in the answer I meant "delete sda3", not "delete sda7". I am not sure about your new `sda2` size. It should extend from `4615235` to `92901375`, so it should have a size of `88286141` sectors. Logical partitions in the extended partitions have gaps of at least 1 sector between them to accomodate an EBR. You seem to have enough gaps. – Piotr P. Karwasz Dec 07 '19 at 21:57
  • Replacing [original](https://gist.github.com/vegerot/c1c5d86aecc42c90e7a9f8192d0bf0da) with [this](https://gist.github.com/vegerot/63527f3f989c50d67eb1e4436462611c). Using `sfdisk /dev/sdc – Max Coplan Dec 08 '19 at 21:31