2017-12-09 21:51:48 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
PATH=/bin
|
|
|
|
|
|
|
|
/bin/busybox --install
|
|
|
|
busybox sleep 1
|
|
|
|
busybox mount -t proc proc /proc
|
|
|
|
busybox mount -t sysfs sysfs /sys
|
|
|
|
busybox mount -t devtmpfs none /dev
|
|
|
|
|
|
|
|
root_sfs=""
|
|
|
|
root=""
|
2018-01-18 14:23:48 +01:00
|
|
|
overlay_dev=""
|
2017-12-09 21:51:48 +01:00
|
|
|
singleuser=0
|
|
|
|
|
|
|
|
for param in `cat /proc/cmdline`; do
|
|
|
|
case $param in
|
|
|
|
root=*)
|
|
|
|
root="${param##root=}"
|
|
|
|
;;
|
|
|
|
root_sfs=*)
|
|
|
|
root_sfs="${param##root_sfs=}"
|
|
|
|
;;
|
2018-01-18 14:23:48 +01:00
|
|
|
overlay_dev=*)
|
|
|
|
overlay_dev="${param##overlay_dev=}"
|
|
|
|
;;
|
2017-12-09 21:51:48 +01:00
|
|
|
single*)
|
|
|
|
singleuser=1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ -z "${root}" ]; then
|
|
|
|
echo "No root drive specified!"
|
|
|
|
exec /bin/busybox sh
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "${root_sfs}" ]; then
|
|
|
|
echo "No squashfs image specified!"
|
|
|
|
exec /bin/busybox sh
|
|
|
|
fi
|
|
|
|
|
|
|
|
while [ ! -e ${root} ]; do
|
|
|
|
echo "Waiting for root"
|
|
|
|
busybox sleep 1
|
|
|
|
done
|
|
|
|
|
2018-01-18 14:23:48 +01:00
|
|
|
# file system setup
|
|
|
|
overlay_mount() {
|
|
|
|
local lower=/newroot/${1}
|
|
|
|
local upper=/newroot/${2}
|
|
|
|
local work=/newroot/${2}_work
|
|
|
|
local target=/newroot/${3}
|
|
|
|
|
|
|
|
mount -t overlay overlay -olowerdir=${lower},upperdir=${upper},workdir=${work} ${target}
|
|
|
|
}
|
|
|
|
|
|
|
|
bind_mount() {
|
|
|
|
mount --bind /newroot/${1} /newroot/${2}
|
|
|
|
}
|
|
|
|
|
2018-02-05 11:35:21 +01:00
|
|
|
setup_vfs() {
|
|
|
|
local root=${1}
|
|
|
|
|
|
|
|
mount -t proc proc $root/proc
|
|
|
|
mount -t sysfs sysfs $root/sys
|
|
|
|
mount -t devtmpfs none $root/dev
|
|
|
|
|
|
|
|
# setup /dev
|
|
|
|
[ -e $root/dev/fd ] || ln -snf /proc/self/fd $root/dev/fd
|
|
|
|
[ -e $root/dev/stdin ] || ln -snf /proc/self/fd/0 $root/dev/stdin
|
|
|
|
[ -e $root/dev/stdout ] || ln -snf /proc/self/fd/1 $root/dev/stdout
|
|
|
|
[ -e $root/dev/stderr ] || ln -snf /proc/self/fd/2 $root/dev/stderr
|
|
|
|
[ -e $root/proc/kcore ] && ln -snf /proc/kcore $root/dev/core
|
|
|
|
|
|
|
|
for x in \
|
|
|
|
"mqueue dev/mqueue 1777 ,nodev mqueue" \
|
|
|
|
"devpts dev/pts 0755 ,gid=5,mode=0620 devpts" \
|
|
|
|
"tmpfs dev/shm 1777 ,nodev,mode=1777 shm" \
|
|
|
|
; do
|
|
|
|
set -- $x
|
|
|
|
grep -Eq "[[:space:]]+$1$" /proc/filesystems || continue
|
|
|
|
|
|
|
|
if [ ! -d "$root/$2" ]; then
|
|
|
|
mkdir -m $3 -p "$root/$2" >/dev/null 2>&1 || \
|
|
|
|
echo "Could not create $root/$2!"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -d "$root/$2" ]; then
|
|
|
|
mount -t $1 -o noexec,nosuid$4 $5 "$root/$2"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# set up additional mounts in /proc and /sys
|
|
|
|
for x in \
|
|
|
|
"sys/kernel/security securityfs nodev,noexec,nosuid" \
|
|
|
|
"sys/kernel/config configfs nodev,noexec,nosuid" \
|
|
|
|
"sys/fs/fuse/connections fusectl nodev,noexec,nosuid" \
|
|
|
|
"sys/firmware/efi/efivars efivarfs ro" \
|
|
|
|
"proc/sys/fs/binfmt_misc binfmt_misc nodev,noexec,nosuid" \
|
|
|
|
; do
|
|
|
|
set -- $x
|
|
|
|
|
|
|
|
if [ -d "$root/$1" ]; then
|
|
|
|
if grep -qs $2 "$root/proc/filesystems"; then
|
|
|
|
echo "Mounting $2 filesystem"
|
|
|
|
mount -n -t $2 -o $3 $2 "$root/$1"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
unset -v x
|
|
|
|
}
|
|
|
|
|
2017-12-09 21:51:48 +01:00
|
|
|
mount ${root} /images
|
|
|
|
|
|
|
|
if [ ! -e /images/${root_sfs} ]; then
|
|
|
|
echo "${root_sfs} not found!"
|
|
|
|
exec /bin/busybox sh
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
mount -t squashfs /images/${root_sfs} /newroot
|
2018-02-05 11:35:21 +01:00
|
|
|
setup_vfs "/newroot"
|
2017-12-09 21:51:48 +01:00
|
|
|
mount -t tmpfs none /newroot/tmp
|
|
|
|
mount -t tmpfs none /newroot/var
|
|
|
|
mount -t tmpfs none /newroot/run
|
|
|
|
|
|
|
|
chmod 755 /newroot/var
|
|
|
|
mkdir -p /newroot/run/log/journal
|
|
|
|
mkdir -p /newroot/var/log/fsck
|
|
|
|
mkdir -p /newroot/var/log/nginx
|
|
|
|
mkdir -p /newroot/var/log/mpd
|
|
|
|
mkdir -p /newroot/var/spool/
|
|
|
|
mkdir -p /newroot/var/lib/
|
|
|
|
mkdir -p /newroot/var/tmp
|
|
|
|
ln -s /run /newroot/var/run
|
|
|
|
ln -s /run/lock /newroot/var/lock
|
|
|
|
|
2018-01-18 14:23:48 +01:00
|
|
|
if [ ! -z "$overlay_dev" ]; then
|
|
|
|
while [ ! -e ${overlay_dev} ]; do
|
|
|
|
echo "Waiting for overlay device"
|
|
|
|
busybox sleep 1
|
|
|
|
done
|
|
|
|
|
|
|
|
mount ${overlay_dev} /newroot/cfg/overlay
|
|
|
|
|
|
|
|
overlay_mount "cfg/preserve/etc" "cfg/overlay/etc" "etc"
|
|
|
|
overlay_mount "cfg/preserve/var_lib" "cfg/overlay/var_lib" "var/lib"
|
2018-01-23 16:34:33 +01:00
|
|
|
overlay_mount "cfg/preserve/root" "cfg/overlay/root" "root"
|
2018-01-18 14:23:48 +01:00
|
|
|
else
|
|
|
|
bind_mount "cfg/preserve/etc" "etc"
|
|
|
|
bind_mount "cfg/preserve/var_lib" "var/lib"
|
2018-01-23 16:34:33 +01:00
|
|
|
bind_mount "cfg/preserve/root" "root"
|
2018-01-18 14:23:48 +01:00
|
|
|
fi
|
|
|
|
|
2017-12-09 21:51:48 +01:00
|
|
|
# cleanup mounts
|
|
|
|
umount -l /images
|
|
|
|
umount -l /dev
|
|
|
|
umount /sys
|
|
|
|
umount /proc
|
|
|
|
|
2018-01-18 21:22:09 +01:00
|
|
|
unset -v singleuser overlay_dev root root_sfs
|
|
|
|
|
2018-02-08 20:44:18 +01:00
|
|
|
/bin/busybox chroot /newroot /bin/openrc
|
|
|
|
|
|
|
|
exec /bin/busybox switch_root /newroot /bin/openrc-init
|