This repository has been archived by the owner on Oct 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheclosion-gen-conf.sh
executable file
·279 lines (244 loc) · 7.13 KB
/
eclosion-gen-conf.sh
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env sh
set -o errexit -o nounset
ROOT_CONF=/etc/eclosion/eclosion.conf
TMP_CONF=/tmp/eclosion.conf
# user variables
OLD_ZPOOL=
OLD_CUSTOM_CMDLINE=
OLD_CUSTOM_ECLOSION_ARGS=
die() { echo "$1"; exit 1; }
add_conf() { echo "$1" >> $TMP_CONF; }
display_disk() {
lsblk -po NAME,MODEL,SIZE
}
search_uuid() {
BY_UUID=$(find -L /dev/disk/by-uuid -samefile "$1")
[ -z "$BY_UUID" ] && {
echo "No UUID found for $1"
return 1
}
[ -n "$BY_UUID" ] && return 0
}
search_disk() {
while :; do
echo
echo "Which disk is your $1 ? (Full path, e.g: /dev/sda1)"
printf "> "; read -r
[ -b "$REPLY" ] && {
search_uuid "$REPLY"
res=$?
[ $res -eq 0 ] && break
}
done
}
rem_old_entry() {
[ -f "$2" ] || {
echo "File $2 no found"
return
}
old="$(grep "$1" "$2")"
[ -z "$old" ] && return
echo "clean $2 with $1"
sed -i "/$1 / d" "$2" 1>/dev/null # Add a space to remove multiple occurence of boot
echo $?
}
need_root() {
[ "$(id -u)" -ne 0 ] && die "I need root privilege to $1"
return 0
}
write_fstab() {
_DEV=$1
_PATH=$2
_FS=$3
_ARGS=$4
_DUMP=0
_FSCK=2
need_root "change the fstab"
echo "$_DEV $_PATH $_FS $_ARGS $_DUMP $_FSCK" >> /etc/fstab
}
write_cryptboot() {
_NAME=cryptboot
_PASS=none
_OPTS=luks
need_root "change the crypttab"
echo "$_NAME $CRYPTBOOT $_PASS $_OPTS" >> /etc/crypttab
}
detect_new_efi_part() {
display_disk
search_disk ESP
EFI=UUID=${BY_UUID##*/}
echo "$EFI"
rem_old_entry "\/boot\/efi" /etc/fstab
write_fstab "$EFI" /boot/efi vfat "noauto,rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro"
}
detect_new_cryptboot_part() {
display_disk
search_disk CRYPTBOOT
CRYPTBOOT=UUID=${BY_UUID##*/}
echo "$CRYPTBOOT"
rem_old_entry cryptboot /etc/crypttab
write_cryptboot
rem_old_entry "\/boot" /etc/fstab
write_fstab /dev/mapper/cryptboot /boot ext4 "noauto,rw,relatime,nojournal_checksum,barrier,user_xattr"
}
search_root_fs() {
echo "Exec zfs list..."
zfs list -H -o name -d 2 | head -n 7
echo "No old zpool found, which dataset is your ROOT fs? (e.g: poolname/ROOT/gentoo)"
printf "> "; read -r
OLD_ZPOOL=$REPLY
}
get_user_vars() {
OLD_ZPOOL=$(grep ^ZPOOL $ROOT_CONF) || echo "No old zpool found"
OLD_CUSTOM_CMDLINE="$(grep ^CUSTOM_CMDLINE $ROOT_CONF)" || echo "No old cmdline found"
OLD_CUSTOM_ECLOSION_ARGS="$(grep ^CUSTOM_ECLOSION_ARGS $ROOT_CONF)" ||
echo "No old eclosin args found"
}
search_zpool() {
[ -f $ROOT_CONF ] || search_root_fs
printf "check pool... %s" "$OLD_ZPOOL"
if zfs get exec "${OLD_ZPOOL#*=}" >/dev/null ; then
echo " ...[Ok]"
else
echo "Fail to found ${OLD_ZPOOL#*=}"
search_root_fs
fi
}
search_bydisk() {
printf "Check EFI %s" "$EFI_PARTITION"
if find -L /dev/disk -samefile "$(findfs "$EFI_PARTITION")" >/dev/null ; then
echo " ...[Ok]"
else
EFI_PARTITION=
fi
}
control_values() {
[ -n "$OLD_ZPOOL" ] && search_zpool
[ -n "$EFI_PARTITION" ] && search_bydisk
}
detect_init() {
if [ -f /lib/systemd/systemd ] ; then
INIT=/lib/systemd/systemd
elif [ -f /usr/lib/systemd/systemd ] ; then
INIT=/usr/lib/systemd/systemd
elif [ -f /sbin/init ] ; then
INIT=/sbin/init
fi
[ -z $INIT ] && die "Init is no found, please, post an issue on https://github.com/szorfein/eclosion/issues"
echo "init found: $INIT"
}
# use eselect kernel list to detect your kernel version
detect_kernel() {
KERNEL_LIST=$(eselect kernel list | grep "\*" | awk '{print $2}' | sed s/linux-//)
[ -z "$KERNEL_LIST" ] && die "eselect kernel list is void..."
[ ! -d /usr/src/linux-"$KERNEL_LIST" ] && die "path /usr/src/linux-$KERNEL_LIST no found"
echo "kernel found: $KERNEL_LIST"
}
detect_efi_partition() {
[ -f /etc/fstab ] || detect_new_efi_part
if FSTAB=$(grep -i efi /etc/fstab | awk '{print $1}') ; then
echo "Found your EFI partition $FSTAB in the fstab"
EFI_PARTITION="$FSTAB"
return 0
elif LSBLK=$(lsblk -f | grep vfat | awk '{print $3}') ; then
# lsblk work only if there are only one vfat device plugged, maybe disable this part...
echo "Found your EFI partition $FSTAB with lsblk"
EFI_PARTITION="UUID=$LSBLK"
return 0
else
detect_new_efi_part
EFI_PARTITION=$(grep -i efi /etc/fstab | awk '{print $1}')
fi
[ -z "$EFI_PARTITION" ] && die "Can't detect your EFI partition"
}
# From the crypttab, TODO check if you use a cryptboot first !
detect_cryptboot_partition() {
[ -f /etc/crypttab ] || detect_new_cryptboot_part
if CRYPTTAB=$(grep cryptboot /etc/crypttab | awk '{print $2}') ; then
echo "Found cryptboot $CRYPTTAB in the crypttab"
CRYPTBOOT=$CRYPTTAB
return 0
else
detect_new_cryptboot_part
CRYPTBOOT=$(grep cryptboot /etc/crypttab | awk '{print $2}')
fi
[ -z "$CRYPTBOOT" ] && die "cryptboot partition is no found"
}
detect_all_values() {
detect_init
detect_kernel
detect_efi_partition # from the fstab
detect_cryptboot_partition # from the crypttab
}
default_values() {
ZPOOL="" CUSTOM_ECLOSION_ARGS=\"\" CUSTOM_CMDLINE=\"\"
[ -n "$OLD_ZPOOL" ] && ZPOOL=${OLD_ZPOOL#*=}
[ -n "$OLD_CUSTOM_ECLOSION_ARGS" ] && CUSTOM_ECLOSION_ARGS="${OLD_CUSTOM_ECLOSION_ARGS#*=}"
[ -n "$OLD_CUSTOM_CMDLINE" ] && CUSTOM_CMDLINE="${OLD_CUSTOM_CMDLINE#*=}"
return 0
}
write_user_vars() {
add_conf "### Users variables #############"
add_conf "# Pool name"
add_conf ZPOOL="$ZPOOL"
add_conf "# Args to pass to eclosion, do not set the kernel"
add_conf CUSTOM_ECLOSION_ARGS="$CUSTOM_ECLOSION_ARGS"
add_conf "# Custom kernel arguments"
add_conf CUSTOM_CMDLINE="$CUSTOM_CMDLINE"
add_conf ""
}
write_others_vars() {
add_conf "### Automatically detected, don't edit them #############"
add_conf "# Post an issue if something is incorrect at: https://github.com/szorfein/eclosion/issues"
add_conf "# init detected"
add_conf INIT="$INIT"
add_conf "# last kernel found with eselect kernel list"
add_conf KERNEL="$KERNEL_LIST"
add_conf "# Your EFI partition here by LABEL, UUID, PARTUUID, etc..."
add_conf EFI_PARTITION="$EFI_PARTITION"
add_conf "# Your cryptboot partition here"
add_conf CRYPTBOOT="$CRYPTBOOT"
add_conf "# CMDLINE"
add_conf CMDLINE=\""init=\${INIT} root=ZFS=\${ZPOOL} \${CUSTOM_CMDLINE}\""
add_conf "# Eclosion args"
add_conf ECLOSION_ARGS=\""--kernel \${KERNEL} \${CUSTOM_ECLOSION_ARGS}\""
}
show_diff() {
[ -f $ROOT_CONF ] || return 0
if diff $ROOT_CONF $TMP_CONF >/dev/null ; then
echo "No change, bye"
exit 0
else
echo "Differences between config files..."
# hack to display diff without produce error
for i in $(diff $ROOT_CONF $TMP_CONF); do
echo "$i"
done
printf "Apply change? " ; read -r
if echo "$REPLY" | grep -qP "^y|^Y" ; then
echo "Copying the new file..."
return 0
else
echo "Ok, i (lol) do nothing..."
exit 0
fi
fi
}
copy_config_file() {
need_root "copy the config $ROOT_CONF"
cp $TMP_CONF $ROOT_CONF
chmod 644 $ROOT_CONF
}
main() {
# Erase previous content
:>$TMP_CONF
[ -f $ROOT_CONF ] && get_user_vars
detect_all_values
control_values
default_values
write_user_vars
write_others_vars
show_diff
copy_config_file
}
main $\@