public inbox for isar-users@googlegroups.com
 help / color / mirror / Atom feed
From: Tobias Schmidl <tobiasschmidl@siemens.com>
To: <isar-users@googlegroups.com>
Cc: "Florian Bezdeka" <florian.bezdeka@siemens.com>,
	"Jan Kiszka" <jan.kiszka@siemens.com>,
	"Henning Schild" <henning.schild@siemens.com>,
	"Felix Mößbauer" <felix.moessbauer@siemens.com>,
	"Tobias Schmidl" <tobiasschmidl@siemens.com>
Subject: [PATCH] expand-on-first-boot: Check size before resizing
Date: Fri, 8 Jul 2022 13:15:21 +0200	[thread overview]
Message-ID: <20220708111520.662382-1-tobiasschmidl@siemens.com> (raw)

If the disk is too small, resizing might have detrimental effects.
Because of aligning issues, it could happen the the final partition is
_smaller_ than desired, which might lead to an unusable last partition.

Signed-off-by: Tobias Schmidl <tobiasschmidl@siemens.com>
---
 .../files/expand-last-partition.sh              | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh b/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh
index c0edde7..3b944be 100755
--- a/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh
+++ b/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh
@@ -22,6 +22,23 @@ if [ "${ROOT_DEV}" = "${BOOT_DEV}" ]; then
 	exit 1
 fi
 
+# this value is in blocks. Normally a block has 512 bytes.
+BUFFER_SIZE=32768
+BOOT_DEV_NAME=${BOOT_DEV##*/}
+DISK_SIZE="$(cat /sys/class/block/"${BOOT_DEV_NAME}"/size)"
+ALL_PARTS_SIZE=0
+for PARTITION in /sys/class/block/"${BOOT_DEV_NAME}"[1-9]*; do
+	PART_SIZE=$(cat "${PARTITION}"/size)
+	ALL_PARTS_SIZE=$((ALL_PARTS_SIZE + PART_SIZE))
+done
+echo "Disk ${BOOT_DEV}: ${DISK_SIZE}, all partitions combined: ${ALL_PARTS_SIZE}" >&2
+
+MINIMAL_SIZE=$((ALL_PARTS_SIZE + BUFFER_SIZE))
+if [ "$DISK_SIZE" -lt "$MINIMAL_SIZE" ]; then
+	echo "Disk is not big enough, won't resize. Current size: $DISK_SIZE, minimal size for resizing: $MINIMAL_SIZE" >&2
+	exit 0
+fi
+
 LAST_PART="$(sfdisk -d "${BOOT_DEV}" 2>/dev/null | tail -1 | cut -d ' ' -f 1)"
 
 # Transform the partition table as follows:
-- 
2.36.1


             reply	other threads:[~2022-07-08 11:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-08 11:15 Tobias Schmidl [this message]
2022-07-08 15:05 ` Henning Schild

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220708111520.662382-1-tobiasschmidl@siemens.com \
    --to=tobiasschmidl@siemens.com \
    --cc=felix.moessbauer@siemens.com \
    --cc=florian.bezdeka@siemens.com \
    --cc=henning.schild@siemens.com \
    --cc=isar-users@googlegroups.com \
    --cc=jan.kiszka@siemens.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox