Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ##============================= mk-usb =============================##
- # mk-usb <DEVICE>
- # Automated Arch Linux USB install:
- # * for use with 16GB USB stick
- # * creates two bootable host OS partitions
- # * OS partitions load completely to RAM by default with ramroot
- # * enabled large page memory support for mining
- # * tweaked journaling for running OS from a USB
- # * reverted to traditional network device naming
- # * disabled terminal bell
- # * added custom repo to /etc/pacman.conf
- # * set NOPASSWD for various user commands
- # * various other tweaks
- # Partition scheme:
- # * sdx1 10M
- # * sdx2 vfat 500M
- # * sdx3 ext4 $HOME_SIZE
- # * sdx4 ext4 $HOST1_SIZE
- # * sdx5 ext4 [remaining]
- # Mount scheme:
- # * sdx4/sdx5 /
- # * sdx3 /home
- # * sdx2 /EFI
- # * /EFI/$HOSTNAME /boot
- ##=========================== VARIABLES ============================##
- SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
- # HOST/USER settings:
- HOME_SIZE='1G'
- HOST_KEYMAP='/usr/share/kbd/keymaps/i386/qwerty/us.map.gz'
- HOST_LANG='en_US.UTF-8'
- HOST_TIMEZONE='America/Chicago'
- ROOT_PASS='xxxxxxx'
- USER_COMMANDS_NOPASS=('ALL')
- USER_NAME='bikes-n-math'
- USER_PASS='xxxxxxx'
- HOST_PKGS_COMMON=(
- base
- base-devel
- pacman-contrib
- sudo
- acpi
- arch-install-scripts
- arch-wiki-lite
- bc
- binwalk
- cifs-utils
- cmake
- curl
- dialog
- dmidecode
- dosfstools
- efitools
- efivar
- elinks
- exfat-utils
- fbida
- figlet
- fuse2
- fuse3
- fuseiso
- git
- gnu-netcat
- gnupg
- gptfdisk
- hddtemp
- htop
- ifplugd
- iw
- jq
- libusbmuxd
- lm_sensors
- ltrace
- macchanger
- nano
- nmap
- ntfs-3g
- openssh
- openssl
- p7zip
- polkit
- python
- python2
- ranger
- rsync
- sbsigntools
- smbclient
- sshfs
- strace
- sysstat
- tmux
- tor
- torsocks
- unrar
- unzip
- usbmuxd
- vim
- w3m
- wget
- wpa_supplicant
- inxi
- ramroot
- )
- # HOST1 settings:
- HOST1_NAME='arch-cpu'
- HOST1_SIZE='4G'
- HOST1_PART=4
- HOST1_PKGS=(
- "${HOST_PKGS_COMMON[@]}"
- )
- # HOST2 settings:
- HOST2_NAME='arch-cpu-gpu'
- HOST2_PART=5
- HOST2_PKGS=(
- "${HOST_PKGS_COMMON[@]}"
- cuda
- opencl-amd
- )
- # control:
- ANSWER=
- HOST_NAME=
- HOST_PART=
- HOST_PKGS=()
- # USB:
- USB_MOUNT='/mnt/usb'
- USB_DEVICE=
- USB_INFO=
- USB_SIZE_BYTES=
- USB_SIZE_HUMAN=
- USB_BLOCK_COUNT=
- USB_BLOCK_SIZE=
- # basic colors:
- C_BLUE_B=$'\e[1;38;5;27m'
- C_GREEN_B=$'\e[1;38;5;46m'
- C_RED_B=$'\e[1;38;5;196m'
- C_YELLOW_B=$'\e[1;33m'
- C_WHITE_B=$'\e[1;37m'
- C_GRAY=$'\e[0;37m'
- ##=========================== FUNCTIONS ============================##
- msg() {
- # print status message:
- printf '%s==>%s %s%s\n' "$C_GREEN_B" "$C_WHITE_B" "$1" "$C_GRAY"
- }
- msg_ask() {
- # print ask message:
- printf '%s::>%s %s%s ' "$C_BLUE_B" "$C_WHITE_B" "$1" "$C_GRAY"
- }
- msg_error() {
- # print error message:
- printf '%s==> ERROR:%s %s%s\n' "$C_RED_B" "$C_WHITE_B" \
- "$1" "$C_GRAY"
- }
- msg_warn() {
- # print warning message:
- printf '%s==> WARNING:%s %s%s\n' "$C_YELLOW_B" "$C_WHITE_B" \
- "$1" "$C_GRAY"
- }
- msg2() {
- # print status sub-message:
- printf '%s ->%s %s%s\n' "$C_BLUE_B" "$C_WHITE_B" "$1" "$C_GRAY"
- }
- internet_status() {
- # return internet connection status:
- ping -q -c1 -W3 google.com &>/dev/null || \
- ping -q -c1 -W5 archlinux.org &>/dev/null
- }
- usb_pacstrap() {
- # pacstrap and configure basic system:
- msg "Installing $HOST_NAME Arch Linux to $USB_DEVICE$HOST_PART..."
- # pacstrap all packages:
- msg2 'pacstrap'
- pacstrap -c "$USB_MOUNT" "${HOST_PKGS[@]}"
- arch-chroot "$USB_MOUNT" paccache -rk0
- # create fstab:
- genfstab -U "$USB_MOUNT" >> "$USB_MOUNT/etc/fstab"
- sed -i "s~${USB_MOUNT}~~" "$USB_MOUNT/etc/fstab"
- msg2 'fstab created'
- # set time zone
- arch-chroot "$USB_MOUNT" ln \
- -sf "/usr/share/zoneinfo/$HOST_TIMEZONE" /etc/localtime
- msg2 "time zone set to $HOST_TIMEZONE"
- # gen /etc/adjtime:
- arch-chroot "$USB_MOUNT" hwclock --systohc
- # set language:
- sed -i "s/#${HOST_LANG}/${HOST_LANG}/" "$USB_MOUNT/etc/locale.gen"
- arch-chroot "$USB_MOUNT" locale-gen
- printf 'LANG=%s\n' "$HOST_LANG" > "$USB_MOUNT/etc/locale.conf"
- msg2 "language/locale set to $HOST_LANG"
- # set hostname:
- echo "$HOST_NAME" > "$USB_MOUNT/etc/hostname"
- echo "127.0.1.1 $HOST_NAME.localdomain $HOST_NAME" \
- > "$USB_MOUNT/etc/hosts"
- msg2 "hostname set to $HOST_NAME"
- # set root password:
- msg2 'user root'
- if [ -n "$ROOT_PASS" ]; then
- # use ROOT_PASS as root passwd:
- printf "printf '%s\\\n%s\\\n' | passwd\n" \
- "$ROOT_PASS" "$ROOT_PASS" | arch-chroot "$USB_MOUNT"
- else
- # prompt for root passwd:
- arch-chroot "$USB_MOUNT" passwd
- fi
- }
- usb_config() {
- # optimize system for a portable USB installation:
- # set systemd journal to use RAM:
- sed -i -e 's/#?Storage=.*/Storage=volatile/' \
- -e 's/#*SystemMaxUse=.*/SystemMaxUse=16M/' \
- "$USB_MOUNT/etc/systemd/journald.conf"
- msg2 'journaling set to use RAM'
- # disable file access time record keeping:
- sed -i 's/relatime/noatime/' "$USB_MOUNT/etc/fstab"
- msg2 'file access time record keeping disabled'
- # disable autodetect kernel hook:
- sed -i 's/^ *\(HOOKS=.*\)autodetect */\1/' \
- "$USB_MOUNT/etc/mkinitcpio.conf"
- msg2 'autodetect kernel build hook removed'
- # disable fallback initramfs image:
- sed -i "s/ *PRESETS=.*/PRESETS=('default')/" \
- "$USB_MOUNT"/etc/mkinitcpio.d/linux.preset
- rm -rf "$USB_MOUNT/boot/"*fallback*
- msg2 'fallback initrams image generation disabled'
- # enable traditional network interface naming:
- arch-chroot "$USB_MOUNT" ln -sf /dev/null \
- /etc/udev/rules.d/80-net-setup-link.rules
- msg2 'traditional network interface naming enabled'
- # disable terminal bell:
- mkdir -p "$USB_MOUNT/etc/modprobe.d"
- printf 'blacklist pcspkr\n' \
- > "$USB_MOUNT/etc/modprobe.d/nobeep.conf"
- msg2 'terminal bell disabled'
- # enable pacman color:
- sed -i 's/#*Color/Color/' "$USB_MOUNT/etc/pacman.conf"
- msg2 'pacman color enabled'
- }
- usb_config_grub() {
- # install and configure grub on USB mounted at USB_MOUNT:
- local HOST1_MENU HOST1_UUID HOST2_MENU HOST2_UUID
- msg 'Installing and configuring grub...'
- # install grub:
- msg2 'pacstrap'
- pacstrap -c "$USB_MOUNT" grub efibootmgr
- arch-chroot "$USB_MOUNT" grub-install --target=i386-pc \
- --boot-directory /boot "$USB_DEVICE"
- arch-chroot "$USB_MOUNT" grub-install --target=x86_64-efi \
- --efi-directory /ESP --boot-directory /boot --removable
- # set menu colors and timeout:
- sed -i -e 's/#\(GRUB_COLOR_NORMAL\).*/\1="light-blue\/black"/' \
- -e 's/#\(GRUB_COLOR_HIGHLIGHT\).*/\1="light-cyan\/black"/' \
- -e 's/GRUB_TIMEOUT=.*/GRUB_TIMEOUT=2/' \
- "$USB_MOUNT/etc/default/grub"
- msg2 'grub menu colors set to light-cyan on light-blue'
- msg2 'grub menu timeout set to 2 seconds'
- # disable submenus:
- printf "\nGRUB_DISABLE_SUBMENU=y\n" >> "$USB_MOUNT/etc/default/grub"
- msg2 'grub submenus disabled'
- # create custom grub menu entries in /etc/grub.d/40_custom:
- arch-chroot "$USB_MOUNT" grub-mkconfig -o /boot/grub/grub.cfg
- HOST1_UUID=$(lsblk -f | \
- grep -Po "${USB_DEVICE##*/}${HOST1_PART}\s+ext4\s+\K[^\s]+")
- HOST2_UUID=$(lsblk -f | \
- grep -Po "${USB_DEVICE##*/}${HOST2_PART}\s+ext4\s+\K[^\s]+")
- HOST1_MENU="$(sed -n "/^menuentry 'Arch/,/^}/p" \
- "$USB_MOUNT/boot/grub/grub.cfg" | \
- sed "s/, with Linux linux/ (${HOST1_NAME})/g")"
- HOST2_MENU="$(sed -e "s/${HOST1_UUID}/${HOST2_UUID}/g" \
- -e "s/${HOST1_NAME}/${HOST2_NAME}/g" <<<"$HOST1_MENU")"
- { printf '%s\n\n%s\n\n' "$HOST1_MENU" "$HOST2_MENU"
- printf 'if [ %s == "efi" ]; then\n' "\${grub_platform}"
- printf ' menuentry "Firmware Setup" {\n'
- printf ' fwsetup\n }\nfi\n\n'
- printf 'menuentry "Shutdown" {\n halt\n}\n'
- } >> "$USB_MOUNT/etc/grub.d/40_custom"
- msg2 'custom grub menu entries created in /etc/grub.d/40_custom'
- # disable automatic grub menu entry generation for linux partitions:
- arch-chroot "$USB_MOUNT" chmod -x /etc/grub.d/10_linux
- msg2 'automatic menu entry generation for linux partitions disabled'
- # generate grub config:
- arch-chroot "$USB_MOUNT" grub-mkconfig -o /boot/grub/grub.cfg
- }
- usb_config_shim_signed() {
- # install and enable shim-signed boot loader:
- msg 'Installing and enabling shim-signed...'
- msg2 'pacstrap'
- pacstrap -c "$USB_MOUNT" shim-signed
- mv "$USB_MOUNT/ESP/EFI/BOOT/BOOTX64.efi" \
- "$USB_MOUNT/ESP/EFI/BOOT/grubx64.efi"
- cp "$USB_MOUNT/usr/share/shim-signed/shimx64.efi" \
- "$USB_MOUNT/ESP/EFI/BOOT/BOOTX64.efi"
- cp "$USB_MOUNT/usr/share/shim-signed/mmx64.efi" \
- "$USB_MOUNT/ESP/EFI/BOOT/mmx64.efi"
- }
- usb_config_large_memory_pages_enable() {
- # enable large memory pages:
- sed -i 's/# End of file//' "$USB_MOUNT/etc/security/limits.conf"
- { printf '* soft memlock 262144\n* hard memlock 262144\n'
- printf '# End of file\n'
- } >> "$USB_MOUNT/etc/security/limits.conf"
- mkdir -p "$USB_MOUNT/etc/sysctl.d"
- printf 'vm.nr_hugepages=128\n' \
- > "$USB_MOUNT/etc/sysctl.d/99-sysctl.conf"
- msg2 'large memory pages enabled'
- }
- usb_config_sudo_lecture_disable() {
- # disable sudo lecture message:
- printf 'Defaults lecture = never\n' \
- > "$USB_MOUNT/etc/sudoers.d/01-neverlecture"
- }
- usb_config_autologin_enable() {
- # enable autologin for USER_NAME on tty1:
- local SYSTEM_DIR="$USB_MOUNT/etc/systemd/system"
- mkdir -p "$SYSTEM_DIR/getty@tty1.service.d"
- { printf '[Service]\nExecStart=\nExecStart=/usr/bin/agetty --auto'
- printf 'login %s --noclear %s %s\n' "$USER_NAME" '%I' "\$TERM"
- } >> "$SYSTEM_DIR/getty@tty1.service.d/override.conf"
- }
- usb_config_capslock_to_escape() {
- # map Caps_Lock key to Escape key:
- gunzip -c "$HOST_KEYMAP" | sed 's/Caps_Lock/Escape/' \
- > "$USB_MOUNT/etc/vconsole.map"
- printf 'KEYMAP=/etc/vconsole.map\n' > "$USB_MOUNT/etc/vconsole.conf"
- }
- usb_issue_copy() {
- # copy custom /etc/issue:
- if [ -f "$SCRIPT_DIR/.root/etc/issue_$HOST_NAME" ]; then
- cp "$SCRIPT_DIR/.root/etc/issue_$HOST_NAME" \
- "$USB_MOUNT/etc/issue"
- msg2 'custom /etc/issue copied'
- elif [ -f "$SCRIPT_DIR/.root/etc/issue" ]; then
- cp "$SCRIPT_DIR/.root/etc/issue" "$USB_MOUNT/etc/issue"
- msg2 'custom /etc/issue copied'
- fi
- }
- usb_root_copy() {
- # copy root home folder:
- if [ -d "$SCRIPT_DIR/.root/root/" ]; then
- rsync -a "$SCRIPT_DIR/.root/root/" "$USB_MOUNT/root/"
- msg2 "root home folded copied from $SCRIPT_DIR/.root/root/"
- fi
- }
- usb_user_add() {
- # add USER_NAME and configure sudo NOPASSWD commands:
- msg2 "useradd $USER_NAME"
- arch-chroot "$USB_MOUNT" useradd -m "$USER_NAME" &>/dev/null
- if [ -n "$USER_PASS" ]; then
- printf "printf '%s\\\n%s\\\n' | passwd %s\n" \
- "$USER_PASS" "$USER_PASS" "$USER_NAME" | \
- arch-chroot "$USB_MOUNT"
- else
- arch-chroot "$USB_MOUNT" passwd "$USER_NAME"
- fi
- # configure sudo user commands:
- mkdir -p "$USB_MOUNT/etc/sudoers.d"
- { printf '%s ALL=(ALL) ALL\n' "$USER_NAME"
- if [ -n "${USER_COMMANDS_NOPASS[0]}" ]; then
- printf '%s ALL=(ALL) NOPASSWD: ' "$USER_NAME"
- printf '%s' "${USER_COMMANDS_NOPASS[0]}"
- for CMD in "${USER_COMMANDS_NOPASS[@]:1}"; do
- printf ', %s' "$CMD"
- done
- printf '\n'
- fi ;} > "$USB_MOUNT/etc/sudoers.d/10-$USER_NAME"
- msg2 "$USER_NAME sudo access configured"
- }
- usb_user_copy() {
- # copy USER_NAME home folder:
- if [ -d "$SCRIPT_DIR/$USER_NAME/" ]; then
- rsync -a "$SCRIPT_DIR/$USER_NAME/" "$USB_MOUNT/home/$USER_NAME"
- arch-chroot "$USB_MOUNT" chown -R \
- "$USER_NAME:$USER_NAME" "/home/$USER_NAME"
- msg2 "$USER_NAME files copied from $SCRIPT_DIR/$USER_NAME/"
- elif [ -d "$SCRIPT_DIR/user/" ]; then
- rsync -a "$SCRIPT_DIR/user/" "$USB_MOUNT/home/$USER_NAME"
- arch-chroot "$USB_MOUNT" chown -R \
- "$USER_NAME:$USER_NAME" "/home/$USER_NAME"
- msg2 "$USER_NAME files copied from $SCRIPT_DIR/user/"
- fi
- }
- usb_user_repo_add() {
- # add local user repo to pacman.conf:
- if [ -d "$USB_MOUNT/home/$USER_NAME/repo" ]; then
- { printf '[%s-repo]\nSigLevel = Optional\n' "$USER_NAME"
- printf 'Server = file:///home/%s/repo\n' "$USER_NAME"
- } >> "$USB_MOUNT/etc/pacman.conf"
- arch-chroot "$USB_MOUNT" pacman -Sy
- msg2 "/home/$USER_NAME/repo added to /etc/pacman.conf"
- fi
- }
- usb_reboot_script_make() {
- # create post reboot script:
- cat <<'REBOOT_SCRIPT' > "$USB_MOUNT/root/post-reboot.sh"
- #!/bin/bash
- C_BLUE_B=$'\e[1;38;5;27m'
- C_GREEN_B=$'\e[1;38;5;46m'
- C_RED_B=$'\e[1;38;5;196m'
- C_YELLOW_B=$'\e[1;33m'
- C_WHITE_B=$'\e[1;37m'
- C_GRAY=$'\e[0;37m'
- msg() {
- printf '%s==>%s %s%s\n' "$C_GREEN_B" "$C_WHITE_B" "$1" "$C_GRAY"
- }
- msg_error() {
- printf '%s==> ERROR:%s %s%s\n' "$C_RED_B" "$C_WHITE_B" \
- "$1" "$C_GRAY"
- }
- msg_warn() {
- printf '%s==> WARNING:%s %s%s\n' "$C_YELLOW_B" "$C_WHITE_B" \
- "$1" "$C_GRAY"
- }
- # setup ifplugd for automatic ethernet configuration:
- if (ip link | grep 'eth0:' &>/dev/null); then
- cp /etc/netctl/examples/ethernet-dhcp "/etc/netctl/eth0-$HOSTNAME"
- systemctl start netctl-ifplugd@eth0.service
- systemctl enable netctl-ifplugd@eth0.service
- msg 'automatic ethernet configuration enabled'
- else
- msg_warn 'eth0 not found: automatic ethernet configuration disabled'
- fi
- # start network time protocol:
- timedatectl set-ntp true
- msg 'network time sync enabled'
- # configure sshd.socket service:
- mkdir -p /etc/systemd/system/sshd.socket.d/
- printf '[Socket]\nListenStream=\nListenStream=22322\n' \
- > /etc/systemd/system/sshd.socket.d/override.conf
- printf 'Port 22322\nAllowUsers bot\nPermitRootLogin no\n' \
- >> /etc/ssh/sshd_config
- systemctl start sshd.socket
- systemctl enable sshd.socket
- msg 'ssh server enabled on port 22322'
- # enable tor:
- systemctl start tor.service
- systemctl enable tor.service
- msg 'tor service enabled'
- REBOOT_SCRIPT
- chmod +x "$USB_MOUNT/root/post-reboot.sh"
- msg_warn 'execute /root/post-reboot.sh after reboot'
- }
- usb_mount() {
- # set $1=HOST_NAME; set HOST_PART, HOST_PKGS; mount HOST_NAME:
- if [ "$1" = "$HOST1_NAME" ]; then
- HOST_NAME="$HOST1_NAME"
- HOST_PART="$HOST1_PART"
- HOST_PKGS=("${HOST1_PKGS[@]}")
- elif [ "$1" = "$HOST2_NAME" ]; then
- HOST_NAME="$HOST2_NAME"
- HOST_PART="$HOST2_PART"
- HOST_PKGS=("${HOST2_PKGS[@]}")
- elif [ -n "$1" ]; then
- msg_error "hostname $1 in usb_mount() not found"
- exit 3
- else
- msg_error 'no hostname supplied to usb_mount()'
- exit 3
- fi
- # mount root:
- mkdir -p "$USB_MOUNT"
- mount "${USB_DEVICE}$HOST_PART" "$USB_MOUNT"
- # mount ESP:
- mkdir -p "$USB_MOUNT/ESP"
- mount "${USB_DEVICE}2" "$USB_MOUNT/ESP"
- # mount home:
- mkdir -p "$USB_MOUNT/home"
- mount "${USB_DEVICE}3" "$USB_MOUNT/home"
- # bind-mount boot:
- mkdir -p "$USB_MOUNT/boot"
- mkdir -p "$USB_MOUNT/ESP/$HOST_NAME"
- mount --bind "$USB_MOUNT/ESP/$HOST_NAME" "$USB_MOUNT/boot"
- msg "${USB_DEVICE}$HOST_PART ${USB_DEVICE}2 ${USB_DEVICE}3 mounted"
- }
- usb_umount() {
- # umount USB partitions:
- local FLAG_UMOUNT='false'
- if (mountpoint "$USB_MOUNT" &>/dev/null); then
- printf '%s==>%s umount%s' "$C_GREEN_B" "$C_WHITE_B" "$C_GRAY"
- FLAG_UMOUNT='true'
- fi
- # umount boot:
- if (mountpoint "$USB_MOUNT/boot" &>/dev/null); then
- umount "$USB_MOUNT/boot"
- printf '%s %s/boot%s' "$C_WHITE_B" "$USB_MOUNT" "$C_GRAY"
- fi
- # umount ESP:
- if (mountpoint "$USB_MOUNT/ESP" &>/dev/null); then
- umount "$USB_MOUNT/ESP"
- printf '%s %s/ESP%s' "$C_WHITE_B" "$USB_MOUNT" "$C_GRAY"
- fi
- # umount home:
- if (mountpoint "$USB_MOUNT/home" &>/dev/null); then
- umount "$USB_MOUNT/home"
- printf '%s %s/home%s' "$C_WHITE_B" "$USB_MOUNT" "$C_GRAY"
- fi
- # umount root:
- if (mountpoint "$USB_MOUNT" &>/dev/null); then
- umount "$USB_MOUNT"
- printf '%s %s%s' "$C_WHITE_B" "$USB_MOUNT" "$C_GRAY"
- fi
- if [ "$FLAG_UMOUNT" = 'true' ]; then
- printf '\n'
- fi
- }
- usb_sync() {
- # sync USB memory:
- msg "syncing $USB_DEVICE..."
- sync
- }
- mk-usb_kill() {
- # kill trap:
- usb_umount
- exit 3
- }
- ##============================= SCRIPT =============================##
- # exit on error:
- set -e
- # FAIL: must be run as root:
- if [ "$EUID" -ne 0 ]; then
- msg_error 'must be run as root'
- exit 2
- fi
- # verify packages and settings:
- msg 'Verifying packages and settings...'
- USB_DEVICE="$1"
- # FAIL: no argument given:
- if [ -z "$USB_DEVICE" ]; then
- msg_error 'no argument given'
- exit 1
- # FAIL: argument not a device:
- elif ! [[ "$USB_DEVICE" =~ ^/dev/sd[a-z]$ ]]; then
- msg_error "not a device: $USB_DEVICE"
- exit 1
- fi
- # FAIL: invalid hostname HOST1_NAME:
- if ! [[ "$HOST1_NAME" =~ ^[a-z0-9\-]+$ ]]; then
- msg_error "invalid hostname: $HOST1_NAME"
- exit 1
- fi
- # FAIL: invalid hostname HOST2_NAME:
- if ! [[ "$HOST2_NAME" =~ ^[a-z0-9\-]+$ ]]; then
- msg_error "invalid hostname: $HOST2_NAME"
- exit 1
- fi
- # FAIL: hostnames not unique:
- if [ "$HOST1_NAME" = "$HOST2_NAME" ]; then
- msg_error "non-unique hostnames"
- exit 1
- fi
- # FAIL: invalid language HOST_LANG:
- if ! (grep -P "^\s?#?\s?$HOST_LANG" /etc/locale.gen &>/dev/null); then
- msg_error "invalid language: $HOST_LANG"
- exit 1
- fi
- # FAIL: invalid time zone HOST_TIMEZONE:
- if ! [ -f "/usr/share/zoneinfo/$HOST_TIMEZONE" ]; then
- msg_error "invalid time zone: $HOST_TIMEZONE"
- exit 1
- fi
- # FAIL: invalid username USER_NAME:
- if ! [[ "$USER_NAME" =~ ^[A-Za-z0-9\-]+$ ]]; then
- msg_error "invalid username: $USER_NAME"
- exit 1
- fi
- # FAIL: keymap not valid:
- if [ -n "$HOST_KEYMAP" ] && [ ! -f "$HOST_KEYMAP" ]; then
- msg_error "invalid keymap: $HOST_KEYMAP"
- elif [ -z "$HOST_KEYMAP" ]; then
- HOST_KEYMAP='/usr/share/kbd/keymaps/i386/qwerty/us.map.gz'
- fi
- # FAIL: device mounted:
- if (grep "$USB_DEVICE" /proc/mounts &>/dev/null); then
- msg_error "already mounted: $USB_DEVICE"
- exit 1
- fi
- # FAIL: no internet connection:
- if ! (internet_status); then
- msg_error 'no internet connection'
- exit 1
- fi
- # FAIL: mount point not clear:
- if (mountpoint "$USB_MOUNT" &>/dev/null) || \
- { [ -d "$USB_MOUNT" ] && [ -n "$(ls -A "$USB_MOUNT")" ]; }; then
- msg_error "$USB_MOUNT not clear"
- exit 1
- fi
- # get USB device info:
- USB_INFO="$(fdisk --color=never -l "$USB_DEVICE")"
- USB_SIZE_BYTES="$(grep -Po "$USB_DEVICE:.+ \K[0-9]+(?= bytes)" \
- <<< "$USB_INFO")"
- USB_SIZE_HUMAN="$(grep -Po "$USB_DEVICE: *\K[^,]+" <<< "$USB_INFO")"
- USB_BLOCK_SIZE="$(grep -Po "Sector size.* \K[0-9]+" <<< "$USB_INFO")"
- USB_BLOCK_COUNT=$((USB_SIZE_BYTES / USB_BLOCK_SIZE))
- # FAIL: USB not big enough:
- if [ "$USB_SIZE_BYTES" -lt 15560000000 ]; then
- msg_error 'USB must be at least 14.5G'
- exit 1
- fi
- # FAIL: package not found:
- for PKG in $(printf '%s\n%s\n' "${HOST1_PKGS[@]}" "${HOST2_PKGS[@]}" | \
- sort -u); do if ! (pacman -Si "$PKG" &>/dev/null) && \
- ! (pacman -Sg "$PKG" &>/dev/null); then
- msg_error "package not found: $PKG"
- exit 1
- fi; done
- # warn: all data will be lost:
- msg_warn "All data on $USB_DEVICE ($USB_SIZE_HUMAN) will be lost."
- msg_ask 'Proceed with installation? [Y/n]'
- read -r ANSWER
- if [[ "${ANSWER,,}" =~ ^(n|no)$ ]]; then
- exit 0
- fi
- # zero out USB:
- msg_ask 'Zero out USB? [Y/n]'
- read -r ANSWER
- if ! [[ "${ANSWER,,}" =~ ^(n|no)$ ]]; then
- dd status=progress if=/dev/zero of="$USB_DEVICE" \
- bs="$USB_BLOCK_SIZE" count="$USB_BLOCK_COUNT"
- fi
- # partition USB:
- msg 'Partitioning USB...'
- sgdisk -o \
- -n 1:0:+10M -t 1:EF02 \
- -n 2:0:+500M -t 2:EF00 \
- -n "3:0:+$HOME_SIZE" -t 3:8300 \
- -n "${HOST1_PART}:0:+$HOST1_SIZE" -t 3:8300 \
- -n "${HOST2_PART}:0:-5M" -t 4:8300 \
- "$USB_DEVICE"
- # format partitions:
- msg 'Formatting partitions...'
- mkfs.fat -F32 "${USB_DEVICE}2"
- mkfs.ext4 -F "${USB_DEVICE}3"
- mkfs.ext4 -F "${USB_DEVICE}4"
- mkfs.ext4 -F "${USB_DEVICE}5"
- # set kill umount trap:
- trap mk-usb_kill ABRT EXIT HUP ILL INT QUIT SYS TERM
- # HOST2:
- usb_mount "$HOST2_NAME"
- usb_pacstrap
- usb_config
- usb_config_capslock_to_escape
- usb_config_large_memory_pages_enable
- usb_config_autologin_enable
- usb_config_sudo_lecture_disable
- usb_issue_copy
- usb_root_copy
- usb_user_add
- usb_user_copy
- usb_user_repo_add
- usb_reboot_script_make
- usb_umount
- usb_sync
- # HOST1: installed second to handle grub configuration:
- usb_mount "$HOST1_NAME"
- usb_pacstrap
- usb_config
- usb_config_grub
- usb_config_shim_signed
- usb_config_capslock_to_escape
- usb_config_large_memory_pages_enable
- usb_config_autologin_enable
- usb_config_sudo_lecture_disable
- usb_issue_copy
- usb_root_copy
- usb_user_add
- usb_user_repo_add
- usb_reboot_script_make
- usb_umount
- usb_sync
- msg "$USB_DEVICE Arch Linux install complete"
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement