|
#!/bin/sh |
|
TEMP_D="" |
|
|
|
Usage() { |
|
cat <<EOF |
|
${0##*/} out.cpio tar-or-ociref [init] |
|
EOF |
|
} |
|
|
|
winit() { |
|
cat <<"EOF" |
|
#!/bin/sh |
|
# vi: ts=4 expandtab |
|
msg() { |
|
local d="" written=false |
|
for d in /dev/ttyS0 /dev/tty1; do |
|
[ -c "$d" ] || continue |
|
echo "$@" >"$d" 2>&1 && written=true; |
|
done |
|
[ "$written" = "true" ] && return 0 |
|
echo "$@" |
|
} |
|
panic() { |
|
msg "FATAL:" "$@" |
|
} |
|
|
|
msg "==== MINI INITRAMFS INIT ====" |
|
mkdir -p /proc /dev /tmp /sys |
|
mount -t devtmpfs /dev /dev || |
|
panic "failed mount devtmpfs" |
|
mount -t proc /proc /proc || |
|
panic "failed mount proc" |
|
mount -t sysfs /sys /sys || |
|
panic "failed mount /sys" |
|
|
|
echo "6 4 1 7" >/proc/sys/kernel/printk |
|
|
|
read cmdline < /proc/cmdline || |
|
panic "failed to read cmdline" |
|
|
|
msg "KERNEL COMMAND LINE:" "$cmdline" |
|
|
|
echo o >/proc/sysrq-trigger |
|
|
|
read || panic "Read returned $?" |
|
panic "Read returned success; What to do now?" |
|
exit 1 |
|
EOF |
|
} |
|
|
|
fail() { echo "$@" 1>&2; exit 1; } |
|
cleanup() { [ "$TEMP_D" = "" ] || rm -Rf "$TEMP_D"; } |
|
stderr() { echo "$@" 1>&2; } |
|
|
|
[ "$1" = "-h" -o "$1" = "--help" ] && { Usage ; exit 1; } |
|
[ $# -gt 1 -a $# -lt 4 ] || fail "got $# expect 2 or 3" |
|
|
|
|
|
trap cleanup EXIT |
|
out=$1 |
|
input=$2 |
|
init=${3:-builtin} |
|
|
|
|
|
[ "$(id -u)" = "0" ] || fail "have to be root. or use fakeroot" |
|
|
|
TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXX") || fail "failed mktemp -d" |
|
|
|
initf="" |
|
if [ "$init" = "builtin" ]; then |
|
initf="${TEMP_D}/init" |
|
winit > "$initf" |
|
elif [ -n "$init" ]; then |
|
[ -f "$initf" ] || fail "init '$initf' not a file" |
|
initf="$init" |
|
fi |
|
|
|
case "$input" in |
|
cgr.dev/*) |
|
tar="${TEMP_D}/tmp.tar" |
|
stderr "downloading $input to $tar" |
|
crane export "$input" "$tar" || fail "crane export failed" |
|
;; |
|
*) |
|
[ -f "$input" ] || fail "$input: didn't look like a tarball" |
|
tar="$input" |
|
;; |
|
esac |
|
|
|
|
|
rootd="${TEMP_D}/rootfs" |
|
flist="${TEMP_D}/files" |
|
mkdir "$rootd" |
|
tar -C "$rootd" -xf "$tar" || fail "failed etract $tar" |
|
|
|
if [ -n "$init" ]; then |
|
cp "$initf" "$rootd/init" && |
|
chmod 755 "$rootd/init" || fail "failed to write $init" |
|
fi |
|
|
|
( cd "$rootd" && find . > "$flist" ) || fail "failed find files" |
|
|
|
( cd "$rootd" && cpio --format=newc -o ) < "$flist" | |
|
gzip --to-stdout > "$out" || |
|
fail "failed to create update cpio" |
|
|
|
stderr "wrote $out" |