first_boot script

This commit is contained in:
Michel-FK
2020-05-09 23:49:37 +02:00
parent 2c2949f73e
commit 10150b111d
27 changed files with 915 additions and 368 deletions

View File

@@ -0,0 +1,176 @@
#!/bin/sh
# Uncomment the following line to get debug info
#set -x
SELF=$(basename $0)
source /usr/local/lib/utils
check_swap () {
[ -f /swap ] && die 0 "nothing to do"
return 0
}
check_root_id () {
[ $(id -u) -ne 0 ] && die 1 "this script must be run as root, aborting"
return 0
}
resize_rootfs_partition () {
local root_part=$(cat /proc/cmdline | sed -n 's|^.*root=/dev/\([^ ]*\).*|\1|p')
local part_num=${root_part#mmcblk0p}
if [ "${part_num}" = "{$root_part}" ]; then
die 2 "${root_part} is not an SD card. Don't know how to expand it, aborting"
fi
if [ "${part_num}" -ne 2 ]; then
die 3 "your partition layout is not currently supported by this tool, aborting"
fi
local last_part_line=$(fdisk /dev/mmcblk0 -l | grep '^/' | tail -n 1)
set ${last_part_line}
local last_part=${1#/dev/}
local part_start=${2}
if [ "${last_part}" != "${root_part}" ]; then
die 4 "${root_part} is not the last partition. Don't know how to expand, aborting"
fi
# Return value will likely be error for fdisk as it fails to reload the
# partition table because the root fs is mounted
# NOTE: This script only works the genuine fdisk, NOT with the busybox one!!!
fdisk /dev/mmcblk0 >/dev/null 2>&1 <<EOF
d
${part_num}
n
p
${part_num}
${part_start}
+1G
w
EOF
if [ $? -ne 0 ]; then
die 5 "cannot resize the rootfs partition, aborting"
fi
sync
return 0
}
reload_partition_table () {
partprobe /dev/mmcblk0 >/dev/null 2>&1 || die 6 "cannot reload the partition table, aborting"
return 0
}
resize_rootfs_filesystem () {
local root_part=$(cat /proc/cmdline | sed -n 's|^.*root=/dev/\([^ ]*\).*|\1|p')
resize2fs /dev/${root_part} >/dev/null 2>&1 || die 7 "cannot resize the root filesystem, aborting"
return 0
}
write_bootloader_env () {
fw_saveenv /etc/u-boot.env || die 8 "cannot write bootloader inevrionment, aborting"
return 0
}
create_swap_file () {
local root_part_line=$(df | grep /dev/root)
set ${root_part_line}
local space_left=${4}
if [ ${space_left} -lt 131072 ]; then
die 9 "not enough free space for swap file found, aborting"
fi
dd if=/dev/zero of=/swap bs=1M count=128 >/dev/null 2>&1 &&
chmod 0600 /swap >/dev/null 2>&1 &&
mkswap /swap >/dev/null 2>&1
if [ $? -ne 0 ]; then
rm /swap
die 10 "cannot create swap file, aborting"
fi
return 0
}
enable_swap_file () {
swapon -a >/dev/null 2>&1 || die 11 "cannot enable swap file, aborting"
return 0
}
create_backing_store_partition () {
mount | grep -q /dev/mmcblk0p3
if [ $? -ne 0 ]; then
# Find out the root partition number from kernel command line
local root_part=$(cat /proc/cmdline | sed -n 's|^.*root=/dev/\([^ ]*\).*|\1|p')
local part_num=${root_part#mmcblk0p}
if [ "$part_num" = "$root_part" ]; then
die 5 "$root_part is not an SD card. Don't know how to create the backing store partition"
fi
if [ "$part_num" -ne 2 ]; then
die 6 "your partition layout is not currently supported"
fi
# Check that the last partition is the root partition
local last_part_line=$(fdisk /dev/mmcblk0 -l 2>/dev/null | grep '^/' | tail -n 1)
set $last_part_line
local last_part=${1#/dev/}
if [ "$last_part" != "$root_part" ]; then
die 7 "$root_part is not the last partition. Don't know how to create the backing store partition"
fi
# The return value will likely be error for fdisk as it fails
# to reload the partition table because the root fs is mounted
# NOTE: This script only works the genuine fdisk, NOT with the
# busybox one!!!
# Create a third FAT32 partition that filsl the disk
fdisk /dev/mmcblk0 >/dev/null 2>&1 <<EOF
n
p
3
t
3
c
w
EOF
sync
fi
return 0
}
format_backing_store_partition () {
# Format the backing store as FAT32
mkfs.vfat /dev/mmcblk0p3 >/dev/null 2>&1 || die 9 "cannot format the backing store partition"
return 0
}
info "First boot detected!"
check_swap
check_root_id
info "Step 1/9 - Resize the rootfs partition"
resize_rootfs_partition
info "Step 2/9 - Reload the partition table"
reload_partition_table
info "Step 3/9 - Resize the root filesystem"
resize_rootfs_filesystem
info "Step 4/9 - Write the bootloader environment"
write_bootloader_env
info "Step 5/9 - Create the swap file"
create_swap_file
info "Step 6/9 - Enable the swap file"
enable_swap_file
info "Step 7/9 - Create the backing store partition"
create_backing_store_partition
info "Step 8/9 - Reload the partition table"
reload_partition_table
info "Step 9/9 - Format the backing store partition"
format_backing_store_partition
info "First boot setup finished!"