From 8a77b201bdc1ddbd4cf020cd9b790f5ca21efa73 Mon Sep 17 00:00:00 2001
From: Marty Oehme <marty.oehme@gmail.com>
Date: Fri, 21 Feb 2025 11:41:14 +0100
Subject: [PATCH] Split up playbooks into host-level and guest-level

---
 guest.yaml | 54 ++++++++++++++++++++++++++++++++
 host.yaml  | 32 +++++++++++++++++++
 play.yaml  | 91 ++++++++++++------------------------------------------
 3 files changed, 105 insertions(+), 72 deletions(-)
 create mode 100644 guest.yaml
 create mode 100644 host.yaml

diff --git a/guest.yaml b/guest.yaml
new file mode 100644
index 0000000..b909cdd
--- /dev/null
+++ b/guest.yaml
@@ -0,0 +1,54 @@
+- name: Void prep
+  hosts: guest
+  become: true
+  vars:
+    ansible_chroot_exe: arch-chroot
+    disable_root_check: true
+  tasks:
+    # Prefer booster to dracut so make sure to never install it
+    - name: Ignore dracut
+      ansible.builtin.lineinfile:
+        path: /etc/xbps.d/ignore-dracut.conf
+        line: ignorepkg=dracut
+        state: present
+        create: true # create file if absent
+
+    - name: Update xbps and system
+      community.general.xbps:
+        name:
+          - xbps
+        state: latest
+        update_cache: true
+        upgrade: true
+
+    - name: Install Base system
+      community.general.xbps:
+        name:
+          - base-system
+        state: present
+
+    - name: Get rid of temporary container metapackage
+      community.general.xbps:
+        name:
+          - base-container-full
+        state: absent
+
+    - name: Install booster
+      community.general.xbps:
+        name: booster
+        state: present
+      changed_when: True
+      notify: installed-booster
+
+  handlers:
+    - name: List installed linux kernels
+      find:
+        paths: "/usr/lib/modules"
+      register: found_kernel
+      listen: installed-booster
+
+    - name: Find kernel
+      debug:
+        msg: "{{ found_kernel['files'] | map(attribute='path') | map('regex_replace', '^.*/(.*)$', '\\1') | list }}"
+      listen: installed-booster
+
diff --git a/host.yaml b/host.yaml
new file mode 100644
index 0000000..1e7eb51
--- /dev/null
+++ b/host.yaml
@@ -0,0 +1,32 @@
+#   # The subvol collection seems to be misbehaving?
+#   # https://github.com/ansible-collections/community.general/issues/7098
+# # (and btrfs_subvol module working)
+# - name: Create void btrfs subvol
+#   hosts: host
+#   vars:
+#     btrfs_target: 441a90a5-3da2-46ce-9e32-987569b746c9
+#     mount_dir: /mnt/void
+#   become: true
+#   tasks:
+#     - name: Create @voidroot subvool under root
+#       community.general.btrfs_subvolume:
+#         state: present
+#         name: "/@"
+#         filesystem_uuid: "{{ btrfs_target }}"
+
+- name: Create voidlinux guest
+  hosts: host
+  become: true
+  vars:
+    mount_dir: /mnt/void
+    tarball_url: "https://repo-default.voidlinux.org/live/current/void-x86_64-ROOTFS-20250202.tar.xz"
+  tasks:
+    - name: Unpack rootfs
+      ansible.builtin.unarchive:
+        remote_src: yes # we already downloaded it to the 'remote' system
+        src: "{{ tarball_url }}"
+        dest: "{{ mount_dir }}"
+        # ONLY run if this file does not exist (could use any rootfs file to check)
+        creates: "{{ mount_dir }}/etc/os-release"
+
+# - name: Mount /boot part into voidroot mount
diff --git a/play.yaml b/play.yaml
index 0bf00b3..afcb1ad 100644
--- a/play.yaml
+++ b/play.yaml
@@ -1,37 +1,6 @@
 ---
-
-#   # The subvol collection seems to be misbehaving?
-#   # https://github.com/ansible-collections/community.general/issues/7098
-# # (and btrfs_subvol module working)
-# - name: Create void btrfs subvol
-#   hosts: host
-#   vars:
-#     btrfs_target: 441a90a5-3da2-46ce-9e32-987569b746c9
-#     mount_dir: /mnt/void
-#   become: true
-#   tasks:
-#     - name: Create @voidroot subvool under root
-#       community.general.btrfs_subvolume:
-#         state: present
-#         name: "/@"
-#         filesystem_uuid: "{{ btrfs_target }}"
-
-
-- name: Create voidlinux guest
-  hosts: host
-  become: true
-  vars:
-    mount_dir: /mnt/void
-    tarball_url: "https://repo-default.voidlinux.org/live/current/void-x86_64-ROOTFS-20250202.tar.xz"
-  tasks:
-    - name: Unpack rootfs
-      ansible.builtin.unarchive:
-        remote_src: yes # we already downloaded it to the 'remote' system
-        src: "{{ tarball_url }}"
-        dest: "{{ mount_dir }}"
-        # ONLY run if this file does not exist (could use any rootfs file to check)
-        creates: "{{ mount_dir }}/etc/os-release"
-
+- name: Import host tasks
+  ansible.builtin.import_playbook: host.yaml
 
 # Get python onto void - otherwise we can not interact through ansible
 - name: Bootstrap void python
@@ -43,43 +12,21 @@
       register: python_install
       changed_when: "'installed successfully' in python_install.stdout"
 
+- name: Import chroot guest tasks
+  ansible.builtin.import_playbook: guest.yaml
 
-- name: Void prep
-  hosts: guest
-  become: true
-  vars:
-    ansible_chroot_exe: arch-chroot
-    disable_root_check: true
-  tasks:
-    # Prefer booster to dracut so make sure to never install it
-    - name: Ignore dracut
-      ansible.builtin.lineinfile:
-        path: /etc/xbps.d/ignore-dracut.conf
-        line: ignorepkg=dracut
-        state: present
-        create: true # create file if absent
-
-    - name: Update xbps and system
-      community.general.xbps:
-        name:
-          - xbps
-        state: latest
-        update_cache: true
-        upgrade: true
-
-    - name: Install Base system
-      community.general.xbps:
-        name:
-          - base-system
-        state: present
-
-    - name: Get rid of temporary container metapackage
-      community.general.xbps:
-        name:
-          - base-container-full
-        state: absent
-
-    - name: Install booster
-      community.general.xbps:
-        name: booster
-        state: present
+    #   - I chose to use booster instead of dracut
+    # - easiest way to achieve this is before installing `base-system`
+    #   to `echo 'ignorepkg=dracut' >> /etc/xbps.d/ignore-dracut.conf`
+    #   and manually install `booster` instead
+    # - Lastly, you need to manually create initramfs when in a chroot
+    #   since booster will detect the host kernel instead. See here:
+    #   <https://github.com/anatol/booster/issues/230>
+    #   Essentially, run `booster build --kernel-version <krnl> myboot.img`
+    #   with the version you want to build for.
+    #   E.g. at time of writing, `booster build --kernel-version 6.12.13_1 myboot.img`.
+    #   To find correct version, use the name of the `/usr/lib/modules/XYZ` XYZ dir.
+    #
+    # - do the remaining steps in the 'installation configuration' setup of the guide chroot guide
+    # - if wanted (and you should) uncomment wheel group in sudoers and set up a sudo-enabled user
+    #   `useradd -m -G wheel -U -s /bin/zsh <username>` (if zsh is already installed)