mirror of
https://gitlab.alpinelinux.org/alpine/cloud/tiny-cloud.git
synced 2025-12-16 11:52:43 +03:00
Refactor expand_root
Simplify it a bit. - use awk /proc/mounts instead of busybox mountpoint - use sysfs to find partition and volume. This is more robust - add tests for it
This commit is contained in:
parent
d0f352b276
commit
a626e694c6
@ -7,22 +7,19 @@
|
|||||||
expand_root() {
|
expand_root() {
|
||||||
skip_action expand_root && return
|
skip_action expand_root && return
|
||||||
|
|
||||||
# explicitly use busybox, in case util-linux is also installed
|
local dev=$(awk '$2 == "/" {print $1}' "$ROOT"/proc/mounts)
|
||||||
local mountpoint=$(busybox mountpoint -n / | cut -d' ' -f1)
|
local partition=$(cat "$ROOT/sys/class/block/${dev#/dev/}/partition" 2>/dev/null)
|
||||||
local volume=$(echo "$mountpoint" |
|
|
||||||
sed -Ee "s/(nvme\d+n\d|(xv|s)d[a-z])p?\d?$/\1/"
|
|
||||||
)
|
|
||||||
local partition
|
|
||||||
|
|
||||||
if [ "$mountpoint" != "$volume" ]; then
|
if [ -n "$partition" ]; then
|
||||||
# it's a partition, resize it
|
# it's a partition, resize it
|
||||||
partition=$(echo "$mountpoint" | sed -Ee "s/.*(\d+)$/\1/")
|
local volume=$(readlink -f "$ROOT/sys/class/block/${dev#/dev/}/..")
|
||||||
echo ", +" | sfdisk -q --no-reread -N "$partition" "$volume"
|
volume="/dev/${volume##*/}"
|
||||||
partx -u "$volume"
|
echo ", +" | $MOCK sfdisk -q --no-reread -N "$partition" "$volume"
|
||||||
|
$MOCK partx -u "$volume"
|
||||||
fi
|
fi
|
||||||
# resize filesystem
|
# resize filesystem
|
||||||
mount -orw,remount /
|
$MOCK mount -orw,remount /
|
||||||
resize2fs "$mountpoint"
|
$MOCK resize2fs "$dev"
|
||||||
}
|
}
|
||||||
|
|
||||||
has_cloud_hotplugs() { [ -n "$HOTPLUG_MODULES" ]; }
|
has_cloud_hotplugs() { [ -n "$HOTPLUG_MODULES" ]; }
|
||||||
|
|||||||
51
tests/init-early.test
Executable file
51
tests/init-early.test
Executable file
@ -0,0 +1,51 @@
|
|||||||
|
#!/usr/bin/env atf-sh
|
||||||
|
|
||||||
|
. $(atf_get_srcdir)/test_env.sh
|
||||||
|
|
||||||
|
export PREFIX="$srcdir"
|
||||||
|
export MOCK=echo
|
||||||
|
|
||||||
|
init_tests \
|
||||||
|
expand_root \
|
||||||
|
expand_root_partition
|
||||||
|
|
||||||
|
PROVIDERS="aws azure gcp nocloud oci"
|
||||||
|
|
||||||
|
expand_root_body() {
|
||||||
|
fake_bin test-expand-root <<-EOF
|
||||||
|
#!/bin/sh
|
||||||
|
. "$srcdir"/lib/tiny-cloud/init-early
|
||||||
|
expand_root
|
||||||
|
EOF
|
||||||
|
mkdir proc
|
||||||
|
echo "/dev/xvda / ext4 rw,noatime 0 0" > proc/mounts
|
||||||
|
for provider in $PROVIDERS; do
|
||||||
|
CLOUD="$provider" atf_check \
|
||||||
|
-o match:"resize2fs /dev/xvda" \
|
||||||
|
test-expand-root
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
expand_root_partition_body() {
|
||||||
|
fake_bin test-expand-root <<-EOF
|
||||||
|
#!/bin/sh
|
||||||
|
. "$srcdir"/lib/tiny-cloud/init-early
|
||||||
|
expand_root
|
||||||
|
EOF
|
||||||
|
mkdir -p proc sys/class/block \
|
||||||
|
sys/devices/pci0000:00/0000:00:1b.0/0000:01:00.0/nvme/nvme0/nvme0n1/nvme0n1p2 \
|
||||||
|
sys/devices/pci0000:00/0000:00:1b.0/0000:01:00.0/nvme/nvme0/nvme0n1/device
|
||||||
|
ln -s ../../devices/pci0000:00/0000:00:1b.0/0000:01:00.0/nvme/nvme0/nvme0n1 sys/class/block/nvme0n1
|
||||||
|
ln -s ../../devices/pci0000:00/0000:00:1b.0/0000:01:00.0/nvme/nvme0/nvme0n1/nvme0n1p2 sys/class/block/nvme0n1p2
|
||||||
|
echo 2 > sys/class/block/nvme0n1p2/partition
|
||||||
|
|
||||||
|
echo "/dev/nvme0n1p2 / ext4 rw,noatime 0 0" > proc/mounts
|
||||||
|
for provider in $PROVIDERS; do
|
||||||
|
CLOUD="$provider" atf_check \
|
||||||
|
-o match:"sfdisk .*/dev/nvme0n1" \
|
||||||
|
-o match:"partx .*/dev/nvme0n1" \
|
||||||
|
-o match:"resize2fs /dev/nvme0n1p2" \
|
||||||
|
test-expand-root
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user