SHA256
Initial commit
This commit is contained in:
commit
73cb3db834
4 files changed
+386
No files matched your search
@@ -0,0 +1 @@
|
||||
/target
|
||||
Executable
+29
@@ -0,0 +1,29 @@
|
||||
#!/bin/sh
|
||||
|
||||
target_arch=${TARGET_ARCH:-$(uname -m)}
|
||||
|
||||
cd $(dirname $0)
|
||||
mkdir -p ./target
|
||||
rm -rf ./target/${target_arch}
|
||||
mkdir ./target/${target_arch}
|
||||
cd ./target/${target_arch}
|
||||
|
||||
mkdir -p ./var/config/packie
|
||||
cp -r /var/config/packie/repos.d/ ./var/config/packie/
|
||||
mkdir tmp dev sys proc home etc
|
||||
if [ -e "/etc/resolv.conf" ]; then
|
||||
cp /etc/resolv.conf ./etc/resolv.conf
|
||||
fi
|
||||
if [ -e "/var/config/network/resolv.conf" ]; then
|
||||
cp /var/config/network/resolv.conf.conf ./var/config/network/resolv.conf
|
||||
fi
|
||||
|
||||
packie --chroot . update
|
||||
packie --chroot . install -y \
|
||||
mksh coreutils util-linux-mount \
|
||||
util-linux-fsck util-linux-fs kmod \
|
||||
libgcc-compat libunwind lxdeviced
|
||||
packie --chroot . clean
|
||||
|
||||
cp ../../../init.sh ./init
|
||||
chmod +x ./init
|
||||
@@ -0,0 +1,187 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Global arguments (collected from kernel command line)
|
||||
early_mount=
|
||||
root=
|
||||
live_copytoram=
|
||||
rootfstype="auto"
|
||||
rootflags=
|
||||
init="/bin/init /sbin/init /init /bin/sh"
|
||||
|
||||
# Global state
|
||||
services=
|
||||
|
||||
# Helper functions
|
||||
|
||||
log_info() {
|
||||
printf '[\033[1mInfo\033[0m] %s\n' "$1"
|
||||
}
|
||||
|
||||
log_error() {
|
||||
printf '[\033[31mError\033[0m] %s\n' "$1"
|
||||
}
|
||||
|
||||
start_service() {
|
||||
"$@" >>"/tmp/$1.log" 2>&1 &
|
||||
local pid=$!
|
||||
services="$services $pid"
|
||||
log_info "Started initramfs service $@ (pid $pid)."
|
||||
}
|
||||
|
||||
kill_services() {
|
||||
for kill_pid in $services; do
|
||||
kill -SIGTERM $kill_pid
|
||||
done
|
||||
}
|
||||
|
||||
# Error handling
|
||||
|
||||
emergency() {
|
||||
echo
|
||||
echo "===== EMERGENCY SHELL ====="
|
||||
echo "The operating system failed to boot. An emergency shell is now launched,"
|
||||
echo "you can inspect, and do changes here. Once the shell exits, we will try"
|
||||
echo "to boot the system again."
|
||||
sh
|
||||
umount -q /proc
|
||||
umount -q /sys
|
||||
umount -q /dev
|
||||
umount -q /run
|
||||
kill_services
|
||||
exec /init
|
||||
}
|
||||
|
||||
# Child steps
|
||||
|
||||
setup_live() {
|
||||
mount --mkdir -t "${rootfstype}" -o "${rootflags}" "$1" /liveroot || emergency
|
||||
if [ "${live_copytoram}" -eq "1" ]; then
|
||||
log_info "Copying LiveCD root filesystem to system RAM..."
|
||||
mount --mkdir -t tmpfs none /newroot || emergency
|
||||
cp -a /liveroot/. /newroot/ || emergency
|
||||
else
|
||||
log_error "Live system must enable copytoram to boot currently."
|
||||
emergency
|
||||
fi
|
||||
}
|
||||
|
||||
# Boot steps
|
||||
|
||||
prepare_env() {
|
||||
# Set PATH to /bin
|
||||
export PATH=/bin
|
||||
|
||||
# Mount necessary kernel filesystems.
|
||||
mount --mkdir -t proc none /proc || emergency
|
||||
mount --mkdir -t sysfs none /sys || emergency
|
||||
mount --mkdir -t devtmpfs none /dev || emergency
|
||||
mount --mkdir -t tmpfs none /run || emergency
|
||||
mkdir -p /tmp || emergency
|
||||
|
||||
# Some kernels may require `/sbin` or `/usr/sbin` for modprobe helpers.
|
||||
ln -sf /bin /sbin
|
||||
mkdir -p /usr
|
||||
ln -sf /bin /usr/bin
|
||||
ln -sf /bin /usr/sbin
|
||||
|
||||
# Activate console output by emitting a newline
|
||||
echo
|
||||
}
|
||||
|
||||
start_lxdeviced() {
|
||||
LXDEVICED_HAS_DEVTMPFS=1 start_service lxdeviced
|
||||
until device wait-for-idle >>/tmp/device-start-loop.log 2>&1; do
|
||||
sleep 0.05
|
||||
done
|
||||
device trigger --action=add
|
||||
}
|
||||
|
||||
parse_kcmdline() {
|
||||
local kcmdline=$(cat /proc/cmdline)
|
||||
for entry in $kcmdline; do
|
||||
case $entry in
|
||||
early_mount=*)
|
||||
early_mount="${early_mount} ${entry#early_mount=}"
|
||||
;;
|
||||
root=*)
|
||||
root="${entry#root=}"
|
||||
;;
|
||||
copytoram)
|
||||
live_copytoram="1"
|
||||
;;
|
||||
rootfstype=*)
|
||||
rootfstype="${entry#rootfstype=}"
|
||||
;;
|
||||
rootflags=*)
|
||||
rootflags="${rootflags},${entry#rootflags=}"
|
||||
;;
|
||||
ro)
|
||||
rootflags="${rootflags},ro"
|
||||
;;
|
||||
init=*)
|
||||
init="${entry#init=}"
|
||||
;;
|
||||
*=*)
|
||||
export "${entry}"
|
||||
;;
|
||||
*)
|
||||
;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
do_early_mount() {
|
||||
device wait-for-idle
|
||||
for early_mount_spec in ${early_mount}; do
|
||||
IFS=':' read -r early_mount_src early_mount_dst <<< "$early_mount_spec"
|
||||
log_info "Mounting ${early_mount_src} to ${early_mount_dst}..."
|
||||
mount --mkdir "${early_mount_src}" "${early_mount_dst}" || emergency
|
||||
done
|
||||
}
|
||||
|
||||
do_fsck() {
|
||||
return
|
||||
}
|
||||
|
||||
enter_newroot() {
|
||||
device wait-for-idle
|
||||
case "${root}" in
|
||||
live:*)
|
||||
liveroot="${root#live:}"
|
||||
setup_live "${liveroot}"
|
||||
;;
|
||||
"")
|
||||
log_error "Cannot find a root directory to be mounted."
|
||||
emergency
|
||||
;;
|
||||
*)
|
||||
log_info "Mounting new root filesystem ${root}(type ${rootfstype}, flags ${rootflags})..."
|
||||
mount --mkdir -t "${rootfstype}" -o "${rootflags}" "${root}" /newroot || emergency
|
||||
;;
|
||||
esac
|
||||
|
||||
for init_candidate in $init; do
|
||||
if [ -L "/newroot/${init_candidate}" ] || [ -e "/newroot/${init_candidate}" ]; then
|
||||
log_info "Found init at ${init_candidate}, passing system control..."
|
||||
kill_services
|
||||
exec env -i switch_root /newroot "${init_candidate}"
|
||||
fi
|
||||
done
|
||||
|
||||
log_error "Error: No init program is available."
|
||||
emergency
|
||||
}
|
||||
|
||||
# Main logic
|
||||
|
||||
main() {
|
||||
trap emergency EXIT
|
||||
prepare_env
|
||||
start_lxdeviced
|
||||
parse_kcmdline
|
||||
do_early_mount
|
||||
do_fsck
|
||||
enter_newroot
|
||||
}
|
||||
|
||||
main
|
||||
Executable
+169
@@ -0,0 +1,169 @@
|
||||
#!/bin/mksh
|
||||
|
||||
# Compile-time confguration of the program. The variables set here may be
|
||||
# auto-replaced by tool scripts.
|
||||
DEFAULT_ARCH_SUPPORT_DIR=
|
||||
|
||||
# Pre-defined constants.
|
||||
KMOD_DIRS="/lib/lkm /lib/modules"
|
||||
BUNDLE_BUS="virtio-pci virtio-mmio xhci-pci xhci-plat-hcd scsi_mod"
|
||||
BUNDLE_BLOCKHW="ahci nvme virtio-blk sd_mod usb-storage uas sr_mod"
|
||||
BUNDLE_VBLOCKDEV="md_mod dm_mod dm-crypt loop"
|
||||
BUNDLE_FILESYSTEMS="9p smb nfs ext4 xfs btrfs f2fs erofs squashfs vfat iso9660 udf overlay efivarfs"
|
||||
BUNDLE_INTERACTION="usbhid"
|
||||
|
||||
# Global options
|
||||
kernel="$(uname -r)"
|
||||
module_root="/"
|
||||
output_file=
|
||||
arch_support_dir="${DEFAULT_ARCH_SUPPORT_DIR}"
|
||||
quiet=
|
||||
verbose=
|
||||
|
||||
# Global state
|
||||
tempdir="$(mktemp -d)"
|
||||
bundle_modules="${BUNDLE_BUS} ${BUNDLE_FILESYSTEMS} ${BUNDLE_BLOCKHW} ${BUNDLE_VBLOCKDEV} ${BUNDLE_INTERACTION}"
|
||||
|
||||
# Functions
|
||||
|
||||
log_info() {
|
||||
if [ "${quiet}" = "1" ]; then
|
||||
return
|
||||
fi
|
||||
printf "\033[1m=> %s\033[0m\n" "$@"
|
||||
}
|
||||
|
||||
log_trace() {
|
||||
if [ "${verbose}" != "1" ]; then
|
||||
return
|
||||
fi
|
||||
printf "\033[2m ~ %s\033[0m\n" "$@"
|
||||
}
|
||||
|
||||
help() {
|
||||
echo "usage: $0 [-qV] [-k ...] [-o ...] [-a ...]"
|
||||
echo "options:"
|
||||
echo " -k Specify kernel version"
|
||||
echo " -o Specify output file [default: ./initramfs-<KERNEL>.img]"
|
||||
echo " -a Specify architecture support pack directory"
|
||||
echo " -m Specify module root [default: /]"
|
||||
echo " -q Do not output, unless any error occurred"
|
||||
echo " -V Enable verbose output"
|
||||
exit 1
|
||||
}
|
||||
|
||||
parse_args() {
|
||||
while getopts "qVk:o:a:h:m:" opt; do
|
||||
case $opt in
|
||||
q)
|
||||
quiet="1"
|
||||
;;
|
||||
V)
|
||||
verbose="1"
|
||||
;;
|
||||
k)
|
||||
kernel="$OPTARG"
|
||||
;;
|
||||
o)
|
||||
output_file="$OPTARG"
|
||||
;;
|
||||
a)
|
||||
arch_support_dir="$OPTARG"
|
||||
;;
|
||||
m)
|
||||
module_root="$OPTARG"
|
||||
;;
|
||||
h)
|
||||
help
|
||||
;;
|
||||
\?)
|
||||
help
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
output_file=${output_file:-"$(pwd)/initramfs-${kernel}.img"}
|
||||
|
||||
if [ -z "${arch_support_dir}" ]; then
|
||||
echo "mkinitramfs: Please specify arch support dir."
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
copy_base_files() {
|
||||
log_info "Copying base files..."
|
||||
log_trace "Source directory \"${arch_support_dir}\", destination directory \"${tempdir}\""
|
||||
cp -a -r "${arch_support_dir}"/* "${tempdir}/" || exit 1
|
||||
}
|
||||
|
||||
copy_lkm() {
|
||||
local modules_list=$(modprobe -S "${kernel}" --show-depends -d "${module_root}" "$1" 2>/dev/null | grep '^insmod' | awk '{print $2}')
|
||||
if [ -z "$modules_list" ]; then
|
||||
return 1
|
||||
fi
|
||||
|
||||
for module_path in ${modules_list}; do
|
||||
local relative_to_modroot=${module_path#"$module_root"}
|
||||
local relative=$(echo "${relative_to_modroot}" | cut -d/ -f5-)
|
||||
log_trace "Copying module \"${module_path}\"..."
|
||||
mkdir -p "${tempdir}/lib/lkm/${kernel}/$(dirname $relative)"
|
||||
cp -a "${module_path}" "${tempdir}/lib/lkm/${kernel}/${relative}"
|
||||
done
|
||||
}
|
||||
|
||||
init_kmod() {
|
||||
log_info "Initializing kernel modules..."
|
||||
|
||||
mkdir -p "${tempdir}/lib/lkm/${kernel}"
|
||||
ln -s "lkm" "${tempdir}/lib/modules"
|
||||
|
||||
for kmod_dir in ${KMOD_DIRS}; do
|
||||
if [ -e "${module_root}/${kmod_dir}/${kernel}" ]; then
|
||||
local current_kmod_dir="${module_root}/${kmod_dir}/${kernel}"
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -z "${current_kmod_dir}" ]; then
|
||||
echo "mkinitramfs: Kmod directory was not found."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cp "${current_kmod_dir}/modules.order" "${tempdir}/lib/lkm/${kernel}/" || exit 1
|
||||
cp "${current_kmod_dir}/modules.builtin" "${tempdir}/lib/lkm/${kernel}/" || exit 1
|
||||
cp "${current_kmod_dir}/modules.builtin.modinfo" "${tempdir}/lib/lkm/${kernel}/" || exit 1
|
||||
|
||||
for i in ${bundle_modules}; do
|
||||
copy_lkm $i
|
||||
done
|
||||
|
||||
depmod -b "${tempdir}" "${kernel}"
|
||||
}
|
||||
|
||||
generate_image() {
|
||||
log_info "Generating CPIO image (Zstandard compressed)..."
|
||||
|
||||
uncompressed_file=$(mktemp)
|
||||
log_trace "Generating raw CPIO image at \"${uncompressed_file}\""
|
||||
cd "${tempdir}" && find . | cpio -o -H newc > "${uncompressed_file}" && cd "${OLDPWD}"
|
||||
rm -f "${output_file}"
|
||||
log_trace "Zstandard compressing image to \"${output_file}\""
|
||||
zstd -q -o "${output_file}" "${uncompressed_file}"
|
||||
rm -f "${uncompressed_file}"
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
log_info "Cleaning up temp files..."
|
||||
rm -rf "${tempdir}"
|
||||
}
|
||||
|
||||
# Main logic
|
||||
|
||||
parse_args "$@"
|
||||
|
||||
copy_base_files
|
||||
|
||||
init_kmod
|
||||
|
||||
generate_image
|
||||
|
||||
cleanup
|
||||
Reference in new issue
Block a user