-
Notifications
You must be signed in to change notification settings - Fork 0
/
alis-recovery.sh
executable file
·223 lines (194 loc) · 6.49 KB
/
alis-recovery.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
#!/usr/bin/env bash
set -eu
# Arch Linux Install Script Recovery (alis-recovery) start a recovery for an
# failed installation or broken system.
# Copyright (C) 2022 picodotdev
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
# This script is hosted at https://github.com/picodotdev/alis. For new features,
# improvements and bugs fill an issue in GitHub or make a pull request.
# Pull Request are welcome!
#
# If you test it in real hardware please send me an email to pico.dev@gmail.com with
# the machine description and tell me if somethig goes wrong or all works fine.
#
# Please, don't ask for support for this script in Arch Linux forums, first read
# the Arch Linux wiki [1], the Installation Guide [2] and the General
# Recomendations [3], later compare the commands with those of this script.
#
# [1] https://wiki.archlinux.org
# [2] https://wiki.archlinux.org/index.php/Installation_guide
# [3] https://wiki.archlinux.org/index.php/General_recommendations
# Starts the reccovery of a Arch Linux system installed with alis.
#
# Usage:
# # loadkeys es
# # curl -sL https://raw.githubusercontent.com/picodotdev/alis/master/download.sh | bash
# # vim alis-recovery.conf
# # ./alis-recovery.sh
function init_config() {
local COMMONS_FILE="alis-commons.sh"
source "$COMMONS_FILE"
source "$COMMONS_CONF_FILE"
source "$RECOVERY_CONF_FILE"
}
function sanitize_variables() {
DEVICE=$(sanitize_variable "$DEVICE")
FILE_SYSTEM_TYPE=$(sanitize_variable "$FILE_SYSTEM_TYPE")
PARTITION_MODE=$(sanitize_variable "$PARTITION_MODE")
SWAP_SIZE=$(sanitize_variable "$SWAP_SIZE")
PARTITION_CUSTOMMANUAL_BOOT=$(sanitize_variable "$PARTITION_CUSTOMMANUAL_BOOT")
PARTITION_CUSTOMMANUAL_ROOT=$(sanitize_variable "$PARTITION_CUSTOMMANUAL_ROOT")
for I in "${BTRFS_SUBVOLUMES_MOUNTPOINTS[@]}"; do
IFS=',' SUBVOLUME=($I)
if [ ${SUBVOLUME[0]} == "root" ]; then
BTRFS_SUBVOLUME_ROOT=("${SUBVOLUME[@]}")
elif [ ${SUBVOLUME[0]} == "swap" ]; then
BTRFS_SUBVOLUME_SWAP=("${SUBVOLUME[@]}")
fi
done
}
function check_variables() {
check_variables_value "KEYS" "$KEYS"
check_variables_boolean "LOG" "$LOG"
check_variables_value "DEVICE" "$DEVICE"
check_variables_boolean "DEVICE_TRIM" "$DEVICE_TRIM"
check_variables_boolean "LVM" "$LVM"
check_variables_equals "LUKS_PASSWORD" "LUKS_PASSWORD_RETYPE" "$LUKS_PASSWORD" "$LUKS_PASSWORD_RETYPE"
check_variables_list "FILE_SYSTEM_TYPE" "$FILE_SYSTEM_TYPE" "ext4 btrfs xfs f2fs reiserfs" "true" "true"
check_variables_size "BTRFS_SUBVOLUME_ROOT" ${#BTRFS_SUBVOLUME_ROOT[@]} 3
check_variables_list "BTRFS_SUBVOLUME_ROOT" "${BTRFS_SUBVOLUME_ROOT[2]}" "/" "true" "true"
if [ -n "$SWAP_SIZE" ]; then
check_variables_size "BTRFS_SUBVOLUME_SWAP" ${#BTRFS_SUBVOLUME_SWAP[@]} 3
fi
for I in "${BTRFS_SUBVOLUMES_MOUNTPOINTS[@]}"; do
IFS=',' SUBVOLUME=($I)
check_variables_size "SUBVOLUME" ${#SUBVOLUME[@]} 3
done
check_variables_list "PARTITION_MODE" "$PARTITION_MODE" "auto custom manual" "true" "true"
check_variables_boolean "CHROOT" "$CHROOT"
}
function warning() {
echo -e "${BLUE}Welcome to Arch Linux Install Script Recovery${NC}"
echo ""
read -p "Do you want to continue? [y/N] " yn
case $yn in
[Yy]* )
;;
[Nn]* )
exit
;;
* )
exit
;;
esac
}
function init() {
print_step "init()"
init_log "$LOG" "$RECOVERY_LOG_FILE"
loadkeys "$KEYS"
}
function facts() {
print_step "facts()"
facts_commons
if [ -n "$(echo "$DEVICE" | grep "^/dev/[a-z]d[a-z]")" ]; then
DEVICE_SATA="true"
elif [ -n "$(echo "$DEVICE" | grep "^/dev/nvme")" ]; then
DEVICE_NVME="true"
elif [ -n "$(echo "$DEVICE" | grep "^/dev/mmc")" ]; then
DEVICE_MMC="true"
fi
}
function prepare() {
print_step "prepare()"
prepare_partition
configure_network
ask_passwords
}
function prepare_partition() {
if [ -d /mnt/boot ]; then
umount /mnt/boot
umount /mnt
fi
if [ -e "/dev/mapper/$LVM_VOLUME_GROUP-$LVM_VOLUME_LOGICAL" ]; then
umount "/dev/mapper/$LVM_VOLUME_GROUP-$LVM_VOLUME_LOGICAL"
fi
if [ -e "/dev/mapper/$LUKS_DEVICE_NAME" ]; then
cryptsetup close $LUKS_DEVICE_NAME
fi
partprobe $DEVICE
}
function ask_passwords() {
if [ "$LUKS_PASSWORD" == "ask" ]; then
PASSWORD_TYPED="false"
while [ "$PASSWORD_TYPED" != "true" ]; do
read -sp 'Type LUKS password: ' LUKS_PASSWORD
echo ""
read -sp 'Retype LUKS password: ' LUKS_PASSWORD_RETYPE
echo ""
if [ "$LUKS_PASSWORD" == "$LUKS_PASSWORD_RETYPE" ]; then
PASSWORD_TYPED="true"
else
echo "LUKS password don't match. Please, type again."
fi
done
fi
}
function partition() {
print_step "partition()"
# setup
partition_setup
# luks and lvm
if [ -n "$LUKS_PASSWORD" ]; then
echo -n "$LUKS_PASSWORD" | cryptsetup --key-file=- open $PARTITION_ROOT $LUKS_DEVICE_NAME
sleep 5
fi
if [ -n "$LUKS_PASSWORD" ]; then
DEVICE_ROOT="/dev/mapper/$LUKS_DEVICE_NAME"
fi
if [ "$LVM" == "true" ]; then
DEVICE_ROOT="/dev/mapper/$LVM_VOLUME_GROUP-$LVM_VOLUME_LOGICAL"
fi
# options
partition_options
# mount
partition_mount
}
function recovery() {
arch-chroot /mnt
}
function end() {
echo ""
if [ "$CHROOT" == "false" ]; then
echo -e "${GREEN}Recovery started.${NC}"
else
echo -e "${GREEN}Recovery ended.${NC}"
fi
echo "You must do an explicit reboot after finalize recovery (exit if in arch-chroot, ./alis-reboot.sh)."
echo ""
}
function main() {
init_config
sanitize_variables
check_variables
warning
init
facts
prepare
partition
if [ "$CHROOT" == "true" ]; then
recovery
fi
end
}
main