Initial commit

This commit is contained in:
Your Name
2026-04-23 17:07:55 +08:00
commit b7e39e063b
16725 changed files with 1625565 additions and 0 deletions
@@ -0,0 +1,82 @@
#!/bin/sh
# Copyright (C) 2011 O.S. Systems Software LTDA.
# Licensed on MIT
# Adds support to dynamic debugging of initramfs using bootparam in
# following format:
# shell : starts a shell before and after each module
# shell=before:<module> : starts a shell before <module> is loaded and run
# shell=after:<module> : starts a shell after <module> is loaded and run
#
# shell-debug : run set -x as soon as possible
# shell-debug=before:<module> : run set -x before <module> is loaded and run
# shell-debug=after:<module> : run set -x after <module> is loaded and run
DEBUG_SHELL="false"
debug_hook_handler() {
status=$1
module=$2
if [ -n "$bootparam_shell" ] && [ "$bootparam_shell" != "true" ]; then
shell_wanted_status=`expr $bootparam_shell : '\(.*\):.*'`
shell_wanted_module=`expr $bootparam_shell : '.*:\(.*\)'`
if [ "$shell_wanted_status" = "before" ]; then
shell_wanted_status=pre
else
shell_wanted_status=post
fi
fi
if [ "$bootparam_shell" = "true" ] ||
( [ "$status" = "$shell_wanted_status" ] &&
[ "$module" = "$shell_wanted_module" ] ); then
if [ "$status" = "pre" ]; then
status_msg="before"
else
status_msg="after"
fi
msg "Starting shell $status_msg $module..."
sh
fi
if [ -n "$bootparam_shell_debug" ] && [ "$bootparam_shell_debug" != "true" ]; then
shell_debug_wanted_status=`expr $bootparam_shell_debug : '\(.*\):.*'`
shell_debug_wanted_module=`expr $bootparam_shell_debug : '.*:\(.*\)'`
if [ "$shell_debug_wanted_status" = "before" ]; then
shell_debug_wanted_status=pre
else
shell_debug_wanted_status=post
fi
fi
if [ "$bootparam_shell_debug" = "true" ] ||
( [ "$status" = "$shell_debug_wanted_status" ] &&
[ "$module" = "$shell_debug_wanted_module" ] ); then
if [ "$DEBUG_SHELL" = "true" ]; then
return 0
fi
if [ "$status" = "pre" ]; then
status_msg="before"
else
status_msg="after"
fi
msg "Starting shell debugging $status_msg $module..."
DEBUG_SHELL="true"
set -x
fi
}
debug_enabled() {
return 0
}
debug_run() {
add_module_pre_hook "debug_hook_handler"
add_module_post_hook "debug_hook_handler"
}
@@ -0,0 +1,28 @@
#!/bin/sh
# Copyright (C) 2011 O.S. Systems Software LTDA.
# Licensed on MIT
e2fs_enabled() {
return 0
}
e2fs_run() {
filesystems="ext4 ext3 ext2"
# load modules
for fs in $filesystems; do
load_kernel_module $fs
done
for fs in $filesystems; do
eval "fs_options=\$bootparam_${fs}"
if [ -n "$fs_options" ]; then
dev=`expr "$fs_options" : '\([^:]*\).*'`
path=`expr "$fs_options" : '[^:]*:\([^:]*\).*'`
info "Mounting $dev as $fs on $path as $fs..."
mkdir -p $path
mount -t $fs $dev $path
fi
done
}
@@ -0,0 +1,29 @@
#!/bin/sh
# Copyright (C) 2017 O.S. Systems Software LTDA.
# Licensed on MIT
EXEC_DIR=/exec.d # place to look for modules
exec_enabled() {
return 0
}
exec_run() {
if [ ! -d $EXEC_DIR ]; then
msg "No contents to exec in $EXEC_DIR. Starting shell ..."
sh
fi
# Load and run modules
for m in $EXEC_DIR/*; do
# Skip backup files
if [ "`echo $m | sed -e 's/\~$//'`" != "$m" ]; then
continue
fi
debug "Starting $m"
# process module
./$m
done
}
@@ -0,0 +1,48 @@
#!/bin/sh
# Copyright (C) 2011 O.S. Systems Software LTDA.
# Licensed on MIT
finish_enabled() {
return 0
}
finish_run() {
if [ -n "$ROOTFS_DIR" ]; then
if [ ! -d $ROOTFS_DIR/dev ]; then
fatal "ERROR: There's no '/dev' on rootfs."
fi
# Unmount anything that was automounted by busybox via mdev-mount.sh.
# We're about to switch_root, and leaving anything mounted will prevent
# the next rootfs from modifying the block device. Ignore ROOT_DISK,
# if it was set by setup-live, because it'll be mounted over loopback
# to ROOTFS_DIR.
local dev
for dev in /run/media/*; do
if mountpoint -q "${dev}" && [ "${dev##*/}" != "${ROOT_DISK}" ]; then
umount -f "${dev}" || debug "Failed to unmount ${dev}"
fi
done
info "Switching root to '$ROOTFS_DIR'..."
debug "Moving basic mounts onto rootfs"
for dir in `awk '/\/dev.* \/run\/media/{print $2}' /proc/mounts`; do
# Parse any OCT or HEX encoded chars such as spaces
# in the mount points to actual ASCII chars
dir=`printf $dir`
mkdir -p "${ROOTFS_DIR}/media/${dir##*/}"
mount -n --move "$dir" "${ROOTFS_DIR}/media/${dir##*/}"
done
debug "Moving /dev, /proc and /sys onto rootfs..."
mount --move /dev $ROOTFS_DIR/dev
mount --move /proc $ROOTFS_DIR/proc
mount --move /sys $ROOTFS_DIR/sys
cd $ROOTFS_DIR
exec switch_root -c /dev/console $ROOTFS_DIR ${bootparam_init:-/sbin/init}
else
debug "No rootfs has been set"
fi
}
@@ -0,0 +1,163 @@
#!/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]'
# Register a function to be called before running a module
# The hook is called as:
# <function> pre <module>
add_module_pre_hook() {
MODULE_PRE_HOOKS="$MODULE_PRE_HOOKS $1"
}
# Register a function to be called after running a module
# The hook is called as:
# <function> post <module>
add_module_post_hook() {
MODULE_POST_HOOKS="$MODULE_POST_HOOKS $1"
}
# 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
ROOTFS_DIR="/rootfs" # where to do the switch root
MODULE_PRE_HOOKS="" # functions to call before running each module
MODULE_POST_HOOKS="" # functions to call after running each module
MODULES_DIR=/init.d # place to look for modules
EFI_DIR=/sys/firmware/efi # place to store device firmware information
# make mount stop complaining about missing /etc/fstab
touch /etc/fstab
# initialize /proc, /sys, /run/lock and /var/lock
mkdir -p /proc /sys /run/lock /var/lock
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
mkdir $ROOTFS_DIR
# Load and run modules
for m in $MODULES_DIR/*; do
# Skip backup files
if [ "`echo $m | sed -e 's/\~$//'`" != "$m" ]; then
continue
fi
module=`basename $m | cut -d'-' -f 2`
debug "Loading module $module"
# pre hooks
for h in $MODULE_PRE_HOOKS; do
debug "Calling module hook (pre): $h"
eval "$h pre $module"
debug "Finished module hook (pre): $h"
done
# process module
. $m
if ! eval "${module}_enabled"; then
debug "Skipping module $module"
continue
fi
debug "Running ${module}_run"
eval "${module}_run"
# post hooks
for h in $MODULE_POST_HOOKS; do
debug "Calling module hook (post): $h"
eval "$h post $module"
debug "Finished module hook (post): $h"
done
done
# Catch all
fatal "ERROR: Initramfs failed to initialize the system."
@@ -0,0 +1,13 @@
#!/bin/sh
lvm_enabled() {
if ! lvscan |grep -i -w "inactive" &>/dev/null;then
return 1
fi
return 0
}
lvm_run() {
lvm pvscan --cache --activate ay
udevadm trigger --action=add
}
@@ -0,0 +1,30 @@
#!/bin/sh
# Copyright (C) 2011, 2017 O.S. Systems Software LTDA.
# Licensed on MIT
mdev_enabled() {
if [ ! -e /sbin/mdev ]; then
debug "/sbin/mdev doesn't exist"
return 1
fi
return 0
}
mdev_run() {
# setup the environment
mount -t tmpfs tmpfs /dev
mkdir -m 1777 /dev/shm
mkdir -m 0755 /dev/pts
mount -t devpts devpts /dev/pts
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
# load modules for devices
find /sys -name modalias | while read m; do
load_kernel_module $(cat "$m")
done
}
@@ -0,0 +1,48 @@
#!/bin/sh
nfsrootfs_enabled() {
if [ ${bootparam_root} != "/dev/nfs" ] || [ -z ${bootparam_nfsroot} ]; then
return 1
fi
return 0
}
nfsrootfs_run() {
local nfs_opts
local location
local flags
local server_ip
nfs_opts=""
if [ "${bootparam_nfsroot#*,}" != "${bootparam_nfsroot}" ]; then
nfs_opts="-o ${bootparam_nfsroot#*,}"
fi
location="${bootparam_nfsroot%%,*}"
if [ "${location#*:}" = "${location}" ]; then
# server-ip not given. Get server ip from ip option
server_ip=""
if [ "${bootparam_ip#*:}" != "${bootparam_ip}" ]; then
server_ip=$(echo "$bootparam_ip" | cut -d: -f2)
fi
if [ -z "$server_ip" ]; then
fatal "Server IP is not set. Update ip or nfsroot options."
fi
location=${server_ip}:${location}
fi
flags="-o nolock"
if [ -n "$bootparam_ro" ] && ! echo "$bootparam_rootflags" | grep -w -q "ro"; then
if [ -n "$bootparam_rootflags" ]; then
bootparam_rootflags="$bootparam_rootflags,"
fi
bootparam_rootflags="${bootparam_rootflags}ro"
fi
if [ -n "$bootparam_rootflags" ]; then
flags="$flags -o $bootparam_rootflags"
fi
mount -t nfs ${flags} ${nfs_opts} ${location} ${ROOTFS_DIR}
}
@@ -0,0 +1,112 @@
#!/bin/sh
# SPDX-License-Identifier: MIT
#
# Copyright 2022 (C), Microsoft Corporation
# Simple initramfs module intended to mount a read-write (RW)
# overlayfs on top of /, keeping the original root filesystem
# as read-only (RO), free from modifications by the user.
#
# NOTE: The read-only IMAGE_FEATURE is not required for this to work
#
# This script is based on the overlay-etc.bbclass, which sets up
# an overlay on top of the /etc directory, but in this case allows
# accessing the original, unmodified rootfs at /rofs after boot.
#
# It relies on the initramfs-module-rootfs to mount the original
# root filesystem, and requires 'rootrw=<foo>' to be passed as a
# kernel parameter, specifying the device/partition intended to
# use as RW.
#
# This module needs to be executed after the initramfs-module-rootfs
# since it relies on it to mount the filesystem at initramfs startup
# but before the finish module which normally switches root.
# After overlayroot is executed the usual boot flow continues from
# the real init process.
#
# If something goes wrong while running this module, the rootfs
# is still mounted RO (with no overlay) and the finish module is
# executed to continue booting normally.
#
# It also has a dependency on overlayfs being enabled in the
# running kernel via KERNEL_FEATURES (kmeta) or any other means.
PATH=/sbin:/bin:/usr/sbin:/usr/bin
# We get OLDROOT from the rootfs module
OLDROOT="/rootfs"
NEWROOT="${RWMOUNT}/root"
RWMOUNT="/overlay"
ROMOUNT="${RWMOUNT}/rofs"
UPPER_DIR="${RWMOUNT}/upper"
WORK_DIR="${RWMOUNT}/work"
MODULES_DIR=/init.d
# Something went wrong, make sure / is mounted as read only anyway.
exit_gracefully() {
echo $1 >/dev/console
echo >/dev/console
echo "OverlayRoot mounting failed, starting system as read-only" >/dev/console
echo >/dev/console
# The following is borrowed from rootfs-postcommands.bbclass
# This basically looks at the real rootfs mounting options and
# replaces them with "ro"
# Tweak the mount option and fs_passno for rootfs in fstab
if [ -f ${OLDROOT}/etc/fstab ]; then
sed -i -e '/^[#[:space:]]*\/dev\/root/{s/defaults/ro/;s/\([[:space:]]*[[:digit:]]\)\([[:space:]]*\)[[:digit:]]$/\1\20/}' ${OLDROOT}/etc/fstab
fi
# Tweak the "mount -o remount,rw /" command in busybox-inittab inittab
if [ -f ${OLDROOT}/etc/inittab ]; then
sed -i 's|/bin/mount -o remount,rw /|/bin/mount -o remount,ro /|' ${OLDROOT}/etc/inittab
fi
# Continue as if the overlayroot module didn't exist to continue booting
. $MODULES_DIR/99-finish
eval "finish_run"
}
if [ -z "$bootparam_rootrw" ]; then
exit_gracefully "rootrw= kernel parameter doesn't exist and its required to mount the overlayfs"
fi
mkdir -p ${RWMOUNT}
# Mount RW device
if mount -n -t ${bootparam_rootfstype:-ext4} -o ${bootparam_rootflags:-defaults} ${bootparam_rootrw} ${RWMOUNT}
then
# Set up overlay directories
mkdir -p ${UPPER_DIR}
mkdir -p ${WORK_DIR}
mkdir -p ${NEWROOT}
mkdir -p ${ROMOUNT}
# Remount OLDROOT as read-only
mount -o bind ${OLDROOT} ${ROMOUNT}
mount -o remount,ro ${ROMOUNT}
# Mount RW overlay
mount -t overlay overlay -o lowerdir=${ROMOUNT},upperdir=${UPPER_DIR},workdir=${WORK_DIR} ${NEWROOT} || exit_gracefully "initramfs-overlayroot: Mounting overlay failed"
else
exit_gracefully "initramfs-overlayroot: Mounting RW device failed"
fi
# Set up filesystems on overlay
mkdir -p ${NEWROOT}/proc
mkdir -p ${NEWROOT}/dev
mkdir -p ${NEWROOT}/sys
mkdir -p ${NEWROOT}/rofs
mount -n --move ${ROMOUNT} ${NEWROOT}/rofs
mount -n --move /proc ${NEWROOT}/proc
mount -n --move /sys ${NEWROOT}/sys
mount -n --move /dev ${NEWROOT}/dev
exec chroot ${NEWROOT}/ ${bootparam_init:-/sbin/init} || exit_gracefully "Couldn't chroot into overlay"
@@ -0,0 +1,66 @@
#!/bin/sh
# Copyright (C) 2011 O.S. Systems Software LTDA.
# Licensed on MIT
rootfs_enabled() {
return 0
}
rootfs_run() {
if [ -z "$ROOTFS_DIR" ]; then
return
fi
C=0
delay=${bootparam_rootdelay:-1}
timeout=${bootparam_roottimeout:-5}
while ! mountpoint -q $ROOTFS_DIR; do
if [ $(( $C * $delay )) -gt $timeout ]; then
fatal "root '$bootparam_root' doesn't exist or does not contain a /dev."
fi
if [ -n "$bootparam_root" ]; then
debug "No e2fs compatible filesystem has been mounted, mounting $bootparam_root..."
if [ "`echo ${bootparam_root} | cut -c1-5`" = "UUID=" ]; then
root_uuid=`echo $bootparam_root | cut -c6-`
bootparam_root="/dev/disk/by-uuid/$root_uuid"
elif [ "`echo ${bootparam_root} | cut -c1-9`" = "PARTUUID=" ]; then
root_partuuid=`echo $bootparam_root | cut -c10-`
bootparam_root="/dev/disk/by-partuuid/$root_partuuid"
elif [ "`echo ${bootparam_root} | cut -c1-10`" = "PARTLABEL=" ]; then
root_partlabel=`echo $bootparam_root | cut -c11-`
bootparam_root="/dev/disk/by-partlabel/$root_partlabel"
elif [ "`echo ${bootparam_root} | cut -c1-6`" = "LABEL=" ]; then
root_label=`echo $bootparam_root | cut -c7-`
bootparam_root="/dev/disk/by-label/$root_label"
fi
if [ -e "$bootparam_root" ]; then
flags=""
if [ -n "$bootparam_ro" ] && ! echo "$bootparam_rootflags" | grep -w -q "ro"; then
if [ -n "$bootparam_rootflags" ]; then
bootparam_rootflags="$bootparam_rootflags,"
fi
bootparam_rootflags="${bootparam_rootflags}ro"
fi
if [ -n "$bootparam_rootflags" ]; then
flags="$flags -o$bootparam_rootflags"
fi
if [ -n "$bootparam_rootfstype" ]; then
flags="$flags -t$bootparam_rootfstype"
fi
mount $flags $bootparam_root $ROOTFS_DIR
if mountpoint -q $ROOTFS_DIR; then
break
else
# It is unlikely to change, but keep trying anyway.
# Perhaps we pick a different device next time.
umount $ROOTFS_DIR
fi
fi
fi
debug "Sleeping for $delay second(s) to wait root to settle..."
sleep $delay
C=$(( $C + 1 ))
done
}
@@ -0,0 +1,64 @@
#!/bin/sh
# Copyright (C) 2011 O.S. Systems Software LTDA.
# Licensed on MIT
setup_enabled() {
return 0
}
setup_run() {
ROOT_IMAGE="rootfs.img"
ISOLINUX=""
ROOT_DISK=""
shelltimeout=30
if [ -z "$bootparam_root" -o "$bootparam_root" = "/dev/ram0" ]; then
echo "Waiting for removable media..."
C=0
while true
do
for i in `ls /run/media 2>/dev/null`; do
if [ -f /run/media/$i/$ROOT_IMAGE ] ; then
found="yes"
ROOT_DISK="$i"
break
elif [ -f /run/media/$i/isolinux/$ROOT_IMAGE ]; then
found="yes"
ISOLINUX="isolinux"
ROOT_DISK="$i"
break
fi
done
if [ "$found" = "yes" ]; then
break;
fi
# don't wait for more than $shelltimeout seconds, if it's set
if [ -n "$shelltimeout" ]; then
echo -n " " $(( $shelltimeout - $C ))
if [ $C -ge $shelltimeout ]; then
echo "..."
echo "Mounted filesystems"
mount | grep media
echo "Available block devices"
cat /proc/partitions
fatal "Cannot find $ROOT_IMAGE file in /run/media/* , dropping to a shell "
fi
C=$(( C + 1 ))
fi
sleep 1
done
# The existing rootfs module has no support for rootfs images. Assign the rootfs image.
bootparam_root="/run/media/$ROOT_DISK/$ISOLINUX/$ROOT_IMAGE"
fi
if [ "$bootparam_LABEL" != "boot" -a -f /init.d/$bootparam_LABEL.sh ] ; then
if [ -f /run/media/$i/$ISOLINUX/$ROOT_IMAGE ] ; then
./init.d/$bootparam_LABEL.sh $i/$ISOLINUX $ROOT_IMAGE $video_mode $vga_mode $console_params
else
fatal "Could not find $bootparam_LABEL script"
fi
# If we're getting here, we failed...
fatal "Target $bootparam_LABEL failed"
fi
}
@@ -0,0 +1,50 @@
#!/bin/sh
# Copyright (C) 2011, 2012 O.S. Systems Software LTDA.
# Licensed on MIT
udev_shutdown_hook_handler() {
status=$1
module=$2
if [ "$status" = "pre" ] && [ "$module" = "finish" ]; then
udevadm settle
killall `basename $_UDEV_DAEMON` 2>/dev/null
fi
}
udev_daemon() {
OPTIONS="/sbin/udev/udevd /sbin/udevd /lib/udev/udevd /lib/systemd/systemd-udevd"
for o in $OPTIONS; do
if [ -x "$o" ]; then
echo $o
return 0
fi
done
return 1
}
_UDEV_DAEMON=`udev_daemon`
udev_enabled() {
if [ -z "$_UDEV_DAEMON" ]; then
msg "WARNING: Cannot find the udev daemon; daemon will not be started in initramfs."
return 1
fi
return 0
}
udev_run() {
add_module_pre_hook "udev_shutdown_hook_handler"
mkdir -p /run
mkdir -p /var/run
# Workaround if console=null, systemd-udevd needs valid stdin, stdout and stderr to work
sh -c "exec 4< /dev/console" || { exec 0> /dev/null; exec 1> /dev/null; exec 2> /dev/null; }
$_UDEV_DAEMON --daemon
udevadm trigger --action=add
udevadm settle
}