#!/bin/sh . /usr/lib/xitui.sh . /usr/lib/glyphs.sh . /usr/lib/colors.sh DEBUG_MODE=false logfile="installer.log" default_packages="base xipkg dracut grub doas neofetch vim tzdata mksh" additional_services="networkmanager xorg iwd" cmdline="" OPT_KERNEL="linux-libre" OPT_BOOTSYSTEM="efi" OPT_ENCRYPTION="none" welcome_splash () { t_radio "Welcome to the xilinux installer" "install xilinux" "network configuration" "exit to shell" case "$T_RESULT" in "network configuration") t_clean nmtui t_init welcome_splash ;; "install xilinux") return 0 ;; *) t_clean exit 0 ;; esac } list_disks () { lsblk -r | while read -r line; do set - $line [ "$6" = "disk" ] && { printf '"/dev/%s (%s)" ' $1 $4 } done } list_partitions () { lsblk $1 -nlp -o NAME echo "none" } partition_disk () { t_msg "Partitioning $1..." # if is efi, create an efi partition efi= [ "$OPT_BOOTSYSTEM" = "efi" ] && { export EFI_PART=$11 export SYS_PART=$12 efi="type=ef, start=2048, size=210000" } || { export SYS_PART=$11 } export SWAP_PART=none echo " unit: sectors sector-size: 512 $efi type=83 " | sfdisk $1 >>$logfile && t_msg "Partitioned $1!" } detect_efi () { [ -d "/sys/firmware/efi" ] \ && OPT_BOOTSYSTEM="efi" \ || OPT_BOOTSYSTEM="bios" t_yesno "Detected boot system as '$OPT_BOOTSYSTEM', is this correct?" || { [ $OPT_BOOTSYSTEM = "efi" ] \ && OPT_BOOTSYSTEM="bios" \ || OPT_BOOTSYSTEM="efi" } return 0 } encrypt_disk () { #t_yesno "${LIGHT_RED}Warning this will ${RED}irreversibly ${LIGHT_RED} erase all data.\n${BLUE}continue?" || { #partition_disks #return 1 #} #t_msg "Wiping disk $ROOT_DISK..." #printf "YES\n" | cryptsetup open --type plain -d /dev/urandom $ROOT_DISK to_be_wiped >> $logfile #dd if=/dev/zero of=/dev/mapper/to_be_wiped >> $logfile #cryptsetup close to_be_wiped >> $logfile default_packages="$default_packages crypsetup lvm2" partition_disk $ROOT_DISK enter_password "disk encryption password" t_msg "Encrypting $ROOT_DISK.." printf "$passwd" | cryptsetup luksFormat $SYS_PART -d ->> $logfile printf "$passwd" | cryptsetup luksOpen $SYS_PART cryptlvm -d - >> $logfile pvcreate /dev/mapper/cryptlvm >> $logfile vgcreate xilinux /dev/mapper/cryptlvm >> $logfile lvcreate -l 100%FREE xilinux -n root >> $logfile uuid="$(blkid --probe --output value --match-tag UUID $SYS_PART)" cmdline="rd.luks.uuid=luks-$uuid rd.lvm.lv=xilinux/root root=/dev/xilinux/root rootflags=rw,relatime" default_packages="$default_packages lvm2 cryptsetup" export SYS_PART=/dev/xilinux/root return 0 } partition_disks () { # need to use eval to work with spaces in line names eval "t_radio 'Select install disk' $(list_disks)" local selected=$(echo $T_RESULT | cut -d' ' -f1) export ROOT_DISK=$selected t_yesno "${BLUE}Auto-partition $selected disk?\n${RED}(Warning: existing data will be overwritten)" && { t_yesno "${BLUE}Use full disk encryption?" && { export OPT_ENCRYPTION="lvm-luks" encrypt_disk $ROOT_DISK return 0 } || { partition_disk $selected || return 1 } } || { cfdisk $selected && { t_radio 'Select primary system partition' $(list_partitions $selected) export SYS_PART=$T_RESULT [ $OPT_BOOTSYSTEM = "efi" ] && { t_radio 'Select efi system partition' $(list_partitions $selected) export EFI_PART=$T_RESULT } t_radio 'Select swap partition' $(list_partitions $selected) export SWAP_PART=$T_RESULT } } } format_disks () { t_msg "Formatting partitions... ${TABCHAR}System Partition ${TABCHAR}EFI Partition " [ -b "$SYS_PART" ] && mkfs.ext4 $SYS_PART >> $logfile t_msg "Formatting partitions... ${GREEN}${TABCHAR}System partition ${CHECKMARK} (ext4) ${TABCHAR}EFI Partition " [ -b "$EFI_PART" ] && mkfs.fat -F 32 $EFI_PART >> $logfile t_msg "Formatting partitions... ${GREEN}${TABCHAR}System partition ${CHECKMARK} (ext4) ${GREEN}${TABCHAR}EFI Partition ${CHECKMARK} (fat32) " [ -b "$SWAP_PART" ] && mkswap $SWAP_PART >> $logfile return 0 } mount_disks () { t_msg "Mounting disks..." export sysroot=/xilinux.mnt export efi_mntpoint=/xilinux.mnt/boot [ ! -f "$sysroot" ] && mkdir -p $sysroot [ -b "$SYS_PART" ] && { mount $SYS_PART $sysroot true } || { t_prompt "${RED}No system partition is available!" return 1 } echo "[efi] mounting $EFI_PART to ${efi_mntpoint}" >> $logfile [ -b "$EFI_PART" ] && { mkdir -p $efi_mntpoint mount $EFI_PART $efi_mntpoint true } [ -b "$SWAP_PART" ] && swapon $SWAP_PART return 0 } prompt_kernel () { t_radio "Select which kernel you would like to use:" "linux-libre" "linux" OPT_KERNEL=$T_RESULT } bootstrap_system () { t_msg "Creating directories..." xi -qvy -r $sysroot bootstrap >> $logfile } install_base () { { t_msg "Syncing repos..." xi -qvy -r $sysroot sync >> $logfile for pkg in $default_packages $OPT_KERNEL; do t_msg "${BLUE}Installing packages...${RESET}$pkg" xi -qlvy -r $sysroot install $pkg >> $logfile done } } copy_resolvconf () { [ ! -d $sysroot/etc ] && mkdir $sysroot/etc cp /etc/resolv.conf $sysroot/etc/resolv.conf } sync_system () { t_msg "Syncing system..." xichroot $sysroot xi sync >> $logfile return 0 } generate_fstab () { t_msg "Generating fstab..." xichroot $sysroot genfstab -U / > $sysroot/etc/fstab } build_initramfs () { mkdir -p $sysroot/var/tmp for kernel_version in $(ls $sysroot/usr/lib/modules); do t_msg "Building initramfs for $kernel_version..." xichroot $sysroot dracut --kver $kernel_version 2>> $logfile >> $logfile done } install_grub () { t_yesno "Install grub?" && { [ "$OPT_BOOTSYSTEM" = "efi" ] && { target="x86_64-efi" opts="--target=$target --efi-directory=${efi_mntpoint#$sysroot}" t_yesno "Install as removable system?" && opts="--removable $opts " || true } || { target="i386-pc" opts="--target=$target $ROOT_DISK" } t_msg "Installing grub for target $target..." echo "[grub] installing with $opts" >> $logfile xichroot $sysroot grub-install $opts >> $logfile t_msg "Creating grub configuration..." sed -i "s@GRUB_CMDLINE_LINUX_DEFAULT=\".*\"@GRUB_CMDLINE_LINUX_DEFAULT=\"$cmdline\"@g" $sysroot/etc/default/grub xichroot $sysroot grub-mkconfig -o /boot/grub/grub.cfg t_prompt "Installed grub for $target" } || return 0 } configure_hostname () { t_input "Enter hostname:" local hostname=$T_RESULT echo $hostname > $sysroot/etc/hostname cat > $sysroot/etc/hosts << EOF 127.0.0.1 localhost ::1 localhost 127.0.1.1 $hostname.local $hostname EOF } enter_password () { local label=${1:-password} export passwd="" t_input_hidden "Enter $label:" passwd=$T_RESULT t_input_hidden "Confirm $label" local cpasswd=$T_RESULT [ "$passwd" = "$cpasswd" ] || { t_prompt "Passwords do not match!" enter_password $* } } configure_users () { t_input "Enter username:" local username=$T_RESULT enter_password t_msg "Creating user..." xichroot $sysroot useradd -s /bin/mksh -m $username printf "$passwd\n$passwd\n" | xichroot $sysroot passwd $username t_yesno "Allow this user to use doas?" && { mkdir -p $sysroot/etc/doas.d/ echo "permit persist david" >> $sysroot/etc/doas.d/doas.conf } t_yesno "Set a password for the root user?" && { enter_password printf "$passwd\n$passwd\n" | xichroot $sysroot passwd } return 0 } fix_permissions () { xichroot $sysroot chmod 755 / xichroot $sysroot chmod 755 /usr xichroot $sysroot chmod 755 /usr/bin xichroot $sysroot chmod 755 /usr/lib } set_timezone () { zoneinfo="$sysroot/usr/share/zoneinfo" cp $zoneinfo/$1 $sysroot/etc/localtime echo "$1" > /etc/timezone t_cls_ptrn t_prompt "Successfully set timezone!" } select_timezone () { t_cls_ptrn zoneinfo="$sysroot/usr/share/zoneinfo" selection=$1 t_paged_radio "Select your timezone: $selection" $(ls "$zoneinfo/$selection") "more..." selection="$selection/$T_RESULT" [ -f "$zoneinfo/$selection" ] && { t_yesno "Use $selection as your system timezone? " && { set_timezone $selection } || { select_timezone } return 0 } [ -d "$zoneinfo/$selection" ] && { select_timezone $selection } || { t_prompt "The timezone you entered does not exist!" select_timezone } } install_additional () { t_check "Install and configure additional services: " $additional_services local services="$T_RESULT" for service in $services; do service_$service done } service_networkmanager () { t_msg "Installing NetworkManager..." { xi -qvly -r $sysroot install networkmanager xichroot $sysroot rc-update add networkmanager } >> $logfile } service_iwd () { t_msg "Installing iwd..." { xi -qvly -r $sysroot install iwd xichroot $sysroot rc-update add iwd } >> $logfile } service_xorg () { t_msg "Installing xorg..." xi -qvly -r $sysroot install base-xorg base-fonts >> $logfile t_check "Select video drivers:" $(xi search xf86-video- | cut -f2 -d/) t_msg "Installing $T_RESULT..." [ "${#T_RESULT}" != "0" ] && xi -r $sysroot install $T_RESULT >> $logfile t_prompt "Installed basic xorg functionality TODO: preconfigured window managers, for now you need to configure them yourself" } prompt_shell () { t_yesno "Would you like to drop into the shell of your new system before rebooting?" && { t_clean xichroot $sysroot /bin/mksh -l t_init t_no_cur t_cls_ptrn } return 0 } umount_disks () { t_msg "Unmounting disks..." umount -R $sysroot [ -b "$SWAP_PART" ] && swapoff $SWAP_PART [ "$OPT_ENCRYPTION" = "lvm-luks" ] && cryptsetup close cryptlvm return 0 } prompt_reboot () { t_yesno "Install complete, reboot?" && { t_clean reboot & exit 0 } } t_init t_no_cur checkroot steps="welcome_splash detect_efi partition_disks format_disks mount_disks prompt_kernel bootstrap_system install_base build_initramfs install_grub configure_hostname select_timezone copy_resolvconf generate_fstab configure_users fix_permissions install_additional prompt_shell umount_disks prompt_reboot " $DEBUG_MODE && { echo > $logfile { tail -f $logfile | while read -r line; do t_tail $logfile done } & } for step in $steps; do t_cls_ptrn echo "[installer] doing step $step" >> $logfile $step 2>> $logfile || { t_prompt "${RED}An error occurred!" t_clean exit 1 } done t_prompt "Completed install!" t_clean