forked from abadraja/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauto_install.sh
executable file
·285 lines (237 loc) · 7.3 KB
/
auto_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
#!/bin/bash
help() {
echo "Usage: auto_install.sh [-d|--disk <disk>] [-p|--partition-table <mbr/gpt>] [-l|--use-lvm] [-h|--hostname <hostname>] [-z|--use-zen] [--dotfiles <?url>] [-u|--username <username>] [-t|--timezone <timezone>]"
echo "Disk: The disk to install Arch Linux on. WILL BE WIPED COMPLETELY."
echo "Partition table: The partition table format to use. If unsure, choose gpt."
echo "LVM: Use LVM with the new installation."
echo "Hostname: The hostname for the new pc."
echo "Zen: Use the Zen kernel in the new pc."
echo "Dotfiles: The Dotfiles repo. Must have the same install.py format as https://github.com/eyedevelop/dotfiles"
echo "Username: The username for the new system."
exit 0
}
round() {
python -c "import math;import sys;sys.stdout.write(str(math.ceil($1)))"
}
calculate_swap() {
memsize=$(free -h | grep Mem | awk '{print $2}' | sed 's/[A-Za-z]//g')
memsize=$(round "$memsize")
memsize=$((memsize*1024+512))
echo -n "$memsize"
}
partition_mbr_nlvm() {
parted --script "$DISK" mklabel msdos
parted --script "$DISK" mkpart primary 1MiB 500MiB
parted --script "$DISK" set 1 boot on
memsize=$(calculate_swap)
parted --script "$DISK" mkpart primary 500MiB "${memsize}MiB"
parted --script "$DISK" mkpart primary "${memsize}MiB" 100%
}
partition_mbr_lvm() {
parted --script "$DISK" mklabel msdos
parted --script "$DISK" mkpart primary 1MiB 500MiB
parted --script "$DISK" set 1 boot on
parted --script "$DISK" mkpart primary 500MiB 100%
(pvcreate "${DISK}p2" || pvcreate "${DISK}2") || (echo "Cannot partition disk." && exit 1)
vgcreate archvg "${DISK}p2" || vgcreate archvg "${DISK}2"
memsize=$(calculate_swap)
memsize=$((memsize-512))
lvcreate -L "${memsize}M" -n swap archvg
lvcreate -l 100%FREE -n root archvg
}
format_lvm() {
mkfs.ext4 "${DISK}p1" || mkfs.ext4 "${DISK}1"
mkswap /dev/mapper/archvg-swap
mkfs.ext4 /dev/mapper/archvg-root
}
format_nlvm() {
mkfs.ext4 "${DISK}p1" || mkfs.ext4 "${DISK}1"
mkswap "${DISK}p2" || mkswap "${DISK}2"
mkfs.ext4 "${DISK}p3" || mkfs.ext4 "${DISK}3"
}
partition_gpt_nlvm() {
parted --script "$DISK" mklabel gpt
parted --script "$DISK" mkpart primary 1MiB 500MiB
parted --script "$DISK" set 1 esp on
memsize=$(calculate_swap)
parted --script "$DISK" mkpart primary 500MiB "${memsize}MiB"
parted --script "$DISK" mkpart primary "${memsize}MiB" 100%
}
partition_gpt_lvm() {
parted --script "$DISK" mklabel gpt
parted --script "$DISK" mkpart primary 1MiB 500MiB
parted --script "$DISK" set 1 esp on
parted --script "$DISK" mkpart primary 500MiB 100%
(pvcreate "${DISK}p2" || pvcreate "${DISK}2") || (echo "Cannot partition disk." && exit 1)
vgcreate archvg "${DISK}p2" || vgcreate archvg "${DISK}2"
memsize=$(calculate_swap)
memsize=$((memsize-512))
lvcreate -L "${memsize}M" -n swap archvg
lvcreate -l 100%FREE -n root archvg
}
mount_lvm() {
mount /dev/mapper/archvg-root /mnt
mkdir -p /mnt/boot
mount "${DISK}p1" /mnt/boot || mount "${DISK}1" /mnt/boot
swapon /dev/mapper/archvg-swap
}
mount_nlvm() {
mount "${DISK}p3" /mnt || mount "${DISK}3" /mnt
mkdir -p /mnt/boot
mount "${DISK}p1" /mnt/boot || mount "${DISK}1" /mnt/boot
swapon "${DISK}p2" || swapon "${DISK}2"
}
install_main() {
if [[ "$ZEN" -eq 1 ]]; then
KERNEL="linux-zen linux-zen-headers dkms"
else
KERNEL="linux linux-headers"
fi
bash -c "pacstrap /mnt base base-devel networkmanager $KERNEL"
genfstab -U /mnt > /mnt/etc/fstab
if [[ ! -f "/usr/share/zoneinfo/$TIMEZONE" ]]; then
TIMEZONE="Europe/Amsterdam"
fi
arch-chroot /mnt ln -sf "/usr/share/zoneinfo/$TIMEZONE" /etc/localtime
arch-chroot /mnt hwclock --systohc
arch-chroot /mnt sed -i 's/#en_US.UTF-8/en_US.UTF-8/g' /etc/locale.gen
arch-chroot /mnt sed -i 's/#en_GB.UTF-8/en_GB.UTF-8/g' /etc/locale.gen
arch-chroot /mnt locale-gen && echo -e "LANG=en_GB.UTF-8\nLANGUAGE=en_US.UTF-8" > /etc/locale.conf
arch-chroot /mnt echo "$HOSTNAME" > /etc/hostname
arch-chroot /mnt cat << EOF > /etc/hosts
127.0.0.1 localhost localhost.localdomain
::1 localhost localhost.localdomain
127.0.1.1 $HOSTNAME $HOSTNAME.localdomain
EOF
if [[ "$LVM" -eq 1 ]]; then
arch-chroot /mnt sed -i 's/block filesystems/block lvm2 filesystems/g' /etc/mkinitcpio.conf
arch-chroot /mnt mkinitcpio -p "$(ls /etc/mkinitcpio.d/ | sed 's/.preset//g')"
fi
arch-chroot /mnt sed -i 's/^# %wheel ALL=(ALL) ALL/%wheel ALL=(ALL) ALL/g' /etc/sudoers
arch-chroot /mnt useradd -m -G wheel "$USERNAME"
arch-chroot /mnt passwd "$USERNAME"
}
bootloader_mbr() {
arch-chroot /mnt pacman -S --confirm grub
arch-chroot /mnt grub-install "$DISK"
arch-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg
}
bootloader_gpt() {
arch-chroot /mnt pacman -S --confirm refind-efi
arch-chroot /mnt refind-install
}
# Switch cli options
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-d|--disk)
DISK="$2"
shift
shift
;;
-p|--partition-table)
FORMAT="$2"
shift
shift
;;
-l|--use-lvm)
LVM=1
shift
;;
-h|--hostname)
HOSTNAME="$2"
shift
shift
;;
-z|--use-zen)
ZEN=1
shift
;;
--dotfiles)
if [[ -z "$2" ]]; then
DOTFILES=1
DOTFILES_URL="https://github.com/eyedevelop/dotfiles"
else
DOTFILES=1
DOTFILES_URL="$2"
shift
fi
shift
;;
-u|--username)
USERNAME="$2"
shift
shift
;;
-t|--timezone)
TIMEZONE="$2"
shift
shift
;;
--help)
help
;;
*)
echo "Unknown command line argument."
help
;;
esac
done
# Parse commandline arguments.
if [[ -z "$DISK" || -z "$FORMAT" || -z "$HOSTNAME" || -z "$USERNAME" ]]; then
echo "The following arguments are required:"
echo "Disk, Partition table, Hostname, Username."
exit 1
fi
if [[ "$FORMAT" != "gpt" && "$FORMAT" != "mbr" ]]; then
echo "Partition table must either be 'gpt' or 'mbr'."
exit 1
fi
if [[ $(ls "$DISK" >/dev/null 2>&1) ]]; then
echo "The chosen disk does not exist."
exit 1
fi
# Ask for user consent.
echo "WARNING: ALL DATA ON DISK $DISK WILL BE DELETED."
echo -n 'DO YOU WISH TO CONTINUE (y|N): '
read -r consent
if [[ "$consent" != "y" ]]; then
echo "User did not give consent."
exit 1
fi
# Check internet
if [[ ! $(ping -c 5 1.1.1.1) && ! $(ping -c 5 8.8.8.8) ]]; then
echo "Please connect to the internet before you run this script."
exit 1
fi
# Format the disk
if [[ "$FORMAT" == "gpt" ]]; then
if [[ "$LVM" -eq 1 ]]; then
partition_gpt_lvm
format_lvm
mount_lvm
else
partition_gpt_nlvm
format_nlvm
mount_nlvm
fi
else
if [[ "$LVM" -eq 1 ]]; then
partition_mbr_lvm
format_lvm
mount_lvm
else
partition_mbr_nlvm
format_nlvm
mount_nlvm
fi
fi
# Run the install.
install_main
# Install the bootloader.
if [[ "$FORMAT" == "gpt" ]]; then
bootloader_gpt
else
bootloader_mbr
fi
echo -e '\n\nDone!!'