On 2019-09-04 02:23, Jorge Fernandez Monteagudo wrote:
Hi all!

Is it possible to get a crypted btrfs in a file? Currently I'm doing this to 
get a crypted ISO filesystem in a file:

# genisoimage -R -J -iso-level 4 -o iso.img <dir_to_put_in_the_image>
# fallocate iso-crypted.img -l $(stat --printf="%s" iso.img)
# cryptsetup -d <key_to_crypt_iso> create test iso-crypted.img
# dd if=iso.img of=/dev/mapper/test bs=512
# sync
# cryptsetup remove test >
Is it possible to do something similar in btrfs? Is there something similar to 
'genisoimage' in btrfs ? Or, at least, given a directory, is it possible to 
know the outcome size of the filesystem to create an empty file with fallocate 
and create a btrfs in a loop device like:

# fallocate btrfs-crypted.img -l <btrfs_size_for_dir_to_put_in_the_image>
# losetup /dev/loop0 btrfs-crypted.img
# mkfs.btrfs /dev/loop0

then we can mount the loop0, copy the files, unmount and then crypt the 
btrfs-crypted.img like the ISO one.
What you want here is mkfs.btrfs with the `-r` and `--shrink` options.

So, for your specific example, replace the genisoimage command from your first example with this and update the file names appropriately:

# mkfs.btrfs -r <dir_to_put_in_the_image> --shrink btrfs.img

Note that you don't need and shouldn't use a loop mount for the target file for the `mkfs` command. It will generate the file at the appropriate size automatically (and as a general rule, `mkfs` for any filesystem works just fine without a loop mount, you just need to have a file of the right size for most of them).

Reply via email to