add swap partition

Signed-off-by: Michel-FK <michel.stempin@funkey-project.com>
This commit is contained in:
Michel-FK 2020-11-02 16:49:47 +01:00
parent c5c4b15523
commit 79a7be9716
11 changed files with 113 additions and 107 deletions

View File

@ -1,9 +1,5 @@
#!/bin/sh
# Add swap partition to fstab
sed -i '/^\/swap/d' "${TARGET_DIR}/etc/fstab"
echo "/swap none swap defaults 0 0" >> "${TARGET_DIR}/etc/fstab"
# Add local path to init scripts
sed -i '3iexport PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin' ${TARGET_DIR}/etc/init.d/rcK
sed -i '3iexport PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin' ${TARGET_DIR}/etc/init.d/rcS

View File

@ -6,6 +6,6 @@ tmpfs /dev/shm tmpfs mode=0777 0 0
tmpfs /tmp tmpfs mode=1777 0 0
tmpfs /run tmpfs mode=0755,nosuid,nodev 0 0
sysfs /sys sysfs defaults 0 0
/swap none swap defaults 0 0
/dev/mmcblk0p3 none swap sw 0 0
configfs /sys/kernel/config configfs rw,relatime 0 0
/dev/mmcblk0p3 /mnt vfat rw,relatime,fmask=0022,dmask=0022,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0 0
/dev/mmcblk0p4 /mnt vfat rw,relatime,fmask=0022,dmask=0022,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0 0

View File

@ -8,8 +8,8 @@ source /usr/local/lib/utils
SELF=$(basename $0)
# Find out the root partition number from the kernel command line
root_part=$(cat /proc/cmdline | sed -n 's|^.*root=/dev/\([^ ]*\).*|\1|p')
root_part_num=${root_part#mmcblk0p}
root_part=$(cat /proc/cmdline | sed -n 's|^.*root=\([^ ]*\).*|\1|p')
root_part_num=${root_part#/dev/mmcblk0p}
if [ "${root_part_num}" -eq 1 ]; then
die 0 "recovery mode"
elif [ "${root_part_num}" = "{$root_part}" ]; then
@ -17,9 +17,13 @@ elif [ "${root_part_num}" = "{$root_part}" ]; then
elif [ "${root_part_num}" -ne 2 ]; then
die 2 "unknown partition layout, aborting"
fi
let swap_part_num=${root_part_num}+1
swap_part=/dev/mmcblk0p${swap_part_num}
let share_part_num=${swap_part_num}+1
share_part=/dev/mmcblk0p${share_part_num}
check_swap () {
[ -f /swap ] && die 0 "nothing to do"
check_first_boot () {
[ -f /.first_boot ] && die 0 "nothing to do"
return 0
}
@ -59,49 +63,57 @@ reload_partition_table () {
}
resize_rootfs_filesystem () {
resize2fs /dev/${root_part} >/dev/null 2>&1 || die 10 "cannot resize the root filesystem, aborting"
resize2fs ${root_part} >/dev/null 2>&1 || die 10 "cannot resize the root filesystem, 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 12 "not enough free space for swap file found, aborting"
fi
# Create an empty 128MB /swap file, change its permissions and format it as swap
dd if=/dev/zero of=/swap bs=32M count=4 >/dev/null 2>&1 &&
chmod 0600 /swap >/dev/null 2>&1 &&
mkswap /swap >/dev/null 2>&1
if [ $? -ne 0 ]; then
rm /swap
die 13 "cannot create swap file, aborting"
fi
return 0
}
enable_swap_file () {
swapon -a >/dev/null 2>&1 || die 14 "cannot enable swap file, aborting"
return 0
}
create_backing_store_partition () {
mount | grep -q /dev/mmcblk0p3
create_swap () {
mount | grep -q ${share_part}
if [ $? -ne 0 ]; then
# Check that the last partition is the rootfs partition
local last_part_line=$(sgdisk -p /dev/mmcblk0 2>/dev/null | tail -n 1)
set ${last_part_line}
local last_part=${1}
if [ "$last_part" != "$root_part_num" ]; then
local last_part_num=${1}
if [ "$last_part_num" != "$root_part_num" ]; then
die 11 "rootfs is not the last partition. Don't know how to create the backing store partition"
fi
# Create an additional linux swap partition
let swap_part_num=${last_part_num}+1
swap_part=/dev/mmcblk0p${swap_part_num}
sgdisk -n ${swap_part_num}:0:+128M -c ${swap_part_num}:swap -t ${swap_part_num}:8200 /dev/mmcblk0 >/dev/null 2>&1 || die 12 "cannot create the swap partition, aborting"
sync
partprobe /dev/mmcblk0 >/dev/null 2>&1 || die 13 "cannot reload the partition table, aborting"
mkswap ${swap_part} >/dev/null 2>&1
if [ $? -ne 0 ]; then
die 14 "cannot create swap file, aborting"
fi
fi
return 0
}
enable_swap () {
swapon -a >/dev/null 2>&1 || die 15 "cannot enable swap file, aborting"
return 0
}
create_backing_store_partition () {
mount | grep -q ${share_part}
if [ $? -ne 0 ]; then
# Check that the last partition is the swap partition
local last_part_line=$(sgdisk -p /dev/mmcblk0 2>/dev/null | tail -n 1)
set ${last_part_line}
local last_part_num=${1}
if [ "${last_part_num}" != "${swap_part_num}" ]; then
die 15 "rootfs is not the last partition. Don't know how to create the backing store partition"
fi
# Create an additional Microsoft basic data partition share partition that fills the disk
let share_part=${last_part}+1
sgdisk -n ${share_part}:0:-0 -c ${share_part}:share -t ${share_part}:0700 /dev/mmcblk0 >/dev/null 2>&1 || die 16 "cannot create the backing store partition, aborting"
let share_part_num=${last_part_num}+1
share_part=/dev/mmcblk0p${share_part_num}
sgdisk -n ${share_part_num}:0:-0 -c ${share_part_num}:share -t ${share_part_num}:0700 /dev/mmcblk0 >/dev/null 2>&1 || die 16 "cannot create the backing store partition, aborting"
sync
fi
return 0
@ -110,7 +122,7 @@ create_backing_store_partition () {
format_backing_store_partition () {
# Format the backing store as FAT32
mkfs.vfat /dev/mmcblk0p3 >/dev/null 2>&1 || die 17 "cannot format the backing store partition"
mkfs.vfat ${share_part} >/dev/null 2>&1 || die 17 "cannot format the backing store partition"
# Add file to force assembly tests
mount /mnt/ || die 18 "Cannot mount /mnt"
@ -119,9 +131,9 @@ format_backing_store_partition () {
return 0
}
notif "First boot detected!"
check_swap
check_root_id
check_first_boot
notif "First boot detected!"
notif "1/8 Resize root partition"
resize_rootfs_partition
@ -133,10 +145,10 @@ notif "3/8 Resize root filsystem"
resize_rootfs_filesystem
notif "4/8 Create swap"
create_swap_file
create_swap
notif "5/8 Enable swap"
enable_swap_file
enable_swap
notif "6/8 Create share partition"
create_backing_store_partition
@ -148,5 +160,6 @@ notif "8/8 Format share partition"
format_backing_store_partition
notif "First boot setup finished!"
touch /.first_boot
sleep 1
clear_notif

View File

@ -13,7 +13,7 @@ source usb_gadget
USB_IF=/etc/network/interfaces.d/usb0
# The USB mass storage backing store file or partition
BACKING_STORE_FILE=/sys/kernel/config/usb_gadget/FunKey/functions/mass_storage.mmcblk0p3/lun.0/file
BACKING_STORE_FILE=/sys/kernel/config/usb_gadget/FunKey/functions/mass_storage.mmcblk0p4/lun.0/file
# Unmount a local share
unmount_share () {
@ -25,11 +25,11 @@ unmount_share () {
# Mount a share locally
mount_share () {
mount | grep -q /dev/mmcblk0p3
mount | grep -q /dev/mmcblk0p4
if [ $? -ne 0 ]; then
# Check if the FAT32 partition has been unmounted cleanly
fsck.fat -n /dev/mmcblk0p3 2>/dev/null | egrep -q "Dirty bit"
fsck.fat -n /dev/mmcblk0p4 2>/dev/null | egrep -q "Dirty bit"
if [ $? -eq 0 ]; then
# The FAT32 partition was not cleanly unmounted, try to
@ -92,7 +92,7 @@ stop_share () {
start_share () {
# 1) Check if the backing store partition is already shared
cat $BACKING_STORE_FILE | egrep -q ^/dev/mmcblk0p3 && die 12 "the backing store partition is already shared"
cat $BACKING_STORE_FILE | egrep -q ^/dev/mmcblk0p4 && die 12 "the backing store partition is already shared"
# 2) Check if USB data is connected
is_usb_data_connected > /dev/null 2>&1 || die 13 "USB sharing impossible, not connected to a host"
@ -102,13 +102,13 @@ start_share () {
# 4) Everything is now clear to start sharing the backing store partition
info "start sharing the backing store partition"
echo /dev/mmcblk0p3 > $BACKING_STORE_FILE || die 14 "cannot share the backing store partition"
echo /dev/mmcblk0p4 > $BACKING_STORE_FILE || die 14 "cannot share the backing store partition"
}
# Return if currently sharing
is_share_started () {
cat $BACKING_STORE_FILE | egrep -q ^/dev/mmcblk0p3
cat $BACKING_STORE_FILE | egrep -q ^/dev/mmcblk0p4
local res=$?
if [ "$res" == "0" ]; then
echo "yes"

View File

@ -53,7 +53,7 @@ init_usb_gadget() {
mkdir ${GADGET}/configs/FunKey.1
mkdir ${GADGET}/configs/FunKey.1/strings/0x409
test ${USBNET} -eq 1 && mkdir ${GADGET}/functions/rndis.usb0
mkdir ${GADGET}/functions/mass_storage.mmcblk0p3
mkdir ${GADGET}/functions/mass_storage.mmcblk0p4
# USB2
echo "0x0200" > ${GADGET}/bcdUSB
@ -124,22 +124,22 @@ init_usb_gadget() {
# Mass Storage Function
# Backing Store file
#echo "/dev/mmcblk0p3" > ${GADGET}/functions/mass_storage.mmcblk0p3/lun.0/file
#echo "/dev/mmcblk0p4" > ${GADGET}/functions/mass_storage.mmcblk0p4/lun.0/file
# Gadget is not allowed to halt bulk endpoints
echo 0 > ${GADGET}/functions/mass_storage.mmcblk0p3/stall
echo 0 > ${GADGET}/functions/mass_storage.mmcblk0p4/stall
# Do not simulate a CDROM
echo 0 > ${GADGET}/functions/mass_storage.mmcblk0p3/lun.0/cdrom
echo 0 > ${GADGET}/functions/mass_storage.mmcblk0p4/lun.0/cdrom
# No SCSI Force Unit Access (FUA) to work in synchronous mode ?!?
echo 0 > ${GADGET}/functions/mass_storage.mmcblk0p3/lun.0/nofua
echo 0 > ${GADGET}/functions/mass_storage.mmcblk0p4/lun.0/nofua
# LUN is removable
echo 1 > ${GADGET}/functions/mass_storage.mmcblk0p3/lun.0/removable
echo 1 > ${GADGET}/functions/mass_storage.mmcblk0p4/lun.0/removable
# Inquiry String
echo "FunKey S Shared Disk" > ${GADGET}/functions/mass_storage.mmcblk0p3/lun.0/inquiry_string
echo "FunKey S Shared Disk" > ${GADGET}/functions/mass_storage.mmcblk0p4/lun.0/inquiry_string
if [ ${USBNET} -eq 1 ]; then
@ -158,7 +158,7 @@ init_usb_gadget() {
fi
# Add the Mass Storage function to the FunKey.1 configuration
ln -s ${GADGET}/functions/mass_storage.mmcblk0p3 ${GADGET}/configs/FunKey.1
ln -s ${GADGET}/functions/mass_storage.mmcblk0p4 ${GADGET}/configs/FunKey.1
# Each interface specifies its own class code
echo "0x00" > ${GADGET}/bDeviceClass

View File

@ -9,7 +9,7 @@ root_mount=/tmp/rootfs
do_preinst()
{
notif "1/6 Extract update"
notif "1/5 Extract update"
exit 0
}
@ -17,41 +17,30 @@ do_postinst()
{
local version
notif "2/6 Resize root filesystem"
notif "2/5 Resize root filesystem"
resize2fs ${root_part}
if [ $? -ne 0 ]; then
notif "Cannot resize root filesystem"
exit 1
fi
notif "3/6 Mount root filesystem"
notif "3/5 Mount root filesystem"
mkdir -p ${root_mount}
mount -t ext4 ${root_part} ${root_mount}
if [ $? -ne 0 ]; then
notif "Cannot mount root filesystem"
exit 1
fi
notif "4/6 Create swap"
# Create an empty 128MB /swap file, change its permissions and format it as swap
dd if=/dev/zero of=${root_mount}/swap bs=32M count=4 &&
chmod 0600 ${root_mount}/swap &&
mkswap ${root_mount}/swap
if [ $? -ne 0 ]; then
rm ${root_mount}/swap
umount ${root_mount}
notif "Cannot create swap file"
exit 1
fi
for file in $(ls /mnt/FunKey-rootfs-*.swu); do
notif "5/6 Erase update file"
rm -f "${file}"
done
notif "6/6 Unmount root filesystem"
notif "4/5 Unmount root filesystem"
touch "${root_mount}/.first_boot"
umount ${root_mount}
if [ $? -ne 0 ]; then
notif "Cannot unmount root filesystem"
exit 1
fi
for file in $(ls /mnt/FunKey-rootfs-*.swu); do
notif "5/5 Erase update file"
rm -f "${file}"
done
exit 0
}

View File

@ -7,4 +7,4 @@ tmpfs /tmp tmpfs mode=1777 0 0
tmpfs /run tmpfs mode=0755,nosuid,nodev 0 0
sysfs /sys sysfs defaults 0 0
configfs /sys/kernel/config configfs rw,relatime 0 0
/dev/mmcblk0p3 /mnt vfat rw,relatime,fmask=0022,dmask=0022,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0 0
/dev/mmcblk0p4 /mnt vfat rw,relatime,fmask=0022,dmask=0022,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0 0

View File

@ -17,7 +17,7 @@ menu_display () {
0)
# USB mount/unmount
mount | grep -q /dev/mmcblk0p3
mount | grep -q /dev/mmcblk0p4
if [ $? -eq 0 ]; then
message=" USB mount"
else
@ -80,7 +80,7 @@ menu_run () {
0)
# USB mount/unmount
mount | grep -q /dev/mmcblk0p3
mount | grep -q /dev/mmcblk0p4
if [ $? -eq 0 ]; then
notif "${message}..."
share start
@ -112,14 +112,14 @@ menu_run () {
1)
# USB check
mount | grep -q /dev/mmcblk0p3
mount | grep -q /dev/mmcblk0p4
if [ $? -eq 0 ]; then
notif "${message}..."
umount /mnt >/dev/null 2>&1
if [ $? -ne 0 ]; then
notif "${message}...^Cannot check USB!"
fi
fsck.fat -a -v -w /dev/mmcblk0p3
fsck.fat -a -v -w /dev/mmcblk0p4
if [ $? -ne 0 ]; then
notif "${message}...^Cannot check USB!"
fi
@ -137,14 +137,14 @@ menu_run () {
2)
# USB format
mount | grep -q /dev/mmcblk0p3
mount | grep -q /dev/mmcblk0p4
if [ $? -eq 0 ]; then
notif "${message}...^Press A to confirm"
while true; do
case $(getkey) in
1e0001|1e0002)
umount /mnt &&
mkfs.vfat /dev/mmcblk0p3 &&
mkfs.vfat /dev/mmcblk0p4 &&
mount /mnt
if [ $? -ne 0 ]; then
notif "${message}...^Cannot format USB!"
@ -171,7 +171,7 @@ menu_run () {
3)
# Network enable/disable
mount | grep -q /dev/mmcblk0p3
mount | grep -q /dev/mmcblk0p4
if [ $? -eq 0 ]; then
if [ -e /mnt/usbnet ]; then
rm -f /mnt/usbnet
@ -196,7 +196,7 @@ menu_run () {
5)
# Factory tests enable/disable
mount | grep -q /dev/mmcblk0p3
mount | grep -q /dev/mmcblk0p4
if [ $? -eq 0 ]; then
if [ -e /mnt/.assembly_tests ]; then
rm -f /mnt/.assembly_tests

View File

@ -13,7 +13,7 @@ source usb_gadget
USB_IF=/etc/network/interfaces.d/usb0
# The USB mass storage backing store file or partition
BACKING_STORE_FILE=/sys/kernel/config/usb_gadget/FunKey/functions/mass_storage.mmcblk0p3/lun.0/file
BACKING_STORE_FILE=/sys/kernel/config/usb_gadget/FunKey/functions/mass_storage.mmcblk0p4/lun.0/file
# Unmount a local share
unmount_share () {
@ -25,11 +25,11 @@ unmount_share () {
# Mount a share locally
mount_share () {
mount | grep -q /dev/mmcblk0p3
mount | grep -q /dev/mmcblk0p4
if [ $? -ne 0 ]; then
# Check if the FAT32 partition has been unmounted cleanly
fsck.fat -n /dev/mmcblk0p3 2>/dev/null | egrep -q "Dirty bit"
fsck.fat -n /dev/mmcblk0p4 2>/dev/null | egrep -q "Dirty bit"
if [ $? -eq 0 ]; then
# The FAT32 partition was not cleanly unmounted, try to
@ -86,7 +86,7 @@ start_share () {
# 1) Check if the backing store partition is already shared
if [ -e $BACKING_STORE_FILE ]; then
cat $BACKING_STORE_FILE | egrep -q ^/dev/mmcblk0p3 && die 12 "the backing store partition is already shared"
cat $BACKING_STORE_FILE | egrep -q ^/dev/mmcblk0p4 && die 12 "the backing store partition is already shared"
fi
# 2) Check if USB data is connected
@ -97,7 +97,7 @@ start_share () {
# 4) Everything is now clear to start sharing the backing store partition
info "start sharing the backing store partition"
echo /dev/mmcblk0p3 > $BACKING_STORE_FILE || die 14 "cannot share the backing store partition"
echo /dev/mmcblk0p4 > $BACKING_STORE_FILE || die 14 "cannot share the backing store partition"
}
# Return if currently sharing
@ -107,7 +107,7 @@ is_share_started () {
echo "no"
return 1
fi
cat $BACKING_STORE_FILE | egrep -q ^/dev/mmcblk0p3
cat $BACKING_STORE_FILE | egrep -q ^/dev/mmcblk0p4
local res=$?
if [ "$res" == "0" ]; then
echo "yes"

View File

@ -52,7 +52,7 @@ init_usb_gadget() {
mkdir ${GADGET}/configs/FunKey.1
mkdir ${GADGET}/configs/FunKey.1/strings/0x409
test ${USBNET} -eq 1 && mkdir ${GADGET}/functions/rndis.usb0
mkdir ${GADGET}/functions/mass_storage.mmcblk0p3
mkdir ${GADGET}/functions/mass_storage.mmcblk0p4
# USB2
echo "0x0200" > ${GADGET}/bcdUSB
@ -123,22 +123,22 @@ init_usb_gadget() {
# Mass Storage Function
# Backing Store file
#echo "/dev/mmcblk0p3" > ${GADGET}/functions/mass_storage.mmcblk0p3/lun.0/file
#echo "/dev/mmcblk0p4" > ${GADGET}/functions/mass_storage.mmcblk0p4/lun.0/file
# Gadget is not allowed to halt bulk endpoints
echo 0 > ${GADGET}/functions/mass_storage.mmcblk0p3/stall
echo 0 > ${GADGET}/functions/mass_storage.mmcblk0p4/stall
# Do not simulate a CDROM
echo 0 > ${GADGET}/functions/mass_storage.mmcblk0p3/lun.0/cdrom
echo 0 > ${GADGET}/functions/mass_storage.mmcblk0p4/lun.0/cdrom
# No SCSI Force Unit Access (FUA) to work in synchronous mode ?!?
echo 0 > ${GADGET}/functions/mass_storage.mmcblk0p3/lun.0/nofua
echo 0 > ${GADGET}/functions/mass_storage.mmcblk0p4/lun.0/nofua
# LUN is removable
echo 1 > ${GADGET}/functions/mass_storage.mmcblk0p3/lun.0/removable
echo 1 > ${GADGET}/functions/mass_storage.mmcblk0p4/lun.0/removable
# Inquiry String
echo "FunKey S Shared Disk" > ${GADGET}/functions/mass_storage.mmcblk0p3/lun.0/inquiry_string
echo "FunKey S Shared Disk" > ${GADGET}/functions/mass_storage.mmcblk0p4/lun.0/inquiry_string
if [ ${USBNET} -eq 1 ]; then
@ -157,7 +157,7 @@ init_usb_gadget() {
fi
# Add the Mass Storage function to the FunKey.1 configuration
ln -s ${GADGET}/functions/mass_storage.mmcblk0p3 ${GADGET}/configs/FunKey.1
ln -s ${GADGET}/functions/mass_storage.mmcblk0p4 ${GADGET}/configs/FunKey.1
# Each interface specifies its own class code
echo "0x00" > ${GADGET}/bDeviceClass

View File

@ -45,13 +45,21 @@ image sdcard.img {
partition-type = 0x83
bootable = "yes"
image = "FunKey/output/images/rootfs.ext4"
size = 100M
size = 100M # This will be resized to 1G during first boot
}
# partition share {
# offset = 1G
# These partitions will be created during first boot
# partition swap {
# offset = 1.01G
# partition-type = 0xC
# bootable = "false"
# size = 0
# size = 128M
# }
#
# partition share {
# offset = 1.138G
# partition-type = 0xC
# bootable = "false"
# size = 0 # Fill in the disk up to its full size
# }
}