Shrink disk partition image: Difference between revisions

From WickyWiki
mNo edit summary
mNo edit summary
Line 24: Line 24:
</syntaxhighlight>
</syntaxhighlight>


Shrink your file system to the minimum size. Note that this does not shrink the image file itself.
Shrink your file system to the minimum size. Note:
* that this does not shrink the image file itself.
* that there can be free space between used space that is not freed


<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>
Line 32: Line 34:
   The filesystem on /dev/loop20 is now 1304407 (4k) blocks long.
   The filesystem on /dev/loop20 is now 1304407 (4k) blocks long.
</syntaxhighlight>
</syntaxhighlight>
1304407 * 4 * 1024 bytes = 5342851072 bytes


Disconnect the device:
Disconnect the device:
Line 38: Line 42:
sudo losetup -d /dev/loop20
sudo losetup -d /dev/loop20
</syntaxhighlight>
</syntaxhighlight>
1304407 * 4 * 1024 bytes = 5342851072 bytes


Truncate the file:
Truncate the file:

Revision as of 22:15, 20 December 2018


Create a copy:

cp org_partition.img new_partition.img

Create a device for the image, note the returned '/dev/loop20' might be different for you:

sudo losetup -f --show new_partition.img

	/dev/loop20

Check and fix the file system:

sudo e2fsck -f /dev/loop20

Shrink your file system to the minimum size. Note:

  • that this does not shrink the image file itself.
  • that there can be free space between used space that is not freed
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.
1304407 * 4 * 1024 bytes = 5342851072 bytes

Disconnect the device:

	 
sudo losetup -d /dev/loop20

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