10

I downloaded the official ubuntu-mate-16.04.img.xz (which is about 1.1GB) and extracted it. But the extracted .img is about 8.1 GB which is about 162 MB larger than the size of my sd card. So, I can't restore the .img to sd card. In other words Raspberry Pi MATE 16.04 Image is too Large for 8GB sd card

Since the .xz file was so small compared to .img, I suspect that the .img has free space, so I mounted the img and found that there was 4GB free space.

How do I resize the .img to reduce its size so that I can restore it to my sd card?

Ashwin Kumar k
  • 301
  • 2
  • 10

3 Answers3

10

Ok so I finally found an answer and I wish to share it with you guys. My guess is that the creators created the compressed file from an expanded image by mistake.

The fact that the compressed file is nearly 1.1 GB, compared to 8.1 GB of the extracted files, proves this. Empty space will compress to a couple of bytes. If there were additional packages, there would have been a noticeable size disparity between the two images.

Shrinking the image

$ sudo modprobe loop # to enable loopback
$ sudo losetup -f  #request a new loopback device

#This will return the path to a free loopback device.this is /dev/loop0 for me

$ sudo losetup /dev/loop0 ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img
$ sudo partprobe /dev/loop0
$ sudo gparted /dev/loop0

Now a separate window will show up with /dev/loop0 .. you will see two mounted partitions a boot and a root partition.

gparted snapshot

In my case the root partition had about 3.74 GB unused space, so right click on root and click resize and reduce the free space to about 10-20MB then click apply.

enter image description here

Now we don't need the loopback-device anymore, so lets unload it:

$ sudo losetup -d /dev/loop0 

Deleting unallocated part

$ fdisk -lu ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img

Here is a sample output:

 $ fdisk -lu ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img Disk
 ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img: 7.5 GiB, 8053063680
 bytes, 15728640 sectors Units: sectors of 1 * 512 = 512 bytes Sector
 size (logical/physical): 512 bytes / 512 bytes I/O size
 (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk
 identifier: 0xd882991d

 Device                                            Boot  Start      End
 Sectors  Size Id Type
 ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img1 *      2048   133119
 131072   64M  c W95 FAT3
 ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img2      133120 11487231
 11354112  5.4G 83 Linux

Then truncate the partition:

$ truncate --size=$[(11487231+1)*512] ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img

Note the number 11487231 in the above command depends on what your output for fdisk is.

Now you should be ready with the image!

goobering
  • 10,750
  • 4
  • 40
  • 64
Ashwin Kumar k
  • 301
  • 2
  • 10
3

Are sure it's Raspberry Pi dedicated image file? Your filename doesn't specify that it's for the Raspberry Pi. You can download the correct image from here: https://ubuntu-mate.org/raspberry-pi/ and also follow the guide to write it on your sd card.

Jacobm001
  • 11,904
  • 7
  • 47
  • 58
Huczu
  • 1,251
  • 9
  • 13
0

There is also a pishrink script that does this: https://github.com/Drewsif/PiShrink

Viesturs
  • 101
  • 2