diff options
author | Andrii <85483797+andriiandrieiev@users.noreply.github.com> | 2021-11-19 13:10:42 +0200 |
---|---|---|
committer | Andrii <85483797+andriiandrieiev@users.noreply.github.com> | 2021-11-19 13:10:42 +0200 |
commit | bfc2d162e0ca07d7d2a9fe4967690b6399057c3d (patch) | |
tree | 1f984e9c63144691440c32f2a153884807b21f0c | |
parent | 1353757247c027f6352000a9450b502c25c460c8 (diff) | |
download | vyos-1x-bfc2d162e0ca07d7d2a9fe4967690b6399057c3d.tar.gz vyos-1x-bfc2d162e0ca07d7d2a9fe4967690b6399057c3d.zip |
filesystem: T3946: partition resize as a service
-rw-r--r-- | interface-definitions/system-option.xml.in | 6 | ||||
-rw-r--r-- | op-mode-definitions/force-root-partition-auto-resize.xml.in (renamed from op-mode-definitions/force-part-resize.xml.in) | 4 | ||||
-rwxr-xr-x | src/conf_mode/system-option.py | 6 | ||||
-rwxr-xr-x | src/op_mode/force_part_resize.sh | 72 | ||||
-rwxr-xr-x | src/op_mode/force_root-partition-auto-resize.sh | 54 | ||||
-rw-r--r-- | src/systemd/root-partition-auto-resize.service | 12 |
6 files changed, 80 insertions, 74 deletions
diff --git a/interface-definitions/system-option.xml.in b/interface-definitions/system-option.xml.in index f73c1ee08..75fa67271 100644 --- a/interface-definitions/system-option.xml.in +++ b/interface-definitions/system-option.xml.in @@ -117,6 +117,12 @@ <valueless/> </properties> </leafNode> + <leafNode name="root-partition-auto-resize"> + <properties> + <help>Enable root partition auto-extention on system boot</help> + <valueless/> + </properties> + </leafNode> </children> </node> </children> diff --git a/op-mode-definitions/force-part-resize.xml.in b/op-mode-definitions/force-root-partition-auto-resize.xml.in index cb76273c7..f84c073b8 100644 --- a/op-mode-definitions/force-part-resize.xml.in +++ b/op-mode-definitions/force-root-partition-auto-resize.xml.in @@ -2,11 +2,11 @@ <interfaceDefinition> <node name="force"> <children> - <node name="resize-partition"> + <node name="root-partition-auto-resize"> <properties> <help>Resize the VyOS partition</help> </properties> - <command>${vyos_op_scripts_dir}/force_part_resize.sh</command> + <command>sudo ${vyos_op_scripts_dir}/force_root-partition-auto-resize.sh</command> </node> </children> </node> diff --git a/src/conf_mode/system-option.py b/src/conf_mode/system-option.py index 55cf6b142..b1c63e316 100755 --- a/src/conf_mode/system-option.py +++ b/src/conf_mode/system-option.py @@ -126,6 +126,12 @@ def apply(options): if 'keyboard_layout' in options: cmd('loadkeys {keyboard_layout}'.format(**options)) + # Enable/diable root-partition-auto-resize SystemD service + if 'root_partition_auto_resize' in options: + cmd('systemctl enable root-partition-auto-resize.service') + else: + cmd('systemctl disable root-partition-auto-resize.service') + if __name__ == '__main__': try: c = get_config() diff --git a/src/op_mode/force_part_resize.sh b/src/op_mode/force_part_resize.sh deleted file mode 100755 index eb0f26d8a..000000000 --- a/src/op_mode/force_part_resize.sh +++ /dev/null @@ -1,72 +0,0 @@ -#!/usr/bin/env bash -# -# Copyright (C) 2021 VyOS maintainers and contributors -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License version 2 as -# published by the Free Software Foundation. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -# -# Function to get the vyos version from the commandline. -# -get_version () { -for item in `cat /proc/cmdline`; do - if [ "vyos-union" == "${item%=*}" ]; then - echo ${item#*=} - fi -done -} - -# -# VERSION is the output of the get_version output. -# DEVICEPART is the device partition where VyOS is mounted on. -# DEVICEPATH is the path to the device where VyOS is mounted on. -# DEVICE is the device of the device partition. -# PARTNR is the device partition number used for parted. -# -VERSION=$(get_version) -DEVICEPART=$(mount | grep $VERSION/grub | cut -d' ' -f1 | rev | cut -d'/' -f1 | rev) -DEVICEPATH=$(mount | grep $VERSION/grub | cut -d' ' -f1 | rev | cut -d'/' -f2- | rev) -DEVICE=$(lsblk -no pkname $DEVICEPATH/$DEVICEPART) -PARTNR=$(grep -c $DEVICEPART /proc/partitions) - -# -# Check if the device really exits. -# -fdisk -l $DEVICEPATH/$DEVICE >> /dev/null 2>&1 || (echo "could not find device $DEVICE" && exit 1) - -# -# START is the partition starting sector. -# CURSIZE is the partition start sector + the partition end sector. -# MAXSIZE is the device end sector. -# -START=$(cat /sys/block/$DEVICE/$DEVICEPART/start) -CURSIZE=$(($START+$(cat /sys/block/$DEVICE/$DEVICEPART/size))) -MAXSIZE=$(($(cat /sys/block/$DEVICE/size)-8)) - -# -# Check if the device size is larger then the partition size -# and if that is the case, resize the partition and grow the filesystem. -# -if [ $MAXSIZE -gt $CURSIZE ]; then -parted "${DEVICEPATH}/${DEVICE}" ---pretend-input-tty > /dev/null 2>&1 <<EOF -unit -s -resizepart -${PARTNR} -Yes -"$MAXSIZE" -quit -EOF - partprobe > /dev/null 2>&1 - resize2fs ${DEVICEPATH}/$DEVICEPART > /dev/null 2>&1 -fi - diff --git a/src/op_mode/force_root-partition-auto-resize.sh b/src/op_mode/force_root-partition-auto-resize.sh new file mode 100755 index 000000000..4f13e3e03 --- /dev/null +++ b/src/op_mode/force_root-partition-auto-resize.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +# +# Copyright (C) 2021 VyOS maintainers and contributors +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# ROOT_PART_DEV – root partition device path +# ROOT_PART_NAME – root partition device name +# ROOT_DEV_NAME – disk device name +# ROOT_DEV – disk device path +# ROOT_PART_NUM – number of root partition on disk +# ROOT_DEV_SIZE – disk total size in 512 bytes sectors +# ROOT_PART_SIZE – root partition total size in 512 bytes sectors +# ROOT_PART_START – number of 512 bytes sector where root partition starts +# AVAILABLE_EXTENSION_SIZE – calculation available disk space after root partition in 512 bytes sectors +ROOT_PART_DEV=$(findmnt /usr/lib/live/mount/persistence -o source -n) +ROOT_PART_NAME=$(echo "$ROOT_PART_DEV" | cut -d "/" -f 3) +ROOT_DEV_NAME=$(echo /sys/block/*/"${ROOT_PART_NAME}" | cut -d "/" -f 4) +ROOT_DEV="/dev/${ROOT_DEV_NAME}" +ROOT_PART_NUM=$(cat "/sys/block/${ROOT_DEV_NAME}/${ROOT_PART_NAME}/partition") +ROOT_DEV_SIZE=$(cat "/sys/block/${ROOT_DEV_NAME}/size") +ROOT_PART_SIZE=$(cat "/sys/block/${ROOT_DEV_NAME}/${ROOT_PART_NAME}/size") +ROOT_PART_START=$(cat "/sys/block/${ROOT_DEV_NAME}/${ROOT_PART_NAME}/start") +AVAILABLE_EXTENSION_SIZE=$((ROOT_DEV_SIZE - ROOT_PART_START - ROOT_PART_SIZE - 8)) + +# +# Check if device have space for root partition growing up. +# +if [ $AVAILABLE_EXTENSION_SIZE -lt 1 ]; then + echo "There is no available space for root partition extension" + exit 0; +fi + +# +# Resize the partition and grow the filesystem. +# +parted -m ${ROOT_DEV} ---pretend-input-tty > /dev/null 2>&1 <<EOF +resizepart +${ROOT_PART_NUM} +Yes +100% +EOF +partprobe > /dev/null 2>&1 +resize2fs ${ROOT_PART_DEV} > /dev/null 2>&1 diff --git a/src/systemd/root-partition-auto-resize.service b/src/systemd/root-partition-auto-resize.service new file mode 100644 index 000000000..a57fbc3d8 --- /dev/null +++ b/src/systemd/root-partition-auto-resize.service @@ -0,0 +1,12 @@ +[Unit] +Description=VyOS root partition auto resizing +After=multi-user.target + +[Service] +Type=oneshot +User=root +Group=root +ExecStart=/usr/libexec/vyos/op_mode/force_root-partition-auto-resize.sh + +[Install] +WantedBy=vyos.target
\ No newline at end of file |