system

the Chamesle handbook, init, live ISO and install helpers
git clone https://github.com/tortr-rs/system
Log | Files | Refs | README | LICENSE

chamesle-install (8925B)


      1 #!/bin/sh
      2 # chamesle-install: the install of chapter 3 of the handbook, asked for in dialog boxes. it takes a whole disk and runs
      3 # chamesle-disk, chamesle-bootstrap and chamesle-boot with what you chose.
      4 die() { clear; echo "chamesle-install: $*" >&2; exit 1; }
      5 [ "$(id -u)" = 0 ] || die "needs root"
      6 command -v dialog >/dev/null || die "dialog is not installed: mag install dialog"
      7 T="Chamesle"
      8 d() { dialog --stdout --backtitle "$T installer" "$@"; }
      9 msg() { d --msgbox "$1" 0 0; }
     10 log=/var/log/chamesle-install.log
     11 
     12 # console keymap and X layout; the console one is loaded right away for the rest of the questions
     13 km=$(d --default-item us --menu "The keyboard" 0 0 8 \
     14 	us "US (QWERTY)" fr "French (AZERTY)" de "German (QWERTZ)" gb "British" es "Spanish" it "Italian" \
     15 	be "Belgian (AZERTY)" pt "Portuguese" ch-fr "Swiss French" us-dvorak "Dvorak" pl "Polish") || die "left"
     16 case $km in *-*) xkb="${km%%-*} ${km#*-}";; *) xkb=$km;; esac
     17 # the console's names for it; on musl the xkb keymaps do not load, the legacy ones do, by their path
     18 case $km in gb) km=uk;; be|pt) km=$km-latin1;; ch-fr) km=fr_CH;; us-dvorak) km=dvorak;; pl) km=pl2;; esac
     19 kmap() { find /usr/share/keymaps/legacy -name "$km.map.gz" | head -n 1; }
     20 loadkeys "$(kmap)" >/dev/null 2>&1
     21 
     22 # three tries: right after boot the first lookup can be slow
     23 online() { for i in 1 2 3; do curl -fsS -m 8 -o /dev/null https://chamesle.org/repo/x86_64/APKINDEX.tar.gz 2>/dev/null && return; sleep 2; done; return 1; }
     24 while ! online; do
     25 	c=$(d --menu "There is no network. A cable just works; for wifi, connect with iwctl:\n  station wlan0 scan\n  station wlan0 get-networks\n  station wlan0 connect NAME" 0 0 8 \
     26 		wifi "connect in iwctl, then come back" retry "try again" quit "leave the installer") || die "left"
     27 	case $c in
     28 	wifi) clear; iwctl;;
     29 	quit) die "left";;
     30 	esac
     31 done
     32 
     33 disks=
     34 for b in /sys/block/*; do
     35 	n=${b##*/}
     36 	case $n in loop*|ram*|sr*|zram*|fd*|md*|dm-*) continue;; esac
     37 	grep -q "^/dev/$n" /proc/mounts && continue
     38 	[ "$(cat "$b/size")" -gt 0 ] || continue
     39 	disks="$disks /dev/$n '$(lsblk -dno SIZE,MODEL "/dev/$n" | tr -cd 'A-Za-z0-9 ._-' | tr -s ' ')'"
     40 done
     41 [ -n "$disks" ] || die "there is no disk to install on"
     42 disk=$(eval d --menu "'Which disk? Everything on it will be erased.'" 0 0 8 "$disks") || die "left"
     43 
     44 libc=musl
     45 [ "$(uname -m)" = x86_64 ] && { libc=$(d --menu "Which Chamesle?" 10 70 2 \
     46 	musl "the small one, its own base" \
     47 	glibc "Artix, Arch and the AUR: NVIDIA, Steam") || die "left"; }
     48 
     49 if [ $libc = glibc ]; then
     50 	fs=$(d --menu "The file system of the root" 0 0 8 \
     51 		btrfs "btrfs: compression, snapshots, subvolumes @ and @home" \
     52 		ext4 "ext4: plain and simple") || die "left"
     53 else
     54 	fs=$(d --default-item zfs --menu "The file system of the root" 0 0 8 \
     55 		zfs "ZFS: compression, snapshots, checksums (the handbook's default)" \
     56 		btrfs "btrfs: compression, snapshots, subvolumes @ and @home" \
     57 		ext4 "ext4: plain and simple") || die "left"
     58 fi
     59 
     60 loader=$(d --menu "The bootloader" 0 0 8 \
     61 	limine "limine: small, UEFI and BIOS" \
     62 	grub "GRUB: UEFI and BIOS") || die "left"
     63 
     64 # the firmware the hardware here wants, ticked already
     65 has() { for v in /sys/bus/pci/devices/*; do [ "$(cat $v/vendor)" = "$1" ] && case $(cat $v/class) in $2*) return 0;; esac; done; return 1; }
     66 on() { has "$@" && echo on || echo off; }
     67 kernel=
     68 if [ $libc = glibc ]; then
     69 	kernel=$(d --menu "The kernel" 0 0 8 \
     70 		linux-lts "the long term one (6.18)" \
     71 		linux "the newest (7.x): new hardware, sound on some laptops wants it") || die "left"
     72 	intel=off
     73 	{ has 0x8086 0x0280 || has 0x8086 0x03; } && intel=on
     74 	fw=$(d --separate-output --checklist "Firmware for the hardware (ticked: what this machine has)" 0 0 8 \
     75 		linux-firmware-intel "Intel wifi, bluetooth and graphics" $intel \
     76 		linux-firmware-amdgpu "AMD graphics" $(on 0x1002 0x03) \
     77 		linux-firmware-realtek "Realtek wifi and ethernet" $(on 0x10ec 0x02) \
     78 		linux-firmware-mediatek "MediaTek wifi" $(on 0x14c3 0x0280) \
     79 		linux-firmware-atheros "Qualcomm Atheros wifi" $(on 0x168c 0x0280) \
     80 		linux-firmware-broadcom "Broadcom wifi" $(on 0x14e4 0x0280) \
     81 		linux-firmware-nvidia "NVIDIA graphics" $(on 0x10de 0x03)) || die "left"
     82 	nvidia=
     83 	has 0x10de 0x03 && d --yesno "There is an NVIDIA card. Install NVIDIA's driver (nvidia-open), not nouveau?" 0 0 && nvidia="nvidia-open$([ $kernel = linux-lts ] && echo -lts) nvidia-utils"
     84 else
     85 fw=$(d --separate-output --checklist "Firmware for the hardware (ticked: what this machine has)" 0 0 8 \
     86 	linux-firmware-intel "Intel wifi and bluetooth" $(on 0x8086 0x0280) \
     87 	linux-firmware-i915 "Intel graphics" $(on 0x8086 0x03) \
     88 	linux-firmware-amdgpu "AMD graphics" $(on 0x1002 0x03) \
     89 	linux-firmware-rtw88 "Realtek wifi (older)" $(on 0x10ec 0x0280) \
     90 	linux-firmware-rtw89 "Realtek wifi (newer)" off \
     91 	linux-firmware-mediatek "MediaTek wifi" $(on 0x14c3 0x0280) \
     92 	linux-firmware-nvidia "NVIDIA graphics (nouveau)" $(on 0x10de 0x03) \
     93 	linux-firmware-rtl_nic "Realtek ethernet" $(on 0x10ec 0x0200)) || die "left"
     94 fi
     95 
     96 wifi=$(d --menu "Wifi on the installed system" 0 0 8 \
     97 	iwd "iwd, connect with iwctl" \
     98 	networkmanager "NetworkManager, connect with nmtui" \
     99 	none "cable only") || die "left"
    100 
    101 host=$(d --inputbox "The host name" 0 0 chamesle) || die "left"
    102 host=$(echo "$host" | tr -cd 'a-zA-Z0-9-')
    103 [ -n "$host" ] || host=chamesle
    104 
    105 pw() {
    106 	while :; do
    107 		a=$(d --insecure --passwordbox "$1" 0 0) || die "left"
    108 		b=$(d --insecure --passwordbox "$1, again" 0 0) || die "left"
    109 		[ -n "$a" ] && [ "$a" = "$b" ] && { echo "$a"; return; }
    110 		msg "They are not the same, or empty. Again."
    111 	done
    112 }
    113 rootpw=$(pw "The root password")
    114 user=$(d --inputbox "Your user name (empty for none)" 0 0) || die "left"
    115 user=$(echo "$user" | tr -cd 'a-z0-9_-')
    116 [ -n "$user" ] && userpw=$(pw "The password of $user")
    117 
    118 d --yesno "Install now?\n\n  disk        $disk (ERASED)\n  Chamesle    $libc${kernel:+, $kernel}\n  keyboard    $km\n  file system $fs\n  bootloader  $loader\n  firmware    $(echo $fw)\n  network     $wifi\n  host name   $host\n  user        ${user:-none}" 0 0 || die "nothing was changed"
    119 
    120 pkgs=
    121 case $libc/$wifi in
    122 musl/iwd) pkgs=iwd;;
    123 musl/networkmanager) pkgs="networkmanager networkmanager-wifi networkmanager-tui";;
    124 glibc/iwd) pkgs="iwd iwd-openrc";;
    125 glibc/networkmanager) pkgs="networkmanager networkmanager-openrc";;
    126 esac
    127 pkgs="$pkgs kbd"
    128 [ $libc = musl ] && pkgs="$pkgs kbd-legacy"
    129 if [ $libc = glibc ]; then
    130 	first= rest="$(echo $fw) $nvidia"
    131 else
    132 	first=$(echo $fw | cut -d' ' -f1)
    133 	rest=$(echo $fw | cut -s -d' ' -f2-)
    134 fi
    135 
    136 step() { echo "==> $1" >> $log; d --infobox "$1" 3 60; }
    137 : > $log
    138 (
    139 	step "Making the file systems on $disk"
    140 	chamesle-disk -t $fs -y "$disk" >> $log 2>&1 || exit 1
    141 	step "Installing the base (a few minutes)"
    142 	n=0
    143 	until chamesle-bootstrap -l $libc -n "$host" ${kernel:+-k $kernel} ${first:+-f $first} /mnt $rest $pkgs >> $log 2>&1; do
    144 		n=$((n + 1))
    145 		[ $n -lt 3 ] || exit 1
    146 		sleep 5
    147 	done
    148 	step "Setting the passwords and the user"
    149 	echo "root:$rootpw" | arch-chroot /mnt chpasswd >> $log 2>&1 || exit 1
    150 	if [ -n "$user" ] && [ $libc = glibc ]; then
    151 		arch-chroot /mnt useradd -m -G wheel,video,input,audio "$user" >> $log 2>&1 || exit 1
    152 		echo "$user:$userpw" | arch-chroot /mnt chpasswd >> $log 2>&1 || exit 1
    153 	elif [ -n "$user" ]; then
    154 		arch-chroot /mnt adduser -D "$user" >> $log 2>&1 || exit 1
    155 		echo "$user:$userpw" | arch-chroot /mnt chpasswd >> $log 2>&1 || exit 1
    156 		for g in wheel video input audio netdev; do arch-chroot /mnt addgroup "$user" $g >> $log 2>&1; done
    157 	fi
    158 	step "Turning on the services"
    159 	arch-chroot /mnt rc-update add nftables boot >> $log 2>&1   # the firewall, before the network comes up
    160 	case $wifi in
    161 	iwd) arch-chroot /mnt rc-update add iwd default >> $log 2>&1;;
    162 	networkmanager)
    163 		arch-chroot /mnt rc-update del dhcpcd default >> $log 2>&1
    164 		[ $libc = glibc ] && s=NetworkManager || s=networkmanager
    165 		arch-chroot /mnt rc-update add $s default >> $log 2>&1;;
    166 	esac
    167 	[ $libc = glibc ] && [ $wifi != networkmanager ] && arch-chroot /mnt rc-update add dhcpcd default >> $log 2>&1
    168 	# the keymap service is keymaps on glibc, loadkeys on musl
    169 	if [ $libc = glibc ]; then ks=keymaps k=$km; else ks=loadkeys k=$(kmap); fi
    170 	sed -i "s|^keymap=.*|keymap=\"$k\"|" /mnt/etc/conf.d/$ks
    171 	arch-chroot /mnt rc-update add $ks boot >> $log 2>&1
    172 	mkdir -p /mnt/etc/X11/xorg.conf.d
    173 	set -- $xkb
    174 	printf 'Section "InputClass"\n\tIdentifier "keyboard"\n\tMatchIsKeyboard "on"\n\tOption "XkbLayout" "%s"\n%bEndSection\n' "$1" \
    175 		"${2:+\tOption \"XkbVariant\" \"$2\"\n}" > /mnt/etc/X11/xorg.conf.d/00-keyboard.conf
    176 	step "Installing the bootloader ($loader)"
    177 	chamesle-boot -b $loader /mnt >> $log 2>&1 || exit 1
    178 	cp $log /mnt/var/log/ 2>/dev/null
    179 	umount -R /mnt
    180 	[ $fs = zfs ] && zpool export rpool
    181 	exit 0
    182 ) || { d --title "It failed" --textbox $log 0 0; die "the install failed, the log is $log"; }
    183 
    184 d --yesno "Chamesle is installed on $disk.\n\nTake out the USB stick and reboot?" 0 0 && reboot
    185 clear