summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2021-11-11 18:54:38 +0100
committerGitHub <noreply@github.com>2021-11-11 18:54:38 +0100
commit7357be6b5d77d3edabd7d75c402d8732770c6361 (patch)
tree4806b4feb99ef95c89170271e73cf36252c54762 /src
parentc45e4beadf30accb1838b3bad1f21c2146469bf8 (diff)
parente8c3b66a17473785044ab1a1515ad1485d0870b2 (diff)
downloadvyos-1x-7357be6b5d77d3edabd7d75c402d8732770c6361.tar.gz
vyos-1x-7357be6b5d77d3edabd7d75c402d8732770c6361.zip
Merge pull request #1069 from UnicronNL/partresize
T3946: Automatically resize the root partition if the drive has extra…
Diffstat (limited to 'src')
-rwxr-xr-xsrc/op_mode/force_part_resize.sh72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/op_mode/force_part_resize.sh b/src/op_mode/force_part_resize.sh
new file mode 100755
index 000000000..eb0f26d8a
--- /dev/null
+++ b/src/op_mode/force_part_resize.sh
@@ -0,0 +1,72 @@
+#!/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
+