-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
·212 lines (166 loc) · 4.81 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
#!/bin/bash
if [[ $# -ne 3 ]]; then
echo "Usage: $0 <root disk> <data disk|none> <username>"
exit 1
fi
if [[ ! -d /sys/firmware/efi ]];then
echo "Script set to install uefi compatible bootloader but you did not boot using uefi"
exit 1
fi
if [[ $1 == "" ]]; then
echo "Please enter the root disk."
exit 1
fi
if [[ $2 == "" ]]; then
echo "Please enter the data disk. Enter none if you don't want to create one."
exit 1
fi
if [[ $3 == "" ]]; then
echo "Please enter the username of your account."
exit 1
fi
RDISK=$1 #root disk
DDISK=$2 #data disk
USR=$3 #username
source config.sh
PARTITION=""
BOOT=""
ROOT=""
HOME=""
DATA=""
yes_no_prompt(){
read -p "$1 [y/N] "
}
contains_element(){
for e in "${@:2}"; do [[ $e == $1 ]] && break; done;
}
select_partition(){
partitions_list=`lsblk | grep 'part' | awk '{print "/dev/" substr($1,3)}'`;
PS3="$1: "
select partition in $partitions_list; do
if contains_element $partition; then
yes_no_prompt "Is $partition the correct partition:"
if [[ $REPLY == "y" ]]; then
PARTITION=$partition
break
fi
fi
done
}
config_disks(){
cfdisk $RDISK
if [[ $DDISK != "none" ]]; then
cfdisk $DDISK
fi
}
setup_luks(){
select_partition "Select root partition"
echo "Encrypting ${PARTITION}..."
cryptsetup luksFormat $PARTITION
cryptsetup open $PARTITION $ROOTLUKS
if [[ $DDISK != "none" ]]; then
select_partition "Select data partition"
dd if=/dev/random of=data.key bs=1 count=32
cryptsetup luksFormat $PARTITION data.key
cryptsetup open $PARTITION $DATALUKS --key-file data.key
DATA=/dev/mapper/$DATALUKS
fi
ROOT=/dev/mapper/$ROOTLUKS
}
mount_filesytems(){
mount -o $BTRFS_OPTS,subvol=@ $ROOT /mnt
mkdir -p /mnt/{boot,dev,proc,sys,home,mnt,var}
mount -o $BTRFS_OPTS,subvol=@home $ROOT /mnt/home
# create seperate subvolumes for log, cache, and tmp to prevent them
# from being in snapshots of the root subvolume
btrfs subvolume create /mnt/var/log
btrfs subvolume create /mnt/var/cache
btrfs subvolume create /mnt/var/tmp
mount $BOOT /mnt/boot
mount --rbind /dev/ /mnt/dev
mount --rbind /proc/ /mnt/proc
mount --rbind /sys/ /mnt/sys
if [[ $DDISK != "none" ]]; then
# move the key for the data drive to the root filesystem
mkdir -p /mnt/root
mv data.key /mnt/root
mkdir -p /mnt/mnt/{vault,snapshots}
mount -o $BTRFS_OPTS,subvol=@vault $DATA /mnt/mnt/vault
mount -o $BTRFS_OPTS,subvol=@snapshots $DATA /mnt/mnt/snapshots
btrfs subvolume create /mnt/mnt/vault/storage
btrfs subvolume create /mnt/mnt/vault/vms
fi
}
setup_btrfs(){
mkfs.btrfs -L root -d single -m single $ROOT
mount -o $BTRFS_OPTS $ROOT /mnt
btrfs subvolume create /mnt/@
btrfs subvolume create /mnt/@home
umount /mnt
if [[ $DDISK != "none" ]]; then
mkfs.btrfs -L data -d single -m dup $DATA
mount -o $BTRFS_OPTS $DATA /mnt
btrfs subvolume create /mnt/@vault
btrfs subvolume create /mnt/@snapshots
umount /mnt
fi
}
boot_partition(){
PS3="Do you need to create a boot partion: "
options=("y" "n")
select opt in "${options[@]}"; do
case "$opt" in
"n")
echo "Using existing boot partition."
select_partition "Select boot partition"
BOOT=$PARTITION
break
;;
"y")
echo "Going to create a boot partition."
cfdisk $RDISK
select_partition "Select boot partition"
BOOT=$PARTITION
mkfs.vfat -F 32 $BOOT
break
;;
*) echo Invalid;;
esac
done
}
bootstrap(){
repo="$REPO/current/"
if [[ $MUSL -eq 1 ]]; then
repo="$REPO/current/musl/"
fi
xbps-install -Sy -R $repo -r /mnt base-system btrfs-progs cryptsetup ntp refind vim
xbps-reconfigure -r /mnt -f base-files
chroot /mnt xbps-reconfigure -a
}
echo "------------------------------------------------"
echo "Username: ${USR}"
echo "Root disk: ${RDISK}"
echo "Data disk: ${DDISK}"
echo "Root LUKS: ${ROOTLUKS}"
echo "Data LUKS: ${DATALUKS}"
echo "------------------------------------------------"
yes_no_prompt "Does the information above look correct"
if [[ $REPLY == "n" ]]; then
echo "Aborting"
exit 1
fi
loadkeys $KEYMAP
boot_partition
config_disks
setup_luks
setup_btrfs
mount_filesytems
bootstrap
cp ./chroot.sh /mnt/
cp ./config.sh /mnt/
chroot /mnt ./chroot.sh $RDISK $ROOT $BOOT $DATA $USR
# cleanup
rm /mnt/chroot.sh /mnt/config.sh
if [[ $DDISK != "none" ]]; then
echo "Don't forget to setup /etc/crypttab and dracut"
fi