build-image 6.91 KB
Newer Older
Pedro Vicente's avatar
Pedro Vicente committed
1
#!/bin/bash
2
3
4
5
6
#
# Copyright (C) 2017 Purism SPC
#
# SPDX-License-Identifier: GPL-3.0+
#
Pedro Vicente's avatar
Pedro Vicente committed
7
8

set -e
Guido Gunther's avatar
Guido Gunther committed
9
set -u
Pedro Vicente's avatar
Pedro Vicente committed
10

11
12
13
usage() {
  echo "Usage : $1 "
  echo "    -h Display this help message."
Guido Gunther's avatar
Guido Gunther committed
14
  echo "    -b [imx6|imx8|devkit|qemu-x86_64] select the board type."
15
  echo "    -d <distro> distro to use. [ green, purple, buster, green+ci, buster+ci, purple+ci ]"
16
17
  # TODO: add stable and drop ci
  echo "    -T kernel/uboot build type (unstable/ci/latest)"
Guido Gunther's avatar
Guido Gunther committed
18
  echo "    -B build rootfs tarball as well"
19
20
}

21
22

fetch_nitrogen_uboot_upgrade() {
23
24
25
26
   local board="$1"
   local url="$2"
   local out="$3"
   dest="${out}/uboot-${board}"
27
28
29
30

   [ ! -d "${dest}" ] || return 0

   echo -n "Fetching uboot upgrade..."
31
   wget -q -Ascr -Aupgrade -A"*nitrogen*" -P"${dest}" -nd -l 1 --mirror "${url}/uboot-${board}/"
32
33
34
   echo "done."
}

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

fetch_latest_kernel() {
    [ -n "${kernel_latest_url}" ] || { echo "kernel_latest_url not set in board config"; exit 1; }
    rm -f files/archive.zip
    echo "Fetching kernel from ${kernel_latest_url}"
    wget -nv --no-glob -Pfiles/ "${kernel_latest_url}"
    (cd files/ && unzip -ojq archive.zip)
}


fetch_latest_uboot() {
    local dir="files/uboot-${1}"

    [ -n "${uboot_latest_url}" ] || { echo "uboot_latest_url not set in board config"; exit 1; }
    rm -f "${dir}/archive.zip"
    echo "Fetching uboot from ${uboot_latest_url}"
    wget -nv --no-glob -P"${dir}" "${uboot_latest_url}"
    (cd "${dir}" && unzip -ojq archive.zip)
}


56
57
58
59
setup_loop() {
    local image="$1"
    set -x

60
    loop=$(/sbin/losetup -f)
61
62
63
64
    if [ -z "${loop}" ]; then
      return 1
    fi

65
    sudo /sbin/losetup -P "${loop}" "${image}"
66
67
68
69
    echo "${loop}"

    return 0
}
70

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
write_meta() {
    local out="$1"
    local image="$2"

    image_sha256sum=$(sha256sum "${image}" | sed 's/\([a-f0-9]\+\) .*/\1/')
    image_size=$(stat --format='%s' "${image}")
    echo "Image '${image} has sha256sum ${image_sha256sum}"

    cat <<EOF >> "${out}"
gitrev: $(git log --format=format:%H -1)
image:
  size: ${image_size}
  sha256sum: ${image_sha256sum}
EOF
}

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
append_boot_image_meta() {
    local out="$1"
    local image="$2"

    image_sha256sum=$(sha256sum "${image}" | sed 's/\([a-f0-9]\+\) .*/\1/')
    image_size=$(stat --format='%s' "${image}")
    echo "Image '${image} has sha256sum ${image_sha256sum}"

    cat <<EOF >> "${out}"
boot-image:
  size: ${image_size}
  sha256sum: ${image_sha256sum}
EOF
}

102
cwd=$(pwd)
Pedro Vicente's avatar
Pedro Vicente committed
103

104
distro=${DIST:-buster+ci}
Guido Gunther's avatar
Guido Gunther committed
105
mirror=${MIRROR:-"http://deb.debian.org/debian"}
106
pureos_board=imx6
107
build_type=latest
Guido Gunther's avatar
Guido Gunther committed
108
NULL=
109
uboot_bin=
Guido Gunther's avatar
Guido Gunther committed
110
build_tarball=
Guido Gunther's avatar
Guido Gunther committed
111
want_qcow2=
112
want_boot_image=
113
apt_sources=
114
debootstrap_dist=
115
kernel_deb=
116
custom_package_opt=
117
board_packages=
118
meta_yml=files/meta.yml
119
fk_machine=
Pedro Vicente's avatar
Pedro Vicente committed
120

Guido Gunther's avatar
Guido Gunther committed
121
while getopts ":hb:d:T:B" opt; do
122
123
124
125
126
127
128
  case ${opt} in
    b)
      pureos_board=$OPTARG
      ;;
    d)
      distro=$OPTARG
      ;;
129
130
131
    T)
      build_type=$OPTARG
      ;;
Guido Gunther's avatar
Guido Gunther committed
132
133
134
    B)
      build_tarball=1
      ;;
135
136
    \?)
      echo "Invalid Option: -$OPTARG" 1>&2
137
      usage "$0"
138
139
140
      exit 0
      ;;
    h )
141
      usage "$0"
142
143
144
145
146
      exit 0
      ;;
  esac
done

147
148
149
150
# If distro ends in CI use ci packages and cleanup distro name
[[ "${distro}" =~ \+ci ]] && ci_pkgs=1 || ci_pkgs=0
distro="${distro/+ci}"

151
mainimg="${pureos_board}.img"
Guido Gunther's avatar
Guido Gunther committed
152

153
. conf/board
154
. conf/packages
155
156
# shellcheck source=conf/dists/buster
. conf/dists/"${distro}"
157

158
[ -n "${debootstrap_dist}" ] || debootstrap_dist="${distro}"
159
echo "Building ${mainimg} (board_type: ${board_type}, arch: ${arch}, build_type: ${build_type}, ci_pkgs: ${ci_pkgs}, ${debootstrap_dist})"
160
echo "Installing extra packages ${packages}"
161
[ -z "${board_packages}" ] || echo "Installing extra board specific packages ${board_packages}"
162

Guido Gunther's avatar
Guido Gunther committed
163
mkdir -p tmp/ files/
164
165
166
rm -f "./tmp/${pureos_board}-report.log"
# Create meta.yml here so it belongs to the user running image-builder
echo > "${meta_yml}"
Guido Gunther's avatar
Guido Gunther committed
167

168
169
170
171
172
173
174
175
176
177
178
179
if [ "${build_type}" == "latest" ]; then
    fetch_latest_kernel
    fetch_latest_uboot "${uboot_type}"
    kernel_deb=
    for deb in files/linux-image-*-*_arm64.deb; do
	! [[ "${deb}" =~ dbg ]] || continue
	if [ -n "${kernel_deb}" ]; then
	    echo "Found more than one possible kernel deb in files/"
	    exit 1
	fi
	kernel_deb="$deb"
    done
180
181
    [ -n "${kernel_deb}" ] || { echo "No kernel found."; exit 1; }
    custom_package_opt=(--custom-package "${kernel_deb}")
182
183
else
    kernel_url=https://storage.puri.sm/librem5/binaries/${build_type}/latest/files/
184
185
186
187
    if [ -n "${kernel_deb}" ]; then
	if [ ! -f "files/${kernel_deb}" ]; then
	    wget -Pfiles/ "${kernel_url}/${kernel_deb}"
	fi
188
	custom_package_opt=(--custom-package "files/${kernel_deb}")
189
    fi
Guido Gunther's avatar
Guido Gunther committed
190

191
192
193
194
    if [ -n "${uboot_bin}" ] && [ ! -f "files/uboot-${uboot_type}/${uboot_bin}" ]; then
	wget -Pfiles/uboot-${uboot_type} "${kernel_url}/uboot-${uboot_type}/${uboot_bin}"
    fi
    [ "${board_type}" != "nitrogen6" ] || fetch_nitrogen_uboot_upgrade "${pureos_board}" "${kernel_url}" files/
195
196
fi

197
echo "Using kernel ${kernel_deb}"
198

199
200
201
202
# Preserve newlines as '|'
# shellcheck disable=SC2001
escaped_apt_sources=$(echo "${apt_sources}" | sed 's/$/|/g')

203
set -x
204
# shellcheck disable=SC2086
205
206
sudo pureos_board="${pureos_board}" \
     distro="${distro}" \
207
     packages="${packages} ${board_packages}" \
208
     patched="${patched}" \
209
210
211
     http_proxy="${http_apt_proxy:-${http_proxy:-}}" \
     https_proxy="${https_apt_proxy:-${https_proxy:-}}" \
     qemu="${qemu}" \
212
     fdt_file="${fdt_file}" \
Guido Gunther's avatar
Guido Gunther committed
213
     build_tarball="${build_tarball}" \
214
     apt_sources="${escaped_apt_sources}" \
215
     ci_pkgs="${ci_pkgs}" \
216
     kernel_deb="${kernel_deb}" \
217
     FK_MACHINE="${fk_machine}" \
Guido Gunther's avatar
Guido Gunther committed
218
219
    vmdebootstrap \
    --enable-dhcp \
220
    --verbose --image "${mainimg}" --log "tmp/${mainimg}.log" --size 3600M  \
221
    --mirror "${mirror}" \
Pedro Vicente's avatar
Pedro Vicente committed
222
    --configure-apt \
223
    --distribution "${debootstrap_dist}" \
Pedro Vicente's avatar
Pedro Vicente committed
224
    --serial-console-command "/sbin/getty -L ttymxc0 115200 vt100" \
Guido Gunther's avatar
Guido Gunther committed
225
    --root-password='root' --hostname='pureos' --user=purism/123456 --sudo \
226
    --customize "${cwd}/root.sh" \
227
    --owner "$(whoami)" \
228
    "${custom_package_opt[@]}" \
229
    "${vmdebootstrap_opts[@]}" \
230
    ${NULL}
231
set +x
232

233

234
if [ "${arch}" == "arm64" ]; then
235
    if [ ! -f "files/uboot-${uboot_type}/m4.bin" ]; then
236
        wget -Pfiles/uboot-${uboot_type}/ "${kernel_url}/uboot-${uboot_type}/m4.bin"
237
238
    fi

239
240
241
242
243
244
245
246
247
248
    if [ "${want_boot_image}" == 1 ]; then
      # Copy the boot image as a separate file
      bootimg="${pureos_board}-boot.img"
      cp "files/uboot-${uboot_type}/${pureos_board}-boot.img" "${bootimg}"
    else
      # Merge boot image inside the main image
      loop=$(setup_loop "${mainimg}")
      sudo dd if="files/uboot-${uboot_type}/${pureos_board}-boot.img" of="${loop}" bs=1024 seek=2
      sudo losetup -d "${loop}"
    fi
249
fi
Guido Gunther's avatar
Guido Gunther committed
250

251
252
[ -f "./tmp/${pureos_board}-report.log" ] || { echo "No install report found."; exit 1; }

253
254
255
256
write_meta "${meta_yml}" "${mainimg}"
if [ "${want_boot_image}" == 1 ]; then
    append_boot_image_meta "${meta_yml}" "${bootimg}"
fi
257

Guido Gunther's avatar
Guido Gunther committed
258
if [ "${want_qcow2}" == 1 ]; then
259
260
    qcow2="$(basename "${mainimg}" .img)".qcow2
    qemu-img convert -f raw -O qcow2 "${mainimg}" "${qcow2}"
Guido Gunther's avatar
Guido Gunther committed
261
262
    qemu-img resize -f qcow2 "${qcow2}" +28G
fi