2

I have 20 identical (in size) USB drive where I need to put a bootable Ubuntu version on. The drive I use have a capacity of 4GB. I partitioned the drive into two partitions (GPT, FAT32), so I have one partition for the OS and one partition to put data on.

I'm using UNetbootin to create the bootable Ubunutu stick. Everything seems to work fine, but it is extremely slow (~30 min installation time).

Now I'm wondering if I can deep clone the sticks to the remaining 19 sticks somehow, without having to manually partition the drives and running UNetbootin on each and every drive.

If not, maybe an alternative would be to write a script that does the partitioning, creating a bootable partition and then dd the content onto the correct partition?

I'm running macOS, so no Gparted installed or any other fancy Linux tool (probably).

3
  • Only one dd command is enough. Sep 20, 2016 at 12:12
  • So a regular sudo dd if=oldUSB of=newUSB will suffice to copy all the partition tables, boot loader etc?
    – Soift
    Sep 20, 2016 at 12:22
  • Check out this link on how to use dd to accomplish this. (wiki.archlinux.org/index.php/disk_cloning).
    – jc__
    Sep 20, 2016 at 13:11

2 Answers 2

2

dd is doing a full copy of the drive inclusive the partition tables and everything else (like ipor more or less stated).

Something like this should do the trick:

dd if=<your reference usb stick> of=<one of the 19 other usb sticks> bs=32M

You could even first dump the content of your reference usb stick to disk as an image to save it for later use ;-)

2

Depending on how large the USB sticks are this might take a while because you are copying and then writing out a lot of empty space.

There is a better way todo this using a bmap tools, which only writes used blocks in a disk image.

There are a number of tools that I use here which are packaged for Ubuntu / Debian but not installed by default to install them run

sudo apt-get install bmap-tools libguestfs-tools pigz

First you need to create the source image:

sudo dd if=/dev/source of=/path/to/source.img bs=1M

Sparsifying the image

Next you need to remove all the zero's from the image to make a sparse file:

cp --sparse=always /path/to/source.img /path/to/source.img.sparse

You might have noticed that this makes a copy of the image and takes up more space, fortunately we can use cp in a pipe:

sudo dd if=/dev/source bs=1M | cp --sparse=always /dev/stdin /path/to/source.img.sparse

bmap-tools

Now we need to produce some metadata about how the image file is laid out using bmaptool:

bmaptool create -o /path/to/source.img.bmap /path/to/source.img.sparse

Should you want to you can now compress the image file, bmaptool will automatically uncompress the image file. pigz is a parallel gzip compression tool.

pigz /path/to/source.img.sparse

To write the file out to a blank USB stick you can now do:

sudo bmaptool copy --bmap /path/to/source.img.bmap /path/to/source.img.sparse.gz /dev/sdX

https://source.tizen.org/documentation/reference/bmaptool

Guestfish

If you are feeling extra clever you can also use tools from the guestfish project to clean up the linux image before you clone it. This removes things like logfiles, server ssh keys etc.

Run these commands before you compress the image to clean up the image.

This command cleans up the image, removing logs, history files, ssh server keys etc

virt-sysprep -a /path/to/source.img.sparse

This command sets up ssh with new server keys on the first boot.

virt-customize --firstboot-command "dpkg-reconfigure openssh-server" /path/to/source.img.sparse

This command runs an fstrim on the image which deletes all unused blocks from the filesystem, just like trimming an SSD.

virt-sparsify --inplace /path/to/source.img.sparse

http://libguestfs.org/

1
  • Now I notice it's for macOS... Sep 23, 2016 at 22:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .