commit 8bb96efb0de605abdce40357401385080d4b4e59
parent 312022e73d2bbfdc24b0c72d2b2b825d37c5e43b
Author: torter <koubbamohamedrayan@gmail.com>
Date: Sat, 14 Nov 2026 19:26:08 +0000
zfs root: chamesle-disk and chamesle-chroot, zfs in the initramfs and on the ISO, a compiler in the base set, the live ISO carries mag's recipes
Diffstat:
10 files changed, 285 insertions(+), 49 deletions(-)
diff --git a/files/etc/rc b/files/etc/rc
@@ -11,6 +11,7 @@ mountpoint -q /dev/pts || mount -t devpts devpts /dev/pts
mountpoint -q /dev/shm || mount -t tmpfs -o mode=1777 tmpfs /dev/shm
[ -d /run/live ] || mountpoint -q /run || mount -t tmpfs tmpfs /run
mount -a
+command -v zfs >/dev/null && zfs mount -a
swapon -a
mountpoint -q /tmp || rm -rf /tmp/* /tmp/.[!.]*
chmod 1777 /tmp
diff --git a/handbook/handbook.md b/handbook/handbook.md
@@ -17,8 +17,8 @@
### 1.1 What it is
Chamesle is a Linux distribution you build from source and put on a disk yourself. There is no installer: you
-partition, format, build the root file system, write the configuration and the boot entry by hand. This handbook goes
-through every step.
+install the root file system, write the configuration and the boot entry by hand. `chamesle-disk` partitions and formats the
+disk for you if you want, and this handbook goes through every other step.
The parts:
@@ -37,7 +37,7 @@ and `gmake`, which musl's Makefile needs.
The package repository is NetBSD's pkgsrc, read directly by `mag` without `bmake`. Chamesle adds hand-written recipes
only for what pkgsrc does not have: musl, the kernel, its headers and firmware, limine, eudev, kmod, util-linux, iw,
-libnl, iproute2, usbutils, netbsd-curses and libtool. Everything is built on the machine that installs it, so the first
+libnl, iproute2, usbutils, netbsd-curses, libtool and ZFS. Everything is built on the machine that installs it, so the first
install takes a while (LLVM is the longest).
### 1.2 The layout of the system
@@ -59,7 +59,7 @@ The PATH of a login shell is `/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin`. Pac
| directory | what is in it |
|-----------|---------------|
-| `system/` | `tools/chamesle-bootstrap`, `files/` (init, rc and the services), `iso/`, this handbook |
+| `system/` | `tools/` (`chamesle-bootstrap`, `chamesle-chroot`, `chamesle-disk`), `files/` (init, rc and the services), `iso/`, this handbook |
| `mag/` | `mag.c`, `build.sh`, `tests/run.sh`, `repo/` with the hand recipes and `PROVIDES` |
| `rifle/` | `rifle.c`, `build.sh`, `tests/run.sh` |
| pkgsrc | NetBSD's package tree, which you fetch yourself (chapter 2) |
@@ -98,7 +98,13 @@ Put `mag` in your PATH:
### 2.3 Getting the pkgsrc tree
-Take a pkgsrc tree from NetBSD and unpack it where you want it, for example `/usr/pkgsrc`:
+Let mag fetch a pkgsrc tree from NetBSD and unpack it into `/usr/pkgsrc`:
+
+ mag sync
+
+`sync` downloads `https://cdn.netbsd.org/pub/pkgsrc/current/pkgsrc.tar.xz` and replaces the tree with it, so it is also how
+the tree is brought up to date. `MAG_SYNC_URL` names another file, and under `MAG_ROOT` the tree goes into `$MAG_ROOT/usr/pkgsrc`.
+By hand it is the same thing:
cd /usr
curl -fsSLO https://cdn.netbsd.org/pub/pkgsrc/current/pkgsrc.tar.xz
@@ -114,7 +120,8 @@ when both have a package of the same name:
export MAG_REPO=$HOME/src/mag/repo:/usr/pkgsrc
-If `MAG_REPO` is not set, mag looks in `/usr/share/mag/repo`. `MAG_ROOT` says where to install; it is the new root
+If `MAG_REPO` is not set, mag looks in `/usr/share/mag/repo` (Chamesle's own recipes, which the live ISO and every
+bootstrapped root carry) and then in `/usr/pkgsrc`, where `mag sync` puts the tree. `MAG_ROOT` says where to install; it is the new root
during an installation (chapter 3) and unset on a running system.
A file called `PROVIDES` in a repo tells mag how our recipes meet pkgsrc's. Its lines are:
@@ -136,6 +143,7 @@ For example `converters/libiconv -` (musl has iconv) and `needs editors/vim dev-
* `JOBS` is `$JOBS` in the build steps, the number to give `make -j`. Without it mag uses 4
* `CFLAGS`, `CXXFLAGS`, `CPPFLAGS` and `LDFLAGS` are set for every build
* `PKG_DEFAULT_OPTIONS` and `PKG_OPTIONS.name` set pkgsrc options (section 4.6)
+* `CONFIGURE_ARGS.name` adds arguments to the `configure` of one pkgsrc package, such as `CONFIGURE_ARGS.libtirpc=--disable-gssapi`
The environment wins over the file: `MAG_JOBS`, `CFLAGS` and the others, when set, are used as they are.
@@ -145,17 +153,44 @@ The environment wins over the file: `MAG_JOBS`, `CFLAGS` and the others, when se
## 3. Installing by hand
Follow the BIOS or the UEFI variant where they differ. The disk here is `/dev/sda`; change it to yours. Everything below
-is run as root on the machine you install from, and it erases the disk.
+is run as root on the machine you install from, usually the live ISO, and it erases the disk.
+
+There are two ways through 3.1 and 3.2: `chamesle-disk` does the partitioning and the file systems and leaves the
+root on ZFS, or you do both yourself. From 3.3 on the steps are the same, and they are yours: `chamesle-disk` is only a helper.
-### 3.1 Partitioning
+### 3.1 chamesle-disk
-Three partitions on a GPT. The layout works for both ways of booting, so you can choose later.
+ chamesle-disk /dev/sda
+
+It lists the disk and asks you to type its name before it touches anything. Then it starts `cfdisk` so you lay out the
+partitions. Three of them work for BIOS and UEFI both:
| partition | size | type | use |
|-----------|------|------|-----|
-| `sda1` | 1M | BIOS boot | limine's BIOS code, not used on UEFI |
-| `sda2` | 512M | EFI System | `/boot`, FAT32 |
-| `sda3` | the rest | Linux filesystem | `/` |
+| 1 | 1M | BIOS boot | limine's BIOS code, not used on UEFI |
+| 2 | 512M | EFI System | `/boot`, FAT32 |
+| 3 | the rest | Linux filesystem | `/`, ZFS |
+
+Write the table and quit cfdisk. chamesle-disk lists the partitions and asks which one is the boot partition and which is
+the root. Then it does, in this order:
+
+ mkfs.fat -F32 BOOT
+ zpool create -f -o ashift=12 -O compression=zstd -O acltype=posixacl -O xattr=sa -O mountpoint=none -R /mnt rpool ROOT
+ zfs create -o mountpoint=/ -o canmount=noauto rpool/root
+ zfs create -o mountpoint=/home rpool/home
+ zfs create -o mountpoint=/var rpool/var
+ zpool set bootfs=rpool/root rpool
+
+and mounts `rpool/root`, `rpool/home` and `rpool/var` under `/mnt` and the boot partition on `/mnt/boot`. It also writes
+`/mnt/etc/fstab` with the boot partition by UUID and the `proc`, `sysfs` and `tmpfs` lines (an existing one is left alone). It writes nothing else: no
+`limine.conf`, no host name, no password. Those are 3.7 and 3.8.
+
+ZFS mounts its own datasets, so the fstab does not list them. limine cannot read ZFS: the kernel, the initrd and
+`limine.conf` live on the FAT partition, which is `/boot`, and the initramfs imports the pool (3.8).
+
+### 3.2 Partitioning and file systems by hand
+
+For a root on ext4. The layout is the one in 3.1, `sda1` to `sda3`:
sfdisk /dev/sda <<P
label: gpt
@@ -166,8 +201,6 @@ Three partitions on a GPT. The layout works for both ways of booting, so you can
On an NVMe disk the partitions are `/dev/nvme0n1p1` and so on. Replace `sda1`, `sda2` and `sda3` throughout.
-### 3.2 File systems and mounting
-
mkfs.fat -F32 /dev/sda2
mkfs.ext4 /dev/sda3
mount /dev/sda3 /mnt
@@ -175,20 +208,30 @@ On an NVMe disk the partitions are `/dev/nvme0n1p1` and so on. Replace `sda1`, `
mount /dev/sda2 /mnt/boot
The FAT partition is `/boot`. The kernel and `limine.conf` end up there, where limine looks for them on both BIOS and
-UEFI.
+UEFI. The root can be ZFS by hand as well: the `zpool` and `zfs` lines of 3.1 are all there is to it.
### 3.3 Copying the live system
The live ISO has no compiler, so on a machine booted from it the system is not built, it is copied. The read-only root of
the ISO is mounted on `/run/live/rootfs` and the ISO itself on `/run/live/medium`:
- cp -a /run/live/rootfs/. /mnt/
+ tar -C /run/live/rootfs --exclude ./etc/fstab -cf - . | tar -C /mnt -xpf -
+ [ -e /mnt/etc/fstab ] || cp /run/live/rootfs/etc/fstab /mnt/etc/fstab
cp /run/live/medium/boot/initrd /mnt/boot/initrd
-That is the whole base system, kernel and modules included, as it was when the ISO was made. The firmware for wifi
-and some network cards is not on the ISO. Add it with mag once the new system runs (section 4), or build everything instead (3.4), which needs a
+That is the whole base system, kernel, modules and ZFS included, as it was when the ISO was made. The copy keeps the
+fstab chamesle-disk wrote. The firmware for wifi and some network cards is not on the ISO. Add it with mag once the new system runs (section 4), or build everything instead (3.4), which needs a
machine with a compiler.
+Work inside the new root with `chamesle-chroot`:
+
+ chamesle-chroot /mnt
+ chamesle-chroot /mnt passwd root
+
+It mounts `proc`, `sys`, `dev`, `dev/pts` and a tmpfs on `run` under the directory, copies `/etc/resolv.conf` into it, runs
+`chroot` with an empty environment (`HOME=/root`, `TERM`, `PATH`) and takes the mounts away again when the command ends. Without a command it
+starts `/usr/bin/mksh -l`.
+
Then go on with 3.6 (fstab) and the rest. rifle (3.5) is on the ISO already, skip it. The kernel is in `/mnt/boot` already.
### 3.4 Building the system from source, instead
@@ -214,6 +257,12 @@ usr/lib usr/sbin usr/share usr/local/bin var/cache var/lib var/log var/tmp`, wit
sys-libs/musl devel/gmake archivers/libarchive archivers/pigz shells/mksh sys-apps/util-linux
sys-apps/kmod sys-apps/eudev net/dhcpcd www/curl sysutils/e2fsprogs sysutils/dosfstools sys-boot/limine
sys-kernel/linux sys-kernel/linux-firmware sys-firmware/sof-firmware sys-firmware/wireless-regdb editors/vim
+ sys-fs/zfs sys-kernel/zfs-module lang/clang devel/lld lang/compiler-rt lang/libunwind
+
+`lang/clang` (with `lang/llvm` under it), `devel/lld`, `lang/compiler-rt` and `lang/libunwind` are the compiler of the new
+system: it builds what `mag install` asks for after `mag sync`, so no other machine is needed. They are the longest part of the bootstrap.
+It also copies the recipes of the first repo that has a `PROVIDES` file into `/usr/share/mag/repo`, installs `chamesle-chroot` and
+`chamesle-disk`, and runs `depmod`.
The sources go in `/mnt/var/cache/mag` and are not fetched twice. An existing file in `/etc` is never overwritten, so
the script can be run again after a failure. A build that fails stops and names the directory it left behind.
@@ -243,16 +292,20 @@ Add the packages you want now, or after the first boot:
### 3.6 fstab
-`blkid` prints the UUIDs:
+If you used chamesle-disk the file is written. Look at it: the boot partition by UUID on `/boot`, then `proc`, `sysfs`
+and `tmpfs`, and nothing for the ZFS datasets, because ZFS mounts them itself.
+
+By hand, `blkid` prints the UUIDs:
blkid /dev/sda2 /dev/sda3
-Open `/mnt/etc/fstab` and add the root and boot lines under the `proc`, `sysfs` and `tmpfs` lines chamesle-bootstrap
-wrote:
+Open `/mnt/etc/fstab` and add the root and boot lines under the `proc`, `sysfs` and `tmpfs` lines already there:
UUID=ROOT-UUID / ext4 defaults 0 1
UUID=BOOT-UUID /boot vfat defaults 0 2
+A root on ZFS gets only the boot line. Check that the UUID is the one `blkid` shows for the boot partition.
+
### 3.7 Host name, users, password
The host name is already in `/mnt/etc/init.conf` from `-n`. The root account is in `/mnt/etc/passwd` with an `x` in the
@@ -287,9 +340,14 @@ Write `/mnt/boot/limine.conf` with the file name you see and the UUID of the roo
module_path: boot():/initrd
cmdline: root=UUID=ROOT-UUID rw console=tty0
-`boot()` is the partition `limine.conf` is on, which is `/boot`. The kernel has ext4 and the disk drivers as modules, so
-the system needs the initramfs. The one from the ISO (`/mnt/boot/initrd`, copied in 3.3) finds the root by `root=UUID=` and
-loads the modules in `iso/profile/modules`. Section 7.5 says how to make your own.
+For a root on ZFS the command line names the dataset, there is no UUID:
+
+ cmdline: root=ZFS=rpool/root rw console=tty0
+
+`boot()` is the partition `limine.conf` is on, which is `/boot`; with ZFS it is the FAT partition, since limine cannot read
+ZFS. The kernel has ext4 and the disk drivers as modules, so
+the system needs the initramfs. The one from the ISO (`/mnt/boot/initrd`, copied in 3.3) finds the root by `root=UUID=` or
+`root=ZFS=` and loads the modules in `iso/profile/modules`. Section 7.5 says how to make your own.
limine's files are in `/mnt/usr/share/limine`.
@@ -313,16 +371,19 @@ Do both and the disk boots either way.
### 3.9 First boot
umount -R /mnt
+ zpool export rpool
reboot
+Leave out `zpool export` if the root is not on ZFS.
+
Take out the installation medium. limine shows the entry for three seconds and boots the kernel. `init` runs `/etc/rc`, which mounts the
file systems and starts the services of `init.conf`, and puts a login on the ttys. Log in as root.
Then:
1. Bring the network up (section 6.7).
-2. To build on the new machine, fetch the pkgsrc tree (section 2.3) and mag's recipes, and
- `export MAG_REPO=...`; put that line in `/etc/profile` to keep it.
+2. `mag sync` fetches the pkgsrc tree (section 2.3). Mag's own recipes are in `/usr/share/mag/repo` already and the
+ default `MAG_REPO` covers both, so `mag install name` works from then on, and the compiler is in the base set.
3. `mag list` shows what the bootstrap put on the machine.
@@ -344,6 +405,7 @@ Then:
deps PKG its dependencies, rdeps PKG for what needs it
fetch PKG... only download and check the sources
build PKG build into a stage directory, install nothing
+ sync download the pkgsrc tree to /usr/pkgsrc
hash FILE... sha256 of files, for writing recipes
-n only say what would happen -f go on when files clash or something needs the package
@@ -398,7 +460,8 @@ puts the real paths back:
| variable | meaning | default |
|----------|---------|---------|
| `MAG_ROOT` | where to install | `/` |
-| `MAG_REPO` | repositories, split by colons | `/usr/share/mag/repo` |
+| `MAG_REPO` | repositories, split by colons | `/usr/share/mag/repo:/usr/pkgsrc` |
+| `MAG_SYNC_URL` | the file `mag sync` fetches | NetBSD's `pkgsrc.tar.xz` |
| `MAG_FETCH` | the download command, given a file and a URL | `curl -fsSL -o` |
| `MAG_PREFIX` | `$PREFIX` in the steps | `/usr` |
| `MAG_JOBS` | `$JOBS` in the steps | `JOBS` of mag.conf, else 4 |
@@ -681,9 +744,12 @@ interface. Then `dhcpcd wlan0`.
mount -t vfat -o ro /dev/sdb1 /mnt/usb
umount /mnt/usb
-`mount`, `umount`, `lsblk`, `blkid`, `sfdisk` and `fdisk` are util-linux's, `mkfs.ext4` and `fsck.ext4` are
+`mount`, `umount`, `lsblk`, `blkid`, `sfdisk`, `fdisk` and `cfdisk` are util-linux's, `zpool` and `zfs` are OpenZFS's, `mkfs.ext4` and `fsck.ext4` are
e2fsprogs', `mkfs.fat` is dosfstools'.
+On a ZFS root, `/etc/rc` runs `zfs mount -a` right after `mount -a`, which mounts `rpool/home` and `rpool/var`; the root itself was
+mounted by the initramfs.
+
## 7. The kernel and firmware
@@ -747,7 +813,8 @@ release is a new `version` and `sha256` in `repo/sys-kernel/linux-firmware`, the
### 7.5 An initramfs
-A root on ext4 on a common disk (SATA, NVMe, virtio, USB storage) needs none. A root that needs a module to be reached, or
+A root on ext4 on a common disk (SATA, NVMe, virtio, USB storage) needs none. A root on ZFS needs one: the pool has to be
+imported before the root can be mounted. A root that needs a module to be reached, or
RAID, LVM or an encrypted disk, does.
`iso/mkinitramfs` makes one, and the live ISO uses it:
@@ -755,15 +822,17 @@ RAID, LVM or an encrypted disk, does.
mkinitramfs [-m modules-file] ROOTFS KVER OUT
`ROOTFS` is the root whose `/lib/modules` and programs are used, `KVER` the name of the kernel's directory in
-`/lib/modules`, `OUT` the file to write. It puts in a few tools (`sh mount umount switch_root insmod mkdir sleep blkid`
+`/lib/modules`, `OUT` the file to write. It puts in a few tools (`sh mount umount switch_root insmod mkdir sleep blkid`, and `zpool` when the root has it,
with the libraries they need), the modules named in the modules file together with what they depend on, and `iso/init`.
+The names after a `[zfs]` line of the modules file go into a second list, which `iso/init` loads only for `root=ZFS=`.
The result is a zstd compressed cpio.
$HOME/src/system/iso/mkinitramfs -m modules /mnt 6.18.63-0-lts /mnt/boot/initrd
`iso/init` mounts `proc`, `sysfs` and `devtmpfs`, loads the modules, reads `root=` from the command line (`root=UUID=...`
or a device), waits up to 15 seconds for the device, mounts it read-only at `/mnt/root`, and runs
-`switch_root /mnt/root /sbin/init`. If anything fails it prints why and drops into a shell.
+`switch_root /mnt/root /sbin/init`. With `root=ZFS=rpool/root` it loads the zfs modules, runs `zpool import -N -d /dev rpool`
+(until it works, 15 seconds at most) and mounts the dataset with `mount -o zfsutil -t zfs`. If anything fails it prints why and drops into a shell.
Name the initrd in `limine.conf`:
@@ -773,13 +842,17 @@ Name the initrd in `limine.conf`:
module_path: boot():/initrd
cmdline: root=UUID=ROOT-UUID rw
+On ZFS the command line is `root=ZFS=rpool/root rw`. The ZFS module is `sys-kernel/zfs-module`, for the kernel of
+`sys-kernel/linux`: when one is updated the other has to be too, and `depmod` has to run after installing it.
+
## 8. The live ISO
### 8.1 What it is
The ISO is a live system to work from. It boots on BIOS and UEFI machines with limine, from a disc or a stick, and has
-mksh, vim, mag, `chamesle-bootstrap`, the disk tools (`sfdisk`, `mkfs.ext4`, `mkfs.fat`) and this handbook in
+mksh, vim, mag with Chamesle's recipes (`mag search` and `mag info` work before `mag sync`), `chamesle-bootstrap`,
+`chamesle-chroot`, `chamesle-disk`, the disk tools (`cfdisk`, `sfdisk`, `mkfs.ext4`, `mkfs.fat`, `zpool`, `zfs`) and this handbook in
`/usr/share/doc/chamesle/handbook.md`. It is where you do chapter 3 on a machine that has nothing on it.
### 8.2 Building
@@ -791,6 +864,8 @@ The machine that builds needs `mksquashfs` (filesystems/squashfs), `xorriso` (sy
The usage:
+ chamesle-chroot DIR [COMMAND...]
+ chamesle-disk DISK
mkiso [-p profile] [-r repos] [-R rootfs] [-w workdir] [-o out.iso]
* `-p` is the profile directory (default `iso/profile`)
@@ -800,14 +875,15 @@ The usage:
* `-o` is the file to write (default `chamesle.iso`)
Environment: `LIMINE` is where limine's files are (default `/usr/share/limine`), `MKISOFS` is the command that makes the
-ISO (default `xorriso -as mkisofs`), `MAG` is the mag to put into the image.
+ISO (default `xorriso -as mkisofs`), `MAG` is the mag to put into the image, `MAGREPO` the repo whose recipes it carries
+in `/usr/share/mag/repo` (default: the `repo` next to a mag checkout, else the first `MAG_REPO` entry with a `PROVIDES` file).
In order:
1. `chamesle-bootstrap` builds a root from `profile/packages`, with the host name `chamesle-live`.
2. `profile/overlay` is copied over it.
-3. `chamesle-bootstrap`, the handbook and `mag` are put into it.
-4. The kernel from `boot/vmlinuz-*` is copied to the ISO.
+3. `chamesle-bootstrap`, `chamesle-chroot`, `chamesle-disk`, the handbook, `mag` and its recipes are put into it.
+4. The kernel from `boot/vmlinuz-*` is copied to the ISO, and `depmod` runs on the root.
5. `mkinitramfs` makes `boot/initrd` from `profile/modules`.
6. The root is written as a zstd squashfs, `LiveOS/rootfs.sfs`.
7. limine's boot files are copied and `profile/limine.conf` becomes `boot/limine/limine.conf`.
@@ -832,7 +908,7 @@ the root yourself and hand it over:
`packages` is the base set of chapter 3. `modules` lists `squashfs overlay loop isofs sr_mod cdrom sd_mod usb_storage uas
xhci_pci ehci_pci ahci ata_piix ata_generic pata_acpi virtio_blk nvme virtio_pci virtio_scsi ext4 vfat nls_cp437
-nls_iso8859_1`, one per line. The boot entry:
+nls_iso8859_1`, one per line, then a `[zfs]` line and `zfs`, which only a root on ZFS loads. The boot entry:
timeout: 3
@@ -875,7 +951,7 @@ To write the ISO to a stick: `dd if=chamesle.iso of=/dev/sdX bs=4M conv=fsync`.
mag upgrade
The first says what would be rebuilt, the second reinstalls whatever the repos have a newer version of. pkgsrc itself is
-updated by unpacking a new tarball over the old tree (section 2.3). One package: `mag install name`. When you work with
+updated with `mag sync` (section 2.3). One package: `mag install name`. When you work with
`MAG_ROOT` set, run `mag relocate` afterwards.
### 9.2 Cleaning up
@@ -965,12 +1041,16 @@ into a staging directory and prints where the files are.
| `/var/cache/mag/` | downloaded sources |
| `/usr/share/limine/` | limine's boot files, for installing it |
| `/usr/share/doc/chamesle/handbook.md` | this book, on the live ISO |
+| `/usr/share/mag/repo/` | Chamesle's recipes |
+| `/usr/pkgsrc/` | the pkgsrc tree, from `mag sync` |
### 10.2 In the sources
| path | what |
|------|------|
| `system/tools/chamesle-bootstrap` | makes a root file system and builds the base set into it |
+| `system/tools/chamesle-chroot` | chroot with the pseudo file systems mounted |
+| `system/tools/chamesle-disk` | cfdisk, vfat boot partition, ZFS root, mounted on `/mnt` |
| `system/files/` | init, rc, `service`, `init.conf` and the service scripts that go into every root |
| `system/iso/mkiso` | builds the live ISO |
| `system/iso/mkinitramfs` | builds an initramfs |
@@ -984,7 +1064,8 @@ into a staging directory and prints where the files are.
| `mag/tests/run.sh` | tests on a fake repo: install, upgrade, remove in a scratch root |
| `mag/repo/PROVIDES` | pkgsrc packages replaced or skipped, and dependencies added |
| `mag/repo/sys-libs/musl` | the libc |
-| `mag/repo/sys-kernel/{linux,linux-headers,linux-firmware}` | kernel, headers, firmware |
+| `mag/repo/sys-kernel/{linux,linux-headers,linux-firmware,zfs-module}` | kernel, headers, firmware, the ZFS module |
+| `mag/repo/sys-fs/zfs` | ZFS userland: `zpool`, `zfs` |
| `mag/repo/sys-boot/limine` | the boot loader |
| `mag/repo/sys-apps/{eudev,kmod,util-linux,usbutils}` | udev, module tools, the basics, usb tools |
| `mag/repo/net-libs/libnl`, `net-wireless/iw`, `net-misc/iproute2` | network tools |
@@ -998,6 +1079,6 @@ into a staging directory and prints where the files are.
chamesle-bootstrap [-r REPOS] [-s PACKAGES] [-n HOSTNAME] TARGET
mkiso [-p profile] [-r repos] [-R rootfs] [-w workdir] [-o out.iso]
mkinitramfs [-m modules] ROOTFS KVER OUT
- mag [-nfk] install|remove|upgrade|autoremove|list|info|search|files|owns|deps|rdeps|fetch|build|relocate|hash
+ mag [-nfk] install|remove|upgrade|autoremove|list|info|search|files|owns|deps|rdeps|fetch|build|sync|relocate|hash
rifle --install DIR | --list
limine bios-install DEVICE
diff --git a/iso/init b/iso/init
@@ -11,6 +11,7 @@ read -r cmdline < /proc/cmdline
for w in $cmdline; do
case $w in
root=UUID=*) uuid=${w#root=UUID=};;
+ root=ZFS=*) zfs=${w#root=ZFS=};;
root=*) dev=${w#root=};;
esac
done
@@ -19,7 +20,16 @@ fail() { echo "init: $*"; echo "init: dropping to a shell"; exec sh; }
mkdir -p /mnt/root
-if [ -n "$uuid$dev" ]; then
+if [ -n "$zfs" ]; then
+ while read -r ko; do insmod "$ko" || fail "cannot load $ko"; done < /modules.zfs
+ n=0
+ until zpool import -N -d /dev "${zfs%%/*}" 2>/dev/null; do
+ n=$((n + 1))
+ [ $n -lt 15 ] || fail "cannot import the pool ${zfs%%/*}"
+ sleep 1
+ done
+ mount -o zfsutil -t zfs "$zfs" /mnt/root || fail "cannot mount $zfs"
+elif [ -n "$uuid$dev" ]; then
n=0
while [ $n -lt 15 ]; do
[ -n "$uuid" ] && dev=$(blkid -U "$uuid" 2>/dev/null)
diff --git a/iso/mkinitramfs b/iso/mkinitramfs
@@ -1,6 +1,7 @@
#!/bin/sh
# mkinitramfs [-m modules-file] ROOTFS KVER OUT: a cpio of the few tools init needs, the modules named in the
-# modules file with what they depend on, and init itself.
+# modules file with what they depend on, and init itself. the names after a [zfs] line are loaded only for root=ZFS=,
+# and zpool goes in when the root has it.
set -e
here=$(cd "$(dirname "$0")" && pwd)
while getopts m: o; do case $o in m) list=$OPTARG;; *) exit 2;; esac; done
@@ -33,18 +34,30 @@ for t in sh mount umount switch_root insmod mkdir sleep blkid; do
cp "$f" "$tmp/bin/$t"
needed "$f"
done
-: > "$tmp/modules"
-if [ -n "$list" ]; then
- grep -v '^#' "$list" | while read -r m; do
+# modules FILE: copies the modules named on stdin with what they depend on and lists their paths in FILE
+modules() {
+ while read -r m; do
[ -n "$m" ] || continue
modprobe -d "$root" -S "$kver" --show-depends "$m" 2>/dev/null | sed -n 's/^insmod //p'
done | awk '!seen[$0]++' | while read -r ko; do
ko=${ko#$root}
mkdir -p "$tmp$(dirname "$ko")"
cp "$root$ko" "$tmp$ko"
- echo "$ko" >> "$tmp/modules"
+ echo "$ko" >> "$1"
done
+}
+: > "$tmp/modules"
+: > "$tmp/modules.zfs"
+if [ -n "$list" ]; then
+ grep -v '^#' "$list" | sed -n '/^\[zfs\]/q;p' | modules "$tmp/modules"
+ sed -n '/^\[zfs\]/,$p' "$list" | grep -v '^#\|^\[' | modules "$tmp/modules.zfs"
fi
+for d in usr/sbin sbin usr/bin bin; do
+ [ -x "$root/$d/zpool" ] || continue
+ cp "$root/$d/zpool" "$tmp/bin/zpool"
+ needed "$root/$d/zpool"
+ break
+done
cp "$here/init" "$tmp/init"
chmod 755 "$tmp/init"
(cd "$tmp" && find . | bsdcpio -o -H newc 2>/dev/null) | zstd -19 -q -o "$out" -f
diff --git a/iso/mkiso b/iso/mkiso
@@ -2,6 +2,7 @@
# mkiso: builds the Chamesle live ISO from a profile.
# mkiso -r $HOME/src/mag/repo:/usr/pkgsrc
# mkiso -R /srv/rootfs -o chamesle.iso
+# MAGREPO=$HOME/src/mag/repo mkiso ... the recipes the image carries for mag search and info
set -e
here=$(cd "$(dirname "$0")" && pwd)
profile=$here/profile
@@ -18,6 +19,11 @@ while getopts p:r:R:w:o: o; do
esac
done
export MAG_REPO
+if [ -z "$MAGREPO" ]; then
+ for d in "$here/../../mag/repo" $(echo "$MAG_REPO" | tr : ' '); do
+ [ -f "$d/PROVIDES" ] && { MAGREPO=$d; break; }
+ done
+fi
: "${MKISOFS:=xorriso -as mkisofs}"
: "${LIMINE:=/usr/share/limine}"
die() { echo "mkiso: $*" >&2; exit 1; }
@@ -45,7 +51,13 @@ MAG_ROOT=$rootfs MAG_PREFIX=/usr "$MAG" relocate ${from:+"$from"}
rm -rf "$rootfs"/var/cache/mag "$rootfs"/var/tmp/mag "$rootfs"/usr/include "$rootfs"/usr/lib/pkgconfig "$rootfs"/lib/firmware
find "$rootfs"/usr/lib -name "*.a" -o -name "*.la" | xargs rm -f
tar -C "$profile/overlay" -cf - . | tar -C "$rootfs" -xf -
-install -Dm755 "$here/../tools/chamesle-bootstrap" "$rootfs/usr/bin/chamesle-bootstrap"
+for t in chamesle-bootstrap chamesle-chroot chamesle-disk; do
+ install -Dm755 "$here/../tools/$t" "$rootfs/usr/bin/$t"
+done
+if [ -n "$MAGREPO" ]; then
+ mkdir -p "$rootfs/usr/share/mag/repo"
+ cp -R "$MAGREPO/." "$rootfs/usr/share/mag/repo/"
+fi
install -Dm644 "$here/../handbook/handbook.md" "$rootfs/usr/share/doc/chamesle/handbook.md"
install -Dm755 "$MAG" "$rootfs/usr/bin/mag"
vmlinuz=$(ls "$rootfs"/boot/vmlinuz-* | head -n 1)
@@ -53,6 +65,7 @@ kver=$(ls "$rootfs/lib/modules" | head -n 1)
[ -f "$vmlinuz" ] && [ -n "$kver" ] || die "no kernel in the root: boot/vmlinuz-* and lib/modules/* are missing"
cp "$vmlinuz" "$iso/boot/vmlinuz"
+depmod -b "$rootfs" "$kver"
"$here/mkinitramfs" -m "$profile/modules" "$rootfs" "$kver" "$iso/boot/initrd"
rm -f "$iso/LiveOS/rootfs.sfs"
diff --git a/iso/profile/modules b/iso/profile/modules
@@ -22,3 +22,5 @@ ext4
vfat
nls_cp437
nls_iso8859_1
+[zfs]
+zfs
diff --git a/iso/profile/packages b/iso/profile/packages
@@ -15,3 +15,5 @@ sys-boot/limine
sys-kernel/linux
sys-firmware/wireless-regdb
editors/vim
+sys-fs/zfs
+sys-kernel/zfs-module
diff --git a/tools/chamesle-bootstrap b/tools/chamesle-bootstrap
@@ -12,7 +12,7 @@ usage() {
echo " -n host name of the new system (default chamesle)" >&2
exit 2
}
-base="sys-libs/musl devel/gmake archivers/libarchive archivers/pigz shells/mksh sys-apps/util-linux sys-apps/kmod sys-apps/eudev net/dhcpcd www/curl sysutils/e2fsprogs sysutils/dosfstools sys-boot/limine sys-kernel/linux sys-kernel/linux-firmware sys-firmware/sof-firmware sys-firmware/wireless-regdb editors/vim"
+base="sys-libs/musl devel/gmake archivers/libarchive archivers/pigz shells/mksh sys-apps/util-linux sys-apps/kmod sys-apps/eudev net/dhcpcd www/curl sysutils/e2fsprogs sysutils/dosfstools sys-boot/limine sys-kernel/linux sys-kernel/linux-firmware sys-firmware/sof-firmware sys-firmware/wireless-regdb editors/vim sys-fs/zfs sys-kernel/zfs-module lang/clang devel/lld lang/compiler-rt lang/libunwind"
name=chamesle
while getopts r:s:n: o; do
case $o in
@@ -58,8 +58,9 @@ put() {
put "$files"
[ -e etc/passwd ] || cat > etc/passwd <<P
root:x:0:0:root:/root:/usr/bin/mksh
+dbus:x:81:81:system bus:/run/dbus:/bin/false
P
-[ -e etc/group ] || printf 'root:x:0:\nbin:x:1:\ndaemon:x:2:\nsys:x:3:\nadm:x:4:\ntty:x:5:\ndisk:x:6:\nlp:x:7:\nmem:x:8:\nkmem:x:9:\nwheel:x:10:\nfloppy:x:11:\nmail:x:12:\nuucp:x:14:\nman:x:15:\naudio:x:18:\ncdrom:x:19:\ndialout:x:20:\ntape:x:26:\nvideo:x:27:\ninput:x:97:\nkvm:x:78:\nrender:x:105:\nclock:x:106:\nusers:x:100:\n' > etc/group
+[ -e etc/group ] || printf 'root:x:0:\nbin:x:1:\ndaemon:x:2:\nsys:x:3:\nadm:x:4:\ntty:x:5:\ndisk:x:6:\nlp:x:7:\nmem:x:8:\nkmem:x:9:\nwheel:x:10:\nfloppy:x:11:\nmail:x:12:\nuucp:x:14:\nman:x:15:\naudio:x:18:\ncdrom:x:19:\ndialout:x:20:\ntape:x:26:\nvideo:x:27:\ninput:x:97:\nkvm:x:78:\nrender:x:105:\nclock:x:106:\nusers:x:100:\ndbus:x:81:\n' > etc/group
[ -e etc/shells ] || printf '/bin/sh\n/usr/bin/mksh\n/usr/bin/ksh\n' > etc/shells
[ -e etc/fstab ] || printf '# device mount type options dump pass\nproc /proc proc defaults 0 0\nsysfs /sys sysfs defaults 0 0\ntmpfs /tmp tmpfs mode=1777 0 0\n' > etc/fstab
[ -e etc/profile ] || cat > etc/profile <<'P'
@@ -69,10 +70,21 @@ export PAGER=less
umask 022
P
[ -e etc/os-release ] || printf 'NAME=Chamesle\nID=chamesle\nPRETTY_NAME="Chamesle"\n' > etc/os-release
-[ -e etc/mag.conf ] || printf '# read by mag, same syntax as pkgsrc mk.conf\nJOBS=%s\nCFLAGS=-O2 -std=gnu17 -include sys/sysmacros.h\nPKG_OPTIONS.curl=-idn\n' "$(nproc 2>/dev/null || echo 4)" > etc/mag.conf
+[ -e etc/mag.conf ] || printf '# read by mag, same syntax as pkgsrc mk.conf\nJOBS=%s\nCFLAGS=-O2 -std=gnu17 -include sys/sysmacros.h\nPKG_OPTIONS.curl=-idn\nCONFIGURE_ARGS.libtirpc=--disable-gssapi\n' "$(nproc 2>/dev/null || echo 4)" > etc/mag.conf
export MAG_ROOT=$PWD MAG_PREFIX=/usr MAG_REPO
echo "installing: $base"
mag install $base
+for r in $(echo "$MAG_REPO" | tr : ' '); do
+ [ -f "$r/PROVIDES" ] || continue
+ mkdir -p usr/share/mag/repo
+ cp -R "$r/." usr/share/mag/repo/
+ break
+done
+for t in chamesle-chroot chamesle-disk; do install -m755 "$here/$t" usr/bin/$t; done
+for k in lib/modules/*; do
+ [ -d "$k" ] || continue
+ depmod -b "$PWD" "${k##*/}"
+done
mkdir -p etc/ssl
for f in cert.pem openssl.cnf x509v3.cnf; do [ -e etc/ssl/$f ] || cp usr/libressl/share/examples/libressl/$f etc/ssl/ 2>/dev/null; done
[ -e usr/bin/sh ] || ln -s mksh usr/bin/sh
diff --git a/tools/chamesle-chroot b/tools/chamesle-chroot
@@ -0,0 +1,34 @@
+#!/bin/sh
+# chamesle-chroot DIR [COMMAND...]: chroot into DIR with /proc, /sys, /dev and /run in place, and take them away again afterwards.
+# chamesle-chroot /mnt
+# chamesle-chroot /mnt mag install vim
+[ $# -ge 1 ] || { echo "usage: chamesle-chroot DIR [COMMAND...]" >&2; exit 2; }
+dir=$1
+shift
+[ -d "$dir" ] || { echo "chamesle-chroot: $dir is not a directory" >&2; exit 1; }
+[ "$(id -u)" = 0 ] || { echo "chamesle-chroot: needs root" >&2; exit 1; }
+[ $# -gt 0 ] || set -- /usr/bin/mksh -l
+
+mounted=
+cleanup() {
+ for m in $mounted; do umount "$dir/$m" 2>/dev/null || umount -l "$dir/$m"; done
+}
+trap cleanup EXIT
+trap 'exit 1' HUP INT TERM
+
+# mnt PLACE MOUNT-ARGS...: mounts on $dir/PLACE, remembering it so cleanup undoes the last one first
+mnt() {
+ place=$1
+ shift
+ mkdir -p "$dir/$place"
+ mount "$@" "$dir/$place" || { echo "chamesle-chroot: cannot mount $dir/$place" >&2; exit 1; }
+ mounted="$place $mounted"
+}
+
+mnt proc -t proc proc
+mnt sys -t sysfs sysfs
+mnt dev --bind /dev
+mnt dev/pts --bind /dev/pts
+mnt run -t tmpfs tmpfs
+[ -f /etc/resolv.conf ] && cp /etc/resolv.conf "$dir/etc/resolv.conf"
+env -i HOME=/root TERM="${TERM:-linux}" PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin chroot "$dir" "$@"
diff --git a/tools/chamesle-disk b/tools/chamesle-disk
@@ -0,0 +1,68 @@
+#!/bin/sh
+# chamesle-disk DISK: erases a disk and sets it up for Chamesle: you lay out the partitions in cfdisk, then the boot partition becomes
+# vfat and the root partition a ZFS pool, both mounted under /mnt with a fstab written. the install itself stays by hand, see the handbook.
+# chamesle-disk /dev/nvme0n1
+die() { echo "chamesle-disk: $*" >&2; exit 1; }
+[ $# = 1 ] || { echo "usage: chamesle-disk DISK" >&2; exit 2; }
+[ "$(id -u)" = 0 ] || die "needs root"
+disk=$1
+[ -b "$disk" ] || die "$disk is not a block device"
+for t in cfdisk mkfs.fat zpool zfs blkid lsblk; do command -v $t >/dev/null || die "$t is not installed"; done
+mountpoint -q /mnt && die "/mnt is in use, unmount it first"
+zpool list rpool >/dev/null 2>&1 && die "a pool called rpool is already imported"
+
+lsblk "$disk"
+echo "Everything on $disk will be erased."
+printf 'Type the name of the disk (%s) to go on: ' "${disk##*/}"
+read -r answer
+[ "$answer" = "${disk##*/}" ] || [ "$answer" = "$disk" ] || die "not confirmed, nothing was changed"
+
+cfdisk "$disk" || die "cfdisk failed"
+sleep 1
+echo
+lsblk "$disk"
+
+# part WHAT: asks for a partition of the disk, takes sda1 as well as /dev/sda1
+part() {
+ printf '%s: ' "$1" >&2
+ read -r p
+ case $p in /dev/*) ;; *) p=/dev/$p;; esac
+ [ -b "$p" ] || die "$p is not a block device"
+ case $p in "$disk"?*) ;; *) die "$p is not a partition of $disk";; esac
+ echo "$p"
+}
+boot=$(part "Which partition is the boot partition (vfat, the EFI system partition)") || exit 1
+root=$(part "Which partition is the root (ZFS)") || exit 1
+[ "$boot" != "$root" ] || die "boot and root cannot be the same partition"
+
+[ -e /dev/zfs ] || modprobe zfs || die "the zfs module is not there"
+mkfs.fat -F32 "$boot" || die "mkfs.fat failed"
+zpool create -f -o ashift=12 -O compression=zstd -O acltype=posixacl -O xattr=sa -O mountpoint=none -R /mnt rpool "$root" || die "zpool create failed"
+zfs create -o mountpoint=/ -o canmount=noauto rpool/root || die "zfs create failed"
+zfs create -o mountpoint=/home rpool/home
+zfs create -o mountpoint=/var rpool/var
+zpool set bootfs=rpool/root rpool
+zfs mount rpool/root || die "cannot mount rpool/root"
+zfs mount -a
+mkdir -p /mnt/boot /mnt/etc
+mount "$boot" /mnt/boot || die "cannot mount $boot"
+
+if [ -e /mnt/etc/fstab ]; then
+ echo "/mnt/etc/fstab is there already, left alone"
+else
+ cat > /mnt/etc/fstab <<F
+# the zfs datasets mount themselves, so they are not listed here
+# device mount type options dump pass
+UUID=$(blkid -s UUID -o value "$boot") /boot vfat rw,noatime 0 2
+proc /proc proc defaults 0 0
+sysfs /sys sysfs defaults 0 0
+tmpfs /tmp tmpfs mode=1777 0 0
+F
+fi
+
+cat <<N
+
+$boot is vfat and mounted on /mnt/boot, the pool rpool is on $root and mounted on /mnt.
+Now install the system into /mnt, then limine.conf, the host name and a password: chapter 3 of the handbook,
+section 3.3 onwards (less /usr/share/doc/chamesle/handbook.md).
+N