-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathinitramfs-init
More file actions
117 lines (96 loc) · 3.98 KB
/
Copy pathinitramfs-init
File metadata and controls
117 lines (96 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/busybox sh
# This is the init script built into the PrawnOS initramfs
# This file is part of PrawnOS (https://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/>.
#add this to start shell at desired point
rescue_shell() {
[ "{$1}" == "debug" ] && echo "Debug flag detected, entering debug shell" > /dev/tty1
echo "Something went wrong. Dropping to a shell." > /dev/tty1
exec setsid /bin/sh -c 'exec /bin/sh </dev/tty1 >/dev/tty1 2>&1'
}
#used to parse the kernel cmdline
cmdline() {
local value
value=" $(cat /proc/cmdline) "
value="${value##* ${1}=}"
value="${value%% *}"
[ "${value}" != "" ] && echo "${value}"
}
#used to get the uuid of the root partiton since findfs isn't in debian busybox-static
rootpartuuid() {
local value
value=$1
value="${value%/*}"
value="${value#*=}"
[ "${value}" != "" ] && echo "${value}"
}
# 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
# mount the bare necesities
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)
BLKID=$(/sbin/blkid | grep $ROOT_PARTUUID )
#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
ROOT_DEV="${BLKID%1:*}"
# happens when kernel takes its time looking for devices
if [ -z ${ROOT_DEV} ]
then
sleep 1
# get the root device, so we can find the boot partiton
UNPARSED=$(cmdline root)
ROOT_PARTUUID=$(rootpartuuid $UNPARSED)
BLKID=$(/sbin/blkid | grep $ROOT_PARTUUID )
#If its an mmcblk device, the kernel partiton will p1. If it is a usb device, the partiton will just be 1
ROOT_DEV="${BLKID%1:*}"
# if device is still not seen, bail out
[ -z ${ROOT_DEV} ] && rescue_shell
fi
# 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
if [ -n "$(blkid ${ROOT_DEV}2 | grep crypto_LUKS)" ]
then
#decrypt and mount the root filesystem, disable kernel log messages to avoid clashing with the prompt
dmesg -n 2
echo "Opening encrypted root partition, this will take 30s..."
cryptsetup --tries 5 luksOpen ${ROOT_DEV}2 luksroot || rescue_shell
mount /dev/mapper/luksroot /newroot
else
# mount the unencrypted root filesystem
[ -d "/newroot" ] || mkdir -p /newroot
mount ${ROOT_DEV}2 /newroot
fi
dmesg -n 1
echo -e '\033[?17;0;0c' > /dev/tty1 # magically make cursor disappear
dd status=none if=/newroot/PrawnOS-$(cat /sys/class/graphics/fb0/virtual_size | tr ',' 'x') of=/dev/fb0 # write splash to framebuffer
umount /sys
umount /proc
#switch to the new rootfs
exec switch_root /newroot /sbin/init