2019-09-18 03:50:27 +02:00
|
|
|
#!/bin/busybox sh
|
|
|
|
|
2019-10-03 07:40:14 +02:00
|
|
|
# This is the init script built into the PrawnOS initramfs
|
|
|
|
|
|
|
|
# This file is part of PrawnOS (http://www.prawnos.com)
|
|
|
|
# Copyright (c) 2018 Hal Emmerich <hal@halemmerich.com>
|
|
|
|
|
|
|
|
# PrawnOS is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 2
|
|
|
|
# as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
# PrawnOS is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with PrawnOS. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2019-09-21 06:50:08 +02:00
|
|
|
echo In PrawnOS Init
|
|
|
|
|
2019-09-30 03:16:30 +02:00
|
|
|
#add this to start shell at desired point
|
|
|
|
rescue_shell() {
|
2019-10-15 16:22:01 +02:00
|
|
|
[ "{$1}" != "debug" ] && echo "Something went wrong. Dropping to a shell." > /dev/tty1
|
|
|
|
[ "{$1}" == "debug" ] && echo "Debug flag detected, entering debug shell" > /dev/tty1
|
2019-10-13 00:39:17 +02:00
|
|
|
echo "Something went wrong. Dropping to a shell." > /dev/tty1
|
2019-09-30 03:16:30 +02:00
|
|
|
exec setsid /bin/sh -c 'exec /bin/sh </dev/tty1 >/dev/tty1 2>&1'
|
|
|
|
}
|
|
|
|
|
2019-10-03 07:40:14 +02:00
|
|
|
#used to parse the kernel cmdline
|
2019-09-18 03:50:27 +02:00
|
|
|
cmdline() {
|
|
|
|
local value
|
|
|
|
value=" $(cat /proc/cmdline) "
|
|
|
|
value="${value##* ${1}=}"
|
|
|
|
value="${value%% *}"
|
|
|
|
[ "${value}" != "" ] && echo "${value}"
|
|
|
|
}
|
|
|
|
|
2019-10-03 07:40:14 +02:00
|
|
|
#used to get the uuid of the root partiton since findfs isn't in debian busybox-static
|
2019-09-18 03:50:27 +02:00
|
|
|
rootpartuuid() {
|
|
|
|
local value
|
|
|
|
value=$1
|
|
|
|
value="${value%/*}"
|
|
|
|
value="${value#*=}"
|
|
|
|
[ "${value}" != "" ] && echo "${value}"
|
|
|
|
}
|
|
|
|
|
2020-04-30 02:03:46 +02:00
|
|
|
# a clever portable shell script to detect occurances of a substring in a string
|
|
|
|
# occur <string> <substring> (optional count)
|
|
|
|
# if optional count is not provided:
|
|
|
|
# returns 1 if substring is not in string
|
|
|
|
# returns 0 otherwise
|
|
|
|
# if optional count is provided:
|
|
|
|
# returns 1 if substring occurs in string < optional count
|
|
|
|
# returns 0 otherwise
|
|
|
|
occur() while case "$1" in (*"$2"*) set -- \
|
|
|
|
"${1#*"$2"}" "$2" "${3:-0}" "$((${4:-0}+1))";;
|
|
|
|
(*) return "$((${4:-0}<${3:-1}))";;esac
|
|
|
|
do : "${_occur:+$((_occur=$4))}";done
|
|
|
|
|
2019-10-13 00:39:17 +02:00
|
|
|
# mount the bare necesities
|
2019-09-18 03:50:27 +02:00
|
|
|
mount -n -t proc proc /proc
|
|
|
|
mount -n -t sysfs sysfs /sys
|
|
|
|
mount -n -t devtmpfs devtmpfs /dev
|
|
|
|
|
|
|
|
# get the root device, so we can find the boot partiton
|
|
|
|
UNPARSED=$(cmdline root)
|
|
|
|
ROOT_PARTUUID=$(rootpartuuid $UNPARSED)
|
2019-10-03 07:40:14 +02:00
|
|
|
echo ${ROOT_PARTUUID} > /dev/tty1
|
2019-09-18 03:50:27 +02:00
|
|
|
BLKID=$(/bin/blkid | grep $ROOT_PARTUUID )
|
2019-10-03 07:40:14 +02:00
|
|
|
echo ${BLKID} > /dev/tty1
|
|
|
|
#If its an mmcblk device, the kernel partiton will p1. If it is a usb device, the partiton will just be 1
|
|
|
|
#Just want everything before the 1
|
2019-09-18 03:50:27 +02:00
|
|
|
ROOT_DEV="${BLKID%1:*}"
|
|
|
|
|
2019-10-03 07:40:14 +02:00
|
|
|
echo ${ROOT_DEV} > /dev/tty1
|
2019-09-18 03:50:27 +02:00
|
|
|
|
2019-10-15 16:22:01 +02:00
|
|
|
# label any partition on the system with RESCUESHELL to enter the initramfs rescue shell before mount and root_switch.
|
|
|
|
# you can do this with "cgpt add -i 1 -l RESCUESHELL /dev/sda" for example to label the first partiton of a usb drive.
|
|
|
|
if [ -n "$(blkid | grep RESCUESHELL)" ]
|
|
|
|
then
|
|
|
|
rescue_shell debug
|
|
|
|
fi
|
|
|
|
|
2019-10-13 00:39:17 +02:00
|
|
|
if [ -n "$(blkid ${ROOT_DEV}2 | grep crypto_LUKS)" ]
|
2019-09-30 03:16:30 +02:00
|
|
|
then
|
2020-01-19 20:01:43 +01:00
|
|
|
#decrypt and mount the root filesystem, disable kernel log messages to avoid clashing with the prompt
|
2020-03-03 03:16:22 +01:00
|
|
|
dmesg -n 2
|
2019-10-03 07:40:14 +02:00
|
|
|
echo "Opening encrypted root partition, this will take 30s..."
|
2019-10-13 00:39:17 +02:00
|
|
|
cryptsetup --tries 5 luksOpen ${ROOT_DEV}2 luksroot || rescue_shell
|
2020-02-23 07:14:35 +01:00
|
|
|
dmesg -n 7
|
2019-09-30 03:16:30 +02:00
|
|
|
mount /dev/mapper/luksroot /newroot
|
|
|
|
else
|
|
|
|
# mount the unencrypted root filesystem
|
2019-10-03 07:40:14 +02:00
|
|
|
[ -d "/newroot" ] || mkdir -p /newroot
|
2019-10-13 00:39:17 +02:00
|
|
|
mount ${ROOT_DEV}2 /newroot
|
2019-09-30 03:16:30 +02:00
|
|
|
fi
|
2019-09-18 03:50:27 +02:00
|
|
|
|
|
|
|
umount /sys
|
|
|
|
umount /proc
|
2019-09-21 06:50:08 +02:00
|
|
|
|
2019-10-15 16:22:01 +02:00
|
|
|
#switch to the new rootfs
|
2020-04-30 02:03:46 +02:00
|
|
|
exec switch_root /newroot /sbin/init
|