This commit is contained in:
illiliti 2020-06-02 14:26:42 +03:00
parent 2d26ea0377
commit 1287f2996b
3 changed files with 31 additions and 40 deletions

View File

@ -19,8 +19,6 @@ create_symlink()
{ {
[ -b "/dev/${dev_name=${DEVPATH##*/}}" ] || exit 1 [ -b "/dev/${dev_name=${DEVPATH##*/}}" ] || exit 1
exec > /dev/null 2>&1
# prevent race condition # prevent race condition
while ! blkid "/dev/${dev_name}"; do sleep 1; done while ! blkid "/dev/${dev_name}"; do sleep 1; done
@ -34,4 +32,4 @@ create_symlink()
mkdir -p /dev/mapper mkdir -p /dev/mapper
ln -sf "../${dev_name}" "/dev/mapper/${dm_name:?}" ln -sf "../${dev_name}" "/dev/mapper/${dm_name:?}"
} }
} } > /dev/null 2>&1

30
init
View File

@ -19,7 +19,7 @@ panic()
"\033[1;31m!!\033[m" >&2; sh "\033[1;31m!!\033[m" >&2; sh
} }
findfs() resolve_device()
{ {
count=0; device= count=0; device=
@ -31,7 +31,7 @@ findfs()
esac esac
# prevent race condition # prevent race condition
while [ ! -e "$device" ]; do sleep 1 while [ ! -b "$device" ]; do sleep 1
[ "$(( count += 1 ))" != 30 ] || { [ "$(( count += 1 ))" != 30 ] || {
panic "failed to lookup partition" panic "failed to lookup partition"
break break
@ -52,10 +52,7 @@ prepare_environment()
mount -t tmpfs -o nosuid,nodev,mode=0755 run /run mount -t tmpfs -o nosuid,nodev,mode=0755 run /run
mount -t devtmpfs -o nosuid,noexec,mode=0755 dev /dev mount -t devtmpfs -o nosuid,noexec,mode=0755 dev /dev
mkdir -p \ mkdir -p /run/cryptsetup /run/lock /run/lvm
/run/cryptsetup \
/run/lock \
/run/lvm
ln -s /proc/self/fd /dev/fd ln -s /proc/self/fd /dev/fd
ln -s fd/0 /dev/stdin ln -s fd/0 /dev/stdin
@ -76,7 +73,7 @@ parse_cmdline()
rootfstype=*) root_type="${line##*=}" ;; rootfstype=*) root_type="${line##*=}" ;;
rootflags=*) root_opts="${line##*=}" ;; rootflags=*) root_opts="${line##*=}" ;;
ro | rw) rorw="-o $line" ;; ro | rw) rorw="-o $line" ;;
--) init_args="${cmdline##*--}"; break ;; --*) init_args="${cmdline##*--}"; break ;;
*=*) command export "$line" ;; *=*) command export "$line" ;;
*) command export "${line}=1" ;; *) command export "${line}=1" ;;
esac 2> /dev/null || continue; done esac 2> /dev/null || continue; done
@ -97,8 +94,6 @@ setup_devmgr()
mdev -s mdev -s
mdev -df & devmgr_pid="$!" mdev -df & devmgr_pid="$!"
[ "$monolith" = 1 ] && return 0
find /sys/devices -name uevent | find /sys/devices -name uevent |
while read -r uevent; do while read -r uevent; do
@ -125,11 +120,11 @@ unlock_luks()
key=*) luks_key="-d ${opt##*=}" ;; key=*) luks_key="-d ${opt##*=}" ;;
esac; done esac; done
findfs "$luks_root" resolve_device "$luks_root"
set -- \ set -- \
"$luks_key" "$luks_header" "$luks_discard" \ "$luks_key" "$luks_header" "$luks_discard" \
"$device" "${luks_name:-luks-${device##*/}}" "$device" "${luks_name:-crypt-${device##*/}}"
cryptsetup open $@ || panic "failed to unlock LUKS" cryptsetup open $@ || panic "failed to unlock LUKS"
} }
@ -165,7 +160,7 @@ mount_root()
{ {
[ "$break" = root ] && { print "break before mount_root()"; sh; } [ "$break" = root ] && { print "break before mount_root()"; sh; }
findfs "$root" resolve_device "$root"
set -- \ set -- \
"${rorw:--o ro}${root_opts:+,$root_opts}" \ "${rorw:--o ro}${root_opts:+,$root_opts}" \
@ -185,12 +180,15 @@ boot_system()
for dir in run dev sys proc; do for dir in run dev sys proc; do
mount -o move "$dir" "/mnt/root/${dir}" || mount -o move "$dir" "/mnt/root/${dir}" ||
mount --move "$dir" "/mnt/root/${dir}" mount --move "$dir" "/mnt/root/${dir}"
done done 2> /dev/null
set -- \ set -- "/mnt/root" "${init:-/sbin/init}" "$init_args"
"/mnt/root" "${init:-/sbin/init}" "$init_args"
exec switch_root $@ || panic "failed to boot system" # use 'env -i' to prevent leaking exported variables
exec env -i \
TERM=linux \
PATH=/bin:/sbin:/usr/bin:/usr/sbin \
switch_root $@ || panic "failed to boot system"
} }
# int main() # int main()

View File

@ -109,7 +109,8 @@ trap_helper()
exit "$ret" exit "$ret"
} }
populate_config() { populate_config()
{
printf "%s\n" "$@" >> "${workdir}/etc/tinyramfs/config" printf "%s\n" "$@" >> "${workdir}/etc/tinyramfs/config"
} }
@ -118,7 +119,7 @@ install_requirements()
print "installing requirements" print "installing requirements"
# install required binaries # install required binaries
for _binary in \[ sh ln kill mkdir \ for _binary in \[ sh ln kill mkdir env \
blkid sleep mount printf \ blkid sleep mount printf \
switch_root "${filesdir}/device-helper" switch_root "${filesdir}/device-helper"
do do
@ -139,7 +140,6 @@ install_requirements()
populate_config \ populate_config \
"root='$root'" \ "root='$root'" \
"devmgr='$devmgr'" \ "devmgr='$devmgr'" \
"monolith='$monolith'" \
"root_type='$root_type'" \ "root_type='$root_type'" \
"root_opts='$root_opts'" "root_opts='$root_opts'"
} }
@ -188,7 +188,9 @@ install_devmgr()
# /sys/kernel/uevent_helper or /proc/sys/kernel/hotplug # /sys/kernel/uevent_helper or /proc/sys/kernel/hotplug
;; ;;
mdev) mdev)
install_binary mdev for _binary in mdev find; do
install_binary "$_binary"
done
printf "%s\n" \ printf "%s\n" \
'SUBSYSTEM=block;.* 0:0 660 @device-helper' \ 'SUBSYSTEM=block;.* 0:0 660 @device-helper' \
@ -196,8 +198,6 @@ install_devmgr()
[ "$monolith" = 1 ] && return 0 [ "$monolith" = 1 ] && return 0
install_binary find
printf "%s\n" \ printf "%s\n" \
'$MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"' \ '$MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"' \
>> "${workdir}/etc/mdev.conf" >> "${workdir}/etc/mdev.conf"
@ -402,9 +402,7 @@ install_modules()
[ "$modules" ] && { [ "$modules" ] && {
for _module in $modules; do for _module in $modules; do
install_module "$_module" install_module "$_module"
done done; populate_config "modules='$modules'"
populate_config "modules='$modules'"
} }
install_binary modprobe install_binary modprobe
@ -422,10 +420,7 @@ install_binary()
binary=$(command -v "$1") binary=$(command -v "$1")
# check if binary exist and builtin # check if binary exist and builtin
case "$binary" in case "$binary" in */*) ;;
*/*)
: no operation
;;
"") "")
panic "$1 doesn't exist" panic "$1 doesn't exist"
;; ;;
@ -434,7 +429,10 @@ install_binary()
# shellcheck disable=2086 # shellcheck disable=2086
{ IFS=:; set -- $PATH; unset IFS; } { IFS=:; set -- $PATH; unset IFS; }
# try to discover external binary/script by checking PATH # assume that `command -v` returned builtin command.
# this behavior depends on shell implementation.
# to be independented we simply iterating over PATH
# to find external alternative ( e.g kill => /bin/kill )
for _dir; do for _dir; do
[ -x "${_dir}/${binary}" ] || ! continue [ -x "${_dir}/${binary}" ] || ! continue
@ -456,11 +454,8 @@ install_binary()
install -m755 "$binary" "${workdirbin}${binary##*/}" || panic install -m755 "$binary" "${workdirbin}${binary##*/}" || panic
strip "${workdirbin}${binary##*/}" > /dev/null 2>&1 || : strip "${workdirbin}${binary##*/}" > /dev/null 2>&1 || :
# skip static binaries/scripts # install binary dependencies if any
ldd "$binary" > /dev/null 2>&1 || return 0 ldd "$binary" 2> /dev/null |
# parse ldd output to find libraries paths
ldd "$binary" |
while read -r _library || [ "$_library" ]; do while read -r _library || [ "$_library" ]; do
@ -509,6 +504,8 @@ create_initramfs()
fi \ fi \
> "$output" 2> /dev/null || > "$output" 2> /dev/null ||
panic "failed to generate initramfs image" panic "failed to generate initramfs image"
print "done! check out $output"
) )
# int main() # int main()
@ -530,6 +527,4 @@ create_initramfs()
install_devmgr install_devmgr
install_requirements install_requirements
create_initramfs create_initramfs
print "done! check out $output"
} }