vinstaller/main.sh
2022-09-12 11:10:51 +02:00

442 lines
11 KiB
Bash
Executable File

#!/bin/bash
PASS="oem"
TIMEZONE="Europe/Berlin"
main="/tmp/vinstall"
target="/mnt"
tmp_target="${main}/tmp_target"
vars="${target}/tmp/vinstaller/vars"
files="./files"
if [ -s ./conf ]; then
. ./conf
fi
umount -R ${target}/boot/efi ${target}/boot ${target}/tmp/vinstaller/ ${target}/home ${target}
vgchange -an
cryptsetup close /dev/mapper/luks*
rm -rf $main
DEBUG=${DEBUG:=0}
if [ "${DEBUG}" == "2" ]; then
set -x
fi
mkdir -p ${target} ${tmp_target}
. ./etc/functions
target_phy_disk() {
header "Choose target disk"
local DISKS_DETAILS
local output
DISKS_DETAILS=$(lsblk -l -o KNAME,TYPE,SIZE,MODEL|grep disk)
echo "${DISKS_DETAILS}"
multiplechoice $(echo "${DISKS_DETAILS}"|awk '{print $1}')
TARGET_PHY_DISK="${output}"
}
target_phy_id() {
local i
local f2
if echo "${TARGET_PHY_DISK}" | grep -qv vd ; then
for i in $(find /dev/disk/by-id ! -type d | grep -v part); do
if realpath "${i}" | grep -q -i "${TARGET_PHY_DISK}"; then
if echo "${i}" | grep -v "wwn" | grep -q -v "eui"; then
TARGET_PHY_ID="${i}"
f2="1"
fi
fi
done
else
TARGET_PHY_ID="/dev/${TARGET_PHY_DISK}"
fi
if [ "$f2" == "1" ]; then
return 0
else
err "no id found"
fi
}
use_efi() {
if test -e /sys/firmware/efi; then
EFI=1
mount -t efivarfs efivarfs /sys/firmware/efi/efivars
else
EFI=0
fi
}
encryption_style() {
ENC=1
header "Choose encryption style"
echo "a) keyfile in initramfs" #bios: 1 uefi: 2
echo "b) no encryption" #bios: 1 uefi: 2
echo "c) no keyfile (double pw enter)" #bios: 1 uefi: 2
#echo "not implemented:"
#echo "d) keyfile on usb" #bios: 1 uefi: 2
echo "e) unencrypted boot" #bios: 2 uefi: 3 (hier fehlt noch eine)
multiplechoice "a" "b" "c" "e" #"d" "e"
ENCRYPTION_STYLE="${output}"
if [ "${ENCRYPTION_STYLE}" == "b" ]; then
ENC=0
fi
}
hibernation() {
ramsize="$(($(getconf _PHYS_PAGES) * $(getconf PAGE_SIZE) / (1024 * 1024)))"
ramsize="$(awk "BEGIN { printf(\"%.0f\n\", ${ramsize}/1024); }")"
HIBERNATE=1
if [[ "${ramsize}" -lt "2" ]]; then
SWAPSIZE="$(( ramsize * 2 ))"
SWAPSIZE="$(( ramsize * 3 ))"
elif [[ "${ramsize}" -ge "2" ]] && [[ "${ramsize}" -lt "8" ]]; then
SWAPSIZE="${ramsize}"
SWAPSIZE="$(( ramsize * 2 ))"
elif [[ "${ramsize}" -ge "8" ]] && [[ "${ramsize}" -lt "16" ]]; then
SWAPSIZE="${ramsize}"
SWAPSIZE="$(awk "BEGIN { printf(\"%.0f\n\", ${ramsize}*1.5); }")"
elif [[ "${ramsize}" -ge "16" ]]; then
SWAPSIZE="4"
echo "hibernate not recommended, turning off"
HIBERNATE="0"
SWAPSIZE="4"
fi
}
do_partition() {
header "do partition"
start=1
end=1
TARGET_PART="1"
if [ "$EFI" == "1" ]; then
parted "${TARGET_PHY_ID}" -s -- mklabel gpt
sleep 2
size=512
end=$(( start + size ))
parted "${TARGET_PHY_ID}" unit mib -s -- mkpart EFI fat32 "$start" "$end"
parted "${TARGET_PHY_ID}" unit mib -s -- set 1 esp on
TARGET_PART="2"
start="$(( end ))"
sleep 2
mkfs.vfat -I -F32 "${TARGET_PHY_ID}-part1"
if [ "${ENCRYPTION_STYLE}" == "e" ]; then
size=2048
end=$(( start + size ))
parted "${TARGET_PHY_ID}" unit mib -s -- mkpart boot ext4 "$start" "${end}"
TARGET_PART="3"
start="$(( end ))"
BOOTDEV="${TARGET_PHY_ID}-part2"
sleep 2
mkfs.btrfs -q -f "${TARGET_PHY_ID}-part2"
fi
parted "${TARGET_PHY_ID}" unit mib -s -- mkpart root ext4 "$start" 100%
dd if=/dev/random of="${TARGET_PHY_ID}-part${TARGET_PART}" bs=512 count=4
else
parted "${TARGET_PHY_ID}" -s -- mklabel msdos
sleep 2
if [ "${ENCRYPTION_STYLE}" == "e" ]; then
size=2048
end=$(( start + size ))
parted "${TARGET_PHY_ID}" unit mib -s -- mkpart primary ext4 "${start}" "${end}"
start="$(( end ))"
TARGET_PART="2"
BOOTDEV="${TARGET_PHY_ID}-part1"
sleep 2
mkfs.btrfs -q -f "${TARGET_PHY_ID}-part1"
fi
parted "${TARGET_PHY_ID}" unit mib -s -- mkpart primary ext4 "$start" -1s
dd if=/dev/random of="${TARGET_PHY_ID}-part${TARGET_PART}" bs=512 count=4
fi
sleep 2
TARGETNAME="$(basename "${TARGET_PHY_ID}-part${TARGET_PART}")"
TARGETNAME_UNDERSCORE="${TARGETNAME//-/_}"
T="${TARGET_PHY_ID}-part${TARGET_PART}"
if [ "${ENCRYPTION_STYLE}" == "a" ] || [ "${ENCRYPTION_STYLE}" == "c" ] || [ "${ENCRYPTION_STYLE}" == "d" ] || [ "${ENCRYPTION_STYLE}" == "e" ]; then
T="/dev/mapper/luks_${TARGETNAME_UNDERSCORE}"
echo -n "${PASS}" | cryptsetup luksFormat --type luks1 "${TARGET_PHY_ID}-part${TARGET_PART}" -d -
#echo -n "${PASS}" | sudo cryptsetup luksOpen /dev/sdc1 sdc1 -d -
echo -n "${PASS}" | cryptsetup luksOpen "${TARGET_PHY_ID}-part${TARGET_PART}" "luks_${TARGETNAME_UNDERSCORE}" -d -
fi
vgcreate -y --force "vg_${TARGETNAME_UNDERSCORE}" "${T}"
lvcreate -y --name swap -L "${SWAPSIZE}G" "vg_${TARGETNAME_UNDERSCORE}"
lvcreate -y --name root -l 100%FREE "vg_${TARGETNAME_UNDERSCORE}"
mkswap -f "/dev/mapper/vg_${TARGETNAME_UNDERSCORE}-swap"
mkfs.btrfs -q -f "/dev/mapper/vg_${TARGETNAME_UNDERSCORE}-root"
mount "/dev/mapper/vg_${TARGETNAME_UNDERSCORE}-root" "${target}"
btrfs subvol create "${target}/rootfs"
btrfs subvol create "${target}/homefs"
mkdir -p "${target}/system/snapshots/update" "${target}/system/snapshots/regular" "${target}/system/backup"
umount "${target}"
mount "/dev/mapper/vg_${TARGETNAME_UNDERSCORE}-root" "${target}" -o subvol=rootfs
for i in boot home dev proc sys tmp etc var/lib/backup/quelle/rootfs var/lib/backup/ziel var/db; do
mkdir -p "${target}/${i}"
done
mount "/dev/mapper/vg_${TARGETNAME_UNDERSCORE}-root" "${target}/home" -o subvol=homefs
if [ "${ENCRYPTION_STYLE}" == "e" ]; then
mkdir -p "${target}/boot" "${target}/var/lib/backup/quelle/bootfs"
mount "${BOOTDEV}" "${target}/boot"
mkdir -p "${target}/boot/system/snapshots/update" "${target}/boot/system/snapshots/regular" "${target}/boot/system/backup"
btrfs subvol create "${target}/boot/bootfs"
umount "${target}/boot"
mount "${BOOTDEV}" "${target}/boot" -o subvol=bootfs
fi
if [ "${EFI}" == "1" ]; then
mkdir -p "${target}/boot/efi"
mount "${TARGET_PHY_ID}-part1" "${target}/boot/efi"
fi
}
user() {
header "Username"
input "Username" "voiduser"
USERNAME="${output}"
}
hostname() {
header "Hostname"
input "Hostname" "voidlinux"
HOSTNAME="${output}"
}
base() {
. ./etc/base
packages
reset packages
}
profile() {
for i in $(find ./etc/profile/ -type f|sort); do
profile+=("$(basename "${i}")")
done
if [ "${#profile[@]}" -gt "1" ]; then
header "Select your Profile"
multiplechoice "${profile[@]}"
PROFILE="${output}"
elif [ "${#profile[@]}" -eq "1" ]; then
PROFILE="${profile[0]}"
fi
if ! [ "${#profile[@]}" -eq "0" ]; then
. "./etc/profile/${PROFILE}"
packages
reset packages
cp "./etc/profile/${PROFILE}" "${tmp_target}"
if [ "${need_gfx}" == "1" ]; then
readin gfx
fi
if [ "${need_xserver}" == "1" ]; then
readin xserver
fi
if [ "${need_sound}" == "1" ]; then
readin soundsystem
fi
fi
}
printing() {
header "Printing"
if yesno "Enable printing?" n; then
readin printing
fi
}
lang() {
header "Language"
multiplechoice "de" "en"
LANGUAGE_="${output}"
L1="en_US"
if [ "${LANGUAGE_}" == "de" ]; then
L1="de_DE"
fi
}
lang_key() {
header "Keyboard layout"
multiplechoice de en
L2="en"
KEY_LANG="${output}"
if [ "${KEY_LANG}" == "de" ]; then
L2="de-latin1-nodeadkeys"
fi
}
do_install() {
header "Installation"
mkdir -p "${target}/var/db/xbps/keys"
cp /var/db/xbps/keys/* "${target}/var/db/xbps/keys"
mkdir -p "${target}/etc/xbps.d"
touch "${target}/etc/xbps.d/10-ignore-pkg.conf"
for i in "${ignorepkgs[@]}"; do
echo "ignorepkg=${i}" >> "${target}/etc/xbps.d/10-ignore-pkg.conf"
done
if [ "${DEBUG}" -ge "1" ]; then
xbps-install -S -R https://alpha.de.repo.voidlinux.org/current -R https://rotce.de/pakete -r "${target}" "${pkgs[@]}"
else
xbps-install -S -y -R https://alpha.de.repo.voidlinux.org/current -R https://rotce.de/pakete -r "${target}" "${pkgs[@]}"
fi
}
echo_vars() {
echo "USERNAME=${USERNAME}"
echo "PASS=${PASS}"
echo "HOSTNAME=${HOSTNAME}"
if [ "${DEBUG}" -ge "1" ]; then
echo "EFI=${EFI}"
echo "LANGUAGE_=${LANGUAGE_}"
echo "L1=${L1}"
echo "L2=${L2}"
echo "KEY_LANG=${KEY_LANG}"
echo "TARGET_PHY_ID=${TARGET_PHY_ID}"
echo "ENCRYPTION_STYLE=${ENCRYPTION_STYLE}"
echo "TIMEZONE=${TIMEZONE}"
echo "ENC=${ENC}"
echo "HIBERNATE=${HIBERNATE}"
echo "DEBUG=${DEBUG}"
fi
}
do_chroot() {
mkdir -p "${target}/tmp/vinstaller/"
mount -t tmpfs -o size=50m tmpfs "${target}/tmp/vinstaller/"
mkdir -p "${target}/tmp/vinstaller/run"
cp ./etc/functions "${target}/tmp/vinstaller/"
cp ./etc/base "${target}/tmp/vinstaller/"
cp -rf "${tmp_target}" "${target}/tmp/vinstaller/run"
#mkdir -p "${target}/tmp/vinstaller/files"
cp -rf "${files}" "${target}/tmp/vinstaller"
echo "USERNAME=${USERNAME}" > "${vars}"
echo "PASS=${PASS}" >> "${vars}"
echo "HOSTNAME=${HOSTNAME}" >> "${vars}"
echo "EFI=${EFI}" >> "${vars}"
echo "LANGUAGE_=${LANGUAGE_}" >> "${vars}"
echo "L1=${L1}" >> "${vars}"
echo "L2=${L2}" >> "${vars}"
echo "KEY_LANG=${KEY_LANG}" >> "${vars}"
echo "TARGET_PHY_ID=${TARGET_PHY_ID}" >> "${vars}"
echo "TARGET_PART=${TARGET_PART}" >> "${vars}"
echo "TARGETNAME_UNDERSCORE=${TARGETNAME_UNDERSCORE}" >> "${vars}"
echo "ENCRYPTION_STYLE=${ENCRYPTION_STYLE}" >> "${vars}"
echo "TIMEZONE=${TIMEZONE}" >> "${vars}"
echo "ENC=${ENC}" >> "${vars}"
echo "HIBERNATE=${HIBERNATE}" >> "${vars}"
echo "DEBUG=${DEBUG}" >> "${vars}"
echo "BOOTDEV=${BOOTDEV}" >> "${vars}"
cat <<EOF > "${target}/tmp/vinstaller/main_chroot"
#!/usr/bin/bash
cd /tmp/vinstaller
FILESDIR=/tmp/vinstaller/files
. /tmp/vinstaller/functions
. /tmp/vinstaller/vars
header base
. /tmp/vinstaller/base
config
reset config
for file in \$(find /tmp/vinstaller/run -type f | sort); do
if grep -q "config()" "\${file}"; then
header \"\$(basename \$file)\"
. \$file
config
reset config
fi
done
EOF
for dir in dev proc sys run; do
mkdir -p "${target}/${dir}"
mount --rbind "/${dir}" "${target}/${dir}"
mount --make-rslave "${target}/${dir}"
done
chmod +x "${target}/tmp/vinstaller/main_chroot"
chroot "${target}" "/tmp/vinstaller/main_chroot"
}
ready() {
header "Ready!!"
echo ""
echo "pakete:"
echo "${pkgs[@]}"
echo ""
echo "ignorierte pakete:"
echo "${ignorepkgs[@]}"
echo ""
echo "vars:"
echo_vars
echo ""
}
bootloader() {
readin bootloader
}
network() {
readin network
}
add_arg_pkg() {
xbps-install -S >/dev/null
for i in "${@}"; do
if xbps-query -Rs ${i}|awk '{print $2}'|grep "^${i}" >/dev/null; then
addpkg "${@}"
else
na_pkg+=("${i}")
fi
done
if [ "${#na_pkg[@]}" -gt "0" ]; then
echo "package$(if [ ${#na_pkg[@]} -gt 1 ]; then printf "s"; fi) ${na_pkg[@]} not available"
if ! yesno "exit?" n; then
exit 1
fi
fi
}
target_phy_disk
target_phy_id
use_efi
encryption_style
hibernation
bootloader
user
lang
lang_key
hostname
base
profile
network
printing
add_arg_pkg "${@}"
header "Do installation"
rly_install=n
if [ "${DEBUG}" -ge "1" ]; then
echo_vars
rly_install=y
fi
if ! yesno "Do you really wanna install?" ${rly_install}; then
exit
fi
do_partition
do_install
do_chroot
cp -rf ~/ansible ${target}/root
ready