windvn/mods/live-installer/data/bin/login
2022-11-07 01:27:00 +01:00

250 lines
6.9 KiB
Bash
Executable File

#!/bin/bash
export PATH="/sbin:/usr/sbin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
export HOME="/root"
function wdvn-chroot {
(
set -e
mount -o bind "/dev" "$1/dev"
mount -o bind "/dev/pts" "$1/dev/pts"
mount -o bind "/sys" "$1/sys"
mount -t proc none "$1/proc"
chroot "$1" ${@:2}
)
if [ $? != 0 ]; then
wdvn-chroot-end "$1"
exit 1
fi
wdvn-chroot-end "$1"
}
function wdvn-chroot-end {
umount -l "$1/proc"
umount -l "$1/sys"
umount -l "$1/dev/pts"
umount -l "$1/dev"
}
CHOICE=1
while true; do
dpkg-reconfigure keyboard-configuration
service console-setup.sh restart
udevadm trigger --subsystem-match=input --action=change
service keyboard-setup.sh restart
clear
echo "Welcome to windvn!"
echo "1: Fast install"
echo "2: Open bash"
echo "3: Reboot"
echo "4: Shut down"
read -p "Choice: " CHOICE
echo ""
if [ "$CHOICE" == "1" ]; then
(
set -e
YN="n"
while [ "$YN" == "n" ]; do
echo ""
lsblk -d -o PATH,SIZE
read -p "Device: " DEVICE
echo ""
lsblk "$DEVICE" -o NAME,SIZE,FSTYPE,PARTLABEL
read -p "All data on this disk will be erased! Continue? [y,n]: " YN
YN="$(echo "$YN" | tr "[:upper:]" "[:lower:]")"
if [ "$YN" != "y" ]; then
YN="n"
fi
done
# Ask for encryption
WDVN_ENCRYPT=""
WDVN_ENCRYPT_PASS=""
while true; do
read -p "Encrypt the disk? [y,n]: " WDVN_ENCRYPT
WDVN_ENCRYPT="$(echo "$WDVN_ENCRYPT" | tr "[:upper:]" "[:lower:]")"
if [ "$WDVN_ENCRYPT" == "y" ]; then
apt -y install cryptsetup
echo ""
loadkeys us
while true; do
read -s -p "Passphrase: " WDVN_ENCRYPT_PASS
echo ""
read -s -p "Confirm passphrase: " WDVN_ENCRYPT_PASS_CONFIRM
echo ""
if [ "$WDVN_ENCRYPT_PASS" == "$WDVN_ENCRYPT_PASS_CONFIRM" ]; then
break
fi
echo "Passwords don't match."
echo ""
done
service console-setup.sh restart
udevadm trigger --subsystem-match=input --action=change
service keyboard-setup.sh restart
break
fi
if [ "$WDVN_ENCRYPT" == "n" ]; then
break
fi
done
# Create partitions
echo "Creating partitions ..."
wipefs --all "$DEVICE"
sgdisk -n 1:2048:+1M -c 1:"BIOS boot partition" -t 1:ef02 "$DEVICE"
sgdisk -n 2::+512M -c 2:"EFI System" -t 2:ef00 "$DEVICE"
sgdisk -n 3::-0 -c 3:"Linux filesystem" -t 3:8300 "$DEVICE"
sgdisk --hybrid=1:2:3 "$DEVICE"
PART_EFI="$(echo "${DEVICE}"*2)"
PART_DATA="$(echo "${DEVICE}"*3)"
DEV_DATA="$PART_DATA"
# Additional passphrases
if [ "$WDVN_ENCRYPT" == "y" ]; then
echo "$WDVN_ENCRYPT_PASS" | cryptsetup luksFormat --type luks1 "$PART_DATA"
while true; do
echo ""
read -p "Add another passphrase? [y,n]: " CHOICE
CHOICE="$(echo "$CHOICE" | tr "[:upper:]" "[:lower:]")"
if [ "$CHOICE" == "y" ]; then
loadkeys us
while true; do
read -s -p "Passphrase: " WDVN_ENCRYPT_PASS_ADD
echo ""
read -s -p "Confirm passphrase: " WDVN_ENCRYPT_PASS_ADD_CONFIRM
echo ""
if [ "$WDVN_ENCRYPT_PASS_ADD" == "$WDVN_ENCRYPT_PASS_ADD_CONFIRM" ]; then
echo "$WDVN_ENCRYPT_PASS
$WDVN_ENCRYPT_PASS_ADD" | cryptsetup luksAddKey "$PART_DEV"
break
fi
echo "Passwords don't match."
echo ""
done
service console-setup.sh restart
udevadm trigger --subsystem-match=input --action=change
service keyboard-setup.sh restart
fi
if [ "$CHOICE" == "n" ]; then
break
fi
done
echo "$WDVN_ENCRYPT_PASS" | cryptsetup luksOpen "$PART_DATA" system
PART_DATA="/dev/mapper/system"
fi
# Format partitions
echo "Formatting partitions ..."
mkfs.fat -F32 "$PART_EFI"
mkfs.ext4 "$PART_DATA"
# Mount target partition
echo "Mounting partitions ..."
mkdir -p "/media/install"
mount "$PART_DATA" "/media/install"
mkdir -p "/media/install/efi"
mount "$PART_EFI" "/media/install/efi"
# Creating swap
echo "Creating swap ..."
dd if=/dev/zero of="/media/install/swap" bs=1M count=512 status=progress
chmod 600 "/media/install/swap"
mkswap "/media/install/swap"
swapon "/media/install/swap"
# Add files
echo "Adding files ..."
unsquashfs -f -d "/media/install" "/lib/live/mount/medium/live/filesystem.squashfs"
if [ "$WDVN_ENCRYPT" == "y" ]; then
echo "GRUB_ENABLE_CRYPTODISK=y" > "/media/install/etc/default/grub.d/cryptodisk.cfg"
fi
# Set hostname
echo "Setting hostname ..."
echo "$(head -c4 </dev/urandom|xxd -p -u)" >"/media/install/etc/hostname"
# Write fstab
UUID_PART_DATA="$(blkid -o value -s UUID "$PART_DATA")"
UUID_DEV_DATA="$(blkid -o value -s UUID "$DEV_DATA")"
echo "Writing fstab ..."
if [ "$WDVN_ENCRYPT" == "y" ]; then
echo "GRUB_CMDLINE_LINUX=\"\$GRUB_CMDLINE_LINUX cryptdevice=UUID=$UUID_DEV_DATA root=UUID=$UUID_PART_DATA\"" >> "/media/install/etc/default/grub.d/cryptodisk.cfg"
fi
if [ "$WDVN_ENCRYPT" == "y" ]; then
echo "system UUID=$UUID_DEV_DATA none luks" >"media/install/etc/crypttab"
fi
echo "UUID=$UUID_PART_DATA / ext4 errors=remount-ro 0 1" >>"/media/install/etc/fstab"
echo "/swap none swap sw 0 0" >>"/media/install/etc/fstab"
# Remove live-specific packages
echo "Removing live-specific packages ..."
wdvn-chroot "/media/install" apt -y remove live-boot live-boot-initramfs-tools
wdvn-chroot "/media/install" apt -y autoremove
if [ "$WDVN_ENCRYPT" == "y" ]; then
wdvn-chroot "/media/install" apt -y install cryptsetup-initramfs
fi
wdvn-chroot "/media/install" update-initramfs -u
rm "/media/install/bin/login"
mv "/media/install/bin/login.oobe" "/media/install/bin/login"
# Install GRUB
echo "Installing grub ..."
wdvn-chroot "/media/install" grub-install --target=i386-efi --uefi-secure-boot --efi-directory="/efi" --boot-directory="/boot" "$DEVICE"
wdvn-chroot "/media/install" grub-install --target=x86_64-efi --uefi-secure-boot --efi-directory="/efi" --boot-directory="/boot" "$DEVICE"
wdvn-chroot "/media/install" grub-install --target=i386-pc --boot-directory="/boot" "$DEVICE"
wdvn-chroot "/media/install" update-grub
# Unmount
echo "Unmounting ..."
swapoff "/media/install/swap"
umount -l "/media/install/efi"
rmdir "/media/install/efi"
umount -l "/media/install"
rmdir "/media/install"
if [ "$WDVN_ENCRYPT" == "y" ]; then
cryptsetup luksClose system
fi
echo ""
echo Success. Press ENTER to quit setup.
read
)
if [ "$?" != "0" ]; then
echo ERROR. Cleaning up...
swapoff "/media/install/swap"
umount -l "/media/install/efi"
umount -l "/media/install"
cryptsetup luksClose system
echo ""
echo An error occured. Press ENTER to quit setup.
read
fi
fi
if [ "$CHOICE" == "2" ]; then
bash
fi
if [ "$CHOICE" == "3" ]; then
reboot
exit
fi
if [ "$CHOICE" == "4" ]; then
poweroff
exit
fi
echo ""
done