Skip to content

Commit ab6587a

Browse files
iavigorpecovnik
authored andcommitted
If rootfs placed on btrfs filesystem, place root on @ subvolume
It makes possible to work with snapshots of a root filesystem
1 parent a73e79a commit ab6587a

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

lib/functions/image/partitioning.sh

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ function prepare_partitions() {
244244
display_alert "Partitioning with the following options" "$partition_script_output" "debug"
245245
echo "${partition_script_output}" | run_host_command_logged sfdisk "${SDCARD}".raw || exit_with_error "Partitioning failed!"
246246
fi
247-
247+
248248
call_extension_method "post_create_partitions" <<- 'POST_CREATE_PARTITIONS'
249249
*called after all partitions are created, but not yet formatted*
250250
POST_CREATE_PARTITIONS
@@ -314,9 +314,54 @@ function prepare_partitions() {
314314
echo "$CRYPTROOT_MAPPER UUID=${physical_root_part_uuid} none luks" >> $SDCARD/etc/crypttab
315315
run_host_command_logged cat $SDCARD/etc/crypttab
316316
fi
317-
317+
318+
if [[ $ROOTFS_TYPE == btrfs ]]; then
319+
mountopts[$ROOTFS_TYPE]='commit=120'
320+
run_host_command_logged btrfs subvolume create $MOUNT/@
321+
# getting the subvolume id of the newly created volume @ to install it
322+
# as the default volume for mounting without explicit reference
323+
324+
run_host_command_logged "btrfs subvolume list $MOUNT | grep 'path @' | cut -d' ' -f2 \
325+
| xargs -I{} btrfs subvolume set-default {} $MOUNT/ "
326+
327+
call_extension_method "btrfs_root_add_subvolumes" <<- 'BTRFS_ROOT_ADD_SUBVOLUMES'
328+
# *custom post btrfs rootfs creation hook*
329+
# Called if rootfs btrfs after creating the subvolume "@" for rootfs
330+
# Used to create other separate btrfs subvolume if needed.
331+
# Mountpoints and fstab records should be created too.
332+
run_host_command_logged btrfs subvolume create $MOUNT/@home
333+
run_host_command_logged btrfs subvolume create $MOUNT/@var
334+
run_host_command_logged btrfs subvolume create $MOUNT/@var_log
335+
run_host_command_logged btrfs subvolume create $MOUNT/@var_cache
336+
run_host_command_logged btrfs subvolume create $MOUNT/@srv
337+
BTRFS_ROOT_ADD_SUBVOLUMES
338+
339+
run_host_command_logged umount $rootdevice
340+
display_alert "Remounting rootfs" "$rootdevice (UUID=${ROOT_PART_UUID})"
341+
run_host_command_logged mount -odefaults,${mountopts[$ROOTFS_TYPE]},subvol=@ $rootdevice $MOUNT/
342+
fi
318343
rootfs="UUID=$(blkid -s UUID -o value $rootdevice)"
319-
echo "$rootfs / ${mkfs[$ROOTFS_TYPE]} defaults,noatime${mountopts[$ROOTFS_TYPE]} 0 1" >> $SDCARD/etc/fstab
344+
echo "$rootfs / ${mkfs[$ROOTFS_TYPE]} defaults,${mountopts[$ROOTFS_TYPE]} 0 1" >> $SDCARD/etc/fstab
345+
if [[ $ROOTFS_TYPE == btrfs ]]; then
346+
call_extension_method "btrfs_root_add_subvolumes_fstab" <<- 'BTRFS_ROOT_ADD_SUBVOLUMES_FSTAB'
347+
run_host_command_logged mkdir -p $MOUNT/home
348+
run_host_command_logged mount -odefaults,${mountopts[$ROOTFS_TYPE]},subvol=@home $rootdevice $MOUNT/home
349+
echo "$rootfs /home btrfs defaults,${mountopts[$ROOTFS_TYPE]},subvol=@home 0 2" >> $SDCARD/etc/fstab
350+
run_host_command_logged mkdir -p $MOUNT/var
351+
run_host_command_logged mount -odefaults,${mountopts[$ROOTFS_TYPE]},subvol=@var $rootdevice $MOUNT/var
352+
echo "$rootfs /var btrfs defaults,${mountopts[$ROOTFS_TYPE]},subvol=@var 0 2" >> $SDCARD/etc/fstab
353+
run_host_command_logged mkdir -p $MOUNT/var/log
354+
run_host_command_logged mount -odefaults,${mountopts[$ROOTFS_TYPE]},subvol=@var_log $rootdevice $MOUNT/var/log
355+
echo "$rootfs /var/log btrfs defaults,${mountopts[$ROOTFS_TYPE]},subvol=@var_log 0 2" >> $SDCARD/etc/fstab
356+
run_host_command_logged mkdir -p $MOUNT/var/cache
357+
run_host_command_logged mount -odefaults,${mountopts[$ROOTFS_TYPE]},subvol=@var_cache $rootdevice $MOUNT/var/cache
358+
echo "$rootfs /var/cache btrfs defaults,${mountopts[$ROOTFS_TYPE]},subvol=@var_cache 0 2" >> $SDCARD/etc/fstab
359+
run_host_command_logged mkdir -p $MOUNT/srv
360+
run_host_command_logged mount -odefaults,${mountopts[$ROOTFS_TYPE]},subvol=@srv $rootdevice $MOUNT/srv
361+
echo "$rootfs /srv btrfs defaults,${mountopts[$ROOTFS_TYPE]},subvol=@srv 0 2" >> $SDCARD/etc/fstab
362+
BTRFS_ROOT_ADD_SUBVOLUMES_FSTAB
363+
fi
364+
320365
run_host_command_logged cat $SDCARD/etc/fstab
321366

322367
else

0 commit comments

Comments
 (0)