1

I'm trying to create an image of /dev/sda and write the output to a new .iso file on the same device. How can I exclude the new image file; so as to prevent the output from becoming its own input, and vice versa? As you can see, it ends up creating something of an infinite feedback loop (like an ouroboros), until there's no free space left.

root@linux:~# df /dev/sda1
Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/sda1      303569136   4313624 283812008   2% /
root@linux:~# dd if=/dev/sda of=/root/image.iso
306419534336 bytes (306 GB, 285 GiB) copied, 10098.4 s, 30.3 MB/s
dd: writing to '/root/image.iso': No space left on device
598483361+0 records in
598483360+0 records out
306423480320 bytes (306 GB, 285 GiB) copied, 10099.5 s, 30.3 MB/s
root@linux:~# df /dev/sda1
Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/sda1      303569136 303552756         0 100% /
root@linux:~# ls -sh /root/
total 286G
286G image.iso
3
  • 1
    Why are you going this? A copy of this nature will be of limited use as you're storing changing data. Is it possible to setup LVM or partition so that you only copy / and not /root? – Ed Neville Feb 21 '17 at 21:10
  • 2
    If you have /root mounted on /dev/sda1, there would be no way anyway to cram all the blocks in. There is filesystem overhead, so the free data space on the disk will always be less than the total of data on all of the partition's blocks. Now something like gzip - < /dev/sda1 > /root/image.iso.gz might work. But the dynamic nature of it would probably mean corruption if you ever tried to restore it on a live partition later – infixed Feb 21 '17 at 22:43
  • Thanks everyone for your comments and answers. I see now, that it won't work, and why. My reason for trying this? I know the basics of how to use dd, and I tried to use that limited knowledge to create an image of the HDD in my laptop. I've used dd for writing images to external storage and thought I could just do the reverse. – voices Feb 25 '17 at 3:56
2

You can't get there from here.

Rather, you can't do what you are trying to do. Remember that dd just reads and writes blocks... when you point it to /dev/sda it will keep on going, as you've noticed.

You could maybe do it if you make sure your disk is entirely unfragmented so the data is all back to back to back and only dd so many blocks (to get your data, but stop before you get to where you are writing to). Check the bs (block size) and count arguments for dd.

What may work for you is if you create an empty file of sufficient size, write a file system to it, mount it, and then use rsync to copy your data to the new file system. Then just umount your sparse file, and you have an "image" of a file system with all your data on it.

2

You're asking dd to read the blocks of the device, not the file contents. If you want to read the file system data, you could use one of the dump utilities, for ext, it is dump, others such as XFS have an xfs_ prefix.

Essentially what you could do is use LVM or ZFS and take a snapshot, this would not then consume data in the same noticeable way as you're not going to duplicate, but take a copy only when the underlying data changes (copy-on-write).

What the dd is going to do is start a block 0, continue through the entire device until it gets to the final cylinder, and there's no way to store that raw data in less space than it came. If the disk is first filled with zeros:

dd if=/dev/zero of=/root/space_hog

You could then remove this file and immediately do the dd through a compression tool:

dd if=/dev/sda | gzip > /root/image.iso.gz

It will take a little longer as compression of random data (is swap on this device?) is tricky work.

2
  • With your proposal to store an image of a disk on the disk itself, beware that the result may be unreadable. If you make a copy of a filesystem that changes during the copy, the output may be inconsistent because some blocks have been moved around. You could be lucky, or you could lose all your data if you count on the image. – Gilles 'SO- stop being evil' Feb 21 '17 at 23:49
  • @Gilles, this. dd/dump are of questionable use, even if going to different block devices. This is explained in dump.sourceforge.net/isdumpdeprecated.html – Ed Neville Feb 22 '17 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.