Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions meta-edgeos/recipes-core/images/edgeos-image.bb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ BUILDCFG_VARS += " \
# Disable WIC's automatic fstab updates
WIC_CREATE_EXTRA_ARGS = "--no-fstab-update"

IMAGE_INSTALL:append = "expand-rootfs"
IMAGE_INSTALL += "${@bb.utils.contains('EDGEOS_DISABLE_ROOT_SSH', '1', 'edgeos-user', '', d)}"

ROOTFS_POSTPROCESS_COMMAND += "edgeos_make_admin_nopass;"
Expand Down
34 changes: 34 additions & 0 deletions meta-edgeos/recipes-support/expand-rootfs/expand-rootfs.bb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
SUMMARY = "First-boot rootfs auto-expansion service"
DESCRIPTION = "Expands the active root partition and grows the filesystem on first boot."
LICENSE = "CLOSED"

PR = "r4"


SRC_URI = " \
file://expand-rootfs.sh \
file://expand-rootfs.service \
"

S = "${WORKDIR}"

inherit systemd

do_install() {
install -d ${D}/usr/local/sbin
install -m 0755 ${WORKDIR}/expand-rootfs.sh ${D}/usr/local/sbin/expand-rootfs.sh

install -d ${D}${systemd_unitdir}/system
install -m 0644 ${WORKDIR}/expand-rootfs.service ${D}${systemd_unitdir}/system/expand-rootfs.service
}

FILES:${PN} += " \
/usr/local/sbin/expand-rootfs.sh \
${systemd_unitdir}/system/expand-rootfs.service \
"

SYSTEMD_SERVICE:${PN} = "expand-rootfs.service"
SYSTEMD_AUTO_ENABLE = "enable"

RDEPENDS:${PN} = "bash coreutils util-linux parted e2fsprogs-resize2fs udev gptfdisk"

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Unit]
Description=Expand root partition and filesystem (first boot)
DefaultDependencies=no
After=local-fs.target systemd-udev-settle.service
Wants=systemd-udev-settle.service
ConditionPathExists=!/var/lib/expand-rootfs.done

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/expand-rootfs.sh
RemainAfterExit=no
TimeoutSec=0
StandardOutput=journal+console
StandardError=journal+console

[Install]
WantedBy=multi-user.target
69 changes: 69 additions & 0 deletions meta-edgeos/recipes-support/expand-rootfs/files/expand-rootfs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env bash
set -euo pipefail

STAMP="/var/lib/expand-rootfs.done"
LOG="/var/log/expand-rootfs.log"

# Redirect all output to log file and console
exec > >(tee -a "$LOG") 2>&1 || true
echo "[expand-rootfs] Start $(date -Is || true)"

# Run only once
if [[ -f "$STAMP" ]]; then
echo "[expand-rootfs] Stamp file exists; exiting."
exit 0
fi

# Detect root device and filesystem type
ROOTDEV="$(findmnt -n -o SOURCE /)"
FSTYPE="$(findmnt -n -o FSTYPE /)"

if [[ "$ROOTDEV" != /dev/* ]]; then
echo "[expand-rootfs] Root device is not a physical partition ($ROOTDEV) -> skip."
mkdir -p "$(dirname "$STAMP")"; touch "$STAMP"; exit 0
fi

# Use sysfs to find partition number and parent disk (portable)
PART_BASENAME="$(basename "$ROOTDEV")" # e.g. mmcblk0p2
PARTNUM="$(cat "/sys/class/block/${PART_BASENAME}/partition")"
PARENT_SYS="$(readlink -f "/sys/class/block/${PART_BASENAME}/..")"
DISK="/dev/$(basename "$PARENT_SYS")" # e.g. /dev/mmcblk0
echo "[expand-rootfs] RootDEV=$ROOTDEV Disk=$DISK Part=$PARTNUM FS=$FSTYPE"

# Skip if an A/B rootfs layout is detected (e.g. mender)
if lsblk -rno PARTLABEL "$DISK" 2>/dev/null | grep -qiE 'root[a-b]'; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part will be interesting. we do want to get to an A/B setup, but at that point I think we'll have a separate partition for data so updates to the os don't wipe everything out.

echo "[expand-rootfs] Detected A/B rootfs layout -> skip."
mkdir -p "$(dirname "$STAMP")"; touch "$STAMP"; exit 0
fi

# GPT backup header when image is flashed to a larger card
if command -v sgdisk >/dev/null 2>&1; then
echo "[expand-rootfs] sgdisk -e $DISK (fix GPT backup at end)"
sgdisk -e "$DISK" || true
command -v partprobe >/dev/null 2>&1 && partprobe "$DISK" || true
udevadm settle || true
fi

#Resize root partition to fill the entire disk
if command -v growpart >/dev/null 2>&1; then
echo "[expand-rootfs] growpart $DISK $PARTNUM"
growpart "$DISK" "$PARTNUM" || true
else
echo "[expand-rootfs] parted -s $DISK unit % resizepart $PARTNUM 100%"
parted -s "$DISK" unit % resizepart "$PARTNUM" 100% || true
fi

command -v partprobe >/dev/null 2>&1 && partprobe "$DISK" || true
udevadm settle || true

#Resize filesystem
case "$FSTYPE" in
ext4|ext3|ext2) resize2fs "$ROOTDEV" ;;
xfs) xfs_growfs / ;;
btrfs) btrfs filesystem resize max / ;;
*) echo "[expand-rootfs] Unknown filesystem type: $FSTYPE -> skipping resize." ;;
esac

echo "[expand-rootfs] OK"
mkdir -p "$(dirname "$STAMP")"
touch "$STAMP"