Home of the Squeezebox™ & Transporter® network music players.
Results 1 to 9 of 9
  1. #1
    Senior Member chill's Avatar
    Join Date
    Mar 2007
    Posts
    758

    Can I resize a disk image?

    I'm hoping someone can tell me how to resize a disk image. Here's my problem:

    I created a bootable USB by writing an image file using 'Etcher' on my Macbook. The source image (this one) requires a 2GB stick. I have a couple of sticks that are nominally 2GB, but in fact they are a fraction too small (1.99GB), so Etcher fails at the last moment. I have another nominally 2GB stick, which is in fact 2.1GB, so Etcher successfully writes the image. It creates a disk that has three partitions (boot=64.0MB, swap=255.9MB, linux=1.7GB)

    I've then made some changes to that base image (by installing Jivelite on a Joggler), and I'd like to take an image of the result, so that it will fit on another '2GB' stick.

    If I do a 'dd' on e.g. /dev/sdb, it makes an image of the entire disk, including all three partitions (boot, swap, linux), but the resulting image is now 2.1GB (even though the three partitions are exactly the same size as the original image). The disk image size seems to be fixed to the physical size of the USB hardware. So now, if I want to put that image on another stick, the stick will have to be at least 2.1GB.

    So is it possible to reduce the disk image somehow, so that it's no bigger than the sum of the three partitions?

  2. #2
    Senior Member
    Join Date
    May 2009
    Location
    Clacton-on-Sea, Essex. UK
    Posts
    533
    Quote Originally Posted by chill View Post
    I'm hoping someone can tell me how to resize a disk image. Here's my problem:

    I created a bootable USB by writing an image file using 'Etcher' on my Macbook. The source image (this one) requires a 2GB stick. I have a couple of sticks that are nominally 2GB, but in fact they are a fraction too small (1.99GB), so Etcher fails at the last moment. I have another nominally 2GB stick, which is in fact 2.1GB, so Etcher successfully writes the image. It creates a disk that has three partitions (boot=64.0MB, swap=255.9MB, linux=1.7GB)

    I've then made some changes to that base image (by installing Jivelite on a Joggler), and I'd like to take an image of the result, so that it will fit on another '2GB' stick.

    If I do a 'dd' on e.g. /dev/sdb, it makes an image of the entire disk, including all three partitions (boot, swap, linux), but the resulting image is now 2.1GB (even though the three partitions are exactly the same size as the original image). The disk image size seems to be fixed to the physical size of the USB hardware. So now, if I want to put that image on another stick, the stick will have to be at least 2.1GB.

    So is it possible to reduce the disk image somehow, so that it's no bigger than the sum of the three partitions?
    Yes there is...

    I've done this a number of times but not for a long time and you will need to adapt paths and sizes for your needs. You will also need to install 'GParted' onto Ubuntu if it's not already there.

    Hope the following helps...

    How to reduce size of disc image
    ====================
    This needs to be done on a Linux system - I use Ubuntu

    Copy image file from iMac to Ubuntu PC

    1. Open terminal...
    sudo modprobe loop
    sudo losetup -f
    sudo losetup /dev/loop0 /path_to_image/name_of.img
    sudo partprobe /dev/loop0
    sudo gparted /dev/loop0
    at this point, terminal will hang but leave as is for now...

    2. Reduce size of partition using GParted (Ignore errors in terminal)
    Select partition
    Option...
    Partition
    Resize/Move
    Decrease Partition Size (to fit: ...as required)
    Press button "Resize/Move"
    Apply (press on tick on menu bar)
    Wait for up to 15mins for completion (show details)
    Press "Close" when complete

    When completed open another terminal instance...
    sudo losetup -d /dev/loop0

    Close first terminal instance - this will quit GParted

    3. Shave off unallocated space...

    fdisk -l /path_to_image/name_of.img

    Make a note of...
    The partition end xxxxxxxx (shown under End for last partition)
    The block-size (usually 512 bytes) - shown as sectors of 1 * 512 = 512 bytes

    truncate --size=$[(xxxxxxxx+1)*512] /path_to_image/name_of.img

    4. Copy shrunk image back to iMac

    Q.E.D

    Go through 1 & 2 again to check it worked.

    Source URL
    https://softwarebakery.com/shrinking-images-on-linux
    2 x Touch
    2 x Radio
    2 x Boom
    1 x Intel-NUC server/squeezelite running LMS 7.91 on Windows 10
    1 X Odroid-XU4 server/squeezelite running LMS 7.91 on Ubuntu 16.04
    1 x iMac server running macos Hich Sierra
    WaveIO USB into Lavry DA-10 DAC
    Starfish Pre-amp : Based on NAIM NAC 72
    Heavily modified NAIM NAP 250 Power-amp
    Behringer DEQ2496
    Linn Isobarik DMS

  3. #3
    Senior Member
    Join Date
    Jan 2011
    Location
    Staffordshire. UK
    Posts
    2,485
    Sorry its a bit of a bodge

    How I do it now is with a program called pishrink (I have only used images based on raspbian, but it will work, I believe, with Ubuntu stuff).

    I keep meaning to try but keep getting side tracked.

    The easiest way is to make either a live usb stick of a Linux distro or a live gParted usb stick.

    On a Windows machine start up with the live distro and navigate to gparted. (Should work on a Mac, restart from the usb stick)

    I used to use a gParted live usb.

    Make a note of the disks that are in the top right dropdown, it will end in something like /sda

    Add the usb stick to be resized to the machine.

    Refresh Devices in gParted and the new stick will show in thw same dropdown, say /sdb

    Select the one you want resize, Make sure it's the correct one!!!!

    Highlight the root partition (in my example it's /dev/sb3)

    Click on Partition > Resize/Move

    adjust

    Click on Resize


    Click on Edit to complete


    Name:  should be one.jpg
Views: 122
Size:  194.9 KB


    Name:  should be 2.jpg
Views: 121
Size:  195.5 KB


    Name:  should be 3.jpg
Views: 121
Size:  195.5 KB

    I would recommend a dummy run through ........Really
    Last edited by Man in a van; 2019-01-22 at 09:03.

  4. #4
    Senior Member chill's Avatar
    Join Date
    Mar 2007
    Posts
    758
    Thanks guys. It's that step to shave off the unused space that I was looking for - that will enable me to make sure that my modified image doesn't keep growing just because it's being produced from a bigger USB stick. But the bit about shrinking the partitions could also be useful, to allow me to fit the original Ubuntu image onto my slightly-less-than-2GB USB drive. I guess there's some free space on the 3rd ('Linux') partition, so potentially it could be reduced in size by a few MB, but is there any way to be certain that the free space is all at the end? Or does the partition resizing work in such a way that used sectors are moved to somewhere safe?

  5. #5
    Senior Member
    Join Date
    Jan 2011
    Location
    Staffordshire. UK
    Posts
    2,485
    If you look at the screen shots you can see the occupied part of the root partition is a different colour and the used and unused size is also noted.


    On Windows the latest version of Win32 lets one back up only the used part of the partition.


    Using piShrink, the size of the image is reduce to the smallest possible (a little bit above the actual used size).

    By default the new image will expand to the full size of the usb stick that it is burned to.

    This can be avoided by selecting a parameter on creation.

    piShrink will work on a raspbian pi image.

  6. #6
    Senior Member chill's Avatar
    Join Date
    Mar 2007
    Posts
    758
    Great stuff, thank you - just setting up an Ubuntu desktop as a virtual machine, to give this a try.

  7. #7
    Senior Member chill's Avatar
    Join Date
    Mar 2007
    Posts
    758
    Those steps worked a treat. First I set up a VM with Ubuntu desktop 18.04. Then I tried the 'truncate' command to see if I could fit the image onto my stingy 2GB stick (actually 1.9GB). The truncate worked perfectly, but the image was still a frustrating 4.19MB too big. So then I shrank the third partition ('linux-root') by 16MB (using the loopback approach and gparted), truncated the image again, and wrote it to my 1.9GB USB stick.

    It booted and ran perfectly. Mission accomplished, thank you. And since it fits that stick it should fit most 2GB sticks.

    Interestingly, in the comments under the linked article in Bobertuk's post, it seems that the initial advice was to use 'dd if=myimage.img of=myimage_shrinked.img bs=BBB count=CCCCCC' rather than truncate. So another tip to file away.

  8. #8
    Senior Member
    Join Date
    Aug 2012
    Location
    Austria
    Posts
    805
    An alternative would be using fsarchiver, which can save partitions and restore them to smaller ones.

    Backup (assuming boot is on sdb1, and root on sdb2)
    Code:
    fsarchiver savefs -Z 18 -j 4 rpi3-rootfs.fsa /dev/sdb1 /dev/sdb2
    (create new partitions of appropriate size, or resize existing ones)
    Restore:
    Code:
    fsarchiver restfs -j 4 rpi3-rootfs.fsa id=0,dest=/dev/sdb1 id=1,dest=/dev/sdb2
    As an added bonus. it will be faster and the archive size will be smaller, since only the used data will be saved, and not the whole SD card.

  9. #9
    Senior Member chill's Avatar
    Join Date
    Mar 2007
    Posts
    758
    Quote Originally Posted by Roland0 View Post
    An alternative would be using fsarchiver, which can save partitions and restore them to smaller ones.
    .
    .
    .
    As an added bonus. it will be faster and the archive size will be smaller, since only the used data will be saved, and not the whole SD card.
    Excellent, thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •