From Fedora Project Wiki

Reinstalling Fedora with Kikcstart on BTRFS

by Gene Czarcinski

In general, kickstart has good support for installing onto btrfs subvolumes. However, there is one case that has a small problem: re-installing onto an existing subvolume rootfs and / or /boot.

Problem: To do an install, Anaconda requires that the rootfs and /boot be "freshly formatted." For regular partitions and LVMlv, this presents no problem, just reformat the partition or LVMlv. On the other hand, a btrfs subvolume more closely resembles a directory than a partition and how do you re-format a directory? ... well, by deleting it and then re-defining it. This can be done easily when a regular GUI or text manual install but kickstart is another matter.

Solution: Use a %pre section to delete the subvolume. Given a system with /boot on a ext4 partition and an existing btrfs volume supporting root6 and home subvolumes, it would look something like:

clearpart --none

part swap  --fstype=swap --noformat --onpart=UUID=a3b0c2ad-88c8-4d6d-876c-7c52881ab854

#part /boot --fstype=ext4 --onpart=sda4
#part /boot --fstype=ext4 --onpart=sda5
part /boot --fstype=ext4 --onpart=sda6
#part /boot --fstype=ext4 --onpart=sda7

part btrfs.10 --fstype=btrfs --noformat --label=ten --onpart=UUID=0f0f82cb-ed27-4468-a18c-64a877b64329
btrfs /                       --subvol --name=root6   ten
btrfs /var                    --subvol --name=var6    ten
btrfs /home                   --subvol --name=home --noformat  ten

%pre --log=/tmp/pre-install.log
echo "manage the btrfs subvolumes"
mkdir /mnt/btrfsvol
mount UUID=0f0f82cb-ed27-4468-a18c-64a877b64329 /mnt/btrfsvol
btrfs subvol list /mnt/btrfsvol
btrfs subvol delete /mnt/btrfsvol/root6
btrfs subvol delete /mnt/btrfsvol/var6
btrfs subvol list /mnt/btrfsvol
umount /mnt/btrfsvol
rmdir /mnt/btrfsvol

%post --nochroot --log=/mnt/sysimage/root/post-install.log
cp -v /tmp/pre-install.log /mnt/sysimage/root

The first time this is run, the btrfs subvol delete will produce errors. Thereafter, reruns will delete the subvols so they can be re-installed.