summaryrefslogtreecommitdiff
path: root/just_scripts/build-installer-iso.sh
blob: 2b1f7aebb40d29128e21eeccdeffdd23459f6d47 (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
#!/usr/bin/env bash

set -euo pipefail

project_root="$(git rev-parse --show-toplevel)"
cd "${project_root}"

image_name="${IMAGE_NAME:-localhost/$(just image_name)}"
image_tag="${IMAGE_TAG:-$(just generate-default-tag)}"
live_base_image="${LIVE_BASE_IMAGE:-quay.io/fedora/fedora-kinoite:43}"
installer_builder_image="${INSTALLER_BUILDER_IMAGE:-ghcr.io/jasonn3/build-container-installer:v1.5.0}"
source_image="${image_name}:${image_tag}"
# Keep the payload in local container storage, but use the public update
# reference as its local tag so Anaconda writes that reference as the boot
# origin instead of persisting the temporary localhost name. OSTREE_IMAGE_REF
# is intentionally tagless, matching the image-info contract.
image_basename="${image_name##*/}"
ostree_image_ref="${OSTREE_IMAGE_REF:-ostree-image-signed:docker://ghcr.io/${REPO_ORGANIZATION:-AeroCore-IO}/${image_basename}}"
install_image_ref="${ostree_image_ref#ostree-image-signed:docker://}"
install_image_ref="${install_image_ref#ostree-unverified-registry:docker://}"
install_image_ref="${install_image_ref#docker://}"
payload="${install_image_ref}:${image_tag}"
output_dir="${project_root}/output"
payload_archive="${output_dir}/install-payload.oci"
iso="${output_dir}/aerocore-os-${image_tag}-amd64.iso"
checksum="${iso}-CHECKSUM"
api_socket="${PODMAN_SOCKET:-/run/podman/podman.sock}"
if [[ "${EUID}" -eq 0 ]]; then
  podman_cmd=(podman)
  socket_test=(test -S "${api_socket}")
else
  podman_cmd=(sudo podman)
  socket_test=(sudo test -S "${api_socket}")
fi

if ! "${socket_test[@]}"; then
  echo "Podman API socket not found: ${api_socket}" >&2
  echo "Set PODMAN_SOCKET to the rootful Podman socket and retry." >&2
  exit 1
fi

ensure_rootful_image() {
  local root_image_id user_image_id user_name user_uid copy_tmp

  root_image_id="$("${podman_cmd[@]}" images \
    --filter "reference=${source_image}" \
    --format "{{.ID}}" | head -n1)"

  if [[ -n "${root_image_id}" ]]; then
    return 0
  fi

  if [[ "${EUID}" -ne 0 ]]; then
    just _rootful_load_image "${image_name}" "${image_tag}"
    return
  fi

  if [[ -z "${SUDO_USER:-}" ]]; then
    return
  fi

  user_name="${SUDO_USER}"
  user_uid="$(id -u "${user_name}")"
  user_image_id="$(sudo -u "${user_name}" \
    env XDG_RUNTIME_DIR="/run/user/${user_uid}" \
    podman images \
      --filter "reference=${source_image}" \
      --format "{{.ID}}" | head -n1)"

  if [[ -z "${user_image_id}" ]]; then
    return
  fi

  copy_tmp="$(mktemp -p "${project_root}" -d -t _build_podman_scp.XXXXXXXXXX)"
  TMPDIR="${copy_tmp}" podman image scp \
    "${user_uid}@localhost::${source_image}" \
    "root@localhost::${source_image}"
  rm -rf "${copy_tmp}"
}

mkdir -p "${output_dir}"
ensure_rootful_image

"${podman_cmd[@]}" tag "${source_image}" "${payload}"
"${podman_cmd[@]}" save --format oci-archive --output "${payload_archive}" "${payload}"

"${podman_cmd[@]}" build \
  --cap-add sys_admin \
  --security-opt label=disable \
  --build-arg "BASE_IMAGE=${live_base_image}" \
  --build-arg "INSTALL_IMAGE_PAYLOAD=${payload}" \
  --build-arg "INSTALL_IMAGE_PAYLOAD_ARCHIVE=/install-payload.oci" \
  --build-arg "INSTALL_IMAGE_TAG=${image_tag}" \
  --build-arg "OSTREE_IMAGE_REF=${ostree_image_ref}" \
  --tag "${payload}" \
  --file installer/Containerfile \
  .

if [[ "${EUID}" -eq 0 ]]; then
  rm -f "${iso}" "${checksum}"
else
  sudo rm -f "${iso}" "${checksum}"
fi

"${podman_cmd[@]}" run --rm --privileged \
  --volume "${api_socket}:/var/run/docker.sock" \
  --volume /var/lib/containers/storage:/var/lib/containers/storage \
  --volume "${project_root}/output:/build-container-installer/build" \
  --volume "${project_root}/installer/lorax_templates:/additional_lorax_templates:ro" \
  "${installer_builder_image}" \
  ADDITIONAL_TEMPLATES="/additional_lorax_templates/remove_root_password_prompt.tmpl /additional_lorax_templates/set_default_user.tmpl" \
  ARCH="x86_64" \
  ENABLE_CACHE_DNF="false" \
  ENABLE_CACHE_SKOPEO="false" \
  ENABLE_FLATPAK_DEPENDENCIES="false" \
  IMAGE_NAME="aerocore-os-installer" \
  IMAGE_REPO="localhost" \
  IMAGE_SRC="containers-storage:${payload}" \
  IMAGE_TAG="latest" \
  ISO_NAME="build/aerocore-os-${image_tag}-amd64.iso" \
  VARIANT="Kinoite" \
  VERSION="${FEDORA_VERSION:-44}"

if [[ -f "${iso}" ]]; then
  if [[ "${EUID}" -ne 0 ]]; then
    sudo chown "$(id -u):$(id -g)" "${iso}"
    sudo rm -f "${checksum}"
    (cd "${output_dir}" && sha256sum "$(basename "${iso}")") | sudo tee "${checksum}"
    sudo chown "$(id -u):$(id -g)" "${checksum}"
  else
    (cd "${output_dir}" && sha256sum "$(basename "${iso}")") | tee "${checksum}"
  fi
fi