qcow2 actual size

8,218

The file is sparse, meaning it contains one or several "holes". When data is read from within a hole, zeroes are returned, but these zeros are not stored on disk but rather generated by the filesystem.

This feature saves actual hard disk space when huge files that are mostly zeroes are stored. The ls utility will report the apparent file size, but du will show the apparent size of the file minus the size of the holes in the file (which is how much space the file uses on the disk).

When you copy the file, cp will not preserve the spareness of the file, and zeroes will fill the holes.

On GNU systems, you may use the --sparse=auto or --sparse=always flag with cp when copying the file to preserve the sparseness, given that the holes are sufficiently large. On some BSD systems (OpenBSD at least), cp tries to retain the holes in sparse files by default.

You may also use rsync with the --sparse (-S) option to copy the file.

Note that even if the holes are filled in by zeroes (wholly or partially), the resulting file will be functionally equivalent to the original file but will use more actual disk space.

See also: Can a file that was originally sparse and then expanded be made sparse again?

Share:
8,218

Related videos on Youtube

dePablo
Author by

dePablo

Updated on September 18, 2022

Comments

  • dePablo
    dePablo almost 2 years

    I am little bit confused with the real size of qcow2 files.

    ls -alh VMs/ubuntu-mini.qcow2
    -rw------- 1 root root 21G mar 31 23:15 VMs/ubuntu-mini.qcow2
    du -h VMs/ubuntu-mini.qcow2
    2,7G    VMs/ubuntu-mini.qcow2
    

    I wanted to copy that file to different partition (ext4). It looks this command copied actually 21G not only 2,7G I expected. And now in new location these both commands (du and ls) shows me the same size - 21G.

    du -h /media/HDD0/VMs/ubuntu-mini.qcow2
    21G     /media/HDD0/VMs/ubuntu-mini.qcow2
    ls -alh /media/HDD0/VMs/ubuntu-mini.qcow2
    -rw------- 1 root root 21G mar 31 23:15 /media/HDD0/VMs/ubuntu-mini.qcow2
    

    What's the proper way of copying qcow2 files? Is there are switch in "cp" command which makes it to copy only 2,7G?

    • dePablo
      dePablo about 6 years
      When I used -s switch instead -l ls shown me the "correct" size: ls -lh ubuntu-mini.qcow2 -rw------- 1 root root 21G kwi 4 14:33 ubuntu-mini.qcow2 ls -sh ubuntu-mini.qcow2 2,7G ubuntu-mini.qcow2
    • Kusalananda
      Kusalananda about 6 years
      Both sizes are "correct", depending on what you want to see.
  • dePablo
    dePablo about 6 years
    Thank you. It solved my problem. There is also -s switch for ls command which shows me the correct size of the file.
  • Kusalananda
    Kusalananda about 6 years
    @dePablo As I wrote in a comment to the question, both sizes are actually correct, but they are measurements of two separate things. If this solves your issue, please consider accepting the answer.