usl-mm/setup-usl-mm

209 lines
5.8 KiB
Bash
Executable File

#!/bin/bash
pre_reboot_script () {
pass_var=$(dialog --passwordbox "Enter password to crypted partition for containers:" 25 25 --output-fd 1)
pass_var2=$(dialog --passwordbox "Enter password to crypted partition for containers again:" 25 25 --output-fd 1)
if [ "$pass_var" == "$pass_var2" ]; then
echo "Passwords match!"
else
echo "Passwords not match!"
exit 1
fi
echo "Doing pacman -Syy"
pacman -Syy
echo "Starting lsblk"
lsblk
read -p "Enter partition to encrypt:" PARTITION
echo "Installing requirements"
pacman -S --noconfirm cryptsetup ecryptfs-utils $(pacman -Q linux | tail -n 1 | awk {'print $1'})-zfs $(pacman -Q linux | tail -n 1 | awk {'print $1'})-headers parted
echo "Loading ZFS kernel module"
modprobe zfs
echo "Setting timezone to Prague"
timedatectl set-timezone Europe/Prague
echo "Starting ecryptfs-setup-swap"
ecryptfs-setup-swap -f
echo "Starting luksFormat"
echo -n "$pass_var" | cryptsetup luksFormat $PARTITION -
echo "Opening crypted partition"
echo "$pass_var" | cryptsetup open $PARTITION crypt -c -
echo "Creating zpool"
zpool create crypt /dev/mapper/crypt -o ashift=12
echo "Enabling compression on zpool"
zfs set compression=on crypt
echo "Creating datasets crypt/lxd/dir crypt/lxd/storage"
zfs create crypt/lxd
zfs create crypt/lxd/dir
zfs create crypt/lxd/storage
echo "Install snapd and lxd"
pacman -S snapd --noconfirm
systemctl enable snapd
systemctl start snapd
sleep 5
snap install lxd
echo "Switch to 4.16 LXD version"
snap switch --channel 4.16/stable lxd
snap refresh
echo "Stopping and disabling lxd"
snap stop --disable lxd
snap disable lxd
echo "Deleting everything in /var/snap/lxd/common/lxd/*"
rm /var/snap/lxd/common/lxd/* -r
echo "Creating mountpoint /var/snap/lxd/common/lxd to crypt/lxd/dir"
zfs set mountpoint=/var/snap/lxd/common/lxd crypt/lxd/dir
echo "Configuring sysctl"
echo "vm.swappiness = 1" > /etc/sysctl.d/50-usl-mm.conf
echo "vm.min_free_kbytes = 131072" >> /etc/sysctl.d/50-usl-mm.conf
echo "vm.dirty_background_ratio = 5" >> /etc/sysctl.d/50-usl-mm.conf
echo "fs.inotify.max_queued_events = 1048576" >> /etc/sysctl.d/50-usl-mm.conf
echo "fs.inotify.max_user_instances = 1048576" >> /etc/sysctl.d/50-usl-mm.conf
echo "fs.inotify.max_user_watches = 1048576" >> /etc/sysctl.d/50-usl-mm.conf
echo "kernel.dmesg_restrict = 1" >> /etc/sysctl.d/50-usl-mm.conf
echo "Configuring arc cache for zfs to min 256MB and max 1536MB + txg_timeout to 3"
echo "options zfs zfs_arc_min=268435456" > /etc/modprobe.d/zfs.conf
echo "options zfs zfs_arc_max=1610612736" >> /etc/modprobe.d/zfs.conf
echo "options zfs zfs_txg_timeout=3" >> /etc/modprobe.d/zfs.conf
echo "Doing mkinitcpio -P"
mkinitcpio -P
echo "Doing pacman -Suu"
pacman -Suu --noconfirm
echo "Creating onstart script in /root"
echo "#!/bin/bash" > /root/onstart
echo "read -p \"Press any button to start...\"" >> /root/onstart
echo "pass_var=\$(dialog --passwordbox \"Enter password:\" 25 25 --output-fd 1)" >> /root/onstart
echo "pass_var2=\$(dialog --passwordbox \"Enter password again:\" 25 25 --output-fd 1)" >> /root/onstart
echo "if [ \"\$pass_var\" == \"\$pass_var2\" ]; then" >> /root/onstart
echo "echo \"Passwords match!\"" >> /root/onstart
echo "else" >> /root/onstart
echo "echo \"Passwords not match!\"" >> /root/onstart
echo "exit 1" >> /root/onstart
echo "fi" >> /root/onstart
echo "echo \"Stopping LXD snap daemon and deleting /var/snap/lxd/common/lxd/*\"" >> /root/onstart
echo "snap disable lxd" >> /root/onstart
echo "rm /var/snap/lxd/common/lxd/* -rf" >> /root/onstart
echo "echo \"Opening encrypted partition\"" >> /root/onstart
echo "echo \$pass_var | cryptsetup open $PARTITION crypt -c -" >> /root/onstart
echo "partprobe" >> /root/onstart
echo "zpool import -d /dev/mapper crypt -f -m" >> /root/onstart
echo "sleep 5" >> /root/onstart
echo "snap enable lxd" >> /root/onstart
echo "snap start lxd" >> /root/onstart
chmod +x /root/onstart
read -p "Press any key for reboot"
reboot
}
post_reboot_script () {
echo "Stopping lxd and delete /var/snap/lxd/common/lxd/*"
systemctl stop snap.lxd.daemon
rm /var/snap/lxd/common/lxd/* -r
echo "Executing /root/onstart"
bash /root/onstart
echo "Configuring LXD"
lxd waitready
cat <<EOF | lxd init
no
yes
storage
zfs
no
crypt/lxd/storage
no
yes
lxdbr0
10.10.10.1/24
yes
none
no
no
no
EOF
echo "Set screen settings"
echo "startup_message off" >> /root/.screenrc
echo "screen -t htop htop" >> /root/.screenrc
echo "screen -t mc mc" >> /root/.screenrc
echo "screen -t bash bash" >> /root/.screenrc
echo "altscreen on" >> /root/.screenrc
echo "term screen-256color" >> /root/.screenrc
echo "bind 'b' prev" >> /root/.screenrc
echo "bind 'n' next" >> /root/.screenrc
echo "hardstatus alwayslastline" >> /root/.screenrc
echo "autodetach on" >> /root/.screenrc
echo "mousetrack on" >> /root/.screenrc
echo "vbell off" >> /root/.screenrc
echo "termcapinfo xterm* ti@:te@" >> /root/.screenrc
echo "defscrollback 5000" >> /root/.screenrc
echo "scrollback 5000" >> /root/.screenrc
echo "hardstatus string \"%{=b kw} %?%-Lw%?%{=br kw}[%n %t]%{=b kw}%?%+Lw%? %= %c\"" >> /root/.screenrc
echo "Install utilities"
pacman -S --noconfirm mc htop screen smartmontools pv
echo "Setting-up wireguard"
pacman -S --noconfirm wireguard-dkms wireguard-tools
lxc profile set default linux.kernel_modules wireguard
echo "Set max processes to 1500 in default profile in LXD"
lxc profile set default limits.processes 1500
echo "Set refresh.retain=2 in snap"
snap set system refresh.retain=2
read -p "Done, press any key to return to main menu"
main_menu
}
main_menu () {
cmd=(dialog --nocancel --menu "Welcome in setup-lxd-mm!" 22 76 16)
options=(
1 "Pre-reboot script"
2 "Post-reboot script"
)
choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
for choice in $choices
do
case $choice in
1)
pre_reboot_script
;;
2)
post_reboot_script
;;
esac
done
clear
}
main_menu