forked from calliecameron/mint-encrypted-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recover-bootloader
executable file
·159 lines (121 loc) · 4.34 KB
/
recover-bootloader
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
#!/bin/bash
function enter-to-continue() {
echo &&
read -r -p "Press ENTER to continue..." &&
echo &&
echo
}
function yn-y() {
# Y is the default
local REPLY
read -p "${1} [Y/n] " -r
if [[ $REPLY =~ ^[Nn]$ ]]; then
return 1
else
return 0
fi
}
function yn-n() {
# N is the default
local REPLY
read -p "${1} [y/N] " -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
return 0
else
return 1
fi
}
function read-existing-path() {
read -r -p "${1}: " name || fail
if [ -z "${name}" ] || [ ! -e "${name}" ]; then
echo "That file doesn't exist!" 1>&2
exit 1
else
echo "${name}"
fi
}
function fail() {
echo -e "\e[31mSomething went wrong!\e[0m"
exit 1
}
cat <<EOF
This script will reinstall the bootloader from the Linux Mint 18, 18.1
or 18.2 installer live USB, if for some reason you can no longer boot
your installed system (make sure you use the Live USB that matches the
version of the installed system).
EOF
if ! yn-n "Continue?"; then
exit 0
fi
# Not completely foolproof, but should do the job...
if ! lsb_release -a 2>/dev/null | grep 'sarah\|serena\|sonya' &>/dev/null || ! type ubiquity &>/dev/null; then
cat <<EOF
You are not running on the Linux Mint 18, 18.1 or 18.2 installer live
USB. Cannot go any further.
EOF
exit 1
fi
if [ -e '/sys/firmware/efi' ]; then
FIRMWARE='uefi'
else
FIRMWARE='bios'
fi
cat <<EOF
Your firmware is '${FIRMWARE}'. If that doesn't sound right (i.e. you
wanted to boot in UEFI mode but booted in BIOS mode instead), type 'n'
at the prompt and reboot in the correct mode.
EOF
if ! yn-y "Is the firmware mode correct?"; then
exit 0
fi
echo &&
CRYPTPART="$(read-existing-path "Enter the name of the physical partition where your encrypted container is located; typically something like /dev/sda1, /dev/sda2, /dev/nvme0n1p2, etc.")" &&
echo &&
sudo cryptsetup open "${CRYPTPART}" "$(basename "${CRYPTPART}")_crypt" &&
echo &&
ROOTDEV="$(read-existing-path "Enter the path of the device where your root partition is located; using the example names from the installation script, this would be /dev/mapper/mint-root, but of course yours might be different.")" &&
echo &&
# shellcheck disable=SC2015
BOOTLOADER="$(read-existing-path "Enter the device on which the bootloader is installed; typically something like /dev/sda or /dev/sdb for hard drives, or /dev/nvme0n1 or /dev/nvme0n2 for NVME SSDs")" || fail
if [ "${FIRMWARE}" = 'uefi' ]; then
read -r -p "Enter the number of the UEFI boot partition, e.g. if the partition is /dev/sda1 on a hard drive, enter 1, or /dev/nvme0n1p2 on an NVME SSD, enter 2: " UEFINUMBER || fail
if [ -z "${UEFINUMBER}" ]; then
echo 'Invalid partition number'
fail
fi
fi
sudo mount "${ROOTDEV}" /mnt &&
sudo mount --bind /dev /mnt/dev &&
sudo mount --bind /dev/pts /mnt/dev/pts &&
sudo mount --bind /sys /mnt/sys &&
sudo mount --bind /proc /mnt/proc &&
# shellcheck disable=SC2015
sudo mount --bind /run /mnt/run || fail
if [ "${FIRMWARE}" = 'uefi' ]; then
sudo mount "${BOOTLOADER}${UEFINUMBER}" /mnt/boot/efi || fail
fi
sudo chroot /mnt locale-gen --purge --no-archive &&
# shellcheck disable=SC2015
sudo chroot /mnt update-initramfs -u || fail
if [ "${FIRMWARE}" = 'bios' ]; then
sudo chroot /mnt update-grub &&
sudo chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg &&
# shellcheck disable=SC2015
sudo chroot /mnt grub-install "${BOOTLOADER}" || fail
elif [ "${FIRMWARE}" = 'uefi' ]; then
sudo chroot /mnt update-grub &&
sudo mkdir -p /mnt/boot/efi/EFI/mint &&
sudo chroot /mnt grub-mkconfig -o /boot/efi/EFI/mint/grub.cfg &&
echo "configfile \${cmdpath}/grub.cfg" | sudo tee /mnt/tmp/grub.cfg &>/dev/null &&
sudo chroot /mnt grub-mkstandalone -d /usr/lib/grub/x86_64-efi/ -O x86_64-efi --compress="xz" --modules="part_gpt part_msdos crypto cryptodisk luks disk diskfilter lvm" --fonts="unicode" -o "/boot/efi/EFI/mint/grubx64.efi" "boot/grub/grub.cfg=/tmp/grub.cfg" -v &&
# shellcheck disable=SC2015
sudo chroot /mnt efibootmgr -c -d "${BOOTLOADER}" -p "${UEFINUMBER}" -L "Mint" -l "\EFI\mint\grubx64.efi" || fail
else
# Should never get here
fail
fi
if [ "${FIRMWARE}" = 'uefi' ]; then
sudo umount /mnt/boot/efi || fail
fi
sudo umount /mnt/proc /mnt/dev/pts /mnt/dev /mnt/sys /mnt/run /mnt &&
echo 'Finished'