arch/arch.sh

475 lines
17 KiB
Bash
Executable File

#!/bin/bash
clear
########################
#Configure this section
########################
TARGET='/mnt'
######################################
clear
echo
HARD_DISK=nvme0n1
EFI="/dev/$(lsblk | grep $HARD_DISK | head -2 | tail -1 | cut -c 7-20 | cut -d ' ' -f1)"
BTRFS="/dev/$(lsblk | grep $HARD_DISK | head -3 | tail -1 | cut -c 7-20 | cut -d ' ' -f1)"
ROOT_NAME='arch'
USB_BACKUP='/dev/disk/by-uuid/770db62e-1893-42ce-be37-c70fa6578fba'
######################################
USER="verita84"
USER_PASSWORD="123456"
ROOT_PASSWORD="123456"
WIRELESS_PASSWORD='123456'
SSID='123456'
WIRELESS_INTERFACE='wlan0'
DISK_PASSWORD='123456'
#AUR Configuration
AUR="/home/$USER/aur"
aur_array+=( trizen )
FLATPAKS+=( app/org.mozilla.firefox org.gnome.Evolution app/com.vscodium.codium app/org.libreoffice.LibreOffice org.gajim.Gajim org.gajim.Gajim.Plugin.omemo org.telegram.desktop app/im.riot.Riot app/io.mpv.Mpv app/org.gimp.GIMP fr.handbrake.ghb org.kde.kdenlive )
#Packages
SHARED_DESKTOP_APPS=" gnome yt-dlp "
PACKAGES=" neofetch toolbox fuse python-axolotl python-qrcode sysstat postgresql redis nfs-utils syncthing samba redis aardvark-dns cockpit cockpit-machines cockpit-podman flatpak bluez bluez-utils powertop blueman pulseaudio-bluetooth avahi pulseaudio pulseaudio-bluetooth cups acpi packagekit cockpit-packagekit cockpit-pcp cockpit-storaged acpid cpupower podman podman-compose samba nfs-utils avahi cronie pulseaudio acpilight aardvark-dns sof-firmware ghostscript arch-install-scripts zsh grml-zsh-config btrfs-progs unzip nfs-utils bash-completion openssh python-pynacl parted dosfstools wget curl cifs-utils ntp mkinitcpio vim-airline rsync screen base linux-lts linux-firmware udev git networkmanager nss-mdns efibootmgr wireless_tools wpa_supplicant autoconf automake binutils fakeroot file findutils flex gawk gcc gettext grep groff gzip libtool m4 make patch pkgconf sed sudo texinfo which networkmanager-openvpn gobject-introspection ntp screen docbook-xsl alsa-utils"
VIRTUALIZATION=" gnu-netcat virt-manager libvirt iptables-nft dnsmasq dmidecode edk2-ovmf qemu-base jack "
PACKAGES=$PACKAGES$SHARED_DESKTOP_APPS$VIRTUALIZATION
SERVICES+=( cronie zram bluetooth cpupower podman powertop podman-hack gdm acpid avahi-daemon cups NetworkManager ntpd libvirtd sshd );
auto_login(){
sed -i "/#WaylandEnable=false/a AutomaticLoginEnable=True" $TARGET/etc/gdm/custom.conf
sed -i "/True/a AutomaticLogin=$USER" $TARGET/etc/gdm/custom.conf
}
clean-old-usb-snapshots(){
echo;echo "[Cleaning up old USB snapshots.....]";echo
btrfs sub delete $TARGET/$ROOT_NAME-$1
btrfs sub delete $TARGET/flatpak
echo;echo "[Cleaning up old EFI data.....]";echo
rm -rf $TARGET/efi-$1
}
clean-old-snapshots() {
echo;echo "[Cleaning up old snapshots.....]";echo
btrfs sub delete /.snapshots/$ROOT_NAME-$1
btrfs sub delete /.snapshots/flatpak
}
create-usb-snapshots(){
echo;echo "[Creating new ROOT and Flatpak snapshots.....]";echo
btrfs sub snap -r / /.snapshots/$ROOT_NAME-$1
btrfs sub snap -r /var/lib/flatpak /.snapshots/flatpak
}
send-snapshots(){
echo;echo "[Sending snapshot to $USB_BACKUP]";echo
btrfs send /.snapshots/$ROOT_NAME-$1 | btrfs receive $TARGET
echo;echo "[Sending Flatpaks to $USB_BACKUP]";echo
btrfs send /.snapshots/flatpak | btrfs receive $TARGET
}
copy-efi-to-usb(){
echo;echo "[Copying EFI data....]";echo
mkdir $TARGET/efi-$1
mkdir $TARGET/$1
cp -rf /boot/* $TARGET/efi-$1/
}
homeBackup() {
echo;echo "[Copying USER data....]";echo
rsync -av --delete /home/ /mnt/home/
}
usb-backup() {
umount $TARGET
echo;echo "[Mounting $USB_BACKUP]";echo
mount $USB_BACKUP $TARGET
homeBackup
clean-old-snapshots "$1"
clean-old-usb-snapshots "$1"
create-usb-snapshots "$1"
send-snapshots "$1"
clean-old-snapshots "$1"
copy-efi-to-usb "$1"
cp -f /usr/bin/arch.sh $TARGET/$1
ls $TARGET/
umount $TARGET
}
usb-restore() {
partitions
rsync -av ../$ROOT_NAME-$1/ $TARGET/ --exclude=/home --delete
rsync -av ../flatpak/ $TARGET/var/lib/flatpak/ --delete
cp -rf ../efi-$1/* $TARGET/boot/
fstab
cp -f arch.sh $TARGET/
arch-chroot $TARGET /arch.sh bootloader
arch-chroot $TARGET /arch.sh btrfs-tweaks
#arch-chroot $TARGET /arch.sh aur
rm -f $TARGET/arch.sh
unmount
}
installSoftware() {
while true; do
if [ ! -f "$TARGET/usr/bin/zsh" ]; then
echo "Downloading packages......."
printf '\n Y' | /usr/sbin/pacstrap $TARGET $PACKAGES
sleep 10
else
break
fi
done
}
install() {
partitions
sed -i '/^SigLevel/s/^\(.*\)$/#\1\n/' /etc/pacman.conf
sed -i '/#SigLevel/a SigLevel = Never' /etc/pacman.conf
pacman -Sy archlinux-keyring --noconfirm
installSoftware
locale
accounts
auto_login
zram
custom_service_files
services
btrfs-tweaks
cpuPower
setup_script
unmount
}
pacman-snapshot-hook(){
echo "[Trigger]" > /usr/share/libalpm/hooks/04-snapshot.hook
echo "Operation = Upgrade" >> /usr/share/libalpm/hooks/04-snapshot.hook
echo "Operation = Install" >> /usr/share/libalpm/hooks/04-snapshot.hook
echo "Operation = Remove" >> /usr/share/libalpm/hooks/04-snapshot.hook
echo "Type = Package" >> /usr/share/libalpm/hooks/04-snapshot.hook
echo "Target = *" >> /usr/share/libalpm/hooks/04-snapshot.hook
echo "[Action]" >> /usr/share/libalpm/hooks/04-snapshot.hook
echo "Description = Creating Snapshot" >> /usr/share/libalpm/hooks/04-snapshot.hook
echo "When = PreTransaction" >> /usr/share/libalpm/hooks/04-snapshot.hook
echo "Exec = /usr/bin/arch.sh snapshot" >> /usr/share/libalpm/hooks/04-snapshot.hook
}
snapshots() {
echo
echo "Creating Snapshots....."
echo
DATE=$(echo $(date +%Y-%m-%d-%H-%M-%S))
btrfs sub snapshot / /.snapshots/root-${DATE}
bootloader_entry "${DATE}" ".snapshots/root-${DATE}"
}
remove-snapshots() {
btrfs sub delete /.snapshots/*
rm -f /boot/loader/entries/root-*
}
chroot() {
printf "$DISK_PASSWORD" | cryptsetup open ${BTRFS} root
mounts
arch-chroot $TARGET /bin/bash
}
flatpaks() {
for i in "${FLATPAKS[@]}"; do
flatpak install $i -y
done
}
bootloader_entry(){
## $1=Boot Entry Name
## $2=Volume Name
echo "default arch.conf" >/boot/loader/loader.conf
echo 'timeout 3 ' >>/boot/loader/loader.conf
echo "title $1" >/boot/loader/entries/$1.conf
echo 'linux /vmlinuz-linux-lts' >>/boot/loader/entries/$1.conf
echo 'initrd /initramfs-linux-lts.img' >>/boot/loader/entries/$1.conf
echo "options mitigations=off cryptdevice=UUID=$(blkid | grep ${BTRFS} | cut -d '"' -f2):root root=\"LABEL=arch\" rootflags=subvol=@$2 rw" >>/boot/loader/entries/$1.conf
}
bootloader() {
echo 'HOOKS=(base udev modconf block encrypt btrfs filesystems keyboard keymap lvm2 fsck)' >/etc/mkinitcpio.conf
mkinitcpio -P
bootctl install
bootloader_entry "$ROOT_NAME" "arch"
}
function setup_script() {
cp -f arch.sh $TARGET/usr/bin/
echo 'bash /usr/bin/arch.sh bootloader' >>$TARGET/setup.sh
#echo 'bash /usr/bin/arch.sh aur' >>$TARGET/setup.sh
echo 'bash /usr/bin/arch.sh flatpaks' >>$TARGET/setup.sh
echo 'bash /usr/bin/arch.sh btrfs-tweaks' >>$TARGET/setup.sh
echo 'bash /usr/bin/arch.sh pacman-snapshot-hook' >>$TARGET/setup.sh
chmod +x $TARGET/usr/bin/arch.sh
chmod +x $TARGET/setup.sh
arch-chroot $TARGET /setup.sh
sed -i '/#NoUpgrade/a IgnorePkg = linux-lts' $TARGET/etc/pacman.conf
rm -f $TARGET/setup.sh
}
function cpuPower() {
#CPU POWERSAVE AND ACPI Events
echo "governor='powersave'" >>$TARGET/etc/default/cpupower
echo "min_freq='400MHz'" >>$TARGET/etc/default/cpupower
echo "max_freq='2GHz'" >>$TARGET/etc/default/cpupower
}
btrfs_filesytem() {
btrfs sub create $TARGET/@$ROOT_NAME
btrfs sub create $TARGET/@.snapshots
btrfs sub create $TARGET/@flatpak
btrfs sub create $TARGET/@libvirt
btrfs sub create $TARGET/@home
btrfs sub create $TARGET/@root
btrfs sub create $TARGET/@postgres
btrfs sub create $TARGET/@containers
echo
echo "Binding BTRFS Root"
echo
umount /mnt
mount -o subvol=@$ROOT_NAME /dev/mapper/root $TARGET
}
mounts() {
echo
echo "Mounting......."
mount /dev/mapper/root $TARGET
btrfs_filesytem
mkdir -p $TARGET/boot
mount $EFI $TARGET/boot
#CONFIGURE DATA DIRS (HOME AND FLATPAK)
mkdir $TARGET/home
mount -o subvol=@home /dev/mapper/root $TARGET/home
mkdir -p $TARGET/var/lib/flatpak
mount -o subvol=@flatpak /dev/mapper/root $TARGET/var/lib/flatpak
}
unmount() {
echo
echo "Unmounting....."
umount $TARGET/proc
umount $TARGET/dev
umount $TARGET/sys
umount $TARGET/boot
umount $TARGET/home
umount -R $TARGET/*
umount -R $TARGET/var/lib/flatpak
umount -R $TARGET
umount -R $TARGET
}
locale() {
echo "ln -sf /usr/share/zoneinfo/US/Mountain /etc/localtime" >>$TARGET/setup.sh
echo "hwclock --systohc" >>$TARGET/setup.sh
echo "en_US.UTF-8 UTF-8" >$TARGET/etc/locale.gen
echo "locale-gen" >>$TARGET/setup.sh
echo "LANG=en_US.UTF-8" >$TARGET/etc/locale.conf
}
partitions() {
echo
echo "Setting Up Partitions....."
printf "$DISK_PASSWORD" | cryptsetup open ${BTRFS} root
unmount
echo
echo "Formatting $EFI"
echo
echo y | mkfs.vfat $EFI
mounts
#Configure /etc/fstab
fstab
}
fstab() {
mkdir $TARGET/etc
echo "UUID=$(blkid | grep ${EFI} | cut -d '"' -f4) /boot vfat umask=0077 0 1" >$TARGET/etc/fstab
echo "/dev/mapper/root / btrfs noatime,nodiratime,autodefrag,compress,subvol=@$ROOT_NAME 0 1" >>$TARGET/etc/fstab
echo "/dev/mapper/root /.snapshots btrfs noatime,nodiratime,autodefrag,compress,subvol=@.snapshots 0 1" >>$TARGET/etc/fstab
echo "/dev/mapper/root /var/lib/flatpak btrfs noatime,nodiratime,autodefrag,compress,subvol=@flatpak 0 1" >>$TARGET/etc/fstab
echo "/dev/mapper/root /var/lib/libvirt btrfs noatime,nodiratime,autodefrag,compress,subvol=@libvirt 0 1" >>$TARGET/etc/fstab
echo "tmpfs /var/log tmpfs defaults 0 0" >>$TARGET/etc/fstab
echo "tmpfs /var/cache/pacman/pkg tmpfs defaults 0 0" >>$TARGET/etc/fstab
echo "/dev/mapper/root /home btrfs noatime,nodiratime,autodefrag,compress,subvol=@home 0 1" >>$TARGET/etc/fstab
echo "/dev/mapper/root /root btrfs noatime,nodiratime,autodefrag,compress,subvol=@root 0 1" >>$TARGET/etc/fstab
echo "/dev/mapper/root /var/lib/postgres btrfs noatime,nodiratime,autodefrag,compress,subvol=@postgres 0 1" >>$TARGET/etc/fstab
echo "/dev/mapper/root /var/lib/containers btrfs noatime,nodiratime,autodefrag,compress,subvol=@containers 0 1" >>$TARGET/etc/fstab
}
accounts() {
echo
echo "Set Password for $USER"
echo "useradd -m -s /bin/bash $USER" >>$TARGET/setup.sh
echo "echo \"$USER:$USER_PASSWORD\"| chpasswd " >>$TARGET/setup.sh
echo "gpasswd -a $USER wheel" >>$TARGET/setup.sh
echo "gpasswd -a $USER network" >>$TARGET/setup.sh
echo "gpasswd -a $USER video" >>$TARGET/setup.sh
echo "gpasswd -a $USER libvirt" >>$TARGET/setup.sh
echo "$USER ALL=(ALL) ALL" >$TARGET/etc/sudoers
echo "root ALL=(ALL) ALL" >>$TARGET/etc/sudoers
echo
echo "Setting ROOT Password:"
echo "echo \"root:$ROOT_PASSWORD\"| chpasswd " >>$TARGET/setup.sh
#Change Shell to ZSH
echo "printf "$USER_PASSWORD" | chsh -s /bin/zsh $USER" >>$TARGET/setup.sh
echo "printf "$ROOT_PASSWORD" | chsh -s /bin/zsh " >>$TARGET/setup.sh
}
printer() {
printer=$(sudo ippfind)
sudo lpadmin -p printer -E -v $printer
}
btrfs-tweaks() {
DISABLE_COW=("/var/lib/docker" "/var/lib/containers" "/var/lib/postgres" "/var/lib/mysql" "/var/lib/libvirt");
for i in "${DISABLE_COW[@]}"
do
chattr -R +C $i
done
}
custom_service_files() {
echo "systemctl set-default graphical.target" >>$TARGET/setup.sh
echo "[Unit]" >$TARGET/etc/systemd/system/powertop.service
echo "Description=Powertop tunings" >>$TARGET/etc/systemd/system/powertop.service
echo "[Service]" >>$TARGET/etc/systemd/system/powertop.service
echo "Type=oneshot" >>$TARGET/etc/systemd/system/powertop.service
echo "ExecStart=/usr/bin/powertop --auto-tune" >>$TARGET/etc/systemd/system/powertop.service
echo "[Install]" >>$TARGET/etc/systemd/system/powertop.service
echo "WantedBy=multi-user.target" >>$TARGET/etc/systemd/system/powertop.service
echo "[Unit]" >$TARGET/etc/systemd/system/podman-hack.service
echo "Description=Podman Hack" >>$TARGET/etc/systemd/system/podman-hack.service
echo "[Service]" >>$TARGET/etc/systemd/system/podman-hack.service
echo "Type=oneshot" >>$TARGET/etc/systemd/system/podman-hack.service
echo "ExecStartPre=/usr/bin/sysctl -w kernel.unprivileged_userns_clone=1" >>$TARGET/etc/systemd/system/podman-hack.service
echo "ExecStartPre=/usr/bin/touch /etc/subuid /etc/subgid" >>$TARGET/etc/systemd/system/podman-hack.service
echo "ExecStartPre=/usr/bin/usermod --add-subuids 100000-165535 --add-subgids 100000-165535 $USER" >>$TARGET/etc/systemd/system/podman-hack.service
echo "ExecStartPre=/usr/bin/chmod 4755 /usr/bin/newgidmap" >>$TARGET/etc/systemd/system/podman-hack.service
echo "ExecStart=/usr/bin/chmod 4755 /usr/bin/newuidmap" >>$TARGET/etc/systemd/system/podman-hack.service
echo "[Install]" >>$TARGET/etc/systemd/system/podman-hack.service
echo "WantedBy=multi-user.target" >>$TARGET/etc/systemd/system/podman-hack.service
echo "[Unit]" >$TARGET/etc/systemd/system/zram.service
echo "Description=Swap with zram" >>$TARGET/etc/systemd/system/zram.service
echo "After=multi-user.target" >>$TARGET/etc/systemd/system/zram.service
echo "[Service]" >>$TARGET/etc/systemd/system/zram.service
echo "Type=oneshot" >>$TARGET/etc/systemd/system/zram.service
echo "RemainAfterExit=true" >>$TARGET/etc/systemd/system/zram.service
echo "ExecStartPre=/usr/bin/modprobe zram" >>$TARGET/etc/systemd/system/zram.service
echo "ExecStartPre=/usr/bin/bash -c 'echo 2G > /sys/block/zram0/disksize'" >>$TARGET/etc/systemd/system/zram.service
echo "ExecStartPre=/sbin/mkswap /dev/zram0" >>$TARGET/etc/systemd/system/zram.service
echo "ExecStart=/sbin/swapon /dev/zram0" >>$TARGET/etc/systemd/system/zram.service
echo "ExecStop=/sbin/swapoff /dev/zram0" >>$TARGET/etc/systemd/system/zram.service
echo "[Install]" >>$TARGET/etc/systemd/system/zram.service
echo "WantedBy=multi-user.target" >>$TARGET/etc/systemd/system/zram.service
echo "ExecStop=/sbin/swapoff /dev/zram0" >>$TARGET/etc/systemd/system/zram.service
echo "ExecStop=/sbin/rmmod zram" >>$TARGET/etc/systemd/system/zram.service
}
services() {
for i in "${SERVICES[@]}"
do
echo "systemctl enable $i" >>$TARGET/setup.sh
done
}
initialize-disk() {
parted /dev/$HARD_DISK mklabel gpt
parted /dev/$HARD_DISK mkpart primary fat32 1MiB 200MiB
parted /dev/$HARD_DISK set 1 esp on
parted /dev/$HARD_DISK mkpart P2 ext3 200MiB 100%
BTRFS="$(lsblk | grep $HARD_DISK | head -3 | tail -1 | cut -c 7-20 | cut -d ' ' -f1)"
printf "$DISK_PASSWORD\n$DISK_PASSWORD" | cryptsetup luksFormat /dev/${BTRFS}
printf "$DISK_PASSWORD" | cryptsetup open /dev/${BTRFS} root
echo
echo "Formatting....."
echo y | mkfs.btrfs -L arch /dev/mapper/root --force
}
wifi() {
iwctl --passphrase $WIRELESS_PASSWORD station $WIRELESS_INTERFACE connect $SSID
}
aur() {
mkdir -p $AUR
for i in "${aur_array[@]}"; do
cd $AUR
git clone "https://aur.archlinux.org/$i.git"
cd $i
git pull
env EUID=1 makepkg -fsi --noconfirm --skippgpcheck
done
}
zram() {
echo "zram" >$TARGET/etc/modules-load.d/zram.conf
echo "options zram num_devices=1" >$TARGET/etc/modprobe.d/zram.conf
echo 'KERNEL=="zram0", ATTR{disksize}="2024M" RUN="/usr/bin/mkswap /dev/zram0", TAG+="systemd""' >$TARGET/etc/udev/rules.d/99-zram.rules
}
show-help() {
echo
echo "arch.sh arguments:"
echo
echo "./arch.sh install"
echo "./arch.sh backup [device name]"
echo "./arch.sh restore [device name"
echo "./arch.sh chroot"
echo "./arch.sh wifi"
echo "./arch.sh bootloader"
echo "./arch.sh initialize"
echo "./arch.sh snapshot"
echo "./arch.sh reomve-snapshot"
echo "./arch.sh btrfs-tweaks"
echo
}
if [ "$1" = "install" ]; then
install "$2"
elif [ "$1" = "pacman-snapshot-hook" ]; then
pacman-snapshot-hook
elif [ "$1" = "upgrade-system" ]; then
upgrade-system
elif [ "$1" = "chroot" ]; then
chroot
elif [ "$1" = "initialize" ]; then
initialize-disk
elif [ "$1" = "aur" ]; then
aur "$2"
elif [ "$1" = "wifi" ]; then
wifi
elif [ "$1" = "flatpaks" ]; then
flatpaks
elif [ "$1" = "bootloader" ]; then
bootloader
elif [ "$1" = "snapshot" ]; then
snapshots
elif [ "$1" = "backup" ]; then
usb-backup "$2"
elif [ "$1" = "btrfs-tweaks" ]; then
btrfs-tweaks
elif [ "$1" = "restore" ]; then
usb-restore "$2"
elif [ "$1" = "remove-snapshot" ]; then
remove-snapshots
elif [ "$1" = "help" ]; then
show-help
else
show-help
fi