build-image 5.55 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
cwd=$(pwd)
Pedro Vicente's avatar
Pedro Vicente committed
72

73
distro=${DIST:-buster+ci}
74
mirror=${MIRROR:-"http://deb.debian.org/debian"}
75
pureos_board=imx6
76
build_type=unstable
Guido Gunther's avatar
Guido Gunther committed
77
NULL=
78
uboot_bin=
79
build_tarball=
80
want_qcow2=
81
apt_sources=
82
debootstrap_dist=
83
kernel_deb=
84
custom_package_opt=
85
board_packages=
Pedro Vicente's avatar
Pedro Vicente committed
86

87
while getopts ":hb:d:T:B" opt; do
88 89 90 91 92 93 94
  case ${opt} in
    b)
      pureos_board=$OPTARG
      ;;
    d)
      distro=$OPTARG
      ;;
95 96 97
    T)
      build_type=$OPTARG
      ;;
98 99 100
    B)
      build_tarball=1
      ;;
101 102
    \?)
      echo "Invalid Option: -$OPTARG" 1>&2
103
      usage "$0"
104 105 106
      exit 0
      ;;
    h )
107
      usage "$0"
108 109 110 111 112
      exit 0
      ;;
  esac
done

113 114 115 116
# If distro ends in CI use ci packages and cleanup distro name
[[ "${distro}" =~ \+ci ]] && ci_pkgs=1 || ci_pkgs=0
distro="${distro/+ci}"

117
bootimg="${pureos_board}.img"
118

119
. conf/board
120
. conf/packages
121 122
# shellcheck source=conf/dists/buster
. conf/dists/"${distro}"
123

124 125
[ -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})"
126
echo "Installing extra packages ${packages}"
127
echo "Installing extra board specific packages ${board_packages}"
128

129
mkdir -p tmp/ files/
130
echo -n > files/meta.yml
131

132 133 134 135 136 137 138 139 140 141 142 143
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
144 145
    [ -n "${kernel_deb}" ] || { echo "No kernel found."; exit 1; }
    custom_package_opt=(--custom-package "${kernel_deb}")
146 147 148 149
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}"
150
	custom_package_opt=(--custom-package "files/${kernel_deb}")
151
    fi
Guido Gunther's avatar
Guido Gunther committed
152

153 154 155 156
    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/
157 158
fi

159
echo "Using kernel ${kernel_deb}"
160

161 162 163 164
# Preserve newlines as '|'
# shellcheck disable=SC2001
escaped_apt_sources=$(echo "${apt_sources}" | sed 's/$/|/g')

165
set -x
166
# shellcheck disable=SC2086
167 168
sudo pureos_board="${pureos_board}" \
     distro="${distro}" \
169
     packages="${packages} ${board_packages}" \
170 171 172
     http_proxy="${http_apt_proxy:-${http_proxy:-}}" \
     https_proxy="${https_apt_proxy:-${https_proxy:-}}" \
     qemu="${qemu}" \
173
     fdt_file="${fdt_file}" \
174
     build_tarball="${build_tarball}" \
175
     apt_sources="${escaped_apt_sources}" \
176
     ci_pkgs="${ci_pkgs}" \
Guido Gunther's avatar
Guido Gunther committed
177 178
    vmdebootstrap \
    --enable-dhcp \
179
    --verbose --image "${bootimg}" --log "tmp/${bootimg}.log" --size 3600M  \
180
    --mirror "${mirror}" \
Pedro Vicente's avatar
Pedro Vicente committed
181
    --configure-apt \
182
    --distribution "${debootstrap_dist}" \
Pedro Vicente's avatar
Pedro Vicente committed
183
    --serial-console-command "/sbin/getty -L ttymxc0 115200 vt100" \
184
    --root-password='root' --hostname='pureos' --user=purism/123456 --sudo \
185
    --customize "${cwd}/root.sh" \
186
    --owner "$(whoami)" \
187
    "${custom_package_opt[@]}" \
188
    "${vmdebootstrap_opts[@]}" \
189
    ${NULL}
190
set +x
191

192
echo "gitrev: $(git log --format=format:%H -1)" >> files/meta.yml
193

194
if [ "${arch}" == "arm64" ]; then
195
    if [ ! -f "files/uboot-${uboot_type}/m4.bin" ]; then
196
        wget -Pfiles/uboot-${uboot_type}/ "${kernel_url}/uboot-${uboot_type}/m4.bin"
197 198
    fi

199
    loop=$(setup_loop "${bootimg}")
200 201
    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
202
    sudo losetup -d "${loop}"
203
fi
204 205 206 207 208 209

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