Friday, May 3, 2024
26
rated 0 times [  26] [ 0]  / answers: 1 / hits: 37749  / 1 Year ago, thu, march 16, 2023, 8:54:34

I'm using the dd command to create a bootable usb from iso file:



sudo dd if=~/Desktop/ubuntu.iso of=/dev/sdx bs=1M


After pressing enter it momentarily exits and gives me:



915+0 records in 915+0 records out 959447040 bytes (959 MB) copied,
0.539375 s, 1.8 GB/s


So it's like running in background because I can see that the flash drive is working. Eventually it will stop copying and I can remove the drive successfully but the question is why doesn't dd command wait for copying to finish. Why does it run in background. And how can I make it wait?


More From » command-line

 Answers
2

Despite popular belief, dd is a perfectly ordinary command, it isn't more low-level than cat or cp. Your command reads from the disk cache and writes to the disk buffers like any other command.


In order to make sure that the data is fully written to the physical media, you need to call sync. The command sync flushes all output buffers to the disk(s). When the sync command returns, the data has been fully written¹.


sudo dd if=~/Desktop/ubuntu.iso of=/dev/sdx bs=1M; sync

Most of the time, you don't need to call sync, because unmounting a filesystem does the same job. When the umount command returns, or when you get a confirmation message after clicking “Eject”, the buffers have been written to the disk. Here, you're directly writing to the disk without going through a mounted filesystem, so you need to explicitly flush the buffer.


Note that instead of dd, you could use tee. This has two advantages: there's less risk of inverting the source and destination due to a typo, and it's probably slightly faster.


<~/Desktop/ubuntu.iso sudo tee /dev/sdx >/dev/null; sync

¹ At least on a “normal” Ubuntu system, or more generally Linux. This may not be true on other Unix-like systems or if Linux is running in a virtual environment (Cygwin, WSL, virtual machine, …). In a virtual environment, flushing writes to persistent storage may require the cooperation of the host system.


[#30384] Thursday, March 16, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
clegian

Total Points: 283
Total Questions: 115
Total Answers: 115

Location: Morocco
Member since Tue, Feb 2, 2021
3 Years ago
clegian questions
;