-
Notifications
You must be signed in to change notification settings - Fork 14
/
frzr-initramfs
executable file
·134 lines (110 loc) · 3.86 KB
/
frzr-initramfs
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#! /bin/bash
if [ $EUID -ne 0 ]; then
echo "$(basename $0) must be run as root"
exit 1
fi
# Check if script is being ran frmo the install media
if [ -d /tmp/frzr_root ]; then
if [ -d "${SUBVOL}" ]; then
cd ${SUBVOL}
# Mount necessary file systems
mount -t proc /proc proc/
mount -t sysfs /sys sys/
mount --rbind /dev dev/
# Set R/W permissions
btrfs property set -fts ${SUBVOL} ro false
chroot ${SUBVOL} /bin/bash <<EOF
# Commands to be ran in chroot
### Create distro mkinitcpio preset
echo '
ALL_config="/etc/mkinitcpio.conf"
ALL_kver="/boot/vmlinuz-linux"
ALL_microcode=(/boot/*-ucode.img)
PRESETS="default"
default_image="/boot/initramfs-linux.img"
' > /etc/mkinitcpio.d/\${NAME%%-*}.preset
### Rebuild Initramfs with custom preset
mkinitcpio -p \${NAME%%-*}
EOF
# Set back to R/O permissions
btrfs property set -fts ${SUBVOL} ro true
# unmount filesystems
umount -l ${SUBVOL}/proc
umount -l ${SUBVOL}/sys
mount --make-rslave ${SUBVOL}/dev
umount -l ${SUBVOL}/dev
else
echo "No deployment directory found"
exit 1
fi
else
echo "We don't appear to be running from an arch install media"
fi
# Build initramfs from within a deployed system
if [ -d /frzr_root ]; then
if [ -n "$SUBVOL" ]; then
### Grabbing name of the new deployed system
ID=$(grep '^ID=' "$SUBVOL/etc/os-release" | awk -F= '{ print $2 }' | sed 's/"//g')
VERSIONID=$(grep '^VERSION_ID=' "$SUBVOL/etc/os-release" | awk -F= '{ print $2 }' | sed 's/"//g')
BUILDID=$(grep '^BUILD_ID=' "$SUBVOL/etc/os-release" | awk -F= '{ print $2 }' | sed 's/"//g')
else
### Grabbing name of the currently deployed system
ID=$(grep '^ID=' /etc/os-release | awk -F= '{ print $2 }' | sed 's/"//g')
VERSIONID=$(grep '^VERSION_ID=' /etc/os-release | awk -F= '{ print $2 }' | sed 's/"//g')
BUILDID=$(grep '^BUILD_ID=' /etc/os-release | awk -F= '{ print $2 }' | sed 's/"//g')
fi
BUILD="$ID"-"$VERSIONID"_"$BUILDID"
DEPLOYMENT_PATH="/frzr_root/deployments/$BUILD"
# Get locked state
RELOCK=0
LOCK_STATE=$(btrfs property get -fts "$DEPLOYMENT_PATH")
if [[ $LOCK_STATE == *"ro=true"* ]]; then
btrfs property set -fts ${DEPLOYMENT_PATH} ro false
RELOCK=1
else
echo "Filesystem appears to be unlocked"
fi
echo "Generating configuration for $BUILD"
### Rebuild Initramfs with custom preset
if [ -n "$SUBVOL" ]; then
cd ${SUBVOL}
# Mount necessary file systems
mount -t proc /proc proc/
mount -t sysfs /sys sys/
mount --rbind /dev dev/
# We have to chroot for new images or else the kernel version can cause initramfs building to fail
chroot ${SUBVOL} /bin/bash <<EOF
# Commands to be ran in chroot
### Create distro mkinitcpio preset
echo '
ALL_config="/etc/mkinitcpio.conf"
ALL_kver="/boot/vmlinuz-linux"
ALL_microcode=(/boot/*-ucode.img)
PRESETS="default"
default_image="/boot/initramfs-linux.img"
' > /etc/mkinitcpio.d/\${NAME%%-*}.preset
### Rebuild Initramfs with custom preset
mkinitcpio -p \${NAME%%-*}
EOF
umount -l ${SUBVOL}/proc
umount -l ${SUBVOL}/sys
mount --make-rslave ${SUBVOL}/dev
umount -l ${SUBVOL}/dev
else
echo '
ALL_config="/etc/mkinitcpio.conf"
ALL_kver="/boot/'$BUILD'/vmlinuz-linux"
ALL_microcode=(/boot/'$BUILD'/*-ucode.img)
PRESETS="default"
default_image="/boot/'$BUILD'/initramfs-linux.img"
' > /etc/mkinitcpio.d/$ID.preset
# If we are not doing a deployment then this will be used for local installs to rebuild initramfs
mkinitcpio -p $ID
fi
if [[ $RELOCK == 1 ]]; then
btrfs property set -fts ${DEPLOYMENT_PATH} ro true
else
# Move rebuilt images to the unlocked location if system was unlocked prior
cp /boot/$BUILD/* /boot
fi
fi