Skip to content

Instantly share code, notes, and snippets.

@alexishida
Created May 19, 2019 04:07
Show Gist options
  • Save alexishida/d3bcd0d57b235de27132ab2c0a7208c4 to your computer and use it in GitHub Desktop.
Save alexishida/d3bcd0d57b235de27132ab2c0a7208c4 to your computer and use it in GitHub Desktop.
Shrink system image on Linux
# Link https://github.com/Adriangarridosanchis/SensorNetwork./wiki/Shrink-system-image-on-Linux
Shrink system image on Linux
benchchair edited this page on 8 Jul 2014 · 16 revisions
Why
The image extracted from a finalized SD card is slightly larger than the capacity of SD card. We need to shrink the size of image so that we can always copy the image into the same capacity SD card or even a smaller one.
# Requirements
A Linux PC or a Linux virtual machine (Virtual machine software: Virtual Box or VMware)
Steps
1 - Creating loopback device
$ sudo modprobe loop
$ sudo losetup -f (Example: /dev/loop0)
$ sudo losetup /dev/loop0 myimage.img
$ sudo partprobe /dev/loop0
2 - Resize partition using GParted
$ sudo gparted /dev/loop0
- Resize the image in the pop-up GParted App.
- Press Apply in GParted.
$ sudo losetup -d /dev/loop0
3 - Shaving the image
$ fdisk -l myimage.img image
$ truncate --size=$[(<Device Boot End>+1)*<IO size bytes>] myimage.img
- Example
$ truncate --size=$[(9181183+1)*512] myimage.img
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment