Shrink disk partition image

From WickyWiki
Revision as of 22:10, 20 December 2018 by Wilbert (talk | contribs) (Created page with "Category:Ubuntu Category:Ubuntu System Category:Raspberry Pi Category:201812 Create a copy <syntaxhighlight lang=bash> cp org_partition.img new_partition.img...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Create a copy

cp org_partition.img new_partition.img

Create a device for the image, note the returned /dev/loop20 for later use

sudo losetup -f --show new_partition.img

	/dev/loop20

Check and fix the filesystem:

sudo e2fsck -f /dev/loop20

Shrink your file system to the minimum size. Note that this does not shrink the image file.

sudo resize2fs -M /dev/loop20

  Resizing the filesystem on /dev/loop20 to 1304407 (4k) blocks.
  The filesystem on /dev/loop20 is now 1304407 (4k) blocks long.

Disconnect the device:

	 
sudo losetup -d /dev/loop20
1304407 * 4 * 1024 bytes = 5342851072 bytes

Truncate the file:

truncate -s 5342851072 new_partition.img

Enlarge the file system again to fit available space:

	 
resize2fs -f new_partition.img

  The filesystem is already 1304407 (4k) blocks long.  Nothing to do!


Check and allow the tool to fix errors:

e2fsck -f new_partition.img
 
 Pass 1: Checking inodes, blocks, and sizes
 Pass 2: Checking directory structure
 Pass 3: Checking directory connectivity
 Pass 4: Checking reference counts
 Pass 5: Checking group summary information
 Block bitmap differences:  -(531654--531655) -(532478--532479) -(857474--858485)
 Fix<y>? yes
 Free blocks count wrong for group #16 (815, counted=819).
 Fix<y>? yes

See also