-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0_live_install.sh
executable file
·280 lines (238 loc) · 7.61 KB
/
0_live_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
#!/bin/bash
# _ _ _ _
# ___ ___ ___ | |_| |_ ______ _ ___| |__ / |
# / __|/ __/ _ \| __| __|_ / _` |/ __| '_ \| |
# \__ \ (_| (_) | |_| |_ / / (_| | (__| | | | |
# |___/\___\___/ \__|\__/___\__,_|\___|_| |_|_|
#
# Zac Scott (github.com/scottzach1)
#
# 0_live_install.sh
# Configuration variables
TARGET_DISK="XXX" # /dev/nvme0n1
SWAP_SIZE="32G"
HOSTNAME="desktop"
USERNAME="zaci"
TIMEZONE="Pacific/Auckland"
LOCALE="en_NZ.UTF-8 UTF-8"
KEYMAP="us"
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Parse command line arguments
VERBOSE=0
while [[ $# -gt 0 ]]; do
case $1 in
-v|--verbose)
VERBOSE=1
shift
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
# Exit on any error
set -e
# Enable command echoing only if verbose mode is on
if [ "$VERBOSE" -eq 1 ]; then
set -x
fi
# Set up logging
exec 1> >(tee "$(basename --suffix .sh "$0")_$(date +%Y%m%d_%H%M%S).log")
exec 2>&1
# Cleanup function
cleanup() {
local exit_code=$?
set +x # Turn off command echoing for cleanup
echo "Script exited with code: $exit_code"
if [ $exit_code -ne 0 ]; then
echo "Installation failed! Check the log file for details."
# Add any necessary cleanup here, like unmounting filesystems
if mountpoint -q /mnt/boot 2>/dev/null; then
umount /mnt/boot
fi
if mountpoint -q /mnt 2>/dev/null; then
umount /mnt
fi
fi
}
# Helper function for logging
log() {
local level=$1
shift
case "$level" in
"INFO")
echo -e "${GREEN}[INFO]${NC} $*"
;;
"WARN")
echo -e "${YELLOW}[WARN]${NC} $*"
;;
"ERROR")
echo -e "${RED}[ERROR]${NC} $*"
;;
esac
}
# User validation
confirmation_prompt() {
local question="$1"
read -p "$question (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log "ERROR" "Operation cancelled by user"
exit 1
fi
}
# Sanity checks
sanity_checks() {
# Check if script is run as root
if [ "$EUID" -ne 0 ]; then
log "ERROR" "This script must be run as root"
exit 1
fi
# Check if running in UEFI mode
if [ ! -d "/sys/firmware/efi/efivars" ]; then
log "ERROR" "System not booted in UEFI mode"
exit 1
fi
# Check if target disk exists
if [ ! -b "$TARGET_DISK" ]; then
log "ERROR" "Target disk $TARGET_DISK not found"
exit 1
fi
# Check if target disk is mounted
if mount | grep -q "$TARGET_DISK"; then
log "ERROR" "Target disk $TARGET_DISK is currently mounted. Please unmount first"
exit 1
fi
# Check internet connectivity
if ! ping -c 1 archlinux.org >/dev/null 2>&1; then
log "ERROR" "No internet connection"
exit 1
fi
log "INFO" "All sanity checks passed"
}
# Disk preparation
prepare_disk() {
# Final warning before disk operations
log "WARN" "This will erase all data on $TARGET_DISK"
confirmation_prompt "Are you sure you want to continue?"
# Create partition table and partitions
log "INFO" "Wiping target disk"
wipefs -a "$TARGET_DISK"
parted -s "$TARGET_DISK" mklabel gpt
parted -s "$TARGET_DISK" mkpart ESP fat32 1MiB 513MiB
parted -s "$TARGET_DISK" set 1 boot on
parted -s "$TARGET_DISK" mkpart primary ext4 513MiB 100%
mapfile -t partitions < <(lsblk -ln -o NAME,TYPE | awk -v dev="$(basename "$TARGET_DISK")" '$2=="part" && $1 ~ dev {print "/dev/" $1}' | sort -V)
partition_efi="${partitions[0]}"
partition_luks="${partitions[1]}"
log "WARN" "EFI Partition: $partition_efi"
log "WARN" "LUKS Partition: $partition_luks"
confirmation_prompt "Does this look correct?"
# Format EFI partition
log "INFO" "Preparing EFI partition"
mkfs.fat -F32 "$partition_efi"
# Format luks partition
log "INFO" "Preparing Luks partition"
cryptsetup luksFormat "$partition_luks"
cryptsetup luksOpen "$partition_luks" luks
log "INFO" "Creating logical volumes"
pvcreate /dev/mapper/luks
vgcreate vg0 /dev/mapper/luks
lvcreate -L "$SWAP_SIZE" vg0 --name swap
lvcreate -l 100%FREE vg0 --name root
mkfs.ext4 /dev/vg0/root
mkswap /dev/vg0/swap
log "INFO" "Mounting drives"
mount /dev/vg0/root /mnt
swapon /dev/vg0/swap
mkdir -p /mnt/boot
mount "$partition_efi" /mnt/boot
}
# Base system installation
install_base_system() {
# Install base packages
log "INFO" "Install base packages"
# shellcheck disable=SC2046
pacstrap /mnt $(cat packages-pacstrap.lst)
# Generate fstab
log "INFO" "Generate and update fstab"
genfstab -U /mnt >> /mnt/etc/fstab
# Reduce SSD wear (root partition only)
sed -i '0,/relatime/s//noatime/' /mnt/etc/fstab
# Add tmpfs entry for /tmp
echo "# /tmp" >> /mnt/etc/fstab
echo "tmpfs /tmp tmpfs rw,noatime,nodev,nosuid 0 0" >> /mnt/etc/fstab
}
# Run command in arch-chroot
chroot_cmd() {
arch-chroot /mnt /bin/bash -c "$1"
}
# System configuration
configure_system() {
# Set timezone
log "INFO" "Set timezone to $TIMEZONE"
chroot_cmd "ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime"
chroot_cmd "hwclock --systohc"
# Set locale
log "INFO" "Set locale to $LOCALE"
chroot_cmd locale-gen
chroot_cmd "echo \"LANG=$LOCALE\" > /etc/locale.conf"
# Set keyboard layout
log "INFO" "Set keyboard layout to $KEYMAP"
chroot_cmd "echo \"KEYMAP=$KEYMAP\" >> /etc/vconsole.conf"
# Set hostname
log "INFO" "Set hostname to $HOSTNAME"
chroot_cmd "echo \"$HOSTNAME\" > /etc/hostname"
# Add additional initramfs hooks (encrypt, lvm2, resume)
log "INFO" "Add additional initramfs hooks (encrypt, lvm2, resume)"
chroot_cmd "sed -i '/^HOOKS=/ s/filesystems/encrypt lvm2 filesystems resume/' /etc/mkinitcpio.conf"
chroot_cmd "mkinitcpio -P"
# Configure and install grub
log "INFO" "Configure grub"
mapfile -t partitions < <(lsblk -ln -o NAME,TYPE | awk -v dev="$(basename "$TARGET_DISK")" '$2=="part" && $1 ~ dev {print "/dev/" $1}' | sort -V)
partition_luks="${partitions[1]}"
chroot_cmd "sed -i 's|^GRUB_CMDLINE_LINUX=.*|GRUB_CMDLINE_LINUX=\"cryptdevice=${partition_luks}:luks:allow-discards resume=/dev/vg0/swap\"|' /etc/default/grub"
chroot_cmd "sed -i 's/^GRUB_TIMEOUT=.*/GRUB_TIMEOUT=0/' /etc/default/grub"
chroot_cmd "sed -i 's/#GRUB_DISABLE_OS_PROBER=false/GRUB_DISABLE_OS_PROBER=false/' /etc/default/grub"
chroot_cmd "echo 'GRUB_DISABLE_OS_PROBER=false' >> /etc/default/grub"
chroot_cmd "grub-install --bootloader-id=Arch --efi-directory=/boot"
chroot_cmd "grub-mkconfig -o /boot/grub/grub.cfg"
# Set root password
log "INFO" "Set password for root:"
chroot_cmd "passwd"
# Create user
chroot_cmd "useradd -m -G wheel -s /usr/bin/fish $USERNAME"
log "INFO" "Set password for $USERNAME:"
chroot_cmd "passwd $USERNAME"
# Configure sudo
chroot_cmd "echo '%wheel ALL=(ALL) ALL' > /etc/sudoers.d/wheel"
# Enable internet
chroot_cmd "systemctl enable NetworkManager"
}
# Main installation process
main() {
sanity_checks
prepare_disk
install_base_system
configure_system
log "INFO" "Initial setup is complete"
read -p "Would you like to reboot? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log "INFO" "You may run the following you are done"
log "INFO" "> umount -R /mnt; swapoff -a; reboot"
log "INFO" "exiting gracefully..."
exit 0
fi
log "INFO" "rebooting..."
umount -R /mnt
swapoff -a
reboot
}
# Run the installation
main