vinstaller/main.sh

322 lines
8.6 KiB
Bash
Raw Normal View History

2022-02-06 22:12:28 +01:00
#!/bin/bash
2022-02-08 18:08:12 +01:00
PASS="oem"
TIMEZONE="Europe/Berlin"
2022-02-07 16:56:04 +01:00
2022-02-06 22:12:28 +01:00
main="/tmp/vinstall"
target="${main}/target"
2022-02-07 05:35:12 +01:00
tmp_target="${main}/tmp_target"
2022-02-07 16:56:04 +01:00
vars="${target}/tmp/vinstaller/vars"
2022-02-06 23:25:49 +01:00
2022-02-07 18:08:48 +01:00
if [ "${DEBUG}" ]; then
2022-02-07 23:49:59 +01:00
umount -R ${target}/boot/efi ${target}/boot ${target}/tmp/vinstaller/ ${target}/home ${target}
2022-02-07 01:34:16 +01:00
vgchange -an
2022-02-07 01:35:12 +01:00
cryptsetup close /dev/mapper/luks*
2022-02-07 17:33:21 +01:00
rm -rf $main
2022-02-07 01:35:12 +01:00
fi
2022-02-07 19:29:15 +01:00
if [ "${DEBUG}" == "2" ]; then
set -x
fi
2022-02-07 01:34:16 +01:00
2022-02-07 18:08:48 +01:00
mkdir -p ${target} ${tmp_target}
2022-02-06 22:12:28 +01:00
2022-02-07 05:06:37 +01:00
. ./etc/functions
2022-02-06 22:12:28 +01:00
target_phy_disk() {
header "Choose target disk"
local DISKS_DETAILS
local output
2022-02-07 15:07:41 +01:00
DISKS_DETAILS=$(lsblk -l -o KNAME,TYPE,SIZE,MODEL|grep disk)
2022-02-07 18:08:48 +01:00
echo "${DISKS_DETAILS}"
2022-02-07 18:12:03 +01:00
multiplechoice $(echo "${DISKS_DETAILS}"|awk '{print $1}')
2022-02-07 15:12:53 +01:00
TARGET_PHY_DISK="${output}"
2022-02-06 22:12:28 +01:00
}
target_phy_id() {
local i
local f2
2022-02-07 18:08:48 +01:00
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"
2022-02-06 22:12:28 +01:00
fi
fi
done
2022-02-07 15:08:31 +01:00
if [ "$f2" == "1" ]; then
2022-02-06 22:12:28 +01:00
return 0
else
err "no id found"
fi
}
use_efi() {
if test -e /sys/firmware/efi; then
EFI=1
2022-02-07 19:25:27 +01:00
mount -t efivarfs efivarfs /sys/firmware/efi/efivars
2022-02-06 22:12:28 +01:00
else
EFI=0
fi
}
encryption_style() {
2022-02-07 16:38:46 +01:00
ENC=1
2022-02-06 22:12:28 +01:00
header "Choose encryption style"
2022-02-07 23:08:12 +01:00
echo "a) keyfile in initramfs" #bios: 1 uefi: 2
2022-02-08 18:57:09 +01:00
echo "b) no encryption" #bios: 1 uefi: 2
2022-02-07 23:08:12 +01:00
echo "c) no keyfile (double pw enter)" #bios: 1 uefi: 2
2022-02-07 05:38:43 +01:00
#echo "not implemented:"
2022-02-08 18:57:09 +01:00
#echo "d) keyfile on usb" #bios: 1 uefi: 2
#echo "e) unencrypted boot" #bios: 2 uefi: 3 (hier fehlt noch eine)
2022-02-07 23:07:46 +01:00
multiplechoice "a" "b" "c" #"d" "e"
2022-02-07 18:08:48 +01:00
ENCRYPTION_STYLE="${output}"
if [ "${ENCRYPTION_STYLE}" == "b" ]; then
2022-02-07 16:38:46 +01:00
ENC=0
fi
2022-02-06 22:12:28 +01:00
}
2022-02-06 23:25:49 +01:00
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
2022-02-07 23:07:46 +01:00
TARGET_PART="1"
2022-02-06 23:25:49 +01:00
if [ "$EFI" == "1" ]; then
2022-02-07 18:08:48 +01:00
parted "${TARGET_PHY_ID}" -s -- mklabel gpt
2022-02-07 15:27:47 +01:00
sleep 2
2022-02-07 15:20:30 +01:00
size=512
2022-02-07 18:08:48 +01:00
parted "${TARGET_PHY_ID}" unit mib -s -- mkpart EFI fat32 "$start" "$(( start + size ))"
parted "${TARGET_PHY_ID}" unit mib -s -- set 1 esp on
2022-02-07 01:36:01 +01:00
sleep 2
2022-02-07 18:08:48 +01:00
start="$(( start + size ))"
parted "${TARGET_PHY_ID}" unit mib -s -- mkpart root ext4 "$start" 100%
2022-02-08 17:14:28 +01:00
mkfs.vfat -I -F32 "${TARGET_PHY_ID}-part1"
2022-02-06 23:25:49 +01:00
TARGET_PART="2"
2022-02-08 19:00:25 +01:00
dd if=/dev/random of="${TARGET_PHY_ID}-part2" bs=512 count=4
2022-02-06 23:25:49 +01:00
else
2022-02-07 18:33:17 +01:00
parted "${TARGET_PHY_ID}" -s -- mklabel msdos
2022-02-07 15:27:47 +01:00
sleep 2
2022-02-08 18:57:09 +01:00
if [ "${ENCRYPTION_STYLE}" == "e" ]; then
size=2048
parted "${TARGET_PHY_ID}" unit mib -s -- mkpart primary ext4 "${start}" "$(( start + size ))"
sleep 2
mkfs.btrfs -q -f "${TARGET_PHY_ID}-part1"
start="$(( start + size ))"
TARGET_PART="2"
fi
parted "${TARGET_PHY_ID}" unit mib -s -- mkpart primary ext4 "$start" -1s
2022-02-08 19:00:25 +01:00
dd if=/dev/random of="${TARGET_PHY_ID}-part${TARGET_PART}" bs=512 count=4
2022-02-06 23:25:49 +01:00
fi
2022-02-07 01:36:01 +01:00
sleep 2
2022-02-07 18:08:48 +01:00
TARGETNAME="$(basename "${TARGET_PHY_ID}-part${TARGET_PART}")"
TARGETNAME_UNDERSCORE="${TARGETNAME//-/_}"
2022-02-07 23:12:05 +01:00
T="${TARGET_PHY_ID}-part${TARGET_PART}"
2022-02-07 23:07:46 +01:00
if [ "${ENCRYPTION_STYLE}" == "a" ] || [ "${ENCRYPTION_STYLE}" == "c" ] || [ "${ENCRYPTION_STYLE}" == "d" ]; then
2022-02-07 18:08:48 +01:00
T="/dev/mapper/luks_${TARGETNAME_UNDERSCORE}"
2022-02-08 19:02:57 +01:00
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 -
2022-02-07 06:15:46 +01:00
fi
2022-02-08 17:37:40 +01:00
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}"
2022-02-07 18:08:48 +01:00
mkswap -f "/dev/mapper/vg_${TARGETNAME_UNDERSCORE}-swap"
2022-02-08 18:57:09 +01:00
mkfs.btrfs -q -f "/dev/mapper/vg_${TARGETNAME_UNDERSCORE}-root"
2022-02-07 18:08:48 +01:00
mount "/dev/mapper/vg_${TARGETNAME_UNDERSCORE}-root" "${target}"
2022-02-07 23:49:59 +01:00
btrfs subvol create "${target}/rootfs"
btrfs subvol create "${target}/homefs"
2022-02-08 18:57:09 +01:00
mkdir -p "${target}/system/snapshots/update" "${target}/system/snapshots/regular" "${target}/system/backup"
2022-02-07 18:08:48 +01:00
umount "${target}"
2022-02-07 23:49:59 +01:00
mount "/dev/mapper/vg_${TARGETNAME_UNDERSCORE}-root" "${target}" -o subvol=rootfs
2022-02-08 18:58:15 +01:00
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
2022-02-07 18:08:48 +01:00
if [ "${EFI}" == "1" ]; then
mkdir -p "${target}/boot/efi"
mount "${TARGET_PHY_ID}-part1" "${target}/boot/efi"
2022-02-06 22:12:28 +01:00
else
2022-02-08 18:57:09 +01:00
if [ "${ENCRYPTION_STYLE}" == "e" ]; then
mkdir -p "${target}/boot" "${target}/var/lib/backup/quelle/bootfs"
2022-02-07 18:08:48 +01:00
mount "${TARGET_PHY_ID}-part1" "${target}/boot"
2022-02-08 18:57:09 +01:00
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 "${TARGET_PHY_ID}-part1" "${target}/boot" -o subvol=bootfs
2022-02-06 23:25:49 +01:00
fi
2022-02-06 22:12:28 +01:00
fi
2022-02-07 03:32:29 +01:00
2022-02-08 18:08:12 +01:00
2022-02-06 22:12:28 +01:00
}
2022-02-07 02:34:12 +01:00
user() {
2022-02-07 16:43:23 +01:00
header "Username"
2022-02-07 04:19:38 +01:00
input "Username" "voiduser"
2022-02-07 18:08:48 +01:00
USERNAME="${output}"
2022-02-07 02:34:12 +01:00
}
2022-02-06 23:25:49 +01:00
2022-02-07 03:32:29 +01:00
hostname() {
2022-02-07 16:43:23 +01:00
header "Hostname"
2022-02-07 04:19:38 +01:00
input "Hostname" "voidlinux"
2022-02-07 18:08:48 +01:00
HOSTNAME="${output}"
2022-02-07 03:32:29 +01:00
}
base() {
2022-02-07 03:50:46 +01:00
. ./etc/base
2022-02-07 03:32:29 +01:00
packages
reset packages
}
2022-02-07 05:35:12 +01:00
profile() {
2022-02-07 17:23:22 +01:00
for i in $(find ./etc/profile/ -type f|sort); do
2022-02-07 18:08:48 +01:00
profile+=("$(basename "${i}")")
2022-02-07 05:35:12 +01:00
done
if [ "${#profile[@]}" -gt "1" ]; then
2022-02-07 16:43:23 +01:00
header "Select your Profile"
2022-02-07 05:35:12 +01:00
multiplechoice "${profile[@]}"
2022-02-07 18:08:48 +01:00
PROFILE="${output}"
2022-02-07 05:35:12 +01:00
elif [ "${#profile[@]}" -eq "1" ]; then
2022-02-07 18:08:48 +01:00
PROFILE="${profile[0]}"
2022-02-07 05:35:12 +01:00
fi
if ! [ "${#profile[@]}" -eq "0" ]; then
2022-02-07 18:08:48 +01:00
. "./etc/profile/${PROFILE}"
2022-02-07 04:18:16 +01:00
packages
reset packages
2022-02-07 18:08:48 +01:00
cp "./etc/profile/${PROFILE}" "${tmp_target}"
if [ "${need_gfx}" == "1" ]; then
2022-02-07 17:58:29 +01:00
readin gfx
2022-02-07 05:35:12 +01:00
fi
2022-02-07 22:50:49 +01:00
if [ "${need_xserver}" == "1" ]; then
readin xserver
fi
2022-02-07 18:08:48 +01:00
if [ "${need_sound}" == "1" ]; then
2022-02-07 17:58:29 +01:00
readin soundsystem
2022-02-07 05:35:12 +01:00
fi
2022-02-07 02:34:12 +01:00
fi
}
2022-02-06 23:25:49 +01:00
2022-02-07 02:34:12 +01:00
lang() {
2022-02-07 16:43:23 +01:00
header "Sprache"
2022-02-07 04:09:57 +01:00
multiplechoice "de" "en"
2022-02-07 18:08:48 +01:00
LANGUAGE="${output}"
if [ "${LANGUAGE}" == "de" ]; then
L1="de_DE"
2022-02-08 18:08:12 +01:00
L2="de-latin1-nodeadkeys"
2022-02-07 02:34:12 +01:00
fi
}
do_install() {
2022-02-07 16:43:23 +01:00
header "Installation"
2022-02-07 18:08:48 +01:00
mkdir -p "${target}/var/db/xbps/keys"
cp /var/db/xbps/keys/* "${target}/var/db/xbps/keys"
2022-02-08 19:59:23 +01:00
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"
2022-02-08 19:59:51 +01:00
done
2022-02-07 18:08:48 +01:00
xbps-install -Sy -R https://alpha.de.repo.voidlinux.org/current -r "${target}" "${pkgs[@]}"
2022-02-07 02:34:12 +01:00
}
2022-02-07 03:32:29 +01:00
do_chroot() {
2022-02-07 16:43:23 +01:00
header "Chroot into new System"
2022-02-07 18:08:48 +01:00
mkdir -p "${target}/tmp/vinstaller/"
2022-02-07 16:10:15 +01:00
2022-02-07 18:08:48 +01:00
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/"
2022-02-07 03:42:38 +01:00
2022-02-07 18:08:48 +01:00
cp -rf "${tmp_target}" "${target}/tmp/vinstaller/run"
2022-02-07 03:42:38 +01:00
2022-02-07 20:49:23 +01:00
echo "USERNAME=${USERNAME}" > "${vars}"
2022-02-08 18:08:12 +01:00
echo "PASS=${PASS}" >> "${vars}"
2022-02-07 20:49:23 +01:00
echo "HOSTNAME=${HOSTNAME}" >> "${vars}"
echo "EFI=${EFI}" >> "${vars}"
echo "LANGUAGE=${LANGUAGE}" >> "${vars}"
echo "L1=${L1}" >> "${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}"
2022-02-08 18:08:12 +01:00
echo "TIMEZONE=${TIMEZONE}" >> "${vars}"
2022-02-07 20:49:23 +01:00
echo "ENC=${ENC}" >> "${vars}"
echo "HIBERNATE=${HIBERNATE}" >> "${vars}"
2022-02-07 03:32:29 +01:00
2022-02-07 18:08:48 +01:00
cat <<EOF > "${target}/tmp/vinstaller/main_chroot"
2022-02-07 17:22:26 +01:00
#!/usr/bin/bash
2022-02-07 05:06:12 +01:00
. /tmp/vinstaller/functions
2022-02-07 16:07:10 +01:00
. /tmp/vinstaller/vars
2022-02-07 18:27:12 +01:00
header base
2022-02-07 16:07:10 +01:00
. /tmp/vinstaller/base
2022-02-07 18:27:12 +01:00
config
reset config
2022-02-07 16:07:10 +01:00
2022-02-07 03:50:46 +01:00
for file in \$(find /tmp/vinstaller/run -type f); do
2022-02-07 19:10:58 +01:00
if grep -q "config()" "\${file}"; then
header \"\$(basename \$file)\"
. \$file
config
reset config
fi
2022-02-07 03:32:29 +01:00
done
EOF
2022-02-07 04:42:31 +01:00
2022-02-08 18:08:12 +01:00
for dir in dev proc sys run; do
mkdir -p "${target}/${dir}"
mount --rbind "/${dir}" "${target}/${dir}"
mount --make-rslave "${target}/${dir}"
done
2022-02-07 18:08:48 +01:00
chmod +x "${target}/tmp/vinstaller/main_chroot"
2022-02-07 18:22:59 +01:00
chroot "${target}" "/tmp/vinstaller/main_chroot"
2022-02-07 03:32:29 +01:00
}
2022-02-07 16:15:28 +01:00
ready() {
2022-02-07 16:43:23 +01:00
header "Ready!!"
2022-02-07 16:15:28 +01:00
echo ""
2022-02-07 16:47:53 +01:00
echo "pakete:"
echo "${pkgs[@]}"
2022-02-07 23:53:57 +01:00
echo ""
echo "vars:"
2022-02-07 17:21:28 +01:00
cat ${vars}
2022-02-07 23:53:57 +01:00
echo ""
2022-02-07 16:15:28 +01:00
}
2022-02-07 02:34:12 +01:00
target_phy_disk
target_phy_id
use_efi
encryption_style
hibernation
2022-02-07 17:58:29 +01:00
readin bootloader
2022-02-07 02:34:12 +01:00
user
2022-02-07 16:56:04 +01:00
lang
2022-02-07 03:32:29 +01:00
hostname
2022-02-07 02:34:12 +01:00
base
2022-02-07 05:35:38 +01:00
profile
2022-02-07 17:58:29 +01:00
readin network
readin printing
2022-02-07 00:29:48 +01:00
do_partition
2022-02-07 03:32:29 +01:00
do_install
2022-02-07 15:07:41 +01:00
do_chroot
2022-02-07 23:49:59 +01:00
ready