I would like to take a dm snapshot of a drive. I do not want to take the snapshot of the unused and deallocated blocks. Is there any way of determining the used blocks and sectors of a hard drive without being file system aware. I would like to take a snapshot without having the empty parts of my disk backed up.
Without being filesystem aware, the best you can get is probably something like this:
- Take a snapshot.
- fsck the snapshot if needed. Then mount it.
- Fill all free space in the snapshot with 0's (e.g.,
cp /dev/zero tmpfile; rm tmpfile). This will take a lot of I/O, and also will require you to have plenty of space allocated to the snapshot.
- Unmount the snapshot.
You can then assume that any sectors filled with 0's are unused. Or, at least, need not be backed up; you can just fill them with 0's again. (You could also feed the image to gzip, it should compress fairly well).
Much faster would be to use tools like partimage or partclone that do understand the filesystem. Or, just use tar.