From d9136e52d8176ff0eba47c60fc8f6dbe674eb7c0 Mon Sep 17 00:00:00 2001 From: Ralf Zerres Date: Fri, 31 Jan 2020 13:19:13 +0100 Subject: [PATCH] arch-create-system: function update Signed-off-by: Ralf Zerres --- usr/bin/arch-create-system | 236 +++++++++++++++++++++++++------------ 1 file changed, 163 insertions(+), 73 deletions(-) diff --git a/usr/bin/arch-create-system b/usr/bin/arch-create-system index d26bbf6..93932c8 100755 --- a/usr/bin/arch-create-system +++ b/usr/bin/arch-create-system @@ -32,6 +32,7 @@ verbose=0 mount_point="/mnt" label_prefix="BTRFS" +root_password="password" disk_dev_root="/dev/sda" disk_label_root="System" disk_uuid_root= @@ -55,7 +56,7 @@ disk_label_machines="System" disk_uuid_machines= disk_mount_machines="/var/lib/machines" -disk_label_uefi=UEFI +disk_label_uefi="UEFI" disk_mount_uefi="/boot" # ascii color @@ -93,7 +94,7 @@ create_bootloader () { local cmd # Needs to be executed in chroot environment (target) - cmd="bootctl --path=boot install" + cmd="bootctl install" if [ $dryrun -eq 1 ]; then printf "${MAGENTA}Would run ${GREEN}'%s'${NO_COLOR}\n" "$cmd" else @@ -133,9 +134,11 @@ create_btrfs () { /dev/disk/by-partlabel/$LABEL" cmd=`echo $cmd | tr -s "[:blank:]"` if [ $dryrun -eq 1 ]; then - printf "${MAGENTA}Would run ${GREEN}'%s'${NO_COLOR}\n" \ + printf "${MAGENTA}Would run: ${GREEN}'%s'${NO_COLOR}\n" \ "$cmd" else + #printf "${MAGENTA}Run: ${GREEN}'%s'${NO_COLOR}\n" \ + # "$cmd" eval "$cmd" fi @@ -179,7 +182,7 @@ create_disk () { while expr $PARTITION '<=' $NR_PARTITIONS >>/dev/null; do if [ $PARTITION '=' 1 ] && [ $NR_PARTITIONS '>' 1 ]; then if [ $dryrun -eq 1 ]; then - printf "${MAGENTA}Would create ${GREEN}UEFI-Partition '%s'${NO_COLOR}\n" $PARTITION + printf "${MAGENTA}Would create ${GREEN}${disk_label_uefi}-Partition '%s'${NO_COLOR}\n" $PARTITION else # create UEFI partitions sgdisk --new=$PARTITION:2048:+512M $TARGET @@ -195,7 +198,7 @@ create_disk () { continue else if [ $dryrun -eq 1 ]; then - printf "${MAGENTA}Would create ${GREEN}OS-Partition '%s'${NO_COLOR}\n" $PARTITION + printf "${MAGENTA}Would create ${GREEN}${LABEL}-Partition '%s'${NO_COLOR}\n" $PARTITION else # create partitions sgdisk --new=$PARTITION $TARGET @@ -211,10 +214,18 @@ create_disk () { fi done + # reread partition-table + cmd="partprobe $TARGET" + if [ $dryrun -eq 1 ]; then + printf "${MAGENTA}Would reread partition table ${GREEN}'%s'${NO_COLOR}\n" $TARGET + else + eval "$cmd" + fi + # dump settings cmd="sgdisk -p $TARGET" if [ $dryrun -eq 1 ]; then - printf "${MAGENTA}Would print created partition table for target ${GREEN}'%s'${NO_COLOR}\n" $TARGET + printf "${MAGENTA}Would created partition table for target ${GREEN}'%s'${NO_COLOR}\n" $TARGET else eval "$cmd" fi @@ -230,25 +241,22 @@ create_fs_structure () { printf "${BLUE}Prepare filesystem structure${GREEN}'subvol=%s'${NO_COLOR}\n" \ "$SUBVOL" - cmd="mount_target $MOUNT_POINT /dev/disk/by-partlabel/$LABEL btrfs $SUBVOL" + cmd="mount_target /dev/disk/by-partlabel/$LABEL $MOUNT_POINT btrfs $SUBVOL" if [ $dryrun -eq 1 ]; then printf "${MAGENTA}Would run ${GREEN}'%s'${NO_COLOR}\n" "$cmd" else + printf "${MAGENTA}Run ${GREEN}'%s'${NO_COLOR}\n" "$cmd" eval "$cmd" + if [ $? -gt 0 ]; then + return 1 + fi fi - if [ $LABEL="OS" ]; then + if [ $LABEL=$disk_label_root ]; then printf "${BLUE}Create target filesystem structure${NO_COLOR}\n" - cmd="mkdir -p $MOUNT_POINT/var/lib" - if [ $dryrun -eq 1 ]; then - printf "${MAGENTA}Would run ${GREEN}'%s'${NO_COLOR}\n" "$cmd" - else - eval "$cmd" - fi - - for subdir in log cache; do - cmd="btrfs subvolume create $MOUNT_POINT/var/$subdir" + for subdir in lib; do + cmd="mkdir -p $MOUNT_POINT/var/$subdir" if [ $dryrun -eq 1 ]; then printf "${MAGENTA}Would run ${GREEN}'%s'${NO_COLOR}\n" "$cmd" else @@ -259,12 +267,14 @@ create_fs_structure () { # Subvolumes storing custom data printf "${BLUE} - Create btrfs subvolumes${NO_COLOR}\n" - for subvol in boot root home data var/lib/machines; do + for subvol in boot root home data var/cache var/log var/lib/machines ; do cmd="btrfs subvolume create $MOUNT_POINT/$subvol" if [ $dryrun -eq 1 ]; then printf "${MAGENTA}Would run ${GREEN}'%s'${NO_COLOR}\n" \ "$cmd" else + printf "${MAGENTA}create btrfs subvol ${GREEN}'%s'${MAGENTA} at ${GREEN}'%s'${NO_COLOR}\n" \ + "$subvol" "$MOUNT_POINT" eval "$cmd" fi done @@ -272,7 +282,7 @@ create_fs_structure () { create_swapfs $PREFIX $MOUNT_POINT fi - cmd="umount $MOUNT_POINT" + cmd="umount --recursive $MOUNT_POINT" if [ $dryrun -eq 1 ]; then printf "${MAGENTA}Would run ${GREEN}'%s'${NO_COLOR}\n" "$cmd" else @@ -296,7 +306,7 @@ create_swapfs () { local cmd - printf "${BLUE}Create target swapfs${NO_COLOR}\n" + printf "${BLUE}create_swapfs${NO_COLOR}\n" # create a subvolume for the swapfs file cmd="btrfs subvol create $MOUNT_POINT/swap" @@ -395,6 +405,9 @@ create_vfat () { die () { error "$@" + if [ $dryrun -eq 1 ]; then + printf "${RED}Error${GREEN}'%s'${NO_COLOR}\n" "$@" + fi exit 1 } @@ -536,27 +549,46 @@ get_config_disk_names () { fi } -mount_target_filesytems () { +mount_target_filesystems () { local LABEL=$1 local MOUNT_POINT=$2 - printf "${BLUE}Install target system ${GREEN}'%s' -> '%s'${NO_COLOR}\n" "$LABEL" "$MOUNT_POINT" + printf "${BLUE}mount_target_filesystems${NO_COLOR}\n" - mount_target $MOUNT_POINT /dev/disk/by-partlabel/$LABEL btrfs root + mount_target /dev/disk/by-partlabel/$disk_label_root $MOUNT_POINT/ btrfs root for subdir in boot data home swap var/log var/cache var/lib/machines; do - mkdir -p $MOUNT_POINT/$subdir + if [ ! -d $MOUNT_POINT/$subdir ]; then + mkdir -p $MOUNT_POINT/$subdir + fi done - mount_target $MOUNT_POINT/boot /dev/disk/by-partlabel/$disk_label_uefi fat - mount_target $MOUNT_POINT/data /dev/disk/by-partlabel/$disk_label_data btrfs / - mount_target $MOUNT_POINT/var/cache /dev/disk/by-partlabel/$LABEL btrfs var/cache - mount_target $MOUNT_POINT/var/lib/machines /dev/disk/by-partlabel/$disk_label_machines btrfs / - mount_target $MOUNT_POINT/var/log /dev/disk/by-partlabel/$LABEL btrfs var/log + mount_target /dev/disk/by-partlabel/$disk_label_uefi $MOUNT_POINT/boot fat + mount_target /dev/disk/by-partlabel/$disk_label_data $MOUNT_POINT/data btrfs data + mount_target /dev/disk/by-partlabel/$disk_label_root $MOUNT_POINT/var/cache btrfs var/cache + mount_target /dev/disk/by-partlabel/$disk_label_machines $MOUNT_POINT/var/lib/machines btrfs var/lib/machines + mount_target /dev/disk/by-partlabel/$disk_label_root $MOUNT_POINT/var/log btrfs var/log +} + +install_target () { + local MOUNT_POINT=$1 + + local cmd + + printf "${BLUE}Install target ${GREEN}'%s' -> '%s'${NO_COLOR}\n" \ + "$MOUNT_POINT" + + # change new root to target + cmd="arch-chroot $MOUNT_POINT arch-create-system install_target" + if [ $dryrun -eq 1 ]; then + printf "${MAGENTA}Would run ${GREEN}'%s'${NO_COLOR}\n" "$cmd" + else + eval "$cmd" + fi } install_target_packages () { - local LABEL=$1 + local MOUNT_POINT=$1 local cmd @@ -572,7 +604,9 @@ install_target_packages () { fi # Basic packages - cmd="pacstrap $MOUNT_POINT arch-install-scripts base btrfs-progs intel-ucode linux linux-firmware dosfstools iptables-nft man sudo" + # "TODO: systemd-firstboot" + cmd="pacstrap $MOUNT_POINT arch-install-scripts base btrfs-progs gdisk intel-ucode \ + linux linux-firmware dosfstools iptables-nft man sudo" if [ $dryrun -eq 1 ]; then printf "${MAGENTA}Would run ${GREEN}'%s'${NO_COLOR}\n" "$cmd" else @@ -595,15 +629,22 @@ install_target_packages () { eval "$cmd" fi - cmd="cp $packagename $MOUNT_POINT/usr/bin/" + install_script=`command -v $progname` + cmd="cp $install_script $MOUNT_POINT/usr/bin/" if [ $dryrun -eq 1 ]; then printf "${MAGENTA}Would run ${GREEN}'%s'${NO_COLOR}\n" "$cmd" else - eval "$cmd" + if [ -x $install_script ]; then + eval "$cmd" + else + die "Can't find '$progname' in default path\n" + fi fi } install_target () { + printf "${BLUE}Install target${NO_COLOR}\n" "$TARGET" + # Needs to be executed in chroot environment (target) prepare_mount_units @@ -618,16 +659,13 @@ install_target () { #systemctl enable hvkvpdaemon.service #systemctl enable hvvssdaemon.service - # if you dislike systemd mount units + # if you dislike systemd mount units uncomment next line #genfstab / >/etc/fstab - - # return to calling process - exit 0 } mount_target () { - local MOUNT_POINT=$1 - local DEV_NAME=$2 + local DEV_NAME=$1 + local MOUNT_POINT=$2 local FS_TYPE=$3 local FS_SUBVOL=$4 @@ -644,6 +682,16 @@ mount_target () { "$FS_SUBVOL" "$DEV_NAME" "$MOUNT_POINT" fi ;; + "fat") + if [ -h $DEV_NAME ]; then + # we are mounting a vfat for gain long filenames + cmd="mount -t vfat \ + $DEV_NAME \ + $MOUNT_POINT" + printf "${BLUE}mounting ${GREEN}'%s' ${BLUE}at ${GREEN}'%s${NO_COLOR}\n" \ + "$DEV_NAME" "$MOUNT_POINT" + fi + ;; "vfat") if [ -h $DEV_NAME ]; then cmd="mount -t $FS_TYPE \ @@ -657,7 +705,8 @@ mount_target () { cmd=`echo $cmd | tr -s "[:blank:]"` if [ $dryrun -eq 1 ]; then - printf "${MAGENTA}Would run ${GREEN}'%s'${NO_COLOR}\n" "$cmd" + printf "${MAGENTA}Would mount ${GREEN}'%s'${MAGENTA} of ${GREEN}'%s'${MAGENTA} at ${GREEN}'%s'${NO_COLOR}\n" \ + "$FS_SUBVOL" "$DEV_NAME" "$MOUNT_POINT" else eval "$cmd" fi @@ -748,27 +797,27 @@ parse_params () { shift $count cmd=get-config-disk-names ;; - --disk_label_data) + --disk-label-data) disk_label_data="$2" shift 2 ;; - --disk_label_machines) + --disk-label-machines) disk_label_machines="$2" shift 2 ;; - --disk_label_root) + --disk-label-root) disk_label_root="$2" shift 2 ;; - --disk_data) + --disk-data) disk_dev_data="$2" shift 2 ;; - --disk_machines) + --disk-machines) disk_dev_machines="$2" shift 2 ;; - --disk_root) + --disk-root) disk_dev_root="$2" shift 2 ;; @@ -776,7 +825,11 @@ parse_params () { dryrun=1 shift 1 ;; - -i|--install|--install-target) + -i|--install) + shift 1 + cmd=install + ;; + --install-target) shift 1 cmd=install-target ;; @@ -784,7 +837,7 @@ parse_params () { mount_point="$2" shift 2 ;; - --labelprefix) + --label-prefix) label_prefix="$2" shift 2 ;; @@ -838,7 +891,7 @@ parse_params () { disk_dev_root=${1#*=} shift ;; - --labelprefix=*) + --label-prefix=*) label_prefix=${1#*=} shift ;; @@ -877,7 +930,6 @@ parse_params () { } prepare_bootloader () { - # check for accessible uefi partition ls /sys/firmware/efi/efivars > /dev/null if [ $? -eq 0 ]; then @@ -898,7 +950,7 @@ prepare_bootloader () { "linux /vmlinuz-linux" \ "initrd /intel-ucode.img" \ "initrd /initramfs-linux.img" \ - "options rootflags=subvol=root root=LABEL=BTRFS-OS" \ + "options rootflags=subvol=root root=LABEL=${label_prefix}-${disk_label_root}" \ > /boot/loader/entries/arch.conf bootctl status @@ -910,6 +962,12 @@ prepare_locale () { hostnamectl set-hostname $LABEL-lin01 localectl set-keymap de-latin1-nodeadkeys + #systemd-firstboot --root-password-file= \ + #systemd-firstboot --root-password=$root_password \ +# --hostname=$LABEL-lin01 \ +# --timezone="Europe/Berlin" \ +# --locale="de_DE.UTF8" \ +# --keymap=de-latin1 } prepare_mount_units () { @@ -1041,6 +1099,8 @@ prepare_network_units () { } prepare_target_disks () { + printf "${BLUE}prepare_target_disks${NO_COLOR}\n" + if [ ${#disk_dev_root} -gt 1 ]; then create_disk ${disk_dev_root} $disk_label_root $label_prefix 2 fi @@ -1053,12 +1113,24 @@ prepare_target_disks () { } prepare_target_filesystems () { - create_fat $disk_label_uefi $label_prefix $mount_point - create_btrfs $disk_label_root $label_prefix $mount_point - create_btrfs $disk_label_machines $label_prefix $mount_point - create_btrfs $disk_label_data $label_prefix $mount_point + printf "${BLUE}prepare_target_filesystems${NO_COLOR}\n" + + if [ ${#disk_dev_root} -gt 1 ]; then + create_fat $disk_label_uefi $label_prefix $mount_point + create_btrfs $disk_label_root $label_prefix $mount_point + fi + if [ ${#disk_dev_machines} -gt 1 ]; then + create_btrfs $disk_label_machines $label_prefix $mount_point + fi + if [ ${#disk_dev_data} -gt 1 ]; then + create_btrfs $disk_label_data $label_prefix $mount_point + fi create_fs_structure $disk_label_root $label_prefix $mount_point / + if [ $? -gt 1 ]; then + die "Can't create filesystem structure on" "$mount_point" + fi + } show_config () { @@ -1099,12 +1171,13 @@ usage () { " --color Enable colored output messages" \ " --dry-run perform a trial run (no changes are written)" \ " --get-config-names show pre-defined configs from config-file" \ - " -i, --install-target install system on given target" \ + " -i, --install prepare chroot environemnt and install on target" \ + " --install-target install on target" \ " --mountpoint Specify MountPoint preparing targets OS-Disk" \ - " --label_prefix Specify the partion label prefix" \ - " -t, --disk_dev_root Specify the blockdevice for the target OS (e.g /dev/sda)" \ - " --disk_dev_machines Specify the blockdevice for containers/machines (e.g /dev/sdb)" \ - " --disk_dev_data Specify the blockdevice to store data (e.g /dev/sdc)" \ + " --label-prefix Specify the partion label prefix" \ + " -t, --disk-dev-root Specify the blockdevice for the target OS (e.g /dev/sda)" \ + " --disk-dev-machines Specify the blockdevice for containers/machines (e.g /dev/sdb)" \ + " --disk-dev-data Specify the blockdevice to store data (e.g /dev/sdc)" \ " -v, --verbose Be verbose on what's going on (min: --verbose=0, max: --verbose=3)" \ " --version show program version" exit 0 @@ -1150,24 +1223,41 @@ case $cmd in exit 0 fi ;; - install-target) + install) + if [ $quiet -eq 0 ]; then + show_config + fi + prepare_target_disks prepare_target_filesystems - mount_target_filesystems - install_target_packages + mount_target_filesystems $disk_label_root $mount_point + install_target_packages $mount_point - #exit 0 - # change new root to target - cmd="arch-chroot $MOUNT_POINT arch-create-system install_target" - if [ $dryrun -eq 1 ]; then - printf "${MAGENTA}Would run ${GREEN}'%s'${NO_COLOR}\n" "$cmd" + install_target $mount_point + if [ $? -eq 0 ]; then + printf "${MAGENTA}Installation succeeded for target mount-point: ${GREEN}'%s'${NO_COLOR}\n" \ + "${TARGET}" + printf "You are save to reboot now.\n" else - eval "$cmd" + printf "${MAGENTA}Installation failed for target mount-point: ${GREEN}'%s'${NO_COLOR}\n" \ + "${TARGET}" + exit 1 fi ;; + install-target) + install_target $mount_point + if [ $? -eq 0 ]; then + printf "${MAGENTA}Installation succeeded for target mount-point: ${GREEN}'%s'${NO_COLOR}\n" \ + "${TARGET}" + printf "You are save to reboot now.\n" + else + printf "${MAGENTA}Installation failed for target mount-point: ${GREEN}'%s'${NO_COLOR}\n" \ + "${TARGET}" + exit 1 + fi + ;; + *) + show_config + ;; esac - -if [ $quiet -eq 0 ]; then - show_config -fi