build-image 6.09 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)"
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=
95
build_tarball=
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

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
      ;;
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

146
mkdir -p tmp/ files/
147
rm -f "./tmp/${pureos_board}-report.log" "${meta_yml}"
148

149 150 151 152 153 154 155 156 157 158 159 160
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
161 162
    [ -n "${kernel_deb}" ] || { echo "No kernel found."; exit 1; }
    custom_package_opt=(--custom-package "${kernel_deb}")
163 164 165 166
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}"
167
	custom_package_opt=(--custom-package "files/${kernel_deb}")
168
    fi
Guido Gunther's avatar
Guido Gunther committed
169

170 171 172 173
    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/
174 175
fi

176
echo "Using kernel ${kernel_deb}"
177

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

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

210

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

216
    loop=$(setup_loop "${bootimg}")
217 218
    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
219
    sudo losetup -d "${loop}"
220
fi
221

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

224 225
write_meta "${meta_yml}" "${bootimg}"

226 227 228 229 230
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