From dc09142e7504cf106d5eaebea45a1384b61003ac Mon Sep 17 00:00:00 2001 From: ThomasKaiser Date: Mon, 28 Aug 2017 09:19:57 -0700 Subject: [PATCH] First take on resizable btrfs rootfs --- lib/configuration.sh | 2 +- lib/debootstrap-ng.sh | 18 ++++++-- packages/bsp/common/etc/init.d/resize2fs | 57 +++++++++++++++--------- 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/lib/configuration.sh b/lib/configuration.sh index 096dfdee9f..74afc2fcf0 100644 --- a/lib/configuration.sh +++ b/lib/configuration.sh @@ -28,7 +28,7 @@ ROOTFS_CACHE_MAX=8 # max number of rootfs cache, older ones will be cleaned up # Fixed image size is in 1M dd blocks (MiB) # to get size of block device /dev/sdX execute as root: # echo $(( $(blockdev --getsize64 /dev/sdX) / 1024 / 1024 )) -[[ "btrfs f2fs" == *$ROOTFS_TYPE* && -z $FIXED_IMAGE_SIZE ]] && exit_with_error "Please define FIXED_IMAGE_SIZE" +[[ "f2fs" == *$ROOTFS_TYPE* && -z $FIXED_IMAGE_SIZE ]] && exit_with_error "Please define FIXED_IMAGE_SIZE" [[ $RELEASE == stretch && $CAN_BUILD_STRETCH != yes ]] && exit_with_error "Building Debian Stretch images with selected kernel is not supported" diff --git a/lib/debootstrap-ng.sh b/lib/debootstrap-ng.sh index b14af3db4d..19adc443ae 100644 --- a/lib/debootstrap-ng.sh +++ b/lib/debootstrap-ng.sh @@ -291,7 +291,7 @@ prepare_partitions() # mountopts[ext2] is empty # mountopts[fat] is empty # mountopts[f2fs] is empty - # mountopts[btrfs] is empty + mountopts[btrfs]=',commit=600,compress=lzo' # mountopts[nfs] is empty # stage: determine partition configuration @@ -330,9 +330,19 @@ prepare_partitions() fi else local imagesize=$(( $rootfs_size + $OFFSET + $BOOTSIZE )) # MiB - # Hardcoded overhead +40% and +128MB for ext4 is needed for desktop images, for CLI it can be lower - # also add extra 128 MiB for the emergency swap file creation and align the size up to 4MiB - local sdsize=$(bc -l <<< "scale=0; ((($imagesize * 1.4) / 1 + 128) / 4 + 1) * 4") + case $ROOTFS_TYPE in + btrfs) + # Used for server images, currently no swap functionality, so disk space + # requirements are rather low since rootfs gets filled with compress-force=zlib + local sdsize=$(bc -l <<< "scale=0; (($imagesize * 0.9) / 4 + 1) * 4") + ;; + *) + # Hardcoded overhead +40% and +128MB for ext4 is needed for desktop images, + # for CLI it could be lower. Also add extra 128 MiB for the emergency swap + # file creation and align the size up to 4MiB + local sdsize=$(bc -l <<< "scale=0; ((($imagesize * 1.4) / 1 + 128) / 4 + 1) * 4") + ;; + esac fi # stage: create blank image diff --git a/packages/bsp/common/etc/init.d/resize2fs b/packages/bsp/common/etc/init.d/resize2fs index 18a7814c8a..e49c6cb587 100755 --- a/packages/bsp/common/etc/init.d/resize2fs +++ b/packages/bsp/common/etc/init.d/resize2fs @@ -119,35 +119,50 @@ do_expand_partition() fi } -do_expand_filesystem() +do_expand_ext4() { - local rootpart=$(findmnt -n -o SOURCE /) # i.e. /dev/mmcblk0p1 + echo -e "\n### [resize2fs] Start resizing ext4 partition $1 now\n" >> ${Log} + resize2fs ${rootpart} >> ${Log} 2>&1 +} - echo -e "\n### [resize2fs] Start resizing partition now\n" >> ${Log} - resize2fs $rootpart >> ${Log} 2>&1 +do_expand_btrfs() +{ + echo -e "\n### [btrfs resize] Start resizing btrfs partition $1 now\n" >> ${Log} + btrfs filesystem resize max / >> ${Log} 2>&1 } case "$1" in start) - # skip resizing if rootfs is not ext4 or if explicitly disabled - if [[ $(findmnt -n -o FSTYPE /) != ext4 || -f /root/.no_rootfs_resize ]]; then - systemctl disable resize2fs + # skip resizing if rootfs is neither ext4 not btrfs or if explicitly disabled + if [[ -f /root/.no_rootfs_resize ]]; then + systemctl disable resize2fs + exit 0 + fi + + rootfstype=$(findmnt -n -o FSTYPE /) + rootpart=$(findmnt -n -o SOURCE /) # i.e. /dev/mmcblk0p1 + case ${rootfstype} in + ext4) + # first stage - resize the rootfs partition + [[ ! -f /var/lib/armbian/resize_second_stage ]] && do_expand_partition + # second stage - resize the filesystem + [[ ! -f /var/run/resize2fs-reboot ]] && do_expand_ext4 ${rootpart} + ;; + btrfs) + # first stage - resize the rootfs partition + [[ ! -f /var/lib/armbian/resize_second_stage ]] && do_expand_partition + # second stage - resize the filesystem + do_expand_btrfs ${rootpart} && systemctl disable resize2fs + ;; + esac + + # disable itself + [[ ! -f /var/run/resize2fs-reboot ]] && systemctl disable resize2fs exit 0 - fi - - # first stage - resize the partition - [[ ! -f /var/lib/armbian/resize_second_stage ]] && do_expand_partition - - # second stage - resize the filesystem - [[ ! -f /var/run/resize2fs-reboot ]] && do_expand_filesystem - - # disable itself - [[ ! -f /var/run/resize2fs-reboot ]] && systemctl disable resize2fs - exit 0 - ;; + ;; *) - echo "Usage: $0 start" - exit 0 + echo "Usage: $0 start" + exit 0 ;; esac