From bb9de502ce0354ad20b4e92508046773c2a8be4d Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Tue, 18 Nov 2025 21:54:43 +0100 Subject: [PATCH] feat: Set up filesystems Automatically set up btrfs root and data filesystem, as well as external HDD. This automation change assumes a layout exactly as in current bob to function by default, can be changed to any btrfs layout with the `btrfs_mounts` configuration option, however. --- roles/filesystem/README.md | 38 +++++++++++++++++++++++++++++ roles/filesystem/defaults/main.yaml | 30 +++++++++++++++++++++++ roles/filesystem/handlers/main.yaml | 7 ++++++ roles/filesystem/tasks/main.yaml | 30 +++++++++++++++++++++++ roles/filesystem/vars/main.yaml | 2 ++ site.yaml | 5 ++++ 6 files changed, 112 insertions(+) create mode 100644 roles/filesystem/README.md create mode 100644 roles/filesystem/defaults/main.yaml create mode 100644 roles/filesystem/handlers/main.yaml create mode 100644 roles/filesystem/tasks/main.yaml create mode 100644 roles/filesystem/vars/main.yaml diff --git a/roles/filesystem/README.md b/roles/filesystem/README.md new file mode 100644 index 0000000..c80df74 --- /dev/null +++ b/roles/filesystem/README.md @@ -0,0 +1,38 @@ +filesystem +========= + +Mounts the filesystem(s) required for bob. +Focused on correct `btrfs` layout and mounting, +but also mounts an external `ext4` HDD by default. + +Requirements +------------ + +This role requires a btrfs filesystem _existing_ on _any device_ that is targeted with the role (using the 'btrfs_mounts') configuration option. +Optionally, an external HDD is required if the mount toggle is true. + +Role Variables +-------------- + +`btrfs_mounts` can be used to set up the various (top-level) btrfs subvolumes, and their later mount points in the system. +Define an entry by giving the `uuid` of the targeted btrfs filesystem (or device), give the name of the `subvol` you intend to give, and define the `path` where it should ultimately be mounted by `fstab`. +You can additionally provide some `opts` which are given to `fstab` for the mount process as well. + +Example: + +```yaml +btrfs_mounts: + - path: "/home" + subvol: "@home" + uuid: "3204f33f-5fa7-4c11-bdd8-979c539fce91" + opts: "defaults,ssd,noatime,compress=zstd:3,space_cache=v2" + + - path: "/srv/media" + subvol: "@media" + uuid: "2256bd23-7751-486a-bfc4-b216a6b0c4f4" + opts: "defaults,noatime,compress=zstd:3,space_cache=v2" +``` + +--- + +`should_mount_external_hdd` can be toggled to automatically mount a USB-connected HDD on boot, or not. diff --git a/roles/filesystem/defaults/main.yaml b/roles/filesystem/defaults/main.yaml new file mode 100644 index 0000000..0c5589e --- /dev/null +++ b/roles/filesystem/defaults/main.yaml @@ -0,0 +1,30 @@ +--- +should_mount_external_hdd: true +should_reboot_machine: false + +btrfs_mounts: + - path: "/" + subvol: "@rootfs" + uuid: "3204f33f-5fa7-4c11-bdd8-979c539fce91" + opts: "defaults,ssd,noatime,compress=zstd:3,space_cache=v2" + - path: "/home" + subvol: "@home" + uuid: "3204f33f-5fa7-4c11-bdd8-979c539fce91" + opts: "defaults,ssd,noatime,compress=zstd:3,space_cache=v2" + + - path: "/srv/media" + subvol: "@media" + uuid: "2256bd23-7751-486a-bfc4-b216a6b0c4f4" + opts: "defaults,noatime,compress=zstd:3,space_cache=v2" + - path: "/srv/files" + subvol: "@files" + uuid: "2256bd23-7751-486a-bfc4-b216a6b0c4f4" + opts: "defaults,noatime,compress=zstd:3,space_cache=v2" + - path: "/srv/documents" + subvol: "@documents" + uuid: "2256bd23-7751-486a-bfc4-b216a6b0c4f4" + opts: "defaults,noatime,compress=zstd:3,space_cache=v2" + - path: "/srv/wolf" + subvol: "@wolf" + uuid: "2256bd23-7751-486a-bfc4-b216a6b0c4f4" + opts: "defaults,noatime,compress=zstd:1,space_cache=v2" diff --git a/roles/filesystem/handlers/main.yaml b/roles/filesystem/handlers/main.yaml new file mode 100644 index 0000000..b00e076 --- /dev/null +++ b/roles/filesystem/handlers/main.yaml @@ -0,0 +1,7 @@ +--- + +- name: Reboot machine + ansible.builtin.reboot: + connect_timeout: 600 + post_reboot_delay: 10 + when: "should_reboot_machine" diff --git a/roles/filesystem/tasks/main.yaml b/roles/filesystem/tasks/main.yaml new file mode 100644 index 0000000..0e85f23 --- /dev/null +++ b/roles/filesystem/tasks/main.yaml @@ -0,0 +1,30 @@ +--- +- name: Ensure btrfs ROOT layout + community.general.btrfs_subvolume: + name: "/{{ item.subvol }}" + # filesystem_device: /dev/sdb1 + # fileystem_label: btrfs-root # only 1 of the 3 required + filesystem_uuid: "{{ item.uuid }}" + loop: "{{ btrfs_mounts }}" + become: true + +- name: Ensure fstab contains btrfs mount entries + ansible.posix.mount: + path: "{{ item.path }}" + src: "UUID={{ item.uuid }}" + fstype: btrfs + opts: "{{ item.opts }},subvol={{ item.subvol }}" + state: present + loop: "{{ btrfs_mounts }}" + become: true + notify: Reboot machine + +- name: Ensure external HDD is mounted + ansible.posix.mount: + path: /mnt/ext + src: "UUID=01b221f2-83a5-49e4-bdef-ee9ee9ac5310" + fstype: ext4 + opts: "noatime" + state: present + become: true + when: "should_mount_external_hdd" diff --git a/roles/filesystem/vars/main.yaml b/roles/filesystem/vars/main.yaml new file mode 100644 index 0000000..664bc9a --- /dev/null +++ b/roles/filesystem/vars/main.yaml @@ -0,0 +1,2 @@ +--- +# vars file for btrfs diff --git a/site.yaml b/site.yaml index 1e43b4d..7cdaf6c 100644 --- a/site.yaml +++ b/site.yaml @@ -20,6 +20,11 @@ - name: Prepare incus server host hosts: host_system tasks: + - name: Prepare host filesystems + ansible.builtin.import_role: + name: filesystem + tags: filesystem + - name: Prepare system ansible.builtin.import_role: name: system