0

Assuming you have a disk backup in raw format sda.img and /dev/sda is initially filled with zeroes, how to skip zeroes while restoring the backup? An analog of dd should detect zero block in input stream and perform an appropriate seek on output device.

Note

I tried cp option --sparse=always, but it looks ineffective, if destination is a block device:

[root@vmarch ~]# truncate sda.img -s1G
[root@vmarch ~]# cat sda.img | cp --sparse=always /dev/stdin sdb.img
[root@vmarch ~]# du -h sda.img sdb.img
0       sda.img
0       sdb.img
[root@vmarch ~]# ls -lh sda.img sdb.img
-rw-r--r-- 1 root root 1.0G Jul 31 09:17 sda.img
-rw------- 1 root root 1.0G Jul 31 09:17 sdb.img
[root@vmarch ~]# losetup -f sdb.img
[root@vmarch ~]# losetup
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE     DIO
/dev/loop0         0      0         0  0 /root/sdb.img   0
[root@vmarch ~]# cp --sparse=always sda.img /dev/loop0
[root@vmarch ~]# du -h sda.img sdb.img
0       sda.img
1.1G    sdb.img
5
  • I'm not sure I fully understand or agree with your assumptions, which makes answering the question a bit touchy. Are you aware that unused sectors may not be filled with zeros and that used sectors could include zeros? Jul 30 '16 at 14:08
  • 1
    You possibly want the sparse option to dd, but unless you know your device is full of NULs before you start then @JuliePelletier's warning is very apt; the resulting filesystem may not be valid! Jul 30 '16 at 14:13
  • @JuliePelletier it's a virtual disk I have just created. Unfortunately I can't use host tools to fill it, only guest tool
    – basin
    Jul 30 '16 at 14:46
  • 1
    @basin: In most cases, especially for a one-shot like this, I wouldn't waste time wondering if it'll be ok and then end up wasting many more hours on troubleshooting issues, I'd just let it do the copy as intended even if it could potentially be optimized. Jul 30 '16 at 14:49
  • 1
    you should also rethink your assumption that "dd is a good, or even reasonable, tool to use for backups".
    – cas
    Jul 31 '16 at 5:53
0

With GNU coreutils (e.g. on non-embedded Linux), you can use cp --sparse=always to make the output file as sparse as possible:

cp --sparse=always sda.img /dev/sda

This may or may not be faster than a straight

cat sda.img >/dev/sda

cp --sparse=always has to spend more time reading and analyzing its input. The cat version spends more time writing, but unless the output device is slow, that's not going to affect the duration of the copy much, since in this scenario the reading and writing can mostly be parallelized.

1
  • Sorry, your solution doesn't work. See "Note" in my question
    – basin
    Jul 31 '16 at 6:25

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.