summaryrefslogtreecommitdiff
path: root/roles/raspios/image/tasks/main.yml
blob: b8296f9accc4e91f6378e3c7be11eaa6c404e6fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
---
- name: check if host is member of the raspios group
  assert:
    msg: "please add the host to the group 'raspios'"
    that:
    - "'raspios' in group_names"

- name: fetch base image
  run_once: true
  import_tasks: fetch.yml

- name: build the image
  block:
  - name: create the output directory for built images
    file:
      path: "{{ raspios_output_dir }}"
      state: directory

  - name: extract image
    decompress:
      src: "{{ raspios_download_dir }}/{{ raspios_download_image_base_name }}"
      dest: "{{ raspios_output_dir }}"
      force: yes
    register: raspios_image_extract

  - set_fact:
      raspios_output_image_base_name: "{{ raspios_image_extract.files | first | basename }}"

  - name: read partition layout from image
    command: "sfdisk -q -r -J '{{ raspios_output_dir }}/{{ raspios_output_image_base_name }}'"
    register: raspios_image_sfdisk

  - set_fact:
      raspios_image_partitions: "{{ (raspios_image_sfdisk.stdout | from_json)['partitiontable']['partitions'] }}"

  - name: bind loop device for boot partition
    command: "udisksctl loop-setup --no-user-interaction -o {{ raspios_image_partitions[0].start * 512 }} -s {{ raspios_image_partitions[0].size * 512 }} -f '{{ raspios_output_dir }}/{{ raspios_output_image_base_name }}'"
    register: raspios_image_loop_setup

  - set_fact:
      raspios_image_loop_device: "{{ raspios_image_loop_setup.stdout | regex_search('as (/dev/loop[0-9]+)\\.$', '\\1') | first }}"

  - name: mount boot partition
    command: "udisksctl mount --no-user-interaction -b '{{ raspios_image_loop_device }}'"
    register: raspios_image_mount

  - set_fact:
      raspios_image_mount_point: "{{ raspios_image_mount.stdout | regex_search('at (/media/.+)\\.?$', '\\1') | first }}"

  - name: edit boot/config.txt
    when: raspios_boot_config is defined
    loop: "{{ raspios_boot_config }}"
    loop_control:
      label: "{{ item.line }}"
    lineinfile:
      path: "{{ raspios_image_mount_point }}/config.txt"
      regexp: "{{ item.regexp }}"
      line: "{{ item.line }}"

  - name: Generate authorized_keys file
    authorized_key:
      user: root
      manage_dir: no
      path: "{{ raspios_image_mount_point }}/firstrun.authorized_keys"
      key: "{{ ssh_keys_root | join('\n') }}"

  - name: install firstrun.sh script
    template:
      src: firstrun.sh.j2
      dest: "{{ raspios_image_mount_point }}/firstrun.sh"
      mode: 0755

  - name: edit boot/cmdline.txt
    lineinfile:
      path: "{{ raspios_image_mount_point }}/cmdline.txt"
      regexp: '^(.*)( systemd.run=/boot/firstrun.sh systemd.run_success_action=reboot systemd.run_failure_action=none systemd.unit=kernel-command-line.target)?(.*?)$'
      backrefs: yes
      line: '\1 systemd.run=/boot/firstrun.sh systemd.run_success_action=reboot systemd.run_failure_action=none systemd.unit=kernel-command-line.target \3'

  always:
  - name: unmount image
    when:
    - not raspios_keep_boot_dir_mounted
    - raspios_image_mount_point is defined
    - raspios_image_mount_point is mount
    command: "udisksctl unmount --no-user-interaction -b '{{ raspios_image_loop_device }}'"

  - name: delete loop_device
    when:
    - not raspios_keep_boot_dir_mounted
    - raspios_image_loop_device is defined
    command: "udisksctl loop-delete --no-user-interaction -b '{{ raspios_image_loop_device }}'"

  - name: print temporary build directory information
    when:
    - raspios_keep_boot_dir_mounted
    debug:
      msg: "As per request the boot partition of the image is still mounted to: {{ raspios_image_mount_point }}"

- name: set output image names
  set_fact:
    output_images:
    - "{{ (raspios_output_dir, raspios_output_image_base_name) | path_join | realpath }}"