install.py (11274B)
1 # progress(step, percent): 0 partition, 1 copy, 2 configure, 3 bootloader, 4 cleanup. 2 # the netinst puts the desktop in after the copy: 2 desktop, and the rest one later 3 import glob 4 import os 5 import shutil 6 import subprocess 7 import sys 8 import time 9 10 import account 11 import disks 12 from choices import console_keymap 13 from disks import run 14 15 SOURCE = "/run/chamesle/rootfs/" 16 T = "/target" 17 # live session only 18 LIVE_ONLY = ["/etc/sudoers.d/live", "/etc/polkit-1/rules.d/49-live.rules", "/etc/lightdm/lightdm.conf.d/90-live.conf", 19 "/etc/sddm.conf.d/90-live.conf", "/usr/share/applications/chamesle-install.desktop", "/usr/bin/chamesle-install", 20 "/usr/bin/live-installer", "/usr/lib/live-installer", "/usr/share/live-installer", 21 "/usr/share/icons/live-installer-progress-dot-on.png", "/usr/share/icons/live-installer-progress-dot-off.png", 22 "/var/lib/AccountsService/users/live", "/usr/bin/jiggity", "/etc/chamesle/autologin"] 23 24 25 def chroot(*cmd, input=None): 26 return run("chroot", T, *cmd, input=input) 27 28 29 def uuid(dev): 30 return subprocess.run(["blkid", "-s", "UUID", "-o", "value", dev], text=True, stdout=subprocess.PIPE).stdout.strip() 31 32 33 def set_grub(key, value): 34 path = T + "/etc/default/grub" 35 lines = open(path).read().split("\n") if os.path.exists(path) else [] 36 line = '%s="%s"' % (key, value) 37 for i, l in enumerate(lines): 38 if l.lstrip("#").startswith(key + "="): 39 lines[i] = line 40 break 41 else: 42 lines.append(line) 43 with open(path, "w") as f: 44 f.write("\n".join(lines).rstrip("\n") + "\n") 45 46 47 # what was picked for seeing the screen: {"reader": bool, "contrast": bool, "text": bool}. chamesle-access puts it 48 # on the desktop the first time someone signs in, the sign-in screen reads it from here 49 def save_access(access): 50 with open(T + "/etc/chamesle/access", "w") as f: 51 f.writelines("%s=%d\n" % (k, access.get(k, False)) for k in ("reader", "contrast", "text")) 52 # gdm's own sign-in screen: through its dconf, over the defaults it ships 53 if os.path.isdir(T + "/etc/gdm") and any(access.values()): 54 os.makedirs(T + "/etc/dconf/db/gdm.d", exist_ok=True) 55 with open(T + "/etc/dconf/profile/gdm", "w") as f: 56 f.write("user-db:user\nsystem-db:gdm\nfile-db:/usr/share/gdm/greeter-dconf-defaults\n") 57 with open(T + "/etc/dconf/db/gdm.d/90-chamesle-access", "w") as f: 58 f.write("[org/gnome/desktop/a11y/applications]\nscreen-reader-enabled=%s\n\n" % str(access.get("reader", False)).lower()) 59 f.write("[org/gnome/desktop/a11y/interface]\nhigh-contrast=%s\n\n" % str(access.get("contrast", False)).lower()) 60 f.write("[org/gnome/desktop/interface]\ntext-scaling-factor=%s\n" % ("1.25" if access.get("text") else "1.0")) 61 chroot("dconf", "update") 62 # a beep when the boot menu is up, so it's known it's there 63 if access.get("reader"): 64 set_grub("GRUB_INIT_TUNE", "480 440 1") 65 66 67 def install(row, passphrase, language, keyboard, progress, desktop=None, user=None, access=None): 68 os.makedirs(T, exist_ok=True) 69 if desktop: 70 step = progress 71 progress = lambda n, pc: step(n + (n >= 2), pc) 72 progress(0, 0) 73 plan = disks.make(row, passphrase is not None) 74 progress(0, 40) 75 root = plan.root 76 if passphrase is not None: 77 run("cryptsetup", "luksFormat", "--batch-mode", "--type", "luks2", "--key-file=-", plan.root, input=passphrase) 78 run("cryptsetup", "open", "--key-file=-", plan.root, "chamesle", input=passphrase) 79 root = "/dev/mapper/chamesle" 80 if plan.format_efi: 81 run("mkfs.fat", "-F", "32", "-n", "EFI", plan.efi) 82 if plan.boot: 83 run("mkfs.ext4", "-F", "-q", "-L", "boot", plan.boot) 84 run("mkfs.btrfs", "-f", "-L", "Chamesle", root) 85 run("mount", root, T) 86 run("btrfs", "subvolume", "create", T + "/@", T + "/@home") 87 run("umount", T) 88 run("mount", "-o", "subvol=@,compress=zstd:1", root, T) 89 os.makedirs(T + "/home", exist_ok=True) 90 run("mount", "-o", "subvol=@home,compress=zstd:1", root, T + "/home") 91 if plan.boot: 92 os.makedirs(T + "/boot", exist_ok=True) 93 run("mount", plan.boot, T + "/boot") 94 if plan.efi: 95 os.makedirs(T + "/boot/efi", exist_ok=True) 96 run("mount", plan.efi, T + "/boot/efi") 97 progress(0, 100) 98 99 # progress = files copied / inodes used on the live root 100 total = int(subprocess.run(["df", "--output=iused", SOURCE], text=True, stdout=subprocess.PIPE).stdout.split()[-1]) 101 excludes = [] 102 for d in "home/* dev/* proc/* sys/* tmp/* run/* mnt/* media/* lost+found".split(): 103 excludes += ["--exclude=/" + d] 104 p = subprocess.Popen(["rsync", "--archive", "--hard-links", "--acls", "--xattrs", "--no-D", "--out-format=%n"] 105 + excludes + [SOURCE, T + "/"], text=True, errors="ignore", stdout=subprocess.PIPE) 106 done = 0 107 for _ in p.stdout: 108 done += 1 109 if done % 200 == 0: 110 progress(1, min(99, done * 100 // max(total, 1))) 111 if p.wait(): 112 raise RuntimeError("copying the files failed") 113 progress(1, 100) 114 115 for d in ("dev", "proc", "sys", "run"): 116 os.makedirs(T + "/" + d, exist_ok=True) 117 run("mount", "--rbind", "/" + d, T + "/" + d) 118 run("mount", "--make-rslave", T + "/" + d) 119 for path in LIVE_ONLY: 120 run("rm", "-rf", T + path) 121 if os.path.exists(T + "/etc/gdm/custom.conf"): 122 run("sed", "-i", "/^# the live session only$/d; /^AutomaticLogin/d", T + "/etc/gdm/custom.conf") 123 if "\nlive:" in "\n" + open(T + "/etc/passwd").read(): 124 chroot("userdel", "-r", "live") 125 chroot("passwd", "-dl", "root") 126 if desktop: 127 add_desktop(desktop, step, language) 128 else: 129 open(T + "/etc/chamesle/firstboot", "w").close() 130 progress(2, 30) 131 132 with open(T + "/etc/fstab", "w") as f: 133 r = uuid(root) 134 f.write("# made by Jiggity\nproc /proc proc defaults 0 0\n") 135 f.write("UUID=%s / btrfs defaults,subvol=@,compress=zstd:1 0 0\n" % r) 136 f.write("UUID=%s /home btrfs defaults,subvol=@home,compress=zstd:1 0 0\n" % r) 137 if plan.boot: 138 f.write("UUID=%s /boot ext4 defaults 0 2\n" % uuid(plan.boot)) 139 if plan.efi: 140 f.write("UUID=%s /boot/efi vfat umask=0077 0 2\n" % uuid(plan.efi)) 141 if passphrase is not None: 142 with open(T + "/etc/crypttab", "a") as f: 143 f.write("chamesle UUID=%s none luks,discard,tries=3\n" % uuid(plan.root)) 144 progress(2, 50) 145 146 locale = language + ".UTF-8" 147 path = T + "/etc/default/libc-locales" 148 lines = open(path).read().split("\n") if os.path.exists(path) else [] 149 if locale + " UTF-8" not in [l.lstrip("#").strip() for l in lines]: 150 lines.append(locale + " UTF-8") 151 lines = [locale + " UTF-8" if l.lstrip("#").strip() == locale + " UTF-8" else l for l in lines] 152 with open(path, "w") as f: 153 f.write("\n".join(lines).rstrip("\n") + "\n") 154 chroot("locale-gen") 155 with open(T + "/etc/locale.conf", "w") as f: 156 f.write("LANG=%s\n" % locale) 157 layout, variant = keyboard 158 os.makedirs(T + "/etc/X11/xorg.conf.d", exist_ok=True) 159 with open(T + "/etc/X11/xorg.conf.d/00-keyboard.conf", "w") as f: 160 f.write('Section "InputClass"\n Identifier "system-keyboard"\n MatchIsKeyboard "on"\n' 161 ' Option "XkbLayout" "%s"\n Option "XkbVariant" "%s"\nEndSection\n' % (layout, variant or "")) 162 with open(T + "/etc/vconsole.conf", "w") as f: 163 f.write("KEYMAP=%s\n" % console_keymap(layout, variant)) 164 if user: 165 account.make(user["zone"], user["user"], user["name"], user["password"], user["host"], T) 166 if access is not None: 167 save_access(access) 168 progress(2, 100) 169 170 if passphrase is not None: 171 set_grub("GRUB_CMDLINE_LINUX", "rd.luks.uuid=%s" % uuid(plan.root)) 172 chroot("dracut", "--force", "--regenerate-all") 173 progress(3, 40) 174 cmdline = "quiet splash loglevel=3 drm.panic_screen=qr_code" 175 if os.path.exists(T + "/usr/lib/chamesle/init"): 176 cmdline += " init=/usr/lib/chamesle/init" 177 set_grub("GRUB_DISTRIBUTOR", "Chamesle") 178 set_grub("GRUB_CMDLINE_LINUX_DEFAULT", cmdline) 179 set_grub("GRUB_DISABLE_OS_PROBER", "false") 180 # our kernel first, void's below it for when ours doesn't start 181 ours = sorted(glob.glob(T + "/boot/vmlinuz-*-cachyos-lts")) 182 if ours: 183 set_grub("GRUB_TOP_LEVEL", ours[-1][len(T):]) 184 set_grub("GRUB_GFXMODE", "auto") 185 set_grub("GRUB_GFXPAYLOAD_LINUX", "keep") 186 if os.path.exists(T + "/usr/share/grub/themes/chamesle/theme.txt"): 187 set_grub("GRUB_THEME", "/usr/share/grub/themes/chamesle/theme.txt") 188 if disks.efi(): 189 chroot("grub-install", "--target=x86_64-efi", "--efi-directory=/boot/efi", "--bootloader-id=chamesle") 190 # some firmware only boots the fallback path 191 chroot("grub-install", "--target=x86_64-efi", "--efi-directory=/boot/efi", "--removable", "--no-nvram") 192 else: 193 chroot("grub-install", "--target=i386-pc", row.disk) 194 progress(3, 70) 195 chroot("grub-mkconfig", "-o", "/boot/grub/grub.cfg") 196 if "Chamesle" not in open(T + "/boot/grub/grub.cfg").read(): 197 raise RuntimeError("the boot menu came out without Chamesle in it") 198 progress(3, 100) 199 200 os.sync() 201 subprocess.run(["umount", "-R", T]) 202 if passphrase is not None: 203 subprocess.run(["cryptsetup", "close", "chamesle"]) 204 progress(4, 100) 205 206 207 # the netinst: the desktop from the repo, with apk's progress. the wi-fi jiggity connected to comes along 208 def add_desktop(desktop, step, language): 209 step(2, 0) 210 shutil.copy("/etc/resolv.conf", T + "/etc/resolv.conf") 211 nm = "/etc/NetworkManager/system-connections" 212 if os.path.isdir(nm): 213 os.makedirs(T + nm, exist_ok=True) 214 for f in os.listdir(nm): 215 shutil.copy2(os.path.join(nm, f), T + nm) 216 # noto-fonts-ttf has no chinese, japanese or korean 217 extra = "noto-fonts-cjk-sans" if language[:2] in ("ja", "ko", "zh") else "" 218 # sourceforge's mirrors drop out now and then, for long enough that chamesle-get's own tries on a file all 219 # fail. it keeps what it got, so going again only fetches what's missing 220 for attempt in range(4): 221 if attempt: 222 print("jiggity: the desktop didn't install, again in a minute (%d of 3)" % attempt, file=sys.stderr) 223 time.sleep(60) 224 r, w = os.pipe() 225 print("jiggity: chamesle-desktop", desktop) 226 p = subprocess.Popen(["chroot", T, "/usr/bin/env", "CHAMESLE_PROGRESS=%d" % w, "CHAMESLE_EXTRA=" + extra, 227 "/usr/bin/chamesle-desktop", desktop], 228 pass_fds=(w,), stdout=sys.stderr, stderr=sys.stderr) 229 os.close(w) 230 with os.fdopen(r) as f: 231 for line in f: 232 done, _, total = line.strip().partition("/") 233 if done.isdigit() and total.isdigit() and int(total): 234 step(2, min(99, int(done) * 100 // int(total))) 235 if not p.wait(): 236 break 237 else: 238 raise RuntimeError("installing the desktop failed, check the internet connection") 239 shutil.copy("/usr/share/chamesle/repositories", T + "/etc/apk/repositories") 240 step(2, 100)