SPS · log · files · refs · tags · releases · people · languages · GitHub · file
lib/mkiso/initrd-init
3725 bytes at 2909919 · GitHub
#!/bin/busybox sh
# Initramfs PID 1. Find the ISO, mount squashfs+overlay, switch_root.
#
# Do not call `busybox --install -s` here. That writes absolute applet
# links, and those panic once the host paths are gone. mkiso already
# planted relative links next to /bin/busybox.
#
# Invoke applets as `/bin/busybox NAME` so a missing symlink cannot
# make PID 1 exit 127.
PATH=/bin:/sbin:/usr/bin:/usr/sbin
export PATH
bb=/bin/busybox
[ -x "$bb" ] || bb=busybox
mkdir -p /proc /sys /dev /tmp /mnt/iso /mnt/ro /mnt/rw /mnt/new
"$bb" mount -t proc proc /proc
"$bb" mount -t sysfs sysfs /sys
"$bb" mount -t devtmpfs devtmpfs /dev 2>/dev/null ||
"$bb" mount -t tmpfs tmpfs /dev
mkdir -p /dev/pts
"$bb" mount -t devpts devpts /dev/pts 2>/dev/null || :
say()
{
printf '%s\n' "$*" >/dev/console 2>/dev/null || printf '%s\n' "$*"
}
rescue()
{
printf '%s\n' "$*"
printf '%s\n' "mkiso: dropping to a shell. PID 1 stays this shell."
# Do not exec a missing binary; keep this process alive.
while :
do
"$bb" setsid "$bb" sh -c 'exec '"$bb"' sh <>/dev/console >&0 2>&1' ||
"$bb" sh
"$bb" sleep 1
done
}
for mod in loop squashfs overlay isofs vfat nls_cp437 nls_iso8859-1 \
usb-storage xhci-pci ehci-pci uhci-hcd sr_mod sd_mod ahci nvme \
virtio_blk virtio_pci virtio_scsi virtio_net virtio_console \
sdhci sdhci-pci mmc_block \
drm drm_kms_helper drm_shmem_helper drm_ttm_helper ttm virtio_dma_buf \
virtio_gpu bochs qxl vmwgfx
do
"$bb" modprobe "$mod" 2>/dev/null || :
done
# simpledrm on a firmware framebuffer next to virtio_gpu is a black KWin
# with an instant cursor. Only load it when no real KMS node appeared.
n=0
while [ "$n" -lt 5 ]
do
if [ -e /dev/dri/card0 ] || [ -e /dev/dri/card1 ]; then
break
fi
n=$((n + 1))
"$bb" sleep 1
done
if [ ! -e /dev/dri/card0 ] && [ ! -e /dev/dri/card1 ]; then
"$bb" modprobe simpledrm 2>/dev/null || :
fi
found=
i=0
while [ "$i" -lt 20 ]
do
i=$((i + 1))
for dev in /dev/sr0 /dev/sr1 /dev/vda /dev/vda1 /dev/vdb /dev/vdb1 \
/dev/nvme0n1 /dev/nvme0n1p1 /dev/nvme1n1p1 \
/dev/sda /dev/sda1 /dev/sdb /dev/sdb1 /dev/sdc /dev/sdc1 \
/dev/xvda /dev/xvda1 /dev/sr2 /dev/cdrom
do
[ -b "$dev" ] || continue
if "$bb" mount -t iso9660 -o ro "$dev" /mnt/iso 2>/dev/null ||
"$bb" mount -o ro "$dev" /mnt/iso 2>/dev/null; then
if [ -f /mnt/iso/live/filesystem.squashfs ]; then
found=$dev
break
fi
"$bb" umount /mnt/iso 2>/dev/null || :
fi
done
[ -n "$found" ] && break
"$bb" sleep 1
done
[ -n "$found" ] || rescue "mkiso: cannot find the SPS Linux live filesystem on a CD/USB."
say "SPS: found live image on $found"
say "SPS: mounting live filesystem..."
"$bb" mount -t squashfs -o ro /mnt/iso/live/filesystem.squashfs /mnt/ro ||
rescue "mkiso: squashfs mount failed"
"$bb" mount -t tmpfs tmpfs /mnt/rw
mkdir -p /mnt/rw/upper /mnt/rw/work
"$bb" mount -t overlay overlay \
-o lowerdir=/mnt/ro,upperdir=/mnt/rw/upper,workdir=/mnt/rw/work \
/mnt/new ||
rescue "mkiso: overlay mount failed"
mkdir -p /mnt/new/mnt/iso /mnt/new/proc /mnt/new/sys /mnt/new/dev \
/mnt/new/run /mnt/new/tmp
"$bb" mount --move /mnt/iso /mnt/new/mnt/iso 2>/dev/null || :
"$bb" mount --move /proc /mnt/new/proc 2>/dev/null || :
"$bb" mount --move /sys /mnt/new/sys 2>/dev/null || :
"$bb" mount --move /dev /mnt/new/dev 2>/dev/null || :
if [ ! -e /mnt/new/sbin/init ] && [ ! -L /mnt/new/sbin/init ]; then
rescue "mkiso: /sbin/init missing on the live filesystem"
fi
say "SPS: starting live system..."
# busybox switch_root replaces PID 1. If the new init cannot exec, we
# would panic; the live /sbin/init is an ELF (busybox or systemd), not
# a script that execs a console.
exec "$bb" switch_root /mnt/new /sbin/init
rescue "mkiso: switch_root failed"