From 40f373ea628ee2de12b59f310ae61b993e1b2739 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20L=C3=B6ffler?= Date: Mon, 1 Jan 2024 04:34:19 +0100 Subject: [PATCH] refactor code style --- test/test.sh | 17 +- ubuntu-installer.sh | 555 ++++++++++++++++---------------------------- 2 files changed, 208 insertions(+), 364 deletions(-) diff --git a/test/test.sh b/test/test.sh index c778ec2..1cf50e8 100755 --- a/test/test.sh +++ b/test/test.sh @@ -12,10 +12,8 @@ function main { source "$SELF_DIR/test.env" if [[ $# -ne 2 ]]; then - echo "$SELF_NAME: require arguments " >&2 exit 1 - fi TASK="$1" @@ -34,21 +32,15 @@ function run_test_suite { before_all if [[ "$CODENAME" == "ALL" ]]; then - for i in $(get_codenames); do - before_each execute_installer "$i" after_each - done - else - before_each execute_installer "$CODENAME" after_each - fi after_all @@ -118,15 +110,13 @@ function get_codenames { echo "$CURRENT_CODENAME" fi - done <<< "$VERSIONS_AVAILABLE" + done <<<"$VERSIONS_AVAILABLE" } function before_each { if [[ "$TASK" == "install-system" ]]; then - mkfs.ext4 "$DEV_LOOP_HOME" - fi echo "$SELF_NAME: begin installation (task: $TASK)" @@ -143,22 +133,19 @@ function before_all { mkdir -p "$(dirname "$IMAGE")" dd "if=/dev/zero" "of=$IMAGE" bs=1G count=10 - sfdisk "$IMAGE" < "$SELF_DIR/sfdisk.txt" + sfdisk "$IMAGE" <"$SELF_DIR/sfdisk.txt" DEV_LOOP_IMAGE="$(losetup --show --find --partscan "$IMAGE")" DEV_LOOP_SYSTEM="$DEV_LOOP_IMAGE""p1" DEV_LOOP_HOME="$DEV_LOOP_IMAGE""p2" - fi } function after_all { if [[ "$TASK" == "install-system" ]]; then - [[ -n "${DEV_LOOP_IMAGE:-}" ]] && losetup -d "$DEV_LOOP_IMAGE" [[ -f "$IMAGE" ]] && rm "$IMAGE" - fi } diff --git a/ubuntu-installer.sh b/ubuntu-installer.sh index 9cce061..8c9e6ef 100755 --- a/ubuntu-installer.sh +++ b/ubuntu-installer.sh @@ -5,6 +5,7 @@ function main { # declare local variables local TASK local LONG_OPTIONS + local OPTIONS_PARSED # set default values and configuration HOME="/tmp" @@ -43,126 +44,122 @@ function main { # apply arguments while [[ $# -gt 0 ]]; do case "$1" in - -h | --help) - SHOW_HELP=true - shift 1 - ;; - -l | --login) - SHELL_LOGIN=true - shift 1 - ;; - -e | --efi) - USE_EFI=true - shift 1 - ;; - -s | --separate-home) - USE_SEPARATE_HOME=true - shift 1 - ;; - -k | --copy-network-settings) - COPY_NETWORK_SETTINGS=true - shift 1 - ;; - -u | --username) - USERNAME_NEW="$2" - shift 2 - ;; - -n | --hostname) - HOSTNAME_NEW="$2" - shift 2 - ;; - -c | --codename) - CODENAME="$2" - shift 2 - ;; - -x | --dev-root) - DEV_ROOT="$2" - shift 2 - ;; - -y | --dev-home) - DEV_HOME="$2" - shift 2 - ;; - -z | --dev-boot) - DEV_BOOT="$2" - shift 2 - ;; - -b | --bundles) - BUNDLES="$2" - shift 2 - ;; - --bundles-file) - BUNDLES_FILE="$2" - shift 2 - ;; - --debconf-file) - DEBCONF_FILE="$2" - shift 2 - ;; - --dconf-file) - DCONF_FILE="$2" - shift 2 - ;; - --mirror) - MIRROR="$2" - shift 2 - ;; - --locales) - LOCALES="$2" - shift 2 - ;; - --time-zone) - TZ="$2" - shift 2 - ;; - --user-gecos) - USER_GECOS="$2" - shift 2 - ;; - --password) - PASSWORD="$2" - shift 2 - ;; - --keyboard-model) - XKBMODEL="$2" - shift 2 - ;; - --keyboard-layout) - XKBLAYOUT="$2" - shift 2 - ;; - --keyboard-variant) - XKBVARIANT="$2" - shift 2 - ;; - --keyboard-options) - XKBOPTIONS="$2" - shift 2 - ;; - --) - shift 1 - break - ;; - *) - break - ;; + -h | --help) + SHOW_HELP=true + shift 1 + ;; + -l | --login) + SHELL_LOGIN=true + shift 1 + ;; + -e | --efi) + USE_EFI=true + shift 1 + ;; + -s | --separate-home) + USE_SEPARATE_HOME=true + shift 1 + ;; + -k | --copy-network-settings) + COPY_NETWORK_SETTINGS=true + shift 1 + ;; + -u | --username) + USERNAME_NEW="$2" + shift 2 + ;; + -n | --hostname) + HOSTNAME_NEW="$2" + shift 2 + ;; + -c | --codename) + CODENAME="$2" + shift 2 + ;; + -x | --dev-root) + DEV_ROOT="$2" + shift 2 + ;; + -y | --dev-home) + DEV_HOME="$2" + shift 2 + ;; + -z | --dev-boot) + DEV_BOOT="$2" + shift 2 + ;; + -b | --bundles) + BUNDLES="$2" + shift 2 + ;; + --bundles-file) + BUNDLES_FILE="$2" + shift 2 + ;; + --debconf-file) + DEBCONF_FILE="$2" + shift 2 + ;; + --dconf-file) + DCONF_FILE="$2" + shift 2 + ;; + --mirror) + MIRROR="$2" + shift 2 + ;; + --locales) + LOCALES="$2" + shift 2 + ;; + --time-zone) + TZ="$2" + shift 2 + ;; + --user-gecos) + USER_GECOS="$2" + shift 2 + ;; + --password) + PASSWORD="$2" + shift 2 + ;; + --keyboard-model) + XKBMODEL="$2" + shift 2 + ;; + --keyboard-layout) + XKBLAYOUT="$2" + shift 2 + ;; + --keyboard-variant) + XKBVARIANT="$2" + shift 2 + ;; + --keyboard-options) + XKBOPTIONS="$2" + shift 2 + ;; + --) + shift 1 + break + ;; + *) + break + ;; esac done # either print the help text or process task if "$SHOW_HELP"; then - # show help text show_help - else # check if there is a unassigned argument to interpret it as task if [[ $# -eq 0 ]]; then - echo "$SELF_NAME: require a task to continue" >&2 exit 1 - fi # assign the task @@ -171,119 +168,98 @@ function main { # check if there is no unassigned argument left if [[ $# -ne 0 ]]; then - echo "$SELF_NAME: cannot handle unassigned arguments: $*" >&2 exit 1 - fi # select task case "$TASK" in - create-user) - task_create_user - ;; - modify-user) - task_modify_user - ;; - manage-package-sources) - task_manage_package_sources - ;; - install-packages-base) - task_install_packages_base - ;; - install-packages-system-minimal) - task_install_packages_system_minimal - ;; - install-packages-container-image-minimal) - task_install_packages_container_image_minimal - ;; - install-system) - task_install_system - ;; - install-lxc-image) - task_install_lxc_image - ;; - install-docker-image) - task_install_docker_image - ;; - configure-locales) - task_configure_locales - ;; - configure-tzdata) - task_configure_tzdata - ;; - configure-keyboard) - task_configure_keyboard - ;; - configure-tools) - task_configure_tools - ;; - *) - echo "$SELF_NAME: require a valid task" >&2 - exit 1 - ;; + create-user) + task_create_user + ;; + modify-user) + task_modify_user + ;; + manage-package-sources) + task_manage_package_sources + ;; + install-packages-base) + task_install_packages_base + ;; + install-packages-system-minimal) + task_install_packages_system_minimal + ;; + install-packages-container-image-minimal) + task_install_packages_container_image_minimal + ;; + install-system) + task_install_system + ;; + install-lxc-image) + task_install_lxc_image + ;; + install-docker-image) + task_install_docker_image + ;; + configure-locales) + task_configure_locales + ;; + configure-tzdata) + task_configure_tzdata + ;; + configure-keyboard) + task_configure_keyboard + ;; + configure-tools) + task_configure_tools + ;; + *) + echo "$SELF_NAME: require a valid task" >&2 + exit 1 + ;; esac - fi } function verify_root_privileges { if [[ $EUID -ne 0 ]]; then - echo "$SELF_NAME: require root privileges" >&2 exit 1 - fi } function verify_username { if [[ -n "${USERNAME_NEW:-}" ]]; then - if ! echo "$USERNAME_NEW" | grep -qE "$NAME_REGEX"; then - echo "$SELF_NAME: require username that matches regular expression $NAME_REGEX" >&2 exit 1 - fi - else - # by default, use the name of the user who runs the script USERNAME_NEW="$(get_username)" - fi # make sure the username is different to root if [[ "${USERNAME_NEW:-}" == "root" ]]; then - echo "$SELF_NAME: require username different to root" >&2 exit 1 - fi } function verify_username_exists { - if getent passwd "${USERNAME_NEW:-}" > /dev/null; then - + if getent passwd "${USERNAME_NEW:-}" >/dev/null; then if ! "$1"; then - echo "$SELF_NAME: the username has already been taken" >&2 exit 1 - fi - else - if "$1"; then - echo "$SELF_NAME: the username does not exist" >&2 exit 1 - fi - fi } @@ -291,19 +267,15 @@ function verify_hostname { # by default, use the hostname of the running system if [[ -z "${HOSTNAME_NEW:-}" ]]; then - HOSTNAME_NEW="$HOSTNAME" - fi } function verify_codename { if [[ -z "${CODENAME:-}" ]] || ! echo "${CODENAME:-}" | grep -qE '^[a-z]+$'; then - echo "$SELF_NAME: require valid Ubuntu codename" >&2 exit 1 - fi } @@ -311,10 +283,8 @@ function verify_mounting_root { # the block device file for "/" must exist and be unmounted if [[ -z "${DEV_ROOT:-}" ]] || [[ ! -b "${DEV_ROOT:-}" ]] || mount | grep -q "${DEV_ROOT:-}"; then - echo "$SELF_NAME: require unmounted device file for /" >&2 exit 1 - fi } @@ -322,15 +292,11 @@ function verify_mounting_home { # only perform checks if a separate home partition is used if "$USE_SEPARATE_HOME"; then - # the block device file for "/home" must exist if [[ -z "${DEV_HOME:-}" ]] || [[ ! -b "${DEV_HOME:-}" ]]; then - echo "$SELF_NAME: require device file for /home" >&2 exit 1 - fi - fi } @@ -340,29 +306,22 @@ function verify_mounting_boot { # use mounted UEFI partition by default if [[ -z "${DEV_BOOT:-}" ]]; then - DEV_BOOT="$(cat /proc/mounts | grep -E /boot/efi | cut -d ' ' -f 1)" - fi # the block device file for /boot/efi must exist if [[ -z "${DEV_BOOT:-}" ]] || [[ ! -b "${DEV_BOOT:-}" ]]; then - echo "$SELF_NAME: require device file for /boot/efi" >&2 exit 1 - fi else # the block device file for GRUB installation must exist if [[ -z "${DEV_BOOT:-}" ]] || [[ ! -b "${DEV_BOOT:-}" ]]; then - echo "$SELF_NAME: require device file for GRUB installation" >&2 exit 1 - fi - fi } @@ -375,14 +334,10 @@ function task_create_user { # create user and home-directory if not exist if [[ -n "${PASSWORD:-}" ]]; then - adduser --add_extra_groups --disabled-password --gecos "${USER_GECOS:-}" "$USERNAME_NEW" usermod --password "$PASSWORD" "$USERNAME_NEW" - else - adduser --add_extra_groups --gecos "${USER_GECOS:-}" "$USERNAME_NEW" - fi } @@ -398,13 +353,9 @@ function task_modify_user { # add user to extra groups for i in $EXTRA_GROUPS; do - if grep -qE "^$i:" /etc/group; then - usermod -aG "$i" "$USERNAME_NEW" - fi - done } @@ -427,9 +378,7 @@ function task_manage_package_sources { # by default, use the whole Ubuntu mirror list if [[ -z "${MIRROR:-}" ]]; then - MIRROR='mirror://mirrors.ubuntu.com/mirrors.txt' - fi # set OS variables @@ -442,22 +391,22 @@ function task_manage_package_sources { add-apt-repository -y -s "deb $MIRROR $UBUNTU_CODENAME-backports $COMPONENTS" # add package sources for sbt - wget -qO - 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x2EE0EA64E40A89B84B2DF73499E82A75642AC823' \ - | gpg --no-default-keyring --keyring "gnupg-ring:$TRUSTED_GPG/scalasbt-release.gpg" --import + wget -qO - 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x2EE0EA64E40A89B84B2DF73499E82A75642AC823' | + gpg --no-default-keyring --keyring "gnupg-ring:$TRUSTED_GPG/scalasbt-release.gpg" --import chmod 644 "$TRUSTED_GPG/scalasbt-release.gpg" - echo 'deb https://repo.scala-sbt.org/scalasbt/debian all main' > "$SOURCES_LIST/sbt.list" + echo 'deb https://repo.scala-sbt.org/scalasbt/debian all main' >"$SOURCES_LIST/sbt.list" # add package sources for chrome browser - wget -qO - 'https://dl-ssl.google.com/linux/linux_signing_key.pub' \ - | gpg --no-default-keyring --keyring "gnupg-ring:$TRUSTED_GPG/google-chrome.gpg" --import + wget -qO - 'https://dl-ssl.google.com/linux/linux_signing_key.pub' | + gpg --no-default-keyring --keyring "gnupg-ring:$TRUSTED_GPG/google-chrome.gpg" --import chmod 644 "$TRUSTED_GPG/google-chrome.gpg" - echo 'deb https://dl.google.com/linux/chrome/deb/ stable main' > "$SOURCES_LIST/google-chrome.list" + echo 'deb https://dl.google.com/linux/chrome/deb/ stable main' >"$SOURCES_LIST/google-chrome.list" # add package sources for firefox add-apt-repository -y ppa:mozillateam/ppa - echo 'Package: *' > "$PREFERENCES/mozilla-firefox" - echo 'Pin: release o=LP-PPA-mozillateam' >> "$PREFERENCES/mozilla-firefox" - echo 'Pin-Priority: 601' >> "$PREFERENCES/mozilla-firefox" + echo 'Package: *' >"$PREFERENCES/mozilla-firefox" + echo 'Pin: release o=LP-PPA-mozillateam' >>"$PREFERENCES/mozilla-firefox" + echo 'Pin-Priority: 601' >>"$PREFERENCES/mozilla-firefox" # update package lists apt-get update @@ -592,7 +541,6 @@ function install_bundles { # install APT packages echo "$LINE" | xargs apt-get -y install fi - fi done <"$FILE" } @@ -618,16 +566,12 @@ function task_install_packages_system_minimal { # install GRUB bootloader if "$USE_EFI"; then - apt-get -y install grub-efi grub-install --target=x86_64-efi --efi-directory=/boot/efi echo 'The boot order must be adjusted manually using the efibootmgr tool.' - else - apt-get -y install grub-pc grub-install "$DEV_BOOT" - fi # install Linux kernel @@ -693,13 +637,9 @@ function task_install_system { # install basic tools, kernel and bootloader if "$USE_EFI"; then - chroot "$CHROOT" "$SELF_NAME" install-packages-system-minimal --efi - else - chroot "$CHROOT" "$SELF_NAME" install-packages-system-minimal --dev-boot "$DEV_BOOT" - fi # configure packages @@ -735,10 +675,8 @@ function task_install_system { # login to shell for diagnostic purposes if "$SHELL_LOGIN"; then - echo "$SELF_NAME: You are now logged in to the chroot environment for diagnostic purposes. Press Ctrl-D to escape." chroot "$CHROOT" /bin/bash - fi # unmount everything @@ -752,7 +690,7 @@ function task_install_system { function task_install_lxc_image { # declare local variables - local TEMPDIR + local TEMP_DIR local IMAGE_RELEASE local IMAGE_NAME @@ -761,10 +699,10 @@ function task_install_lxc_image { verify_codename # create temporary directory - TEMPDIR="$(mktemp -d)" + TEMP_DIR="$(mktemp -d)" # set root directory - CHROOT="$TEMPDIR/rootfs" + CHROOT="$TEMP_DIR/rootfs" # create root directory mkdir -p "$CHROOT" @@ -830,23 +768,23 @@ function task_install_lxc_image { echo " - copy" echo " - rename" echo " template: hostname.tpl" - } > "$TEMPDIR/metadata.yaml" + } >"$TEMP_DIR/metadata.yaml" # create template directory - mkdir "$TEMPDIR/templates" + mkdir "$TEMP_DIR/templates" # create templates (use container name as hostname) - configure_hosts_template "{{ container.name }}" "$TEMPDIR/templates/hostname.tpl" "$TEMPDIR/templates/hosts.tpl" + configure_hosts_template "{{ container.name }}" "$TEMP_DIR/templates/hostname.tpl" "$TEMP_DIR/templates/hosts.tpl" # create tarballs for rootfs and metadata - tar -czf "$TEMPDIR/rootfs.tar.gz" -C "$CHROOT" . - tar -czf "$TEMPDIR/metadata.tar.gz" -C "$TEMPDIR" 'metadata.yaml' 'templates' + tar -czf "$TEMP_DIR/rootfs.tar.gz" -C "$CHROOT" . + tar -czf "$TEMP_DIR/metadata.tar.gz" -C "$TEMP_DIR" 'metadata.yaml' 'templates' # install image - lxc image import "$TEMPDIR/metadata.tar.gz" "$TEMPDIR/rootfs.tar.gz" --alias "$IMAGE_NAME" + lxc image import "$TEMP_DIR/metadata.tar.gz" "$TEMP_DIR/rootfs.tar.gz" --alias "$IMAGE_NAME" # remove temporary directory - rm -rf "$TEMPDIR" + rm -rf "$TEMP_DIR" # show that we are done here echo "$SELF_NAME: LXC image $IMAGE_NAME imported" @@ -855,7 +793,7 @@ function task_install_lxc_image { function task_install_docker_image { # declare local variables - local TEMPDIR + local TEMP_DIR local IMAGE_RELEASE local IMAGE_NAME local CONTAINER_CMD @@ -865,10 +803,10 @@ function task_install_docker_image { verify_codename # create temporary directory - TEMPDIR="$(mktemp -d)" + TEMP_DIR="$(mktemp -d)" # set root directory - CHROOT="$TEMPDIR/rootfs" + CHROOT="$TEMP_DIR/rootfs" # create root directory mkdir -p "$CHROOT" @@ -912,26 +850,20 @@ function task_install_docker_image { IMAGE_RELEASE="$(cat '/proc/sys/kernel/random/uuid' | tr -dc '[:alnum:]')" IMAGE_NAME="custom/ubuntu:$CODENAME-$IMAGE_RELEASE" - if command -v docker &> /dev/null; then - + if command -v docker &>/dev/null; then CONTAINER_CMD="docker" - - elif command -v podman &> /dev/null; then - + elif command -v podman &>/dev/null; then CONTAINER_CMD="podman" - else - echo "$SELF_NAME: missing container tooling" >&2 exit 1 - fi # install image tar -cC "$CHROOT" . | "$CONTAINER_CMD" import - "$IMAGE_NAME" # remove temporary directory - rm -rf "$TEMPDIR" + rm -rf "$TEMP_DIR" # show that we are done here echo "$SELF_NAME: Docker image $IMAGE_NAME imported" @@ -952,14 +884,10 @@ function task_configure_locales { PRIMARY_LOCAL="$(echo "$LOCALES_LIST" | cut -d ' ' -f 1)" for i in $LOCALES_LIST; do - if [[ "$i" != "POSIX" ]] && [[ "$i" != "C" ]] && [[ "$i" != "C."* ]]; then - # generate a locale for each entry in list locale-gen "$i" - fi - done export LANG="$PRIMARY_LOCAL" @@ -982,10 +910,8 @@ function task_configure_locales { dpkg-reconfigure --frontend noninteractive locales else - # interactive configuration by user dpkg-reconfigure locales - fi } @@ -995,15 +921,11 @@ function task_configure_tzdata { verify_root_privileges if [[ -n "${TZ:-}" ]]; then - # set preconfigured time zone ln -fs "/usr/share/zoneinfo/$TZ" /etc/localtime && dpkg-reconfigure --frontend noninteractive tzdata - else - # interactive configuration by user dpkg-reconfigure tzdata - fi } @@ -1019,42 +941,32 @@ function task_configure_keyboard { FILE="/etc/default/keyboard" if [[ -n "${XKBMODEL:-}" ]]; then - sed -i 's/^XKBMODEL=.*/XKBMODEL="'"$XKBMODEL"'"/' "$FILE" - fi if [[ -n "${XKBLAYOUT:-}" ]]; then - sed -i 's/^XKBLAYOUT=.*/XKBLAYOUT="'"$XKBLAYOUT"'"/' "$FILE" - fi if [[ -n "${XKBVARIANT:-}" ]]; then - sed -i 's/^XKBVARIANT=.*/XKBVARIANT="'"$XKBVARIANT"'"/' "$FILE" - fi if [[ -n "${XKBOPTIONS:-}" ]]; then - sed -i 's/^XKBOPTIONS=.*/XKBOPTIONS="'"$XKBOPTIONS"'"/' "$FILE" - fi if [[ -n "${XKBMODEL:-}" ]] || - [[ -n "${XKBLAYOUT:-}" ]] || - [[ -n "${XKBVARIANT:-}" ]] || - [[ -n "${XKBOPTIONS:-}" ]]; then + [[ -n "${XKBLAYOUT:-}" ]] || + [[ -n "${XKBVARIANT:-}" ]] || + [[ -n "${XKBOPTIONS:-}" ]]; then # set preconfigured keyboard layout dpkg-reconfigure --frontend noninteractive keyboard-configuration else - # interactive configuration by user dpkg-reconfigure keyboard-configuration - fi } @@ -1073,7 +985,7 @@ function task_configure_tools { FILE_BASHRC="/etc/bash.bashrc" # add vim settings - cat >> "$FILE_VIMRC" << 'EOF' + cat >>"$FILE_VIMRC" <<'EOF' filetype plugin indent on syntax on @@ -1086,11 +998,10 @@ set expandtab EOF # enable bash history search completion - cat >> "$FILE_BASHRC" << 'EOF' + cat >>"$FILE_BASHRC" <<'EOF' # enable bash history search completion if [[ $- == *i* ]]; then - bind '"\e[A": history-search-backward' bind '"\e[B": history-search-forward' fi @@ -1104,8 +1015,8 @@ EOF cut -c 2-)" # enable bash completion - echo "" >> "$FILE_BASHRC" - echo "$COMPLETION_SCRIPT" >> "$FILE_BASHRC" + echo "" >>"$FILE_BASHRC" + echo "$COMPLETION_SCRIPT" >>"$FILE_BASHRC" } function configure_hosts { @@ -1127,7 +1038,7 @@ function configure_hosts_template { FILE_HOSTS="$3" # edit /etc/hostname - echo "$HOSTNAME" > "$FILE_HOSTNAME" + echo "$HOSTNAME" >"$FILE_HOSTNAME" # edit /etc/hosts { @@ -1141,7 +1052,7 @@ function configure_hosts_template { echo "ff02::1 ip6-allnodes" echo "ff02::2 ip6-allrouters" echo "ff02::3 ip6-allhosts" - } > "$FILE_HOSTS" + } >"$FILE_HOSTS" } function configure_fstab { @@ -1160,50 +1071,38 @@ function configure_fstab { UUID_ROOT="$(blkid -s UUID -o value "$DEV_ROOT")" if "$USE_EFI"; then - # get UUID of UEFI partition UUID_UEFI="$(blkid -s UUID -o value "$DEV_BOOT")" - # allows to write UEFI specific entry to fstab file FILE_UEFI="$FILE" - else - # an UUID is not needed in this case UUID_UEFI="" - # discard UEFI specific entry FILE_UEFI="/dev/null" - fi if "$USE_SEPARATE_HOME"; then - # get UUID of home partition UUID_HOME="$(blkid -s UUID -o value "$DEV_HOME")" - # allows to write home specific entry to fstab file FILE_HOME="$FILE" - else - # an UUID is not needed in this case UUID_HOME="" - # discard home specific entry FILE_HOME="/dev/null" - fi # edit /etc/fstab - echo '# /etc/fstab' > "$FILE" - echo '# ' >> "$FILE" - echo "UUID=$UUID_ROOT / ext4 defaults,errors=remount-ro 0 1" >> "$FILE" - echo "UUID=$UUID_UEFI /boot/efi vfat defaults 0 2" >> "$FILE_UEFI" - echo "UUID=$UUID_HOME /home ext4 defaults 0 2" >> "$FILE_HOME" - echo "proc /proc proc defaults 0 0" >> "$FILE" - echo "sys /sys sysfs defaults 0 0" >> "$FILE" - echo "tmpfs /tmp tmpfs defaults,size=40% 0 0" >> "$FILE" + echo '# /etc/fstab' >"$FILE" + echo '# ' >>"$FILE" + echo "UUID=$UUID_ROOT / ext4 defaults,errors=remount-ro 0 1" >>"$FILE" + echo "UUID=$UUID_UEFI /boot/efi vfat defaults 0 2" >>"$FILE_UEFI" + echo "UUID=$UUID_HOME /home ext4 defaults 0 2" >>"$FILE_HOME" + echo "proc /proc proc defaults 0 0" >>"$FILE" + echo "sys /sys sysfs defaults 0 0" >>"$FILE" + echo "tmpfs /tmp tmpfs defaults,size=40% 0 0" >>"$FILE" } function configure_users { @@ -1225,94 +1124,71 @@ function configure_network { # set HTTP proxy if [[ -n "${http_proxy:-}" ]]; then - - echo "http_proxy=$http_proxy" >> "$CHROOT/etc/environment" - echo "HTTP_PROXY=$http_proxy" >> "$CHROOT/etc/environment" - + echo "http_proxy=$http_proxy" >>"$CHROOT/etc/environment" + echo "HTTP_PROXY=$http_proxy" >>"$CHROOT/etc/environment" fi # set HTTPS proxy if [[ -n "${https_proxy:-}" ]]; then - - echo "https_proxy=$https_proxy" >> "$CHROOT/etc/environment" - echo "HTTPS_PROXY=$https_proxy" >> "$CHROOT/etc/environment" - + echo "https_proxy=$https_proxy" >>"$CHROOT/etc/environment" + echo "HTTPS_PROXY=$https_proxy" >>"$CHROOT/etc/environment" fi # set FTP proxy if [[ -n "${ftp_proxy:-}" ]]; then - - echo "ftp_proxy=$ftp_proxy" >> "$CHROOT/etc/environment" - echo "FTP_PROXY=$ftp_proxy" >> "$CHROOT/etc/environment" - + echo "ftp_proxy=$ftp_proxy" >>"$CHROOT/etc/environment" + echo "FTP_PROXY=$ftp_proxy" >>"$CHROOT/etc/environment" fi # set all socks proxy if [[ -n "${all_proxy:-}" ]]; then - - echo "all_proxy=$all_proxy" >> "$CHROOT/etc/environment" - echo "ALL_PROXY=$all_proxy" >> "$CHROOT/etc/environment" - + echo "all_proxy=$all_proxy" >>"$CHROOT/etc/environment" + echo "ALL_PROXY=$all_proxy" >>"$CHROOT/etc/environment" fi # set ignore-hosts if [[ -n "${no_proxy:-}" ]]; then - - echo "no_proxy=$no_proxy" >> "$CHROOT/etc/environment" - echo "NO_PROXY=$no_proxy" >> "$CHROOT/etc/environment" - + echo "no_proxy=$no_proxy" >>"$CHROOT/etc/environment" + echo "NO_PROXY=$no_proxy" >>"$CHROOT/etc/environment" fi # copy DNS settings if [[ -f '/etc/systemd/resolved.conf' ]]; then - cp -f '/etc/systemd/resolved.conf' "$CHROOT/etc/systemd/resolved.conf" - fi # copy connection settings (system without network-manager) if [[ -d '/etc/netplan' ]]; then - mkdir -p "$CHROOT/etc/netplan" cp -rf '/etc/netplan/.' "$CHROOT/etc/netplan" - fi # copy connection settings (system with network-manager) if [[ -d '/etc/NetworkManager/system-connections' ]]; then - mkdir -p "$CHROOT/etc/NetworkManager/system-connections" cp -rf '/etc/NetworkManager/system-connections/.' "$CHROOT/etc/NetworkManager/system-connections" - fi # https://bugs.launchpad.net/ubuntu/+source/network-manager/+bug/1638842 - if command -v nmcli &> /dev/null; then - + if command -v nmcli &>/dev/null; then mkdir -p "$CHROOT/etc/NetworkManager/conf.d" touch "$CHROOT/etc/NetworkManager/conf.d/10-globally-managed-devices.conf" - fi - fi } function configure_desktop { # only apply if flatpak is installed - if command -v flatpak &> /dev/null; then - + if command -v flatpak &>/dev/null; then # add flatpak remote: flathub chroot "$CHROOT" flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo - fi # only apply if the gnome-shell is installed - if command -v gnome-shell &> /dev/null; then - + if command -v gnome-shell &>/dev/null; then # modify default GNOME settings install_default_gnome_settings - fi } @@ -1347,8 +1223,8 @@ function install_default_gnome_settings { cp -v "$FILE" "$CHROOT/etc/dconf/db/site.d/defaults" # change dconf profile - echo 'user-db:user' >> "$CHROOT/etc/dconf/profile/user" - echo 'system-db:site' >> "$CHROOT/etc/dconf/profile/user" + echo 'user-db:user' >>"$CHROOT/etc/dconf/profile/user" + echo 'system-db:site' >>"$CHROOT/etc/dconf/profile/user" # update dconf inside chroot chroot "$CHROOT" dconf update @@ -1364,7 +1240,7 @@ function show_help { # open default browser with project website echo "$SELF_NAME: for help, see the project website $URL" - xdg-open "$URL" &> /dev/null + xdg-open "$URL" &>/dev/null } function get_username { @@ -1380,11 +1256,9 @@ function get_username { CURRENT_USER=$ORIGIN_USER while [[ "$CURRENT_USER" == "root" && $CURRENT_PID -gt 0 ]]; do - RESULT="$(ps -hp $CURRENT_PID -o user,ppid | sed 's/\s\s*/ /')" CURRENT_USER="$(echo "$RESULT" | cut -d ' ' -f 1)" CURRENT_PID="$(echo "$RESULT" | cut -d ' ' -f 2)" - done getent passwd "$CURRENT_USER" | cut -d ':' -f 1 @@ -1418,38 +1292,26 @@ function mounting_step_1 { # mount $DEV_BOOT if mount | grep -q "$DEV_BOOT"; then - BOOT_PATH="$(df "$DEV_BOOT" | grep -oE '(/[[:alnum:]]+)+$' | head -1)" - mkdir -p "$CHROOT/boot/efi" mount -o bind "$BOOT_PATH" "$CHROOT/boot/efi" - else - mkdir -p "$CHROOT/boot/efi" mount "$DEV_BOOT" "$CHROOT/boot/efi" - fi - fi if "$USE_SEPARATE_HOME"; then # mount $DEV_HOME if mount | grep -q "$DEV_HOME"; then - HOME_PATH="$(df "$DEV_HOME" | grep -oE '(/[[:alnum:]]+)+$' | head -1)" - mkdir -p "$CHROOT/home" mount -o bind "$HOME_PATH" "$CHROOT/home" - else - mkdir -p "$CHROOT/home" mount "$DEV_HOME" "$CHROOT/home" - fi - fi } @@ -1463,15 +1325,11 @@ function unmounting_step_1 { sync if "$USE_EFI"; then - umount -l "$CHROOT/boot/efi" - fi if "$USE_SEPARATE_HOME"; then - umount "$CHROOT/home" - fi # unmount directory root @@ -1517,7 +1375,6 @@ function unmounting_step_2 { umount -l "$CHROOT/dev" umount -l "$CHROOT/sys" umount -l "$CHROOT/proc" - fi }