0

I've got a disk image that I need shortened slightly (long story - but it deals with variances in flash drive sizes). The partitions are already short enough, but the drive size is not.

Basically, I have a disk image that is 15678308352 bytes (30621696 sectors). I'm wanting to reduce the disk image size to 15678292992 bytes (30621666 sectors). The last sector being used by any "real" partition is 30621662 (I'm fine ending there, too, but I padded it by 4 sectors just to be safe). In any case, I'm wanting to resize the partition tables, but I don't really know how, especially since the GPT table is inside a "protected MBR".

Here's the output of fdisk showing the protected MBR:

fdisk mydisk.img 
Disk: mydisk.img    geometry: 1906/255/63 [30621696 sectors]
Signature: 0xAA55
         Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
 1: EE    0   0   2 - 1023 255  63 [         1 -   30621695] <Unknown ID>
 2: 00    0   0   0 -    0   0   0 [         0 -          0] unused      
 3: 00    0   0   0 -    0   0   0 [         0 -          0] unused      
 4: 00    0   0   0 -    0   0   0 [         0 -          0] unused  

Here's the output of gdisk showing the GPT info:

GPT fdisk (gdisk) version 1.0.10

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present


Found valid GPT with protective MBR; using GPT.

Command (? for help): p
Disk mydisk.img: 30621696 sectors, 14.6 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): BD99B4B8-8070-0648-AEA4-B28BCD949EB7
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 30621662
Partitions will be aligned on 2048-sector boundaries
Total free space is 18398 sectors (9.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            8192          249855   118.0 MiB   8300  primary
   2          260096        30621662   14.5 GiB    8300  primary

As you can see, I have plenty of room to shrink the disk but I just don't know how.

I tried manually editing the MBR partition size in hexedit, but then I got all sorts of warnings on the GPT partition.

Any ideas?

1
  • You really just need to make sure the last (LBA-wise) partition is "small enough" so that there would be room for the backup GPT (which is 33 blocks long). The PMBR (LBA 0) and the primary GPT (LBA 1-33) at the beginning won't even be relevant regarding this matter. The backup GPT can be reconstructed from the primary one with gdisk or so. (Some tools might even "auto-repair" for you.) Certainly you can "splice" the image with dd and/or head/tail or so yourself too. Commented Jul 10, 2024 at 23:56

1 Answer 1

0

There might be other tools that do this, but I went ahead and wrote my own tool for doing this. I made a Docker image that does this.

Given a multipartition image (YOURIMAGE.img) in your current directory that you want to shrink, run the following command:

docker run --privileged -v .:/app johnnyb61820/diskshrinker YOURIMAGE.img

If you download the image, the file doing the work is /usr/bin/diskshrinker.sh. However, if anyone is interested, the basic steps are:

  1. Use losetup -Pf to create a loopback device out of the file (needed to use e2fs tools on a partition of the image)
  2. Force a FSCK
  3. Do resize2fs -M to minimize the partition size
  4. Use tune2fs to find the number of blocks and blocksize
  5. Undo the loopback device
  6. Calculate new size
  7. Use sgdisk to find current partition info, INCLUDING the partition code and GUID (necessary for some bootloaders)
  8. Remove and re-add the partition with the new, smaller size.
  9. Setup the partition code and GUID.
  10. Truncate the image using truncate to the new size.
  11. Previous step causes minor corruption and inconsistencies, use sgdisk -C to fix the MBR and sgdisk -e to setup the backup GPT partition.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.