-
Notifications
You must be signed in to change notification settings - Fork 2
/
install
executable file
·265 lines (258 loc) · 7.76 KB
/
install
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
#!/bin/bash
red='\033[0;31m'
bold='\033[1m'
reset='\033[0m'
warn="$red$bold[!] $reset"
last_partition(){
echo $(parted $disk print | tail -n 2 | head -n 1 | awk '{print $1}')
}
enough_space(){
[ $(parted $disk unit s print free | grep "Free Space" | tail -n 1 | awk '{print $2}' | sed 's/.$//') -gt $(calculate_start) ]
}
create_partition(){
#name, fs, firstfree, size
if [ "$1" == "0" ]; then
return 1
fi
if [ "$4" == "a" ]; then
size=100%
elif [ $(($3 + $4)) -eq 0 ]; then
return 1
else
size=$(($3+$4))s
fi
parted $disk mkpart $1 $2 $3s $size
}
calculate_start(){
firstfree=$(parted $disk unit s print free | grep "Free Space" | tail -n 1 | awk '{print $1}' | sed 's/.$//')
offset=$(((1024**2) / $(sector_size)))
echo $((firstfree + (offset - (firstfree % offset))))
}
sector_size() {
echo $(lsblk -o LOG-SEC $disk | tail -n 1 | sed 's/^ *//')
}
unallocated_space() {
echo $(parted $disk print free | grep "Free Space" | tail -n 1 | awk '{print $3}')
}
verify_disk() {
disks=$(lsblk -no PATH -d)
if [ "$1" == "" ]; then
return 1
fi
for path in $disks; do
if [ "$path" == "$1" ]; then
return 0
fi
done
clear
echo -e "$bold$1$red$bold is not a valid disk.$reset"
return 1
}
verify_partition() {
partitions=$(lsblk -no PATH)
if [ "$1" == "" ]; then
return 1
fi
for path in $partitions; do
if [ "$path" == "$1" ]; then
return 0
fi
done
clear
echo -e "$bold$1$red$bold is not a valid partition.$reset"
return 1
}
used_partition() {
used=0
if [ "$1" == "0" ]; then
return 0 #Allow for skipping
fi
if [ "$1" == "$efipart" ]; then used=$((used+1)); fi
if [ "$1" == "$swappart" ]; then used=$((used+1)); fi
if [ "$1" == "$rootpart" ]; then used=$((used+1)); fi
if [ "$1" == "$homepart" ]; then used=$((used+1)); fi
if [ $used -gt 1 ]; then echo -e "${red}Partition already in use!$reset"; return 1; fi
return 0
}
partitioning() {
efipart=0;swappart=0;rootpart=0;homepart=0
if ! enough_space; then return; fi
create_partition 0 0 0 0
while [ $? -eq 1 ]; do
echo -e "You have $(unallocated_space) of free (unallocated) space in $bold$disk$reset."
echo -ne "Enter the size of the EFI (boot) partition in MiB (type 0 to skip/choose existing): "
read efisize
if [ "$efisize" == "0" ]; then
echo -e "Skipping creation of a new EFI partition"
else
if create_partition efi FAT32 $(calculate_start) $(((efisize*(1024**2))/ $(sector_size))); then efipart=$(last_partition);fi
fi
done
if ! enough_space; then return; fi
create_partition 0 0 0 0
while [ $? -eq 1 ]; do
echo -e "You have $(unallocated_space) of free (unallocated) space in $bold$disk$reset."
echo -ne "Enter the size of the SWAP partition in GiB (type 0 to skip/choose existing): "
read swapsize
if [ "$swapsize" == "0" ]; then
echo -e "Skipping creation of the SWAP partition"
else
if create_partition swap linux-swap $(calculate_start) $(((swapsize*(1024**3))/ $(sector_size))); then swappart=$(last_partition);fi
fi
done
if ! enough_space; then return; fi
create_partition 0 0 0 0
while [ $? -eq 1 ]; do
echo -e "You have $(unallocated_space) of free (unallocated) space in $bold$disk$reset."
echo -ne "Enter the size of the root partition in GiB (type a to use all remaining space | 0 to choose existing): "
read rootsize
if [ "$rootsize" != "a" ]; then rootsize=$(((rootsize*(1024**3))/ $(sector_size))); fi
if [ "$rootsize" == "0" ]; then
echo -e "Skipping creation of a new root partition"
else
if create_partition root ext4 $(calculate_start) $rootsize; then rootpart=$(last_partition);fi
fi
done
if ! enough_space; then return; fi
create_partition 0 0 0 0
while [ $? -eq 1 ]; do
echo -e "You have $(unallocated_space) of free (unallocated) space in $bold$disk$reset."
echo -ne "Enter the size of the home partition in GiB (type a to use all remaining space or 0 to skip/choose existing): "
read homesize
if [ "$homesize" == "0" ]; then
echo -e "Skipping creation of the home partition"
else
if [ "$homesize" != "a" ]; then homesize=$(((homesize*(1024**3))/ $(sector_size))); fi
if create_partition home ext4 $(calculate_start) $homesize; then homepart=$(last_partition);fi
fi
done
}
partition_selection(){
while [ "$efipart" == "0" ]; do
lsblk $disk
echo -ne "Please choose a partition number for the EFI partition: "
read efipart
if ! used_partition $efipart; then efipart=0; continue; fi
if ! verify_partition $disk$efipart; then efipart=0;fi
done
while [ "$swappart" == "0" ]; do
lsblk $disk
echo -ne "Please choose a partition number for the SWAP partition (type 0 for none): "
read swappart
if ! used_partition $swappart; then swappart=0;continue; fi
if [ "$swappart" == 0 ]; then
echo "Skipping SWAP partition"
break
fi
if ! verify_partition $disk$swappart; then swappart=0;fi
done
while [ "$rootpart" == "0" ]; do
lsblk $disk
echo -ne "Please choose a partition number for the root partition: "
read rootpart
if ! used_partition $rootpart; then rootpart=0; continue; fi
if ! verify_partition $disk$rootpart; then rootpart=0;fi
done
while [ "$homepart" == "0" ]; do
lsblk $disk
echo -ne "Please choose a partition number for the home partition (type 0 for none): "
read homepart
if ! used_partition $homepart; then homepart=0;continue; fi
if [ "$homepart" == 0 ]; then
echo "Skipping home partition"
break
fi
if ! verify_partition $disk$homepart; then homepart=0;fi
done
}
#-------------#
# MAIN SCRIPT #
#-------------#
#Case insensitive matching
shopt -s nocasematch
#Sync clock
timedatectl
#EFI check
if [ "$(cat /sys/firmware/efi/fw_platform_size)" != 64 ]; then
echo -e "$red${bold}System not mounted in EFI mode. Aborting."
exit
fi
#Disk selection
verify_disk ""
while [ $? -eq 1 ]; do
echo ""
lsblk
echo -ne "Select a ${bold}disk$reset to install on: "
read disk
verify_disk $disk
done
clear
echo -e "Selected disk $bold$disk$reset"
#Partition table
parted $disk print 2>&1 | grep -q "unrecognised disk label"
if [ $? -eq 0 ]; then
echo -ne "${warn}Disk $bold$disk$reset has no known partition table, do you want to create one? (y/n) "
read createpart
if [[ "$createpart" == "y" ]]; then
parted $disk mklabel gpt
else
echo -e "$red${bold}Can't proceed with installation without a partition table. Aborting."
exit
fi
fi
#Partitions
partitioning
#Partition selection
partition_selection
#Formatting/mounting
mkfs.fat -F 32 $disk$efipart
if [ "$swappart" != "0" ]; then
mkswap $disk$swappart
fi
mkfs.ext4 $disk$rootpart
mount $disk$rootpart /mnt
mount -m $disk$efipart /mnt/boot
if [ "$homepart" != "0" ]; then
echo -ne "Do you want to format the home partition $bold$disk$homepart$reset? (y/n) "
read formathome
if [[ "$formathome" == "y" ]]; then
mkfs.ext4 $disk$homepart
fi
mount -m $disk$homepart /mnt/home
fi
#Installation
pacman -Sy
pacstrap -K /mnt base base-devel linux linux-firmware git efibootmgr os-prober grub networkmanager neofetch man-db neovim
#fstab
genfstab -U /mnt >> /mnt/etc/fstab
#CHROOT
a="arch-chroot /mnt"
#Timezone
$a ln -sf /usr/share/zoneinfo/Europe/Madrid /etc/localtime
$a hwclock --systohc
#Localization
echo "en_US.UTF-8 UTF-8" >> /mnt/etc/locale.gen
echo "es_ES.UTF-8 UTF-8" >> /mnt/etc/locale.gen
$a locale-gen
echo "LANG=en_US.UTF-8" >> /mnt/etc/locale.conf
echo "KEYMAP=es" >> /mnt/etc/vconsole.conf
#Hostname
echo -ne "Enter your desired hostname: "
read hostname
echo "$hostname" >> /mnt/etc/hostname
#Password
echo -e "Set a root password"
$a passwd
#Bootloader
clear
echo -e "Installing GRUB"
$a grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=GRUB
$a grub-mkconfig -o /boot/grub/grub.cfg
#networkmanager
$a systemctl enable NetworkManager
cd ..
mv archscripts /mnt/root
echo "./archscripts/postinstall" >> /mnt/root/.bash_profile
echo -ne "Installation complete, press ENTER to reboot to arch"
read
shutdown now -r