This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
forked from BlackIkeEagle/archlinux-reinstall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·305 lines (248 loc) · 8.58 KB
/
install.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env bash
set -e
echo "*** WARNING ****************************************************"
echo "* HAVE YOU PASSED IN THE PACKAGE FILES YOU WANT FOR INSTALL ? *"
echo "*** WARNING ****************************************************"
[[ ! -d /media/usb ]] && mkdir -p /media/usb
echo "AVAILABLE BLOCK DEVICES"
lsblk
echo -n "Do you want to encrypt your harddisk (yes/no): "
read encrypt
if [[ "$encrypt" != "no" ]]; then
encrypt="yes"
fi
if [[ "$encrypt" == "yes" ]]; then
echo -n "enter the usb key device name (sda1,sdb1): "
read usbkey
mount /dev/$usbkey /media/usb
fi
echo -n "enter the block device's name (sda,nvme1): "
read blockdev
echo -n "efi booting or legacy (efi|legacy): "
read boottype
echo -n "main filesystem (xfs|ext4|btrfs): "
read filesystem
echo -n "nvme disk or regular (nvme|regular): "
read nvmedisk
echo -n "check blocks (yes|no (default)): "
read checkblocks
if [[ "$blockdev" == "" ]]; then
echo "no blockdev given"
exit 1
fi
if [[ "$boottype" == "" ]]; then
echo "no boottype given"
exit 2
fi
if [[ "$filesystem" == "" ]]; then
echo "no filesystem given"
exit 4
fi
if [[ "$nvmedisk" == "" ]]; then
echo "no nvmedisk given"
exit 5
fi
if [[ "$checkblocks" == "" ]]; then
checkblocks="no"
fi
if [[ "$nvmedisk" == "nvme" ]]; then
partitionextra="p"
else
partitionextra=""
fi
# create random string to append to the keyfile and hostname
randstring="$(date +%s | sha256sum | base64 | head -c 8)"
if [[ "$boottype" == "efi" ]]; then
if [[ "$checkblocks" == "yes" ]]; then
badblocks -c 10240 -s -w -t random -v /dev/$blockdev
fi
parted --script /dev/$blockdev \
mklabel gpt \
mkpart ESP fat32 0% 200MiB \
set 1 esp on \
set 1 legacy_boot on \
mkpart primary 200MiB 400MiB \
mkpart primary 400MiB 4496MiB \
mkpart primary 4496MiB 100%
efipart=1
bootpart=2
swappart=3
rootpart=4
# EFI Partition
mkfs.fat -F32 -n EFI /dev/${blockdev}${partitionextra}${efipart}
mkfs.ext2 -L boot /dev/${blockdev}${partitionextra}${bootpart}
else
if [[ "$checkblocks" == "yes" ]]; then
badblocks -c 10240 -s -w -t random -v /dev/$blockdev
fi
parted --script /dev/$blockdev \
mklabel msdos \
mkpart primary 0% 200MiB \
set 1 boot on \
mkpart primary 200MiB 4296MiB \
mkpart primary 4296MiB 100%
bootpart=1
swappart=2
rootpart=3
mkfs.ext2 -L boot /dev/${blockdev}${partitionextra}${bootpart}
fi
if [[ "$encrypt" == "yes" ]]; then
# encryption on "ROOT"
dd bs=512 count=8 if=/dev/urandom of=/media/usb/keyfile-$randstring
lukspassword="$(date +%s | sha256sum | base64 | head -c 32)"
echo "$lukspassword" > /media/usb/luks-password-$randstring.txt
echo "$lukspassword" | cryptsetup -y luksFormat /dev/${blockdev}${partitionextra}${rootpart}
echo "$lukspassword" | cryptsetup -y luksAddKey /dev/${blockdev}${partitionextra}${rootpart} /media/usb/keyfile-$randstring
cryptsetup open /dev/${blockdev}${partitionextra}${rootpart} archlinux --key-file /media/usb/keyfile-$randstring
rootdev="/dev/mapper/archlinux"
else
rootdev="/dev/${blockdev}${partitionextra}${rootpart}"
fi
basepackagelist=("base-packages.txt")
if [[ "$filesystem" == "btrfs" ]]; then
basepackagelist+=("btrfs-packages.txt")
# "ROOT"
mkfs.btrfs -L ROOT "$rootdev"
mount "$rootdev" /mnt
mkdir -p /mnt/var
mkdir -p /mnt/var/lib
btrfs subvolume create /mnt/root
btrfs subvolume create /mnt/home
btrfs subvolume create /mnt/var/cache
btrfs subvolume create /mnt/var/lib/docker
btrfs subvolume list -p /mnt
umount /mnt
rootmountoptions="rw,noatime,nodiratime,ssd,space_cache,compress=lzo,subvol=root"
mount -o $rootmountoptions "$rootdev" /mnt
mkdir -p /mnt/home
mount -o rw,noatime,nodiratime,ssd,space_cache,compress=lzo,subvol=home "$rootdev" /mnt/home
mkdir -p /mnt/var/cache
mount -o rw,noatime,nodiratime,ssd,space_cache,compress=lzo,subvol=var/cache "$rootdev" /mnt/var/cache
mkdir -p /mnt/var/lib/docker
mount -o rw,noatime,nodiratime,ssd,space_cache,compress=lzo,subvol=var/lib/docker "$rootdev" /mnt/var/lib/docker
elif [[ "$filesystem" == "xfs" ]]; then
basepackagelist+=("xfs-packages.txt")
mkfs.xfs -L ROOT "$rootdev"
rootmountoptions="rw,noatime,attr2,inode64,noquota,discard"
mount -o $rootmountoptions "$rootdev" /mnt
elif [[ "$filesystem" == "ext4" ]]; then
basepackagelist+=("ext4-packages.txt")
mkfs.ext4 -L ROOT "$rootdev"
rootmountoptions="rw,noatime,data=ordered,discard"
mount -o $rootmountoptions "$rootdev" /mnt
else
echo "unsupported filesystem defined"
exit 1
fi
bootloaderpackage=grub
if [[ "$boottype" == "efi" ]]; then
bootloaderpackage="$bootloaderpackage efibootmgr"
mkdir -p /mnt/boot
mount /dev/${blockdev}${partitionextra}${bootpart} /mnt/boot
mkdir -p /mnt/boot/efi
mount /dev/${blockdev}${partitionextra}${efipart} /mnt/boot/efi
mkdir -p /mnt/boot/efi/EFI/archlinux
else
mkdir -p /mnt/boot
mount /dev/${blockdev}${partitionextra}${bootpart} /mnt/boot
fi
# use our mirrorlist, not the one from the iso
cp ./etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist
# install packages
if [[ ! -z $1 ]]; then
pacstrap -C ./etc/pacman.conf /mnt \
$(cat ${basepackagelist[@]}) \
$(cat "$@") \
$bootloaderpackage
else
pacstrap -C ./etc/pacman.conf /mnt \
$(cat ${basepackagelist[@]}) \
$bootloaderpackage
fi
# copy all etc extras
cp -a ./etc/ /mnt/
# generate fstab
genfstab -U /mnt >> /mnt/etc/fstab
# set timezone
ln -sf /usr/share/zoneinfo/Europe/Brussels /mnt/etc/localtime
arch-chroot /mnt hwclock --systohc
# generate locales for en_US
sed -e 's/#en_US/en_US/g' -e 's/#nl_BE/nl_BE/g' -i /mnt/etc/locale.gen
arch-chroot /mnt locale-gen
echo "LANG=en_US.UTF-8" > /mnt/etc/locale.conf
# keyboard
echo "KEYMAP=be-latin1" > /mnt/etc/vconsole.conf
# set hostname
echo "archlinux-$randstring" > /mnt/etc/hostname
echo "127.0.1.1 archlinux-$randstring" >> /mnt/etc/hosts
# make sure firewalld uses iptables
if [[ -e /mnt/etc/firewalld/firewalld.conf ]]; then
sed -e 's/^\(FirewallBackend=\).*/\1iptables/' \
-i /mnt/etc/firewalld/firewalld.conf
fi
if [[ "$encrypt" == "yes" ]]; then
# encrypted swap
mkfs.ext2 -L cryptswap /dev/${blockdev}${partitionextra}${swappart} 1M
printf "\nswap LABEL=cryptswap /dev/urandom swap,offset=2048,cipher=aes-xts-plain64,size=512" \
>> /mnt/etc/crypttab
printf "\n/dev/mapper/swap none swap defaults 0 0" \
>> /mnt/etc/fstab
else
mkswap -L swap /dev/${blockdev}${partitionextra}${swappart}
printf "\n/dev/${blockdev}${partitionextra}${swappart} none swap defaults 0 0" \
>> /mnt/etc/fstab
fi
# bootloader installation
if [[ "$boottype" == "efi" ]]; then
arch-chroot /mnt grub-install \
--target=x86_64-efi \
--boot-directory=/boot \
--efi-directory=/boot/efi \
--bootloader=archlinux \
--boot-directory=/boot/efi/EFI/BOOT \
--removable \
--recheck
mkdir -p /mnt/boot/efi/EFI/BOOT/grub
else
arch-chroot /mnt grub-install \
--target=i386-pc \
--boot-directory=/boot \
--recheck \
/dev/${blockdev}${partitionextra}
fi
# bootloader extra cmd
eval $(blkid -o export /dev/${blockdev}${partitionextra}${rootpart})
ROOTUUID=$UUID
if [[ "$encrypt" == "yes" ]]; then
grubcmd="rd.luks.name=$ROOTUUID=archlinux rd.luks.options=allow-discards"
## find usb with keyfile
usbdev=$(cat /etc/mtab| grep '/media/usb' | awk '{ print $1 }')
if [[ $? -eq 0 ]] && [[ "" != "$usbdev" ]]; then
eval $(blkid -o export "$usbdev")
grubcmd="$grubcmd rd.luks.key=$ROOTUUID=/keyfile-$randstring:UUID=$UUID"
if [[ "ext2" == $TYPE ]] || [[ "ext3" == $TYPE ]]; then
TYPE="ext4"
fi
sed -e "s/^\(MODULES=(.*\)\()\)/\1 $TYPE\2/" \
-i /mnt/etc/mkinitcpio.conf
fi
else
grubcmd="root=/dev/disk/by-uuid/$ROOTUUID"
fi
## root filesystem flags
grubcmd="$grubcmd rootflags=$rootmountoptions"
grubcmd="${grubcmd//\//\\\/}"
grubcmd="$grubcmd mem_sleep_default=deep"
## add grub GRUB_CMDLINE_LINUX
sed -e "s/^\(GRUB_CMDLINE_LINUX=\).*/\1\"$grubcmd\"/" \
-i /mnt/etc/default/grub
if [[ "$boottype" == "efi" ]]; then
arch-chroot /mnt grub-mkconfig -o /boot/efi/EFI/BOOT/grub/grub.cfg
else
arch-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg
fi
arch-chroot /mnt mkinitcpio -p linux-bede || true
# finish the installation
cp -a post-install.sh /mnt
arch-chroot /mnt /post-install.sh
rm /mnt/post-install.sh