|
45 | 45 | loop_control: |
46 | 46 | label: "{{ item.path }}" |
47 | 47 |
|
| 48 | +- name: Create scenario fixture named pipes |
| 49 | + become: true |
| 50 | + ansible.builtin.command: |
| 51 | + argv: [mkfifo, "--mode={{ item.mode }}", "{{ cdi_scenario_dir }}/{{ item.path }}"] |
| 52 | + creates: "{{ cdi_scenario_dir }}/{{ item.path }}" |
| 53 | + loop: "{{ cdi_scenario_fixtures | selectattr('type', 'eq', 'fifo') | list }}" |
| 54 | + loop_control: |
| 55 | + label: "{{ item.path }}" |
| 56 | + |
| 57 | +- name: Set scenario fixture pipe ownership |
| 58 | + become: true |
| 59 | + ansible.builtin.file: |
| 60 | + path: "{{ cdi_scenario_dir }}/{{ item.path }}" |
| 61 | + owner: "{{ item.owner | default('root') }}" |
| 62 | + group: "{{ item.group | default('root') }}" |
| 63 | + mode: "{{ item.mode }}" |
| 64 | + loop: "{{ cdi_scenario_fixtures | selectattr('type', 'eq', 'fifo') | list }}" |
| 65 | + loop_control: |
| 66 | + label: "{{ item.path }}" |
| 67 | + |
| 68 | +# A service that creates the socket, not a socket unit: on SELinux hosts systemd |
| 69 | +# labels a socket it creates from the policy's default for the path, which |
| 70 | +# denies /var/lib. |
| 71 | +# A process creates a file with its directory's type, so the socket the service |
| 72 | +# creates inherits this one, and does so again on every boot. On SELinux hosts |
| 73 | +# (Fedora) a container cannot stat a socket of the default /usr/local type. The |
| 74 | +# file module ignores the type where SELinux is not enabled. |
| 75 | +- name: Label the socket fixture directory for containers |
| 76 | + become: true |
| 77 | + ansible.builtin.file: |
| 78 | + path: "{{ cdi_scenario_dir }}/socket" |
| 79 | + state: directory |
| 80 | + setype: container_file_t |
| 81 | + |
| 82 | +- name: Check for systemd-socket-activate |
| 83 | + ansible.builtin.stat: |
| 84 | + path: /usr/bin/systemd-socket-activate |
| 85 | + register: cdi_socket_activate |
| 86 | + |
| 87 | +- name: Require systemd-socket-activate for the socket fixture |
| 88 | + ansible.builtin.assert: |
| 89 | + that: [cdi_socket_activate.stat.exists] |
| 90 | + fail_msg: systemd-socket-activate is not installed; the socket fixture needs it |
| 91 | + |
| 92 | +- name: Install the socket fixture unit |
| 93 | + become: true |
| 94 | + ansible.builtin.template: |
| 95 | + src: openshell-cdi-echo.service.j2 |
| 96 | + dest: /etc/systemd/system/openshell-cdi-echo.service |
| 97 | + owner: root |
| 98 | + group: root |
| 99 | + mode: "0644" |
| 100 | + |
| 101 | +- name: Start the persistent socket fixture |
| 102 | + block: |
| 103 | + - name: Start the socket fixture service |
| 104 | + become: true |
| 105 | + ansible.builtin.systemd_service: |
| 106 | + name: openshell-cdi-echo.service |
| 107 | + daemon_reload: true |
| 108 | + enabled: true |
| 109 | + state: restarted |
| 110 | + |
| 111 | + - name: Wait for the socket fixture |
| 112 | + become: true |
| 113 | + ansible.builtin.wait_for: |
| 114 | + path: "{{ cdi_scenario_dir }}/socket/echo.sock" |
| 115 | + state: present |
| 116 | + timeout: 15 |
| 117 | + |
| 118 | + # Connect as an unprivileged user, as a sandbox workload would. A fixture |
| 119 | + # that exists but does not echo would otherwise pass the scenario, which |
| 120 | + # only checks the socket's file type. |
| 121 | + - name: Check that the socket fixture echoes |
| 122 | + ansible.builtin.command: |
| 123 | + argv: |
| 124 | + - "{{ ansible_python_interpreter | default('/usr/bin/python3') }}" |
| 125 | + - -c |
| 126 | + - | |
| 127 | + import socket, sys |
| 128 | + payload = b"openshell-cdi-echo\n" |
| 129 | + s = socket.socket(socket.AF_UNIX) |
| 130 | + s.settimeout(5) |
| 131 | + s.connect(sys.argv[1]) |
| 132 | + s.sendall(payload) |
| 133 | + s.shutdown(socket.SHUT_WR) |
| 134 | + data = b"" |
| 135 | + while True: |
| 136 | + chunk = s.recv(1024) |
| 137 | + if not chunk: |
| 138 | + break |
| 139 | + data += chunk |
| 140 | + sys.exit(0 if data == payload else 1) |
| 141 | + - "{{ cdi_scenario_dir }}/socket/echo.sock" |
| 142 | + changed_when: false |
| 143 | + |
| 144 | + rescue: |
| 145 | + - name: Collect socket fixture diagnostics |
| 146 | + become: true |
| 147 | + ansible.builtin.shell: | |
| 148 | + systemctl status openshell-cdi-echo.service --no-pager -l |
| 149 | + journalctl -u openshell-cdi-echo.service --no-pager -n 40 |
| 150 | + ls -lZd {{ cdi_scenario_dir }}/socket |
| 151 | + (ausearch -m avc,user_avc -ts recent 2>&1 || true) | tail -20 |
| 152 | + changed_when: false |
| 153 | + failed_when: false |
| 154 | + register: cdi_socket_diagnostics |
| 155 | + |
| 156 | + - name: Report why the socket fixture did not start |
| 157 | + ansible.builtin.fail: |
| 158 | + msg: "openshell-cdi-echo.service did not start:\n{{ cdi_socket_diagnostics.stdout }}\n{{ cdi_socket_diagnostics.stderr }}" |
| 159 | + |
| 160 | +# The device-node scenario injects /dev/tmachine-cdi-null, a copy of /dev/null, |
| 161 | +# from a host node at the same path. A rootless runtime cannot create nodes, so |
| 162 | +# it bind-mounts the host node, and the container sees the host node's SELinux |
| 163 | +# type: it needs the type of /dev/null, which containers may use. /dev is |
| 164 | +# recreated on every boot, so a service creates and labels the node each time. |
| 165 | +# The label is ignored where SELinux is not enabled. |
| 166 | +- name: Install the host device node unit |
| 167 | + become: true |
| 168 | + ansible.builtin.copy: |
| 169 | + dest: /etc/systemd/system/openshell-cdi-device.service |
| 170 | + content: | |
| 171 | + [Unit] |
| 172 | + Description=OpenShell tmachine CDI host device node fixture |
| 173 | +
|
| 174 | + [Service] |
| 175 | + Type=oneshot |
| 176 | + RemainAfterExit=yes |
| 177 | + ExecStart=/bin/rm -f /dev/tmachine-cdi-null |
| 178 | + ExecStart=/usr/bin/mknod --mode=0666 /dev/tmachine-cdi-null c 1 3 |
| 179 | + ExecStart=-/usr/bin/chcon --type=null_device_t /dev/tmachine-cdi-null |
| 180 | +
|
| 181 | + [Install] |
| 182 | + WantedBy=multi-user.target |
| 183 | + owner: root |
| 184 | + group: root |
| 185 | + mode: "0644" |
| 186 | + |
| 187 | +- name: Create the host device node |
| 188 | + become: true |
| 189 | + ansible.builtin.systemd_service: |
| 190 | + name: openshell-cdi-device.service |
| 191 | + daemon_reload: true |
| 192 | + enabled: true |
| 193 | + state: restarted |
| 194 | + |
48 | 195 | - name: Generate named CDI scenario devices |
49 | 196 | become: true |
50 | 197 | ansible.builtin.template: |
|
0 commit comments