summarylogtreecommitdiffstats
path: root/booster-ukify
blob: 1db5ddb2cebd4b894ff5d722b78e68d1df0c0582 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/bin/bash -e

source /usr/share/makepkg/util/message.sh

function usage {
  echo "$(basename "$0") [OPTIONS]"
  echo "  -h            shows usage"
  echo "  -g <version>  generate UKI image for specified kernel version"
  echo "  -a            generate UKI images for all available kernels"
  exit 0
}

function check_root {
  [ $EUID -eq 0 ] && return
  echo "booster-ukify requires root privileges to work" >&2
  exit 1
}

if [[ ${#} -eq 0 ]]; then
   usage
fi

declare -a ukify_global_args=()
declare -A ukify_variants=()
declare -A ukify_install_path=()
declare -A ukify_cmdline=()

[ -f "/etc/booster-ukify.conf" ] && source /etc/booster-ukify.conf

case "$colorize" in
  "true")
    colorize
    ;;
  "auto")
    grep -q -e '^\w*Color\w*$' /etc/pacman.conf && colorize
    ;;
esac

[ ${#ukify_variants[@]} -eq 0 ] && {
  # Fallback to default image if ukify_variants isn't specified
  ukify_variants[default]="--strip --compression=zstd"
}

ESP_PATH=$(bootctl --print-esp-path)
MACHINE_ID=$(</etc/machine-id)
# shellcheck source=/etc/os-release
source <(grep -E '^(BUILD_)?ID=' /etc/os-release)

function variant_name() {
  local prefix="$1"
  local name="$2"
  if [[ "$name" = "default" ]]; then
    printf "%s" "$prefix"
  else
    printf "%s-%s" "$prefix" "$name"
  fi
}

function ukify_obsolete_syntax() {
  read -r -a ukify_version < <(/usr/lib/systemd/ukify --version)
  [ "${ukify_version[1]}" -lt 254 ]
  return $?
}

function uki_path() {
  local name="$1"
  local version="$2"
  local variant="$3"

  if [ ${ukify_install_path[$variant]+_} ]; then
    local -A substitutions=(
      [id]="$ID"
      [build_id]="$BUILD_ID"
      [name]="$name"
      [version]="$version"
      [machine_id]="$MACHINE_ID"
      [version]="$version"
    )

    local path="${ukify_install_path[$variant]}"
    for key in "${!substitutions[@]}"; do
      local value="${substitutions[$key]}"
      path="${path//\$\{$key\}/${value}}"
    done

    printf "%s" "$path"
  else
    printf "EFI/Linux/%s.efi" "$(variant_name "linux-$version-$MACHINE_ID-$BUILD_ID" "$variant")"
  fi
}

function sanity_check() {
  local verbose="$1"

  local -A sanity_kernel=()
  for candidate in /usr/lib/modules/*; do
    [ -f "$candidate/pkgbase" ] || continue
    if read -r pkgbase &> /dev/null < "$candidate/pkgbase"; then
      sanity_kernel["${pkgbase}"]="$(basename "$candidate")"
    fi
  done

  local -A uki_path=()

  local -i result=0

  for kernel in "${!sanity_kernel[@]}"; do
    for variant in "${!ukify_variants[@]}"; do
      local path=$(uki_path "$kernel" "${sanity_kernel[$kernel]}" "$variant")
      if [ $verbose -ne 0 ]; then
        msg "Resolved UKI path for package %s %s in variant %s: %s\n" "$kernel" "${sanity_kernel[$kernel]}" "$variant" "$path"
      fi

      if [ ${uki_path[$path]+_} ]; then
        error "Found UKI path clash: %s-%s and %s points to the same path: %s\n" "$kernel" "$variant" "${uki_path[$path]}" "$path"
        result=1
      fi
      uki_path[$path]="$kernel-$variant"
    done
  done

  if [ $result -ne 0 ]; then
    exit $result
  fi
}

declare -A kernels
update_all=0

while getopts ":hag:sxyz" arg; do
  case ${arg} in
    g)
      found=0
      for line in $(pacman -Qql "$OPTARG"); do
        if [[ $line =~ ^/usr/lib/modules/([^/]+)/pkgbase$ ]]; then
          read -r pkgbase < "/${line}"
          kernels["${pkgbase}"]="${BASH_REMATCH[1]}"
          found=1
          break
        fi
      done
      if (( ! found )); then
        error "Error occurred during '$OPTARG' package traversal"
        exit 1
      fi
      ;;
    a)
      update_all=1
      ;;
    x)
      check_root
      # Trigger some IO on ESP path to be sure it's mounted by autofs if it's the case
      # Otherwise upgrading systemd may cause ESP partition not mounted at the time booster attempt to write new image
      stat "$ESP_PATH" >/dev/null
      exit 0
      ;;
    y)
      check_root
      sanity_check 0
      while read -r line; do
        if [[ "$line" == 'usr/lib/modules/'+([^/])'/pkgbase' ]]; then
          read -r pkgbase < "/${line}"
          path="$(grep -lE "^${pkgbase}\$" /usr/lib/modules/*/pkgbase)"
          version=$(basename "${path%/pkgbase}")
          for variant in "${!ukify_variants[@]}"; do
            IMAGE="${ESP_PATH}/$(uki_path "$pkgbase" "$version" "$variant")"
            if [ -f "$IMAGE" ]; then
              msg "Removing $IMAGE..."
              rm -f "$IMAGE"
            fi
          done
        fi
      done
      exit 0
      ;;
    s)
      sanity_check 1
      exit 0
      ;;
    z)
      check_root
      while read -r line; do
        if [[ $line =~ ^usr/lib/modules/([^/]+)/pkgbase$ ]]; then
          read -r pkgbase < "/${line}"
          kernels["${pkgbase}"]="${BASH_REMATCH[1]}"
        else
          update_all=1
          break 
        fi
      done
      ;;
    h)
      usage
      ;;
    *)
      usage
      ;;
  esac
done

sanity_check 0

if (( update_all )); then
  for candidate in /usr/lib/modules/*; do
    [ -f "$candidate/pkgbase" ] || continue
    if read -r pkgbase &> /dev/null < "$candidate/pkgbase"; then
      kernels["${pkgbase}"]="$(basename "$candidate")"
    fi
  done
fi

function gen_image() {
  check_root
  local kernel="$1"
  local version="$2"
  local path="/usr/lib/modules/${version}/pkgbase"
  local vmlinuz="/usr/lib/modules/${version}/vmlinuz"

  msg "booster-ukify -g %s" "$kernel"

  for variant in "${!ukify_variants[@]}"; do
    local image="${ESP_PATH}/$(uki_path "$kernel" "$version" "$variant")"
    local version_name=$(variant_name "$version" "$variant")

    read -r pkgbase < "$path"

    local initrd=$(mktemp ukify.XXXXXXXXXX)

    msg2 "Building initrd image %s (%s)" "$kernel" "$version_name"
    booster build --force "--kernel-version=${version}" ${ukify_variants[$variant]} "$initrd"

    local os_release=$(mktemp ukify.XXXXXXXXXX)
    grep -v '^BUILD_ID=' /etc/os-release > "$os_release"
    echo "BUILD_ID=\"$version_name\"" >> "$os_release"
    echo "VERSION_ID=\"$(variant_name "$pkgbase" "$variant")\"" >> "$os_release"

    local -a ukify_args=()
    if ! ukify_obsolete_syntax; then
          ukify_args+=(build)
    fi
    ukify_args+=("${ukify_global_args[@]}")
    ukify_args+=(--uname "$version")
    ukify_args+=(--os-release "@$os_release")
    ukify_args+=(--output "$image")

    if [ ! -z "${ukify_cmdline["$variant"]}" ]; then
      for i in "${!ukify_args[@]}"; do
         [[ "${ukify_args[$i]}" = "--cmdline" ]] && break
      done
      if [ "${ukify_args[$i]}" == "--cmdline" ]; then
        # Global args contains cmdline, override it
        ukify_args[$((i + 1))]="${ukify_cmdline["$variant"]}"
      else
        # There is no global cmdline, append it
        ukify_args+=("--cmdline" "${ukify_cmdline["$variant"]}")
      fi
    fi

    if ukify_obsolete_syntax; then
      ukify_args+=("$vmlinuz" "$initrd")
    else
      ukify_args+=(--linux "$vmlinuz")
      ukify_args+=(--initrd "$initrd")
    fi

    msg2 "Ukify image %s (%s)" "$kernel" "$version_name"

    /usr/lib/systemd/ukify "${ukify_args[@]}"

    # Remove temporary files
    rm "$initrd"
    rm "$os_release"

    # Mark image as default if needed
    if [[ "$pkgbase" == "$default_kernel_package" && "$variant" == "default" ]]; then
      if ! bootctl is-installed --quiet; then
        warning "systemd-boot is not installed, unable to mark image as default"
      else
        msg2 "Mark linux image %s (%s) as default" "$kernel" "$version"
        bootctl set-default "$(basename "$image")" || {
          error "Unable to mark linux image $kernel ($version) as default due to error"
        }
      fi
    fi
  done
}

for kernel in "${!kernels[@]}"; do
  gen_image "$kernel" "${kernels[$kernel]}"
done