I'm working on automating creation of BTRFS filesystems for my purposes in the cloud. The process is a little annoying to be honest, and I'd like to cut out a few steps in the process.

My process looks currently like this:

  1. parted /dev/sdX mklabel gpt
  2. parted /dev/sdX mkpart primary 2048s 100%
  3. Either mkfs.btrfs /dev/sdXY or do LVM hacks:
    1. pvcreate /dev/sdXY
    2. vgcreate mygroup /dev/sdXY
    3. lvcreate -l 100%FREE -n mylv mygroup
  4. MOUNTDIR="$(mktemp -d)"
  5. mount -t btrfs -o defaults,ssd,compress=lzo /dev/sdXY $MOUNTDIR
  6. btrfs subvolume create $MOUNTDIR/root
  7. umount $MOUNTDIR
  8. echo "UUID=$(blkid -s UUID -o value /dev/sdXY) /mount/dir btrfs defaults,ssd,compress=lzo,subvol=root 0 0" >> /etc/fstab

This process is ugly, but it works.

Is there a way that I can get rid of steps 4-7 by creating a subvolume without the filesystem being mounted?

I don't think so.

Btrfs is designed to perform a lot of different operations online, i.e. with the filesystem mounted. This is a good way to avoid having to code everything twice, once in the kernel driver for online actions and once in the userspace tools for offline actions. Not coding these things twice leads to fewer bugs and less chance that the in-kernel and userspace functionality will somehow diverge.

The other method I thought of would be to create a tiny Btrfs filesystem with all the settings and subvolumes you want, unmount it, and take a block-level image of it. Then deploy that image cookie-cutter style onto many new block devices. But that doesn't help you at all since now you have to mount the filesystem in order to resize (grow) it to the size of the underlying block device.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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