How to make an image of a bootable flash drive without wasting space (as with dd) on Ubuntu

10,516

Solution 1

If you want to use dd because with it you can cook very quickly other flash memory:

  • set to zero all free space: dd if=/dev/zero of=/media/usb_device_dir/ZEROFILL ; rm /media/usb_device_dir/ZEROFILL this creates a big file filled with zero and then removes it.

  • dd if=/dev/usb_device | bzip2 > os_image.bz2

Or make a tar of your operating system:

cd /media/usb_device_dir
tar jcpvf /home/foo/os_image.tar.bz2 . 

But before extracting this to a clean flash card, you have to

  • format

  • add bootable flag

  • install a bootloader

Solution 2

dd if=/dev/your_usb | lzma -9c > usb_image.bin.lzma shouldn't waste any space.

Solution 3

If you can assume that empty areas on the filesystem are filled with zeros Let_Me_Be's answer applies.

Otherwise have a look at Partimage

Share:
10,516

Related videos on Youtube

mmm
Author by

mmm

Updated on September 18, 2022

Comments

  • mmm
    mmm almost 2 years

    I've got a bootable flash memory card with an Ubuntu operating system on it along with other programs, ie. a fully functioning system I use on a single-board computer. I'd like to back it up on my PC's (also running Ubuntu) hard disk, but without wasting space as the dd command would do (because it also backs up free space). The flash drive is 32GB big and only 10% full. It doesn't have any partitions on it.

    What are the options?

    EDIT the flash drive is formatted as ext4, so partimage doesn't apply.

  • Michael O'Connor
    Michael O'Connor over 12 years
    Is it likely that ext3/ext4 will fill empty space with zeroes? I have a feeling that it is not so, but have nothing to back it up but performance considerations.
  • Michael O'Connor
    Michael O'Connor over 12 years
    Thanks halo, I edited my question, partimage doesn't support ext4, unfortunately.
  • Admin
    Admin over 12 years
    Hi Alessandro. Could you please explain how the first statement creates a "big file"? What is it's size? Should I replace /dev/zero with anything, or is it a special command to dd?
  • Gaurav
    Gaurav over 12 years
    Thanks Let_Me_Be, is there a way to make this particular dd command execute a bit faster? Eg. less compression? Is there a way for the dd command to output any progress indication? And last but not least, what is the command to uncompress the image to your_usb afterwards?
  • Šimon Tóth
    Šimon Tóth over 12 years
    @mmm lzcat usb_image.bin.lzma | dd of=/dev/your_usb
  • Admin
    Admin over 12 years
    And another thing: shouldn't be rm /media/usb_device_dir/*ZEROFILL* ?
  • Jaap Eldering
    Jaap Eldering over 12 years
    pv /dev/your_usb | lzma -9c > usb_image.bin.lzma wil show progress (requires package pv installed).
  • Alessandro Pezzato
    Alessandro Pezzato over 12 years
    Yes, you are right, it is /media/usb_device_dir/ZEROFILL just a copy-paste error. It writes zero to that file until there's no space available.
  • Kamil Šrot
    Kamil Šrot almost 10 years
    Just a note / adding parameter bs=10M to the dd command speeds up the progress dramatically on my PC