-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage01.sh
executable file
·482 lines (438 loc) · 13.8 KB
/
stage01.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/bin/bash -E
DEPS=(parted dhcpcd arch-chroot pacman pacstrap blkdiscard shred wipefs)
export LANG=C
SCRIPT_DIR=`cd \`dirname $0\`; pwd`
if [ ! "`whoami`" = "root" ]; then
echo "script require root privilege."
exit 1
fi
if cat /etc/os-release | grep "Arch Linux" &> /dev/null ; then
:
else
echo "this is not a distribution of Arch Linux."
exit 1
fi
NOT_INSTALLED=
for DEP in "$DEPS"; do
if which "$DEP" &> /dev/null; then
:
else
NOT_INSTALLED=`echo "$NOT_INSTALLED$DEP"`
fi
done
if [ ! -z "$NOT_INSTALLED" ]; then
echo "command \"$NOT_INSTALLED\" is required, but not installed."
exit 1
fi
echo "checking internet connection..."
I=0
while : ; do
if curl www.waseda.jp &> /dev/null; then
: ok
break
fi
if [ "$I" -ge "3" ]; then
echo "connection error. do you have valid ethernet connection?"
echo "if you have no ethernet adapter, you can connect via wifi"
echo "using \"wifi-menu\"."
exit 1
fi
if ping -c 1 -w 1 www-proxy.waseda.jp &> /dev/null; then
echo "setting Waseda proxy..."
export all_proxy=http://www-proxy.waseda.jp:8080
else
I=`expr "$I" + 1`
for DEV in `ip -o -br link show | sed -e 's/^\([^ ]*\) .*$/\1/'`; do
echo $DEV
if echo "$DEV" | grep -q "enp" &> /dev/null ; then
if ip link show "$DEV" | grep -q "UP" &> /dev/null ; then :; else
ip link set "$DEV" up
sleep 1
if ip link show "$DEV" | grep -q "UP" &> /dev/null ; then
dhcpcd "/dev/$DEV"
break
fi
fi
fi
done
fi
done
if [ -f "$1" ]; then
echo "loading $1 ..."
source "$(pwd)/$1"
fi
: Install Start
if [ -z "$SECURE_ERASE" -o "$SECURE_ERASE" = "n" -o "$SECURE_ERASE" = false ]; then
SECURE_ERASE=false
else
SECURE_ERASE=true
fi
if [ -z "$ENCRYPT" -o "$ENCRYPT" = n -o "$ENCRYPT" = false ]; then
ENCRYPT=false
else
ENCRYPT=true
fi
export ENCRYPT
if [ -n "$BTRFS_OPTS" ]; then
BTRFS_OPTS="$BTRFS_OPTS,"
fi
[ -z "$MOUNT_DIR" ] && MOUNT_DIR="/mnt"
if [ -z "$USER_NAME" ]; then
echo "your name[yasuo]:"
read USER_NAME
[ -z "$USER_NAME" ] && USER_NAME=yasuo
fi
export USER_NAME
if [ -z "$PASSWORD" ]; then
read -sp "Password:" PASSWORD
echo ""
if [ "$ENCRYPT" = true -a -z "$PASSWORD" ]; then
echo "Password should be used to encrypt disk."
exit 1
fi
read -sp "Password(again):" PASSWORD2
echo ""
if [ ! "$PASSWORD" = "$PASSWORD2" ]; then
echo "Passwords mismatch"
exit 1
fi
fi
export PASSWORD
if [ -z "$NEW_HOSTNAME" ]; then
HWNAME=`dmesg | sed -ne '/DMI/p' | sed -e 's/^.*DMI: \([^,.]*\).*$/\1/' | tr '/ ' '_'`
if [ -z "$HWNAME" ]; then
echo "input hostname:"
read NEW_HOSTNAME
else
echo "your PC name[$USER_NAME-$HWNAME]:"
read NEW_HOSTNAME
[ -z "$NEW_HOSTNAME" ] && NEW_HOSTNAME="$USER_NAME-$HWNAME"
fi
fi
export NEW_HOSTNAME
loadkeys jp106
if [ ! -d "/sys/firmware/efi/efivars" ]; then
export BOOTTYPE=legacy
echo "legacy boot detected."
else
export BOOTTYPE=efi
echo "efi boot detected."
fi
MOUNTED=`mount | sed -e 's/^\([^ ]*\) on .*$/\1/'`
DISKS=
for DISK in `lsblk -nlp -o NAME,TYPE | awk '{if($2=="disk"||$2~/^raid/) print $1}' | tr '\n' ' '`; do
if echo "$MOUNTED" | grep -q "$DISK"; then
echo "disk $DISK is mounted. ignore"
elif echo "$DISK" | grep -q "fd"; then
echo "disk $DISK is floppy drive. ignore"
else
if [ -z "$DISKS" ]; then DISKS="$DISK"
else DISKS=`/bin/echo -e "${DISKS}\n$DISK"`
fi
fi
done
DISKS=`echo "$DISKS" | sort | uniq`
DISKS_COUNT=`echo "$DISKS" | wc -l`
echo "you have $DISKS_COUNT disks."
if [ -z "$DISKS" ]; then
echo "no disk is available."
echo "is the target device umounted?"
exit 1
fi
if [ "$DISKS_COUNT" -gt 1 ]; then
DISKS=`echo "$DISKS" | tr '\n' ' '`
while : ; do
echo "which disk will you use as a system partition($DISKS)?"
read TARGET_DISK
TARGET_DISK=`echo $TARGET_DISK | sed -e 's/ .*$//'`
if [ ! "$TARGET_DISK" = "" ] && echo " $DISKS " | grep -q " $TARGET_DISK " > /dev/null; then
break
fi
echo "Wrong input."
done
while : ; do
echo "which disk will you use as a user partition($DISKS)[same]?"
read TARGET_DATA
TARGET_DATA=`echo $TARGET_DATA | sed -e 's/ .*$//'`
[ -z "$TARGET_DATA" ] && TARGET_DATA="$TARGET_DISK"
if echo " $DISKS " | grep -q " $TARGET_DISK " > /dev/null; then
break
fi
echo "Wrong input."
done
else
TARGET_DISK="$DISKS"
TARGET_DATA="$DISKS"
fi
export TARGET_DISK
export TARGET_DATA
echo "target is $TARGET_DISK and $TARGET_DATA"
# HDD=1, SSD=0
TARGET_NAME=`echo "$TARGET_DISK" | sed -e 's/^.*\/\([^\/]*\)$/\1/'`
TARGET_NAME_DATA=`echo "$TARGET_DATA" | sed -e 's/^.*\/\([^\/]*\)$/\1/'`
IS_HDD=`cat /sys/block/$TARGET_NAME/queue/rotational`
IS_HDD_DATA=`cat /sys/block/$TARGET_NAME_DATA/queue/rotational`
if [ -z "$NOWAIT" ]; then
echo "ALL DATA WILL BE DESTROYED. to stop, press Ctrl-C in 10 seconds."
sleep 10
fi
echo "wiping fs..."
if [ "$SECURE_ERASE" = "true" ]; then
echo "performing secure erase on system partition..."
if [ "$IS_HDD" = "0" ] && blkdiscard -s "$TARGET_DISK"; then
: success
else
: failed. try to overwrite
shred -n 3 "$TARGET_DISK"
[ "$IS_HDD" = "0" ] && blkdiscard "$TARGET_DISK"
fi
if [ ! "$TARGET_DATA" = "$TARGET_DISK" ]; then
echo "performing secure erase on user partition..."
if [ "$IS_HDD_DATA" = "0" ] && blkdiscard -s "$TARGET_DATA"; then
: success
else
: failed. try to overwrite
shred -n 3 "$TARGET_DATA"
[ "$IS_HDD_DATA" = "0" ] && blkdiscard "$TARGET_DATA"
fi
fi
else
echo "wiping system partition..."
if [ "$IS_HDD" = "0" ] && echo "hello" && blkdiscard "$TARGET_DISK"; then
:
else
wipefs "$TARGET_DISK"
fi
if [ ! "$TARGET_DATA" = "$TARGET_DISK" ]; then
echo "wiping user partition..."
if [ "$IS_HDD_DATA" = "0" ] && blkdiscard "$TARGET_DATA"; then
:
else
wipefs "$TARGET_DATA"
fi
fi
fi
echo "creating partitions..."
MEMTOTAL=`cat /proc/meminfo | sed -ne '/^MemTotal:/p' | sed -e 's/^[^ ]* *\([0-9]*\) .*$/\1/'`
MEMTOTAL=`expr '(' '(' "$MEMTOTAL" - 1 ')' / 1048576 '+' 2 ')' '*' 1024`
if [ "$BOOTTYPE" = "legacy" ]; then
# BIOS-MBR
if [ "$ENCRYPT" = true ]; then
# (grub) | boot partition (300MiB, ext4) | main partition | swap
parted -s -a cylinder "$TARGET_DISK" -- mklabel msdos mkpart primary ext4 16384s 630784s mkpart primary btrfs 630785s -`expr "$MEMTOTAL" '+' 1` mkpart primary linux-swap -$MEMTOTAL 100% set 1 boot on
else
# (grub) | main partition | swap
parted -s -a cylinder "$TARGET_DISK" -- mklabel msdos mkpart primary btrfs 16384s -`expr "$MEMTOTAL" '+' 1` mkpart primary linux-swap -$MEMTOTAL 100% set 1 boot on
fi
if [ ! "$TARGET_DISK" = "$TARGET_DATA" ]; then
parted -s -a cylinder "$TARGET_DATA" -- mklabel msdos mkpart primary btrfs 0 -0
fi
else
#UEFI-GPT
# EFI System Partition | main partition | swap
parted -s -a cylinder "$TARGET_DISK" ' mklabel gpt mkpart primary fat32 40s 393215s name 1 "EFI System Partition" mkpart primary btrfs 393216s -'`expr "$MEMTOTAL" '+' 1`' name 2 "Linux Filesystem" mkpart primary linux-swap -'$MEMTOTAL' 100% name 3 "Linux Swap" set 1 boot on set 1 esp on' || {
echo "parted error"
exit 1
}
if [ ! "$TARGET_DISK" = "$TARGET_DATA" ]; then
parted -s -a cylinder "$TARGET_DATA" ' mklabel gpt mkpart primary btrfs 40s 100% name 1 "Linux Filesystem"' || {
echo "parted error"
exit 1
}
fi
fi
if [ "$BOOTTYPE" = "efi" ]; then
export PART_EFI=`fdisk -l -o Device,Type "$TARGET_DISK" | sed -ne '/EFI System/p'|cut -f 1 -d ' '`
if echo "$PART_EFI" | grep '/dev/' &> /dev/null; then
echo "EFI partition is $PART_EFI"
else
echo "EFI partition not detected."
exit 1
fi
export PART_LINUX=`fdisk -l -o Device,Type "$TARGET_DISK" | sed -ne '/Linux filesystem/p'|cut -f 1 -d ' '`
if echo "$PART_LINUX" | grep '/dev/' &> /dev/null; then
echo "Linux partition is $PART_LINUX"
else
echo "Linux partition not detected."
exit 1
fi
if [ ! "$TARGET_DISK" = "$TARGET_DATA" ]; then
export PART_DATA=`fdisk -l -o Device,Type "$TARGET_DATA" | sed -ne '/Linux filesystem/p'|cut -f 1 -d ' '`
if echo "$PART_DATA" | grep '/dev/' &> /dev/null; then
echo "Data partition is $PART_DATA"
else
echo "Data partition not detected."
exit 1
fi
fi
else
if [ "$ENCRYPT" = true ]; then
export PART_BOOT=`fdisk -l -o Device,Boot,Type "$TARGET_DISK" | sed -ne '/Linux$/p' | sed -ne '/\*/p' |cut -f 1 -d ' '`
if echo "$PART_BOOT" | grep '/dev/' &> /dev/null; then
echo "Boot partition is $PART_BOOT"
else
echo "Boot partition not detected. Maybe no boot flag?"
exit 1
fi
export PART_LINUX=`fdisk -l -o Device,Boot,Type "$TARGET_DISK" | sed -ne '/Linux$/p' | sed -e '/\*/d' |cut -f 1 -d ' '`
else
export PART_LINUX=`fdisk -l -o Device,Boot,Type "$TARGET_DISK" | sed -ne '/Linux$/p' | sed -ne '/\*/p' |cut -f 1 -d ' '`
fi
if echo "$PART_LINUX" | grep '/dev/' &> /dev/null; then
echo "Linux partition is $PART_LINUX"
else
echo "Linux partition not detected. Maybe no boot flag?"
exit 1
fi
if [ ! "$TARGET_DISK" = "$TARGET_DATA" ]; then
export PART_DATA=`fdisk -l -o Device,Boot,Type "$TARGET_DATA" | sed -ne '/Linux$/p' |cut -f 1 -d ' '`
if echo "$PART_DATA" | grep '/dev/' &> /dev/null; then
echo "Data partition is $PART_DATA"
else
echo "Data partition not detected."
exit 1
fi
fi
fi
export PART_SWAP=`fdisk -l -o Device,Type "$TARGET_DISK" | sed -ne '/Linux swap/p'|cut -f 1 -d ' '`
if echo "$PART_SWAP" | grep '/dev/' &> /dev/null; then
echo "Linux swap is $PART_SWAP"
else
echo "Linux swap not detected."
exit 1
fi
if [ "$ENCRYPT" = true ]; then
/bin/echo -n "$PASSWORD" | cryptsetup luksFormat "$PART_LINUX" -
/bin/echo -n "$PASSWORD" | cryptsetup open --allow-discards --type luks "$PART_LINUX" "part_linux" -
export PART_LINUX_LOCKED="$PART_LINUX"
export PART_LINUX="/dev/mapper/part_linux"
fi
if [ -n "$PART_BOOT" ]; then
mkfs.ext4 "$PART_BOOT"
fi
if [ -n "$PART_EFI" ]; then
mkfs.fat -F32 "$PART_EFI"
fi
IGNORE_LIST=("var/lib/systemd/coredump" "var/cache/pacman/pkg" "var/abs" "var/tmp" "srv")
mkfs.btrfs -L "Linux-System" "$PART_LINUX"
mount "$PART_LINUX" "$MOUNT_DIR"
cd "$MOUNT_DIR"
mkdir -p "root"
btrfs subvolume create "root/__active"
btrfs subvolume create "root/__snapshot"
for IGNORE_DIR in "$IGNORE_LIST"; do
mkdir -p "root/__active/`dirname $IGNORE_DIR`"
btrfs subvolume create "root/__active/$IGNORE_DIR"
done
if [ -z "$PART_DATA" ]; then
mkdir -p "home"
btrfs subvolume create "home/__snapshot"
btrfs subvolume create "home/__active"
fi
cd /
umount "$MOUNT_DIR"
BTRFS_OPTS_BASE="$BTRFS_OPTS"
if [ "$IS_HDD" = "1" ]; then
BTRFS_OPTS="${BTRFS_OPTS}noatime,autodefrag,compress=lzo,space_cache,"
else
BTRFS_OPTS="${BTRFS_OPTS}noatime,compress=lzo,ssd,space_cache,"
fi
mount -o "${BTRFS_OPTS}subvol=root/__active" "$PART_LINUX" "$MOUNT_DIR"
mkdir -p "$MOUNT_DIR/boot"
if [ -n "$PART_BOOT" ]; then
mount "$PART_BOOT" "$MOUNT_DIR/boot"
elif [ -n "$PART_EFI" ]; then
mount "$PART_EFI" "$MOUNT_DIR/boot"
fi
mkdir -p "$MOUNT_DIR/.snapshot"
mount -o "${BTRFS_OPTS}subvol=root/__snapshot" "$PART_LINUX" "$MOUNT_DIR/.snapshot"
if [ "$ENCRYPT" = true ]; then
mkdir -p "$MOUNT_DIR/etc"
dd bs=512 count=4 if=/dev/urandom of=$MOUNT_DIR/etc/keyfile
cryptsetup luksFormat "$PART_SWAP" "$MOUNT_DIR/etc/keyfile"
export PART_SWAP_LOCKED="$PART_SWAP"
export PART_SWAP="/dev/mapper/part_swap"
cryptsetup open --allow-discards --type luks --key-file "$MOUNT_DIR/etc/keyfile" "$PART_SWAP_LOCKED" "part_swap"
fi
mkswap "$PART_SWAP"
mkdir -p "$MOUNT_DIR/home"
if [ -z "$PART_DATA" ]; then
mount -o "${BTRFS_OPTS}subvol=home/__active" "$PART_LINUX" "$MOUNT_DIR/home"
mkdir -p "$MOUNT_DIR/home/.snapshot"
mount -o "${BTRFS_OPTS}subvol=home/__snapshot" "$PART_LINUX" "$MOUNT_DIR/home/.snapshot"
else
if [ "$ENCRYPT" = true ]; then
cryptsetup luksFormat "$PART_DATA" "$MOUNT_DIR/etc/keyfile"
export PART_DATA_LOCKED="$PART_DATA"
export PART_DATA="/dev/mapper/part_data"
cryptsetup open --allow-discards --type luks --key-file "$MOUNT_DIR/etc/keyfile" "$PART_DATA_LOCKED" "part_data"
fi
if [ -n "$PART_DATA" ]; then
mkfs.btrfs -L "Linux-Data" "$PART_DATA"
fi
mount "$PART_DATA" "$MOUNT_DIR"
cd "$MOUNT_DIR"
mkdir -p "home"
btrfs subvolume create "home/__active"
btrfs subvolume create "home/__snapshot"
cd /
umount "$MOUNT_DIR"
if [ "$IS_HDD_DATA" = "1" ]; then
BTRFS_OPTS_DATA="${BTRFS_OPTS_BASE}noatime,autodefrag,compress=lzo,space_cache,"
else
BTRFS_OPTS_DATA="${BTRFS_OPTS_BASE}noatime,compress=lzo,ssd,space_cache,"
fi
mount -o "${BTRFS_OPTS_DATA}subvol=home/__active" "$PART_DATA" "$MOUNT_DIR/home"
mkdir -p "$MOUNT_DIR/home/.snapshot"
mount -o "${BTRFS_OPTS_DATA}subvol=home/__snapshot" "$PART_DATA" "$MOUNT_DIR/home/.snapshot"
fi
timedatectl set-ntp true
echo 'Server = http://ftp.jaist.ac.jp/pub/Linux/ArchLinux/$repo/os/$arch' > /etc/pacman.d/mirrorlist
echo 'Server = http://ftp.tsukuba.wide.ad.jp/Linux/archlinux/$repo/os/$arch' >> /etc/pacman.d/mirrorlist
pacstrap "$MOUNT_DIR" base base-devel || {
echo "pacstrap error"
exit 1
}
if [ "$BOOTTYPE" = "efi" -o -n "$PART_BOOT" ]; then
mount -o remount,ro "$MOUNT_DIR/boot"
fi
genfstab -U "$MOUNT_DIR" > "$MOUNT_DIR/etc/fstab"
if [ "$BOOTTYPE" = "efi" -o -n "$PART_BOOT" ]; then
mount -o remount,rw "$MOUNT_DIR/boot"
fi
sed -i -e '/swap/d' "$MOUNT_DIR/etc/fstab"
echo "$PART_SWAP swap swap defaults 0 0" >> "$MOUNT_DIR/etc/fstab"
cp "$SCRIPT_DIR/stage02.sh" "$MOUNT_DIR/stage02.sh"
chmod +x "$MOUNT_DIR/stage02.sh"
if [ "$ENCRYPT" = true ]; then
(
useradd test
cd "$SCRIPT_DIR/chkboot"
sudo -u test makepkg -f
CHKBOOT_FNAME=`find . -name "*.tar.xz"`
cp "$CHKBOOT_FNAME" "$MOUNT_DIR/$CHKBOOT_FNAME"
userdel test
)
fi
cleaning() {
rm "$MOUNT_DIR/stage02.sh"
[ -n "$CHKBOOT_FNAME" ] && rm "$CHKBOOT_FNAME"
if [ "$BOOTTYPE" = "efi" -o -n "$PART_BOOT" ]; then
umount "$MOUNT_DIR/boot"
fi
umount "$MOUNT_DIR/home/.snapshot"
umount "$MOUNT_DIR/home"
umount "$MOUNT_DIR/.snapshot"
umount "$MOUNT_DIR"
[ -n "$PART_LINUX_LOCKED" ] && cryptsetup close "part_linux"
[ -n "$PART_DATA_LOCKED" ] && cryptsetup close "part_data"
[ -n "$PART_SWAP_LOCKED" ] && cryptsetup close "part_swap"
}
arch-chroot "$MOUNT_DIR" bash -c "/stage02.sh" || {
echo "chroot error"
cleaning
exit 1
}
cleaning
echo "install is finished."
exit 0