-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkinitcpio-multiflash.install
215 lines (188 loc) · 6.2 KB
/
mkinitcpio-multiflash.install
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/bash
_MKINITCPIO_CFG='/etc/mkinitcpio.conf'
_CMDLINE_CFG='/boot/cmdline.txt'
_CMD_CFG='multiflash'
_IP_CMD_CFG='ip=:::::eth0:dhcp'
_CONFIG_CFG='/boot/config.txt'
_INITRAMFS='initramfs.img'
_INITRAMFS_CFG="initramfs $_INITRAMFS followkernel"
_INITRAMFS_PATH="/boot/$_INITRAMFS"
#=====================================================================
# Checks for a hook name in a given configuration file.
# Arguments: hook name, configuration file
# Example: _has_hook my-hook /etc/mkinitcpio.conf
#=====================================================================
_has_hook() {
local hook_name=$1
local config_file=$2
grep -qe "^HOOKS=\".*$hook_name" $config_file
if (($? == 1)); then
return 1
fi
return 0
}
#=====================================================================
# Adds a hook name to a given configuration file.
# Arguments: hook name, configuration file
# Example: _add_hook my-hook /etc/mkinitcpio.conf
#=====================================================================
_add_hook() {
local hook_name=$1
local config_file=$2
sed -i "s|^\(HOOKS=\".*\)\"|\1 $hook_name\"|g" $config_file
}
#=====================================================================
# Removes a hook name from a given configuration file.
# Arguments: hook name, configuration file
# Example: _remove_hook my-hook /etc/mkinitcpio.conf
#=====================================================================
_remove_hook() {
local hook_name=$1
local config_file=$2
sed -i "s|^\(HOOKS=\".*\)\( $hook_name\)\"|\1\"|g" $config_file
}
#=====================================================================
# Checks for a configuration option in a given configuration file.
# Arguments: configuration option, configuration file
# Example: _has_cfg my-cfg=value /boot/config.txt
#=====================================================================
_has_cfg() {
local cfg="$1"
local config_file=$2
grep -qe ".*$cfg" $config_file
if (($? == 1)); then
return 1
fi
return 0
}
#=====================================================================
# Appends the ip kernel argument to a given command line file.
# Arguments: ip kernel argument, configuration file
# Example: _add_ip ip=dhcp /boot/cmdline.txt
#=====================================================================
_add_ip() {
local ip_cfg=$1
local config_file=$2
sed -i "s|$| $ip_cfg|" $config_file
}
#=====================================================================
# Removes the ip kernel argument from a given command line file.
# Arguments: ip kernel argument, configuration file
# Example: _remove_ip ip=dhcp /boot/cmdline.txt
#=====================================================================
_remove_ip() {
local ip_cfg=$1
local config_file=$2
sed -i "s| $ip_cfg||" $config_file
}
#=====================================================================
# Adds the initramfs config to a given config line file.
# Arguments: initramfs config, configuration file
# Example: _add_initramfs 'initramfs initramfs.img' config.txt
#=====================================================================
_add_initramfs() {
local initramfs_cfg="$1"
local config_file=$2
echo "$initramfs_cfg" >> $config_file
}
#=====================================================================
# Removes the initramfs config from a given config line file.
# Arguments: initramfs config, configuration file
# Example: _remove_initramfs 'initramfs initramfs.img' config.txt
#=====================================================================
_remove_initramfs() {
local initramfs_cfg="$1"
local config_file=$2
sed -i "/^$initramfs_cfg/d" $config_file
}
#=====================================================================
# Install scripts.
#=====================================================================
post_install() {
post_upgrade "${@}";
}
post_upgrade() {
# Check mkinitcpio hooks.
for hook in net multiflash; do
if ! _has_hook $hook $_MKINITCPIO_CFG; then
_add_hook $hook $_MKINITCPIO_CFG
cat <<MSG
>>> Adding hook to $_MKINITCPIO_CFG:
> HOOKS="[...] $hook"
MSG
fi
done
# Check ip kernel command line argument.
if ! _has_cfg $_IP_CMD_CFG $_CMDLINE_CFG; then
_add_ip $_IP_CMD_CFG $_CMDLINE_CFG
cat <<MSG
>>> Adding ip kernel argument to $_CMDLINE_CFG:
> [...] $_IP_CMD_CFG
MSG
fi
# Check initramfs bootloader configuration.
if ! _has_cfg "$_INITRAMFS_CFG" $_CONFIG_CFG; then
_add_initramfs "$_INITRAMFS_CFG" $_CONFIG_CFG
cat <<MSG
>>> Adding initramfs config to $_CONFIG_CFG:
> $_INITRAMFS_CFG
MSG
fi
# Prompt for additional kernel command line argument.
grep -qe ".*$_CMD_CFG" $_CMDLINE_CFG || cat <<MSG
>>> [INFO] Add kernel argument '$_CMD_CFG' to $_CMDLINE_CFG:
> [...] $_CMD_CFG=[...]
MSG
# Try to generate initramfs image.
cat <<MSG
>>> Generating initramfs at $_INITRAMFS_PATH:
> mkinitcpio -k $(ls /lib/modules | grep ARCH) -g $_INITRAMFS_PATH ...
MSG
mkinitcpio -k $(ls /lib/modules | grep ARCH) -g $_INITRAMFS_PATH
# Enable service.
systemctl enable mkinitcpio-multiflash
systemctl start mkinitcpio-multiflash
}
post_remove() {
# Try to remove initramfs image.
if [ -f $_INITRAMFS_PATH ]; then
cat <<MSG
>>> Removing initramfs $_INITRAMFS_PATH ...
MSG
rm -f $_INITRAMFS_PATH
fi
# Prompt to remove kernel command line argument.
cat <<MSG
>>> [INFO] Remove kernel argument '$_CMD_CFG' from $_CMDLINE_CFG:
> [...] $_CMD_CFG=[...]
MSG
# Remove initramfs bootloader configuration.
if _has_cfg "$_INITRAMFS_CFG" $_CONFIG_CFG; then
_remove_initramfs "$_INITRAMFS_CFG" $_CONFIG_CFG
cat <<MSG
>>> Removing initramfs config from $_CONFIG_CFG:
> $_INITRAMFS_CFG
MSG
fi
# Remove ip kernel command line argument.
if _has_cfg $_IP_CMD_CFG $_CMDLINE_CFG; then
_remove_ip $_IP_CMD_CFG $_CMDLINE_CFG
cat <<MSG
>>> Removing ip kernel argument from $_CMDLINE_CFG:
> [...] $_IP_CMD_CFG
MSG
fi
# Remove mkinitcpio hooks.
for hook in multiflash net; do
if _has_hook $hook $_MKINITCPIO_CFG; then
_remove_hook $hook $_MKINITCPIO_CFG
cat <<MSG
>>> Removing hook from $_MKINITCPIO_CFG:
> HOOKS="[...] $hook"
MSG
fi
done
# Disable service.
systemctl stop mkinitcpio-multiflash
systemctl disable mkinitcpio-multiflash
}