2020-06-28 09:28:57 +05:30
|
|
|
#!/bin/sh
|
2020-01-30 20:10:13 +05:30
|
|
|
#
|
2020-04-12 01:01:02 +05:30
|
|
|
# tiny init
|
2020-04-15 22:20:45 +05:30
|
|
|
#
|
2020-07-03 21:19:09 +05:30
|
|
|
# false positive
|
|
|
|
# shellcheck disable=2154
|
2020-01-05 23:31:39 +05:30
|
|
|
|
2020-04-21 20:05:55 +05:30
|
|
|
print()
|
|
|
|
{
|
2020-05-07 17:36:34 +05:30
|
|
|
printf "%b %s\n" "${2:-"\033[1;37m>>\033[m"}" "$1"
|
2020-04-21 20:05:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
panic()
|
|
|
|
{
|
2020-05-07 17:36:34 +05:30
|
|
|
print "${1:-unexpected error occurred}" \
|
2020-05-19 09:59:40 +05:30
|
|
|
"\033[1;31m!!\033[m" >&2; sh
|
2020-01-30 20:10:13 +05:30
|
|
|
}
|
2020-01-28 20:43:42 +05:30
|
|
|
|
2020-06-02 16:56:42 +05:30
|
|
|
resolve_device()
|
2020-04-12 01:01:02 +05:30
|
|
|
{
|
2020-06-28 09:28:57 +05:30
|
|
|
count=0; device="$1"
|
2020-04-12 01:01:02 +05:30
|
|
|
|
2020-06-28 09:28:57 +05:30
|
|
|
case "${device%%=*}" in /dev/*) ;;
|
|
|
|
UUID) device="/dev/disk/by-uuid/${device#*=}" ;;
|
|
|
|
LABEL) device="/dev/disk/by-label/${device#*=}" ;;
|
|
|
|
PARTUUID) device="/dev/disk/by-partuuid/${device#*=}" ;;
|
2020-04-12 01:01:02 +05:30
|
|
|
esac
|
|
|
|
|
2020-05-19 09:59:40 +05:30
|
|
|
# prevent race condition
|
2020-06-28 09:28:57 +05:30
|
|
|
# XXX what the hell happens here?
|
|
|
|
# why this loop sometimes trigger panic if i remove '|| :'
|
2020-06-02 16:56:42 +05:30
|
|
|
while [ ! -b "$device" ]; do sleep 1
|
2020-06-28 09:28:57 +05:30
|
|
|
[ "$((count += 1))" = 30 ] && {
|
2020-05-07 17:36:34 +05:30
|
|
|
panic "failed to lookup partition"
|
|
|
|
break
|
|
|
|
}
|
2020-06-28 09:28:57 +05:30
|
|
|
done || :
|
|
|
|
}
|
|
|
|
|
|
|
|
run_hook()
|
|
|
|
{
|
|
|
|
type="$1"; hksdir=/usr/share/tinyramfs/hooks
|
|
|
|
|
|
|
|
# run hooks if any
|
2020-07-03 21:19:09 +05:30
|
|
|
# false positive
|
|
|
|
# shellcheck disable=1090
|
2020-06-28 09:28:57 +05:30
|
|
|
for hook in $hooks; do
|
|
|
|
[ -f "${hksdir}/${hook}/${hook}.${type}" ] || continue
|
|
|
|
. "${hksdir}/${hook}/${hook}.${type}"
|
2020-05-26 19:14:20 +05:30
|
|
|
done
|
2020-02-21 14:27:07 +05:30
|
|
|
}
|
2020-01-05 23:31:39 +05:30
|
|
|
|
2020-04-12 01:01:02 +05:30
|
|
|
prepare_environment()
|
|
|
|
{
|
2020-07-03 21:19:09 +05:30
|
|
|
# false positive
|
|
|
|
# shellcheck disable=1091
|
2020-05-07 17:36:34 +05:30
|
|
|
. /etc/tinyramfs/config
|
2020-04-12 01:01:02 +05:30
|
|
|
|
|
|
|
export \
|
2020-05-19 09:59:40 +05:30
|
|
|
PATH=/bin TERM=linux SHELL=/bin/sh \
|
2020-06-28 09:28:57 +05:30
|
|
|
LANG=C LC_ALL=C PS1="# " HOME=/root
|
2020-03-08 08:59:14 +05:30
|
|
|
|
|
|
|
mount -t proc -o nosuid,noexec,nodev proc /proc
|
|
|
|
mount -t sysfs -o nosuid,noexec,nodev sys /sys
|
2020-05-07 17:36:34 +05:30
|
|
|
mount -t tmpfs -o nosuid,nodev,mode=0755 run /run
|
2020-04-12 01:01:02 +05:30
|
|
|
mount -t devtmpfs -o nosuid,noexec,mode=0755 dev /dev
|
2020-03-08 08:59:14 +05:30
|
|
|
|
2020-05-07 17:36:34 +05:30
|
|
|
ln -s /proc/self/fd /dev/fd
|
|
|
|
ln -s fd/0 /dev/stdin
|
|
|
|
ln -s fd/1 /dev/stdout
|
|
|
|
ln -s fd/2 /dev/stderr
|
2020-04-12 01:01:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
parse_cmdline()
|
|
|
|
{
|
|
|
|
read -r cmdline < /proc/cmdline
|
|
|
|
|
2020-05-19 09:59:40 +05:30
|
|
|
for line in $cmdline; do case "$line" in
|
2020-06-28 09:28:57 +05:30
|
|
|
rootfstype=*) root_type="${line#*=}" ;;
|
|
|
|
rootflags=*) root_opts="${line#*=}" ;;
|
|
|
|
debug=1) set -x ;;
|
2020-05-26 19:14:20 +05:30
|
|
|
ro | rw) rorw="-o $line" ;;
|
2020-06-28 09:28:57 +05:30
|
|
|
--*) init_args="${cmdline#*-- }"; break ;;
|
2020-05-27 14:05:05 +05:30
|
|
|
*=*) command export "$line" ;;
|
|
|
|
*) command export "${line}=1" ;;
|
2020-06-28 09:28:57 +05:30
|
|
|
esac 2> /dev/null || :; done
|
2020-01-30 20:10:13 +05:30
|
|
|
}
|
2020-01-05 23:31:39 +05:30
|
|
|
|
2020-04-12 01:01:02 +05:30
|
|
|
setup_devmgr()
|
|
|
|
{
|
2020-05-19 09:59:40 +05:30
|
|
|
[ "$break" = devmgr ] && { print "break before setup_devmgr()"; sh; }
|
2020-04-12 01:01:02 +05:30
|
|
|
|
2020-02-26 01:13:25 +05:30
|
|
|
case "$devmgr" in
|
|
|
|
udev)
|
2020-05-19 09:59:40 +05:30
|
|
|
udevd -N never & devmgr_pid="$!"
|
2020-02-26 01:13:25 +05:30
|
|
|
udevadm trigger -c add -t subsystems
|
|
|
|
udevadm trigger -c add -t devices
|
|
|
|
udevadm settle
|
2020-04-12 01:01:02 +05:30
|
|
|
;;
|
2020-02-26 01:13:25 +05:30
|
|
|
mdev)
|
2020-05-07 17:36:34 +05:30
|
|
|
mdev -s
|
2020-05-19 09:59:40 +05:30
|
|
|
mdev -df & devmgr_pid="$!"
|
2020-05-07 17:36:34 +05:30
|
|
|
|
2020-05-26 19:14:20 +05:30
|
|
|
find /sys/devices -name uevent |
|
|
|
|
|
|
|
|
while read -r uevent; do
|
|
|
|
printf add > "$uevent"
|
|
|
|
done
|
2020-04-12 01:01:02 +05:30
|
|
|
;;
|
2020-02-26 01:13:25 +05:30
|
|
|
mdevd)
|
2020-05-19 09:59:40 +05:30
|
|
|
mdevd & devmgr_pid="$!"
|
2020-02-26 01:13:25 +05:30
|
|
|
mdevd-coldplug
|
2020-04-12 01:01:02 +05:30
|
|
|
;;
|
2020-05-19 09:59:40 +05:30
|
|
|
esac 2> /dev/null
|
2020-01-30 20:10:13 +05:30
|
|
|
}
|
2020-01-05 23:31:39 +05:30
|
|
|
|
2020-04-12 01:01:02 +05:30
|
|
|
mount_root()
|
|
|
|
{
|
2020-05-19 09:59:40 +05:30
|
|
|
[ "$break" = root ] && { print "break before mount_root()"; sh; }
|
2020-04-12 01:01:02 +05:30
|
|
|
|
2020-06-02 16:56:42 +05:30
|
|
|
resolve_device "$root"
|
2020-04-12 01:01:02 +05:30
|
|
|
|
2020-04-15 22:20:45 +05:30
|
|
|
set -- \
|
|
|
|
"${rorw:--o ro}${root_opts:+,$root_opts}" \
|
2020-05-26 19:14:20 +05:30
|
|
|
"${root_type:+-t $root_type}" "$device" "/mnt/root"
|
2020-04-15 22:20:45 +05:30
|
|
|
|
2020-07-03 21:19:09 +05:30
|
|
|
# word splitting is safe by design
|
|
|
|
# shellcheck disable=2068
|
2020-04-12 01:01:02 +05:30
|
|
|
mount $@ || panic "failed to mount root"
|
2020-01-30 20:10:13 +05:30
|
|
|
}
|
2020-01-05 23:31:39 +05:30
|
|
|
|
2020-05-26 19:14:20 +05:30
|
|
|
boot_system()
|
2020-04-12 01:01:02 +05:30
|
|
|
{
|
2020-05-26 19:14:20 +05:30
|
|
|
[ "$break" = boot ] && { print "break before boot_system()"; sh; }
|
2020-04-12 01:01:02 +05:30
|
|
|
|
2020-05-19 09:59:40 +05:30
|
|
|
kill "$devmgr_pid"
|
2020-02-09 23:15:10 +05:30
|
|
|
|
2020-04-15 22:20:45 +05:30
|
|
|
# temporary workaround until util-linux release a new version
|
2020-04-12 01:01:02 +05:30
|
|
|
# see https://github.com/karelzak/util-linux/issues/997
|
2020-05-19 09:59:40 +05:30
|
|
|
for dir in run dev sys proc; do
|
2020-05-07 17:36:34 +05:30
|
|
|
mount -o move "$dir" "/mnt/root/${dir}" ||
|
|
|
|
mount --move "$dir" "/mnt/root/${dir}"
|
2020-06-02 16:56:42 +05:30
|
|
|
done 2> /dev/null
|
2020-01-05 23:31:39 +05:30
|
|
|
|
2020-06-02 16:56:42 +05:30
|
|
|
set -- "/mnt/root" "${init:-/sbin/init}" "$init_args"
|
2020-01-30 20:10:13 +05:30
|
|
|
|
2020-06-28 09:28:57 +05:30
|
|
|
# POSIX exec has no -c flag to execute command with empty environment
|
2020-06-02 16:56:42 +05:30
|
|
|
# use 'env -i' to prevent leaking exported variables
|
2020-07-03 21:19:09 +05:30
|
|
|
# word splitting is safe by design
|
|
|
|
# shellcheck disable=2068
|
2020-06-02 16:56:42 +05:30
|
|
|
exec env -i \
|
|
|
|
TERM=linux \
|
|
|
|
PATH=/bin:/sbin:/usr/bin:/usr/sbin \
|
|
|
|
switch_root $@ || panic "failed to boot system"
|
2020-04-12 01:01:02 +05:30
|
|
|
}
|
2020-01-30 20:58:03 +05:30
|
|
|
|
2020-04-12 01:01:02 +05:30
|
|
|
# int main()
|
|
|
|
{
|
2020-06-28 09:28:57 +05:30
|
|
|
# enable exit on error and disable globbing
|
|
|
|
# trap EXIT signal
|
|
|
|
set -ef; trap panic EXIT
|
|
|
|
|
2020-04-12 01:01:02 +05:30
|
|
|
prepare_environment
|
2020-06-28 09:28:57 +05:30
|
|
|
run_hook init.early
|
2020-04-12 01:01:02 +05:30
|
|
|
parse_cmdline
|
|
|
|
setup_devmgr
|
2020-02-26 01:13:25 +05:30
|
|
|
|
2020-06-28 09:28:57 +05:30
|
|
|
run_hook init
|
2020-02-26 01:13:25 +05:30
|
|
|
|
2020-04-12 01:01:02 +05:30
|
|
|
mount_root
|
|
|
|
boot_system
|
|
|
|
}
|