build-image 6.17 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."
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
cwd=$(pwd)
Pedro Vicente's avatar
Pedro Vicente committed
88

89
distro=${DIST:-buster+ci}
90
mirror=${MIRROR:-"http://deb.debian.org/debian"}
91
pureos_board=imx6
92
build_type=unstable
Guido Gunther's avatar
Guido Gunther committed
93
NULL=
94
uboot_bin=
Guido Gunther's avatar
Guido Gunther committed
95
build_tarball=
Guido Gunther's avatar
Guido Gunther committed
96
want_qcow2=
97
apt_sources=
98
debootstrap_dist=
99
kernel_deb=
100
custom_package_opt=
101
board_packages=
102
meta_yml=files/meta.yml
Pedro Vicente's avatar
Pedro Vicente committed
103

Guido Gunther's avatar
Guido Gunther committed
104
while getopts ":hb:d:T:B" opt; do
105 106 107 108 109 110 111
  case ${opt} in
    b)
      pureos_board=$OPTARG
      ;;
    d)
      distro=$OPTARG
      ;;
112 113 114
    T)
      build_type=$OPTARG
      ;;
Guido Gunther's avatar
Guido Gunther committed
115 116 117
    B)
      build_tarball=1
      ;;
118 119
    \?)
      echo "Invalid Option: -$OPTARG" 1>&2
120
      usage "$0"
121 122 123
      exit 0
      ;;
    h )
124
      usage "$0"
125 126 127 128 129
      exit 0
      ;;
  esac
done

130 131 132 133
# If distro ends in CI use ci packages and cleanup distro name
[[ "${distro}" =~ \+ci ]] && ci_pkgs=1 || ci_pkgs=0
distro="${distro/+ci}"

134
bootimg="${pureos_board}.img"
135

136
. conf/board
137
. conf/packages
138 139
# shellcheck source=conf/dists/buster
. conf/dists/"${distro}"
140

141 142
[ -n "${debootstrap_dist}" ] || debootstrap_dist="${distro}"
echo "Building ${bootimg} (board_type: ${board_type}, arch: ${arch}, build_type: ${build_type}, ci_pkgs: ${ci_pkgs}, ${debootstrap_dist})"
143
echo "Installing extra packages ${packages}"
144
[ -z "${board_packages}" ] || echo "Installing extra board specific packages ${board_packages}"
145

Guido Gunther's avatar
Guido Gunther committed
146
mkdir -p tmp/ files/
147 148 149
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
150

151 152 153 154 155 156 157 158 159 160 161 162
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
163 164
    [ -n "${kernel_deb}" ] || { echo "No kernel found."; exit 1; }
    custom_package_opt=(--custom-package "${kernel_deb}")
165 166 167 168
else
    kernel_url=https://storage.puri.sm/librem5/binaries/${build_type}/latest/files/
    if [ -n "${kernel_deb}" ] && [ ! -f "files/${kernel_deb}" ]; then
	wget -Pfiles/ "${kernel_url}/${kernel_deb}"
169
	custom_package_opt=(--custom-package "files/${kernel_deb}")
170
    fi
Guido Gunther's avatar
Guido Gunther committed
171

172 173 174 175
    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/
176 177
fi

178
echo "Using kernel ${kernel_deb}"
179

180 181 182 183
# Preserve newlines as '|'
# shellcheck disable=SC2001
escaped_apt_sources=$(echo "${apt_sources}" | sed 's/$/|/g')

184
set -x
185
# shellcheck disable=SC2086
186 187
sudo pureos_board="${pureos_board}" \
     distro="${distro}" \
188
     packages="${packages} ${board_packages}" \
189 190 191
     http_proxy="${http_apt_proxy:-${http_proxy:-}}" \
     https_proxy="${https_apt_proxy:-${https_proxy:-}}" \
     qemu="${qemu}" \
192
     fdt_file="${fdt_file}" \
Guido Gunther's avatar
Guido Gunther committed
193
     build_tarball="${build_tarball}" \
194
     apt_sources="${escaped_apt_sources}" \
195
     ci_pkgs="${ci_pkgs}" \
196
     kernel_deb="${kernel_deb}" \
Guido Gunther's avatar
Guido Gunther committed
197 198
    vmdebootstrap \
    --enable-dhcp \
199
    --verbose --image "${bootimg}" --log "tmp/${bootimg}.log" --size 3600M  \
200
    --mirror "${mirror}" \
Pedro Vicente's avatar
Pedro Vicente committed
201
    --configure-apt \
202
    --distribution "${debootstrap_dist}" \
Pedro Vicente's avatar
Pedro Vicente committed
203
    --serial-console-command "/sbin/getty -L ttymxc0 115200 vt100" \
Guido Gunther's avatar
Guido Gunther committed
204
    --root-password='root' --hostname='pureos' --user=purism/123456 --sudo \
205
    --customize "${cwd}/root.sh" \
206
    --owner "$(whoami)" \
207
    "${custom_package_opt[@]}" \
208
    "${vmdebootstrap_opts[@]}" \
209
    ${NULL}
210
set +x
211

212

213
if [ "${arch}" == "arm64" ]; then
214
    if [ ! -f "files/uboot-${uboot_type}/m4.bin" ]; then
215
        wget -Pfiles/uboot-${uboot_type}/ "${kernel_url}/uboot-${uboot_type}/m4.bin"
216 217
    fi

218
    loop=$(setup_loop "${bootimg}")
219 220
    sudo dd if="files/uboot-${uboot_type}/${uboot_bin}" of="${loop}" bs=1024 seek=33
    sudo dd if="files/uboot-${uboot_type}/m4.bin" of="${loop}" bs=1024 seek=2
221
    sudo losetup -d "${loop}"
222
fi
Guido Gunther's avatar
Guido Gunther committed
223

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

226 227
write_meta "${meta_yml}" "${bootimg}"

Guido Gunther's avatar
Guido Gunther committed
228 229 230 231 232
if [ "${want_qcow2}" == 1 ]; then
    qcow2="$(basename "${bootimg}" .img)".qcow2
    qemu-img convert -f raw -O qcow2 "${bootimg}" "${qcow2}"
    qemu-img resize -f qcow2 "${qcow2}" +28G
fi