Shrink disk partition image: Difference between revisions

From WickyWiki
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..."
 
mNo edit summary
Line 4: Line 4:
[[Category:201812]]
[[Category:201812]]


Create a copy
Create a copy:


<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>
Line 10: Line 10:
</syntaxhighlight>
</syntaxhighlight>


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


<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>
Line 18: Line 18:
</syntaxhighlight>
</syntaxhighlight>


Check and fix the filesystem:
Check and fix the file system:


<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>
Line 24: Line 24:
</syntaxhighlight>
</syntaxhighlight>


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


<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>

Revision as of 22:12, 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.

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