Create a bootable USB stick from an ISO

Great advice given in this thead. I’m being the “community secretary” and collecting it to one post, for a step-by-step guide, in case someone else is wondering how to create a bootable USB stick from an ISO file using command dd in terminal.

Mandatory warnings:

  • When using dd always TRIPLE CHECK the if (input file, the source) and of (output file, the target) assingments. Do it wrong and you just borked your hard disk!
  • Always check the drive assignment with dmesg or fdisk -l. Do not trust “it’s always been /dev/sdc”.

CREATING A BOOTABLE USB STICK FROM AN ISO FILE USING dd IN TERMINAL

Plug the USB stick in. Check dmesg output to find out the device assignment (or check with sudo fdisk -l).

$ dmesg

Look for line “Attached … removable disk” close to the end of the listing. In this example output you can see [sdc] which means it’s /dev/sdc. We will use that in the following steps.

[1234.1234] sd 7:0:0:0: [sdc] Attached SCSI removable disk

Make sure the USB stick is unmounted (ignore the possible error messages for individual partitions):

$ umount /dev/sdc*

Issue the needed commands (dd and its friends)all in one go, individual commands separated by a semicolon:

$ sudo dd if=/home/username/Downloads/example.iso of=/dev/sdc bs=1M status=progress conv=fdatasync;sudo eject /dev/sdc

Commands explained:

sudo dd - will copy “bit-by-bit”
if - input file; name of the ISO file with full path
of - output file; target device (figured out in step 1).
bs - bytes to read and write at a time (10M was suggested, I use 1M, default is 512, I believe)
status=progress - shows a progress bar so you know something is happening (I’ve noticed it’s not always working)
conv=fdatasync - flush buffers to disk, making sure everything is written to the USB disk (does same as a separate “sudo sync” command would do, as pointed out by @ouroumov in this post further down this thread)

sudo eject - unmounts the partitions (if mounted) and detaches the drive. You can now unplug the stick.

WANT YOUR USB STICK BACK FOR DATA USE?

dd stands for Disk Dump, which describes it perfectly. It “dumps” the source disk to your target disk as is, bit-by-bit. In other words: makes a clone. So if you are using a 4 GB stick for a 1.2 GB ISO, you won’t have 2.8 GB at the ready for storing your holiday snaps.

When you want to return the stick to regular data saving use, you need to reformat it. If you prefer using GUI tools, you can do this in GParted or Disks. There are also apps made just for this purpose.

I like the command line, so here are instructions for working in the terminal.

REPARTITION AND FORMAT USB STICK FOR DATA USE

Plug the stick in. Check drive assignment with dmesg (right after plugging the stick in) or with sudo fdisk -l. We use /dev/sdc in this example.

Make sure drive is not mounted:

$ sudo umount /dev/sdc*

Wipe off file system and/or partition table signatures from the device (to ensure fdisk in next step works without issues):

$ sudo wipefs --all /dev/sdc

Create a fresh, new partition table, one new partition (primary) and write the changes to disk. All in one go, again in order to avoid navigating the fdisk menus:

$ echo -e "o\nn\np\n1\n\n\nw" | sudo fdisk /dev/sdc

The “o\nn\np\n1\n\n\nw” stanza explained (\n after each individual option stands for line feed):

echo o # Create a new empty DOS partition table
echo n # Add a new partition
echo p # Primary partition
echo 1 # Partition number
echo   # First sector (Accept default: 1)
echo   # Last sector (Accept default: varies)
echo w # Write changes

Format stick as VFAT (32-bit fat) and give it label WINDISK:

$ sudo mkfs.fat -F32 -v -I -n "WINSTICK " /dev/sdc1

Remove the disk safely:

$ sudo eject -v /dev/sdc
5 Likes

Can’t preparing stick for data use be done be done by Gparted?

Based on samuvuo’s post, I wrote this.

#!/bin/bash

#Ubuntu_Mate 16.04

#Prepare pen drive to hold data

#gxmessage -fg red -font 'sans 20' -timeout 2 'Preparing to create a Data Pen Drive.'

echo tttt | sudo -S sudo umount /dev/sdc*
echo tttt | sudo -S sudo wipefs --all /dev/sdc
echo tttt | sudo -S echo -e "o\nn\np\n1\n\n\nw" | sudo fdisk /dev/sdc
echo tttt | sudo -S sudo mkfs.fat -F32 -v -I -n "DATASTICK " /dev/sdc1
echo datastick > /media/andy/DATASTICK/datastick.txt

I would like to modify the last command so that the file is named month_day_year_datastick.txt
That way a person can see at a glance when the stick was prepared.

P.S. I wish I knew why a cut and paste results in the all fonts not being the same size.
They are all the same in my scripts.

Maybe the boards software misinterprets certain chars. (# etc ?)

Yes, as already mentioned in the post:

Great post, I learned stuff.

I just wanted to discuss a couple of things, for the sake of completeness:

  • “The Linux Programming Interface”, in chapter 13 on “File I/O Buffering”, concludes that near optimal buffer size for I/O throughput is 4096 bytes. It probably doesn’t make a lot of difference but for that reason I’ve taken to using bs=4M as argument.
  • I’ve been using dd option: conv=fdatasync instead of doing a separate sync command, and I always neglected to run eject, did you ever see instances where sync was not sufficient? Have you tried conv=fdatasync?

PS:

I always thought it was “DiskDestroyer”, but that makes more sense. ^^

1 Like

As a note, you can also add a partition for data to dd’d pendrive if said drive is big enough with out ruining the installation as the extra space is just wasted, therefore, no need to reformat and loose the installation. This can easily be done with gparted, etc.

Heathrobinson.

1 Like

Please note that 4096 bytes = 0.00390625 MB. 4M = 4096 kilobytes (4194304 bytes).

The optimal buffer size has to do with sector size of the target device, so it’s device dependent. The output of sudo fdisk -l shows that my USB sticks and smaller (and older) HDDs have:

Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

My 1 TB HDD, on the other hand, uses the 4096 bytesector size you mention:

Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

You are right about conv=fdatasync, of course. Using it eliminates the need for a separate sync command. I will edit the original post to reflect this. Thank you. :slight_smile:

As for eject: while it’s totally safe to remove the stick after dd command has finished and all data written to the device (using conv=fdatasync or separate sync) I’ve made it a habit to eject as well, since it removes the device nodes and re-mounting the device is possible only after unplugging and replugging the stick. You can file that under “security” or next to “tinfoil hat” while listening to “Oops, I did it again”… :grin:

Actually, I used to use udisks --detach, but after udisks was replaced with udisks2 that method is no longer available. Udisks2 has udisksctl, but it doesn’t have the “detach” option - only “power-off” which has the unwanted side effect of powering down the port “upstream”. Not really desired when the stick is connected to a hub (you might have other devices plugged in) or if one is using a 4-in-1 card reader (reboot is needed to get it working again).

I think DiskDestroyer does! :joy:

2 Likes

Wow. Thanks for pointing that out. And I knew it too. <_< I guess you just stop thinking about stuff after doing them for a while.

Nice, I didn’t know that. May I ask what command you use to list the Sector size info?

I noticed killing the process with SIGUSR1 causes it to dump progress info, maybe this is more accurate?

I knew you knew. And I’m sure you knew I knew you knew. :grinning:

The output is from command “sudo fdisk -l”. Sorry, I forgot to add it in my post.

2 Likes

Maybe slightly off topic, but on my Ubuntu Mate 16.04 laptop I ran
System -> Administration -> Startup Disk Creator

That created a bootable USB stick from an ISO file.

Do not use dd for copying isos to an usb stick.
Just use the tool that is meant for it: cp

cp handles all the blocksizes and buffers for you better than dd ever can.

for example:

cp /path/to/my/iso.iso /dev/sdd

Try also this instruction https://bestin-it.com/ubuntu-18-04-how-to-prepare-installable-usb-pendrive/ that clearly explains in few simple steps how to create linux stick.

I tend to differ ...
cp uses its own blocksize which I have no control over but using dd I can specify my blocksize.

cp triggers readahead in the kernel.
Afaik dd does not.

So just use cp and you'll be faster than dd anyway.