Initial commit

This commit is contained in:
DanyLE
2025-03-05 19:53:19 +01:00
parent 05bb55c837
commit a4637ec44b
46 changed files with 1829 additions and 0 deletions

View File

@ -0,0 +1,44 @@
DESCRIPTION = "Auto configuration deployment from media"
DEPENDS = ""
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
SRC_URI += "file://confd \
file://expandfs.sh \
file://80_diya \
file://fs_resize \
file://boot-to-recovery"
inherit update-rc.d useradd
USERADD_PACKAGES = "${PN}"
GROUPADD_PACKAGES = "${PN}"
USERADD_PARAM:${PN} = "-u 1000 -d /home/diya -r -s /bin/sh diya"
INITSCRIPT_NAME = "confd"
INITSCRIPT_PARAMS = "start 30 S ."
do_install() {
install -d ${D}/${sysconfdir}/init.d/
install -d ${DEPLOY_DIR_IMAGE}
install -m 0755 ${WORKDIR}/confd ${D}/${sysconfdir}/init.d/confd
install -d ${D}/sbin/
install -d ${D}/etc/default/volatiles
# install -m 0644 ${WORKDIR}/80_diya ${D}${sysconfdir}/default/volatiles
install -m 0755 ${WORKDIR}/expandfs.sh ${D}/sbin/expandfs.sh
install -m 0755 ${WORKDIR}/boot-to-recovery ${D}/sbin/boot-to-recovery
install -m 0755 ${WORKDIR}/fs_resize ${DEPLOY_DIR_IMAGE}/
cat << EOF > ${DEPLOY_DIR_IMAGE}/config.txt
recovery=false
recovery_image=recovery-${MACHINE}.cpio.gz
console=both
bootlogo=false
EOF
install -d ${D}/var/etc
install -d ${D}/var/etc/upper
install -d ${D}/var/etc/work
}

View File

@ -0,0 +1,3 @@
# d root root 0755 /var/etc/upper none
# d root root 0755 /var/etc/work none
# l root root 0644 /etc/dropbear /var/etc/dropbear

View File

@ -0,0 +1,21 @@
#! /bin/sh
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
echo "$0 shall be run as root"
exit 1
fi
. /etc/profile
if [ ! -e /boot/config.txt ]; then
# prepare env for recovery boot
cat << EOF > /boot/config.txt
recovery=true
recovery_image=recovery-${MACHINE}.cpio.gz
EOF
else
sed -i 's/^.*recovery.*/recovery=true/g' /boot/config.txt || \
echo "recovery=true" >> /boot/config.txt
fi
echo "Rebooting to recovery mode"
reboot

View File

@ -0,0 +1,48 @@
#!/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DESC="Auto deployment configuration from media"
if [ -z "$DISK" ]; then
DISK="mmcblk1"
fi
case "$1" in
start)
echo -n "Start $DESC: "
# mount the overlay which is not auto mount by fstab
# this make /etc editable
mount -t overlay -o lowerdir=/etc,upperdir=/var/etc/upper,workdir=/var/etc/work overlay /etc
# for some reason the LCD backlight is turned off
# need to turn it on manually
if [ -f "/boot/fs_resize" ]; then
/sbin/expandfs.sh
touch /boot/home_partition_should_be_formated
rm /boot/fs_resize
reboot
fi
if [ -f "/boot/home_partition_should_be_formated" ]; then
echo "Trying to format the home partition"
umount /dev/${DISK}p4
yes | mkfs.ext4 /dev/${DISK}p4
mount /dev/${DISK}p4 /home
mkdir -p /home/diya
chown -R diya:diya /home/diya
rm /boot/home_partition_should_be_formated
fi
if [ -e "/boot/wpa_supplicant.conf" ];then
mv /boot/wpa_supplicant.conf /etc/network/
reboot
fi
# display information
# disable wifi power save
# /usr/sbin/iw wlan0 set power_save off
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start}" >&2
exit 1
;;
esac
exit 0

View File

@ -0,0 +1,32 @@
#! /bin/sh
if [ -z "$DISK" ]; then
DISK="mmcblk1"
fi
if [ "$(/usr/bin/id -u)" -ne 0 ]; then
echo "$0 shall be run as root"
exit 1
fi
start_sector=$(cat /sys/block/$DISK/${DISK}p4/start)
echo "Start sector is: $start_sector"
if [ -z "$start_sector" ]; then
echo "Cannot find the start sector"
exit 1
fi
echo "Expanding the partition"
sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | fdisk /dev/$DISK
d # delete partition
4 # number 4
n # new partition
p # primary partition
4 # partition number 4
$start_sector
# default - end of disk
p # print the in-memory partition table
w # write the partition table
q # and we're done
EOF

View File

@ -0,0 +1 @@
resize the partition

View File

@ -0,0 +1,21 @@
#!/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DESC="Custom configuration"
. /etc/profile
case "$1" in
start)
echo -n "Start $DESC: "
sed -i 's/^.*recovery.*/recovery=false/g' /boot/config.txt || \
echo "recovery=false" >> /boot/config.txt
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start}" >&2
exit 1
;;
esac
exit 0

View File

@ -0,0 +1,147 @@
#! /bin/sh
. /etc/profile
DEFAULT_ROOTFS_NAME="rootfs-$MACHINE.tar.bz2"
DEFAULT_KERNEL_NAME="Image-$MACHINE.bin"
DEFAULT_INITRAMFS_NAME="recovery-$MACHINE.cpio.gz"
UPDATE_SRC_PATH="/home/diya/.update"
ROOTFS_DEV="/dev/mmcblk1p2"
SUPPORTED_COMMAND="rootfs kernel initramfs all"
TMP_MOUNT="/tmp/rootfs"
rootfs()
{
filename=$1
if [ -z "$filename" ]; then
filename="$DEFAULT_ROOTFS_NAME"
fi
path="$UPDATE_SRC_PATH/$filename"
backup_file="$UPDATE_SRC_PATH/rootfs-backup.img"
echo "Checking rootfs at: $path"
if [ ! -e "$path" ]; then
echo "Error: rootfs file not found"
return 1
fi
# backup the rootfs file
echo "Backing up the current rootfs"
if ! dd if=$ROOTFS_DEV of=$backup_file; then
echo "Error: unable to backup current rootfs"
return 1
fi
echo "Format rootfs partition"
if ! mkfs.ext4 -F $ROOTFS_DEV; then
echo "Error: Unable to format rootfs partition. Restore and quit"
dd if=$backup_file of=$ROOTFS_DEV
return 1
fi
mkdir -p $TMP_MOUNT
echo "Mount rootfs partition to $TMP_MOUNT"
if ! mount $ROOTFS_DEV $TMP_MOUNT; then
echo "Error: Unable to mount rootfs partition. Restore and quit"
dd if=$backup_file of=$ROOTFS_DEV
return 1
fi
echo "Installing new rootfs"
if ! tar -xpvf "$path" -C $TMP_MOUNT; then
echo "Error: unable to install new rootfs. Restore and quit"
umount $TMP_MOUNT
dd if=$backup_file of=$ROOTFS_DEV
return 1
fi
echo "Patch /etc/fstab"
cat << EOF >> $TMP_MOUNT/etc/fstab
/dev/mmcblk1p1 /boot vfat defaults 0 0
/dev/mmcblk1p3 /var/etc ext4 defaults 0 0
/dev/mmcblk1p4 /home ext4 defaults 0 0
EOF
sync
echo "Unmount the rootfs partition"
umount $TMP_MOUNT
echo "Done"
return 0
}
kernel()
{
filename=$1
if [ -z "$filename" ]; then
filename="$DEFAULT_KERNEL_NAME"
fi
path="$UPDATE_SRC_PATH/$filename"
echo "Checking kernel at: $path"
if [ ! -e "$path" ]; then
echo "Error: kernel file not found"
return 1
fi
echo "Update kernel"
cp -v "$path" /boot/kernel8.img
cd /boot
sync
echo "Done"
return 0
}
initramfs()
{
filename=$1
if [ -z "$filename" ]; then
filename="$DEFAULT_INITRAMFS_NAME"
fi
path="$UPDATE_SRC_PATH/$filename"
echo "Checking initramfs at: $path"
if [ ! -e "$path" ]; then
echo "Error: initramfs file not found"
return 1
fi
echo "Update recovery intramfs"
cp -v "$path" /boot/$DEFAULT_INITRAMFS_NAME
cd /boot
sync
echo "Done"
return 0
}
command_valid() {
VALUE=$1
echo $SUPPORTED_COMMAND | tr " " '\n' | grep -F -q -x "$VALUE"
}
name=$(basename $0)
cmd=${name#diya-update-}
file="$1"
if ! command_valid "$cmd"; then
cmd="$1"
file="$2"
fi
case "$cmd" in
rootfs)
rootfs $file
;;
kernel)
kernel $file
;;
initramfs)
initramfs $file
;;
all)
rootfs && kernel && initramfs
;;
*)
cat << EOF
Usage: $name <command> [file]
commands:
- rootfs: update rootfs
- kernel: update kernel image
- initramfs: update recovery initramfs
EOF
exit 1
;;
esac

107
recipes-diya/initramfs/files/init Executable file
View File

@ -0,0 +1,107 @@
#!/bin/sh
# Copyright (C) 2011 O.S. Systems Software LTDA.
# Licensed on MIT
#
# Provides the API to be used by the initramfs modules
#
# Modules need to provide the following functions:
#
# <module>_enabled : check if the module ought to run (return 1 to skip)
# <module>_run : do what is need
#
# Boot parameters are available on environment in the as:
#
# 'foo=value' as 'bootparam_foo=value'
# 'foo' as 'bootparam_foo=true'
# 'foo.bar[=value] as 'foo_bar=[value|true]'
# Load kernel module
load_kernel_module() {
if modprobe $1 >/dev/null 2>&1; then
info "Loaded module $1"
else
debug "Failed to load module $1"
fi
}
# Prints information
msg() {
echo "$@" >/dev/console
}
# Prints information if verbose bootparam is used
info() {
[ -n "$bootparam_verbose" ] && echo "$@" >/dev/console
}
# Prints information if debug bootparam is used
debug() {
[ -n "$bootparam_debug" ] && echo "DEBUG: $@" >/dev/console
}
# Prints a message and start a endless loop
fatal() {
echo $1 >/dev/console
echo >/dev/console
if [ -n "$bootparam_init_fatal_sh" ]; then
sh
else
while [ "true" ]; do
sleep 3600
done
fi
}
# Variables shared amoung modules
EFI_DIR=/sys/firmware/efi # place to store device firmware information
# initialize /proc, /sys, /run/lock and /var/lock
mkdir -p /proc /sys /run/lock /var/lock /var/run /home
mount -t proc proc /proc
mount -t sysfs sysfs /sys
if [ -d $EFI_DIR ];then
mount -t efivarfs none /sys/firmware/efi/efivars
fi
# populate bootparam environment
for p in `cat /proc/cmdline`; do
if [ -n "$quoted" ]; then
value="$value $p"
if [ "`echo $p | sed -e 's/\"$//'`" != "$p" ]; then
eval "bootparam_${quoted}=${value}"
unset quoted
fi
continue
fi
opt=`echo $p | cut -d'=' -f1`
opt=`echo $opt | sed -e 'y/.-/__/'`
if [ "`echo $p | cut -d'=' -f1`" = "$p" ]; then
eval "bootparam_${opt}=true"
else
value="`echo $p | cut -d'=' -f2-`"
if [ "`echo $value | sed -e 's/^\"//'`" != "$value" ]; then
quoted=${opt}
continue
fi
eval "bootparam_${opt}=\"${value}\""
fi
done
# use /dev with devtmpfs
if grep -q devtmpfs /proc/filesystems; then
mkdir -p /dev
mount -t devtmpfs devtmpfs /dev
else
if [ ! -d /dev ]; then
fatal "ERROR: /dev doesn't exist and kernel doesn't has devtmpfs enabled."
fi
fi
# run init
exec /sbin/init
# Catch all
fatal "ERROR: Initramfs failed to initialize the system."

View File

@ -0,0 +1,47 @@
SUMMARY = "Modular initramfs system"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COREBASE}/meta/COPYING.MIT;md5=3da9cfbcb788c80a0384361b4de20420"
RDEPENDS:${PN} += "${VIRTUAL-RUNTIME_base-utils} sysvinit"
RRECOMMENDS:${PN} = "${VIRTUAL-RUNTIME_base-utils-syslog}"
FILESEXTRAPATHS:prepend := "${THISDIR}/files:"
PR = "r4"
inherit allarch update-rc.d
SRC_URI = "file://init \
file://confd \
file://diya-update \
"
S = "${WORKDIR}"
INITSCRIPT_NAME = "confd"
INITSCRIPT_PARAMS = "start 30 S ."
do_install() {
install -d ${D}/etc/init.d
install -d ${D}/sbin
# base
install -m 0755 ${WORKDIR}/init ${D}/init
install -m 0755 ${WORKDIR}/confd ${D}/etc/init.d/confd
cat << EOF >> ${D}/etc/profile
export MACHINE=${MACHINE}
EOF
install -m 0755 ${WORKDIR}/diya-update ${D}/sbin/
# create symlink
ln -sf /sbin/diya-update ${D}/sbin/diya-update-rootfs
ln -sf /sbin/diya-update ${D}/sbin/diya-update-kernel
ln -sf /sbin/diya-update ${D}/sbin/diya-update-initramfs
ln -sf /sbin/diya-update ${D}/sbin/diya-update-all
# Create device nodes expected by some kernels in initramfs
# before even executing /init.
install -d ${D}/dev
mknod -m 622 ${D}/dev/console c 5 1
}
FILES:${PN} = "/etc /init /dev /sbin"