tar-xz-loading-initramfs/initramfs/init

181 lines
4.3 KiB
Plaintext
Executable File

#!/busybox sh
NORMAL="\\033[0m" # white on black, or whatever your terminal is
FAIL="\\033[1;31m" # red foreground
OK="\\033[1;32m" # green foreground
INFO="\\033[1;34m" # blue foreground
alias tar='/busybox tar -C /roottmpfs -xpf -'
alias bzcat='/busybox bzcat'
alias xzcat='/busybox xzcat'
alias mount='/busybox mount'
alias umount='/busybox umount'
emergency() {
echo ""
echo -e "${FAIL}It failed! Panic shell!${NORMAL}"
echo ""
/busybox --install -s
exec /bin/sh
}
info() {
echo -en "-${INFO}|${NORMAL}-" ${@} "... "
}
isok() {
if [[ $1 -eq 0 ]]; then
echo -e "${INFO}[ ${OK}OK${INFO} ]${NORMAL}"
else
echo -e "${INFO}[ ${FAIL}FAIL${INFO} ]${NORMAL}"
emergency
fi
}
info "disabling console blanking, hopefully"
echo -e '\033[9;0]\033[14;0]' > /dev/console
info "waiting for usb devices to settle"
echo ""
for i in 10 9 8 7 6 5 4 3 2 1 0; do
echo -en ${i}'\r'
/busybox sleep 1
done
echo ""
isok 0
info "mounting devtmpfs"
/busybox mount -n -t devtmpfs devtmpfs ./dev
isok $?
info "mounting proc"
/busybox mount -n -t proc proc ./proc
isok $?
info "getting root tmpfs size"
for var in $(cat /proc/cmdline); do
if [[ ${var/=*} = rootfs_size ]]; then
size=${var/*=}
fi
done
if [[ -z ${size} ]]; then
info "going with default: 4G"
size="4G"
fi
isok 0
info "trying to mount root tmpfs"
/busybox mount -n -o size=${size} -t tmpfs roottmpfs /roottmpfs
isok $?
info "checking if the user wants to use a different init"
for var in $(cat /proc/cmdline); do
if [[ ${var/=*} = init ]]; then
init="${var/*=}"
info "user wants ${init}"
fi
done
if [[ -z "${init}" ]]; then
info "no; default: /sbin/init"
init="/sbin/init"
isok $?
fi
info "checking for network configuration on kernel commandline"
echo ""
for var in $(cat /proc/cmdline); do
case "${var/=*}" in
# Split the "value" part of this parameter into parts, with "," as
# separators and then use them for constructing iproute2 or brctl
# commandline.
# Don't do any input validation; escape to the shell if things break.
# Do it in a subshell to prevent IFS change leak.
ipcmd)
(
IFS=","
set -- ${var/*=}
info "ip ${@}"
/busybox ip ${@}
isok $?
)
;;
brctl)
(
IFS=","
set -- ${var/*=}
info "brctl ${@}"
/busybox brctl ${@}
isok $?
)
;;
esac
done
# syntax examples:
# rootfs_part=uuid|f822d881-d88a-4200-a9cf-6986c5a3e69a/some_directory/file|xzcat|tar
# -> findfs -> mount -> xzcat | tar
# rootfs_part=url|http://10.3.13.37/url.tar.bz2|bzcat|tar
# -> wget -O - | bzcat | tar
# You might find bugs & dragons here; patches accepted.
info "looking for root filesystem tarballs"
echo ""
for var in $(cat /proc/cmdline); do
if [[ "${var/=*}" = rootfs_part ]]; then
part="${var/*=}"
info "found ${part}"
# uuid or url
url_type="${part/|*}"
# url + transformations
url="${part#${url_type}|}"
# transformations
transformations="${url#*|}"
# url
url="${url/|*}"
(
if [[ ${url_type} = "uuid" ]]; then
uuid="${url%%/*}"
filepath="${url#*/}"
info "mounting $(findfs UUID="${uuid}") on /mnt"
mount -n -o ro "$(findfs UUID="${uuid}")" /mnt
isok $?
info "unpacking /mnt/${filepath}"
echo ""
/pv "/mnt/${filepath}" | eval ${transformations}
isok $?
info "unmounting"
umount -n /mnt
isok $?
else
info "wgetting ${url} and transforming it on the fly"
/busybox wget -O - "${url}" | eval ${transformations}
isok $?
fi
)
fi
done
info "unmounting /dev"
/busybox umount -n /dev
isok $?
info "unmounting /proc"
/busybox umount -n /proc
isok $?
info "checking if /roottmpfs/${init} exists"
[[ -e /roottmpfs/${init} ]]
isok $?
info "trying to switch root"
exec /busybox switch_root /roottmpfs /sbin/init
echo ""
echo -e "${FAIL}we should not reach this place! here be dragons!${NORMAL}"
echo ""
emergency