xref: /aosp_15_r20/kernel/tests/net/test/build_rootfs.sh (revision 2f2c4c7ab4226c71756b9c31670392fdd6887c4f)
1*2f2c4c7aSAndroid Build Coastguard Worker#!/bin/bash
2*2f2c4c7aSAndroid Build Coastguard Worker#
3*2f2c4c7aSAndroid Build Coastguard Worker# Copyright (C) 2018 The Android Open Source Project
4*2f2c4c7aSAndroid Build Coastguard Worker#
5*2f2c4c7aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*2f2c4c7aSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*2f2c4c7aSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*2f2c4c7aSAndroid Build Coastguard Worker#
9*2f2c4c7aSAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
10*2f2c4c7aSAndroid Build Coastguard Worker#
11*2f2c4c7aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*2f2c4c7aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*2f2c4c7aSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*2f2c4c7aSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*2f2c4c7aSAndroid Build Coastguard Worker# limitations under the License.
16*2f2c4c7aSAndroid Build Coastguard Worker#
17*2f2c4c7aSAndroid Build Coastguard Worker
18*2f2c4c7aSAndroid Build Coastguard Workerset -e
19*2f2c4c7aSAndroid Build Coastguard Workerset -u
20*2f2c4c7aSAndroid Build Coastguard Worker
21*2f2c4c7aSAndroid Build Coastguard WorkerSCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)
22*2f2c4c7aSAndroid Build Coastguard Worker
23*2f2c4c7aSAndroid Build Coastguard Workerusage() {
24*2f2c4c7aSAndroid Build Coastguard Worker  echo -n "usage: $0 [-h] [-s bullseye|bullseye-cuttlefish|bullseye-rockpi|bullseye-server] "
25*2f2c4c7aSAndroid Build Coastguard Worker  echo -n "[-a i386|amd64|armhf|arm64] -k /path/to/kernel "
26*2f2c4c7aSAndroid Build Coastguard Worker  echo -n "-i /path/to/initramfs.gz [-d /path/to/dtb:subdir] "
27*2f2c4c7aSAndroid Build Coastguard Worker  echo "[-m http://mirror/debian] [-n rootfs|disk] [-r initrd] [-e] [-g]"
28*2f2c4c7aSAndroid Build Coastguard Worker  exit 1
29*2f2c4c7aSAndroid Build Coastguard Worker}
30*2f2c4c7aSAndroid Build Coastguard Worker
31*2f2c4c7aSAndroid Build Coastguard Workermirror=http://ftp.debian.org/debian
32*2f2c4c7aSAndroid Build Coastguard Workerembed_kernel_initrd_dtb=0
33*2f2c4c7aSAndroid Build Coastguard Workerinstall_grub=0
34*2f2c4c7aSAndroid Build Coastguard Workersuite=bullseye
35*2f2c4c7aSAndroid Build Coastguard Workerarch=amd64
36*2f2c4c7aSAndroid Build Coastguard Worker
37*2f2c4c7aSAndroid Build Coastguard Workerdtb_subdir=
38*2f2c4c7aSAndroid Build Coastguard Workerinitramfs=
39*2f2c4c7aSAndroid Build Coastguard Workerkernel=
40*2f2c4c7aSAndroid Build Coastguard Workerramdisk=
41*2f2c4c7aSAndroid Build Coastguard Workerdisk=
42*2f2c4c7aSAndroid Build Coastguard Workerdtb=
43*2f2c4c7aSAndroid Build Coastguard Worker
44*2f2c4c7aSAndroid Build Coastguard Workerwhile getopts ":hs:a:m:n:r:k:O:i:d:eg" opt; do
45*2f2c4c7aSAndroid Build Coastguard Worker  case "${opt}" in
46*2f2c4c7aSAndroid Build Coastguard Worker    h)
47*2f2c4c7aSAndroid Build Coastguard Worker      usage
48*2f2c4c7aSAndroid Build Coastguard Worker      ;;
49*2f2c4c7aSAndroid Build Coastguard Worker    s)
50*2f2c4c7aSAndroid Build Coastguard Worker      if [[ "${OPTARG%-*}" != "bullseye" ]]; then
51*2f2c4c7aSAndroid Build Coastguard Worker        echo "Invalid suite: ${OPTARG}" >&2
52*2f2c4c7aSAndroid Build Coastguard Worker        usage
53*2f2c4c7aSAndroid Build Coastguard Worker      fi
54*2f2c4c7aSAndroid Build Coastguard Worker      suite="${OPTARG}"
55*2f2c4c7aSAndroid Build Coastguard Worker      ;;
56*2f2c4c7aSAndroid Build Coastguard Worker    a)
57*2f2c4c7aSAndroid Build Coastguard Worker      arch="${OPTARG}"
58*2f2c4c7aSAndroid Build Coastguard Worker      ;;
59*2f2c4c7aSAndroid Build Coastguard Worker    m)
60*2f2c4c7aSAndroid Build Coastguard Worker      mirror="${OPTARG}"
61*2f2c4c7aSAndroid Build Coastguard Worker      ;;
62*2f2c4c7aSAndroid Build Coastguard Worker    n)
63*2f2c4c7aSAndroid Build Coastguard Worker      disk="${OPTARG}"
64*2f2c4c7aSAndroid Build Coastguard Worker      ;;
65*2f2c4c7aSAndroid Build Coastguard Worker    r)
66*2f2c4c7aSAndroid Build Coastguard Worker      ramdisk="${OPTARG}"
67*2f2c4c7aSAndroid Build Coastguard Worker      ;;
68*2f2c4c7aSAndroid Build Coastguard Worker    k)
69*2f2c4c7aSAndroid Build Coastguard Worker      kernel="${OPTARG}"
70*2f2c4c7aSAndroid Build Coastguard Worker      ;;
71*2f2c4c7aSAndroid Build Coastguard Worker    O)
72*2f2c4c7aSAndroid Build Coastguard Worker      extradeb="${OPTARG}"
73*2f2c4c7aSAndroid Build Coastguard Worker      ;;
74*2f2c4c7aSAndroid Build Coastguard Worker    i)
75*2f2c4c7aSAndroid Build Coastguard Worker      initramfs="${OPTARG}"
76*2f2c4c7aSAndroid Build Coastguard Worker      ;;
77*2f2c4c7aSAndroid Build Coastguard Worker    d)
78*2f2c4c7aSAndroid Build Coastguard Worker      dtb="${OPTARG%:*}"
79*2f2c4c7aSAndroid Build Coastguard Worker      if [ "${OPTARG#*:}" != "${dtb}" ]; then
80*2f2c4c7aSAndroid Build Coastguard Worker        dtb_subdir="${OPTARG#*:}/"
81*2f2c4c7aSAndroid Build Coastguard Worker      fi
82*2f2c4c7aSAndroid Build Coastguard Worker      ;;
83*2f2c4c7aSAndroid Build Coastguard Worker    e)
84*2f2c4c7aSAndroid Build Coastguard Worker      embed_kernel_initrd_dtb=1
85*2f2c4c7aSAndroid Build Coastguard Worker      ;;
86*2f2c4c7aSAndroid Build Coastguard Worker    g)
87*2f2c4c7aSAndroid Build Coastguard Worker      install_grub=1
88*2f2c4c7aSAndroid Build Coastguard Worker      ;;
89*2f2c4c7aSAndroid Build Coastguard Worker    \?)
90*2f2c4c7aSAndroid Build Coastguard Worker      echo "Invalid option: ${OPTARG}" >&2
91*2f2c4c7aSAndroid Build Coastguard Worker      usage
92*2f2c4c7aSAndroid Build Coastguard Worker      ;;
93*2f2c4c7aSAndroid Build Coastguard Worker    :)
94*2f2c4c7aSAndroid Build Coastguard Worker      echo "Invalid option: ${OPTARG} requires an argument" >&2
95*2f2c4c7aSAndroid Build Coastguard Worker      usage
96*2f2c4c7aSAndroid Build Coastguard Worker      ;;
97*2f2c4c7aSAndroid Build Coastguard Worker  esac
98*2f2c4c7aSAndroid Build Coastguard Workerdone
99*2f2c4c7aSAndroid Build Coastguard Worker
100*2f2c4c7aSAndroid Build Coastguard Worker# Disable Debian's "persistent" network device renaming
101*2f2c4c7aSAndroid Build Coastguard Workercmdline="net.ifnames=0 rw 8250.nr_uarts=2 PATH=/usr/sbin:/bin:/usr/bin"
102*2f2c4c7aSAndroid Build Coastguard Workercmdline="${cmdline} embed_kernel_initrd_dtb=${embed_kernel_initrd_dtb}"
103*2f2c4c7aSAndroid Build Coastguard Workercmdline="${cmdline} install_grub=${install_grub}"
104*2f2c4c7aSAndroid Build Coastguard Worker
105*2f2c4c7aSAndroid Build Coastguard Workercase "${arch}" in
106*2f2c4c7aSAndroid Build Coastguard Worker  i386)
107*2f2c4c7aSAndroid Build Coastguard Worker    cmdline="${cmdline} console=ttyS0 exitcode=/dev/ttyS1"
108*2f2c4c7aSAndroid Build Coastguard Worker    machine="pc-i440fx-2.8,accel=kvm"
109*2f2c4c7aSAndroid Build Coastguard Worker    qemu="qemu-system-i386"
110*2f2c4c7aSAndroid Build Coastguard Worker    partguid="8303"
111*2f2c4c7aSAndroid Build Coastguard Worker    cpu="max"
112*2f2c4c7aSAndroid Build Coastguard Worker    ;;
113*2f2c4c7aSAndroid Build Coastguard Worker  amd64)
114*2f2c4c7aSAndroid Build Coastguard Worker    cmdline="${cmdline} console=ttyS0 exitcode=/dev/ttyS1"
115*2f2c4c7aSAndroid Build Coastguard Worker    machine="pc-i440fx-2.8,accel=kvm"
116*2f2c4c7aSAndroid Build Coastguard Worker    qemu="qemu-system-x86_64"
117*2f2c4c7aSAndroid Build Coastguard Worker    partguid="8304"
118*2f2c4c7aSAndroid Build Coastguard Worker    cpu="max"
119*2f2c4c7aSAndroid Build Coastguard Worker    ;;
120*2f2c4c7aSAndroid Build Coastguard Worker  armhf)
121*2f2c4c7aSAndroid Build Coastguard Worker    cmdline="${cmdline} console=ttyAMA0 exitcode=/dev/ttyS0"
122*2f2c4c7aSAndroid Build Coastguard Worker    machine="virt,gic-version=2"
123*2f2c4c7aSAndroid Build Coastguard Worker    qemu="qemu-system-arm"
124*2f2c4c7aSAndroid Build Coastguard Worker    partguid="8307"
125*2f2c4c7aSAndroid Build Coastguard Worker    cpu="cortex-a15"
126*2f2c4c7aSAndroid Build Coastguard Worker    ;;
127*2f2c4c7aSAndroid Build Coastguard Worker  arm64)
128*2f2c4c7aSAndroid Build Coastguard Worker    cmdline="${cmdline} console=ttyAMA0 exitcode=/dev/ttyS0"
129*2f2c4c7aSAndroid Build Coastguard Worker    machine="virt,gic-version=2"
130*2f2c4c7aSAndroid Build Coastguard Worker    qemu="qemu-system-aarch64"
131*2f2c4c7aSAndroid Build Coastguard Worker    partguid="8305"
132*2f2c4c7aSAndroid Build Coastguard Worker    cpu="cortex-a53" # "max" is too slow
133*2f2c4c7aSAndroid Build Coastguard Worker    ;;
134*2f2c4c7aSAndroid Build Coastguard Worker  *)
135*2f2c4c7aSAndroid Build Coastguard Worker    echo "Invalid arch: ${OPTARG}" >&2
136*2f2c4c7aSAndroid Build Coastguard Worker    usage
137*2f2c4c7aSAndroid Build Coastguard Worker    ;;
138*2f2c4c7aSAndroid Build Coastguard Workeresac
139*2f2c4c7aSAndroid Build Coastguard Worker
140*2f2c4c7aSAndroid Build Coastguard Workerif [[ -z "${disk}" ]]; then
141*2f2c4c7aSAndroid Build Coastguard Worker  if [[ "${install_grub}" = "1" ]]; then
142*2f2c4c7aSAndroid Build Coastguard Worker    base_image_name=disk
143*2f2c4c7aSAndroid Build Coastguard Worker  else
144*2f2c4c7aSAndroid Build Coastguard Worker    base_image_name=rootfs
145*2f2c4c7aSAndroid Build Coastguard Worker  fi
146*2f2c4c7aSAndroid Build Coastguard Worker  disk="${base_image_name}.${arch}.${suite}.$(date +%Y%m%d)"
147*2f2c4c7aSAndroid Build Coastguard Workerfi
148*2f2c4c7aSAndroid Build Coastguard Workerdisk=$(realpath "${disk}")
149*2f2c4c7aSAndroid Build Coastguard Worker
150*2f2c4c7aSAndroid Build Coastguard Workerif [[ -z "${ramdisk}" ]]; then
151*2f2c4c7aSAndroid Build Coastguard Worker  ramdisk="initrd.${arch}.${suite}.$(date +%Y%m%d)"
152*2f2c4c7aSAndroid Build Coastguard Workerfi
153*2f2c4c7aSAndroid Build Coastguard Workerramdisk=$(realpath "${ramdisk}")
154*2f2c4c7aSAndroid Build Coastguard Worker
155*2f2c4c7aSAndroid Build Coastguard Workerif [[ -z "${kernel}" ]]; then
156*2f2c4c7aSAndroid Build Coastguard Worker  echo "$0: Path to kernel image must be specified (with '-k')"
157*2f2c4c7aSAndroid Build Coastguard Worker  usage
158*2f2c4c7aSAndroid Build Coastguard Workerelif [[ ! -e "${kernel}" ]]; then
159*2f2c4c7aSAndroid Build Coastguard Worker  echo "$0: Kernel image not found at '${kernel}'"
160*2f2c4c7aSAndroid Build Coastguard Worker  exit 2
161*2f2c4c7aSAndroid Build Coastguard Workerfi
162*2f2c4c7aSAndroid Build Coastguard Worker
163*2f2c4c7aSAndroid Build Coastguard Workerif [[ -z "${initramfs}" ]]; then
164*2f2c4c7aSAndroid Build Coastguard Worker  echo "Path to initial ramdisk image must be specified (with '-i')"
165*2f2c4c7aSAndroid Build Coastguard Worker  usage
166*2f2c4c7aSAndroid Build Coastguard Workerelif [[ ! -e "${initramfs}" ]]; then
167*2f2c4c7aSAndroid Build Coastguard Worker  echo "Initial ramdisk image not found at '${initramfs}'"
168*2f2c4c7aSAndroid Build Coastguard Worker  exit 3
169*2f2c4c7aSAndroid Build Coastguard Workerfi
170*2f2c4c7aSAndroid Build Coastguard Worker
171*2f2c4c7aSAndroid Build Coastguard Worker# Sometimes it isn't obvious when the script fails
172*2f2c4c7aSAndroid Build Coastguard Workerfailure() {
173*2f2c4c7aSAndroid Build Coastguard Worker  echo "Filesystem generation process failed." >&2
174*2f2c4c7aSAndroid Build Coastguard Worker  rm -f "${disk}" "${ramdisk}"
175*2f2c4c7aSAndroid Build Coastguard Worker}
176*2f2c4c7aSAndroid Build Coastguard Workertrap failure ERR
177*2f2c4c7aSAndroid Build Coastguard Worker
178*2f2c4c7aSAndroid Build Coastguard Worker# Import the package list for this release
179*2f2c4c7aSAndroid Build Coastguard Workerpackages=$(cpp "${SCRIPT_DIR}/rootfs/${suite}.list" | grep -v "^#" | xargs | tr -s ' ' ',')
180*2f2c4c7aSAndroid Build Coastguard Worker
181*2f2c4c7aSAndroid Build Coastguard Worker# For the debootstrap intermediates
182*2f2c4c7aSAndroid Build Coastguard Workertmpdir=$(mktemp -d)
183*2f2c4c7aSAndroid Build Coastguard Workertmpdir_remove() {
184*2f2c4c7aSAndroid Build Coastguard Worker  echo "Removing temporary files.." >&2
185*2f2c4c7aSAndroid Build Coastguard Worker  sudo rm -rf "${tmpdir}"
186*2f2c4c7aSAndroid Build Coastguard Worker}
187*2f2c4c7aSAndroid Build Coastguard Workertrap tmpdir_remove EXIT
188*2f2c4c7aSAndroid Build Coastguard Worker
189*2f2c4c7aSAndroid Build Coastguard Workerworkdir="${tmpdir}/_"
190*2f2c4c7aSAndroid Build Coastguard Workermkdir "${workdir}"
191*2f2c4c7aSAndroid Build Coastguard Workerchmod 0755 "${workdir}"
192*2f2c4c7aSAndroid Build Coastguard Workersudo chown root:root "${workdir}"
193*2f2c4c7aSAndroid Build Coastguard Worker
194*2f2c4c7aSAndroid Build Coastguard Worker# Run the debootstrap first
195*2f2c4c7aSAndroid Build Coastguard Workercd "${workdir}"
196*2f2c4c7aSAndroid Build Coastguard Worker
197*2f2c4c7aSAndroid Build Coastguard Workerretries=5
198*2f2c4c7aSAndroid Build Coastguard Workerwhile ! sudo debootstrap --arch="${arch}" --variant=minbase --include="${packages}" \
199*2f2c4c7aSAndroid Build Coastguard Worker        --foreign "${suite%-*}" . "${mirror}"; do
200*2f2c4c7aSAndroid Build Coastguard Worker    retries=$((${retries} - 1))
201*2f2c4c7aSAndroid Build Coastguard Worker    if [ ${retries} -le 0 ]; then
202*2f2c4c7aSAndroid Build Coastguard Worker	failure
203*2f2c4c7aSAndroid Build Coastguard Worker	exit 1
204*2f2c4c7aSAndroid Build Coastguard Worker    fi
205*2f2c4c7aSAndroid Build Coastguard Worker    echo "debootstrap failed - trying again - ${retries} retries left"
206*2f2c4c7aSAndroid Build Coastguard Workerdone
207*2f2c4c7aSAndroid Build Coastguard Worker
208*2f2c4c7aSAndroid Build Coastguard Worker# Copy some bootstrapping scripts into the rootfs
209*2f2c4c7aSAndroid Build Coastguard Workersudo cp -a "${SCRIPT_DIR}"/rootfs/*.sh root/
210*2f2c4c7aSAndroid Build Coastguard Workersudo cp -a "${SCRIPT_DIR}"/rootfs/net_test.sh sbin/net_test.sh
211*2f2c4c7aSAndroid Build Coastguard Workersudo chown root:root sbin/net_test.sh
212*2f2c4c7aSAndroid Build Coastguard Worker
213*2f2c4c7aSAndroid Build Coastguard Worker# Extract the ramdisk to bootstrap with to /
214*2f2c4c7aSAndroid Build Coastguard Workerlz4 -lcd "${initramfs}" | sudo cpio -idum lib/modules/*
215*2f2c4c7aSAndroid Build Coastguard Worker
216*2f2c4c7aSAndroid Build Coastguard Worker# Create /host, for the pivot_root and 9p mount use cases
217*2f2c4c7aSAndroid Build Coastguard Workersudo mkdir host
218*2f2c4c7aSAndroid Build Coastguard Worker
219*2f2c4c7aSAndroid Build Coastguard Worker# debootstrap workaround: Run debootstrap in docker sometimes causes the
220*2f2c4c7aSAndroid Build Coastguard Worker# /proc being a symlink in first stage. We need to fix the symlink to an empty
221*2f2c4c7aSAndroid Build Coastguard Worker# directory.
222*2f2c4c7aSAndroid Build Coastguard Workerif [ -L "${workdir}/proc" ]; then
223*2f2c4c7aSAndroid Build Coastguard Worker  echo "/proc in debootstrap 1st stage is a symlink. Fixed!"
224*2f2c4c7aSAndroid Build Coastguard Worker  sudo rm -f "${workdir}/proc"
225*2f2c4c7aSAndroid Build Coastguard Worker  sudo mkdir "${workdir}/proc"
226*2f2c4c7aSAndroid Build Coastguard Workerfi
227*2f2c4c7aSAndroid Build Coastguard Worker
228*2f2c4c7aSAndroid Build Coastguard Worker# Leave the workdir, to build the filesystem
229*2f2c4c7aSAndroid Build Coastguard Workercd -
230*2f2c4c7aSAndroid Build Coastguard Worker
231*2f2c4c7aSAndroid Build Coastguard Worker# For the initial ramdisk, and later for the final rootfs
232*2f2c4c7aSAndroid Build Coastguard Workermount=$(mktemp -d)
233*2f2c4c7aSAndroid Build Coastguard Workermount_remove() {
234*2f2c4c7aSAndroid Build Coastguard Worker  rmdir "${mount}"
235*2f2c4c7aSAndroid Build Coastguard Worker  tmpdir_remove
236*2f2c4c7aSAndroid Build Coastguard Worker}
237*2f2c4c7aSAndroid Build Coastguard Workertrap mount_remove EXIT
238*2f2c4c7aSAndroid Build Coastguard Worker
239*2f2c4c7aSAndroid Build Coastguard Worker# The initial ramdisk filesystem must be <=512M, or QEMU's -initrd
240*2f2c4c7aSAndroid Build Coastguard Worker# option won't touch it
241*2f2c4c7aSAndroid Build Coastguard Workerinitrd=$(mktemp)
242*2f2c4c7aSAndroid Build Coastguard Workerinitrd_remove() {
243*2f2c4c7aSAndroid Build Coastguard Worker  rm -f "${initrd}"
244*2f2c4c7aSAndroid Build Coastguard Worker  mount_remove
245*2f2c4c7aSAndroid Build Coastguard Worker}
246*2f2c4c7aSAndroid Build Coastguard Workertrap initrd_remove EXIT
247*2f2c4c7aSAndroid Build Coastguard Workertruncate -s 512M "${initrd}"
248*2f2c4c7aSAndroid Build Coastguard Worker/sbin/mke2fs -F -t ext4 -L ROOT "${initrd}"
249*2f2c4c7aSAndroid Build Coastguard Worker
250*2f2c4c7aSAndroid Build Coastguard Worker# Mount the new filesystem locally
251*2f2c4c7aSAndroid Build Coastguard Workersudo mount -o loop -t ext4 "${initrd}" "${mount}"
252*2f2c4c7aSAndroid Build Coastguard Workerimage_unmount() {
253*2f2c4c7aSAndroid Build Coastguard Worker  sudo umount "${mount}"
254*2f2c4c7aSAndroid Build Coastguard Worker  initrd_remove
255*2f2c4c7aSAndroid Build Coastguard Worker}
256*2f2c4c7aSAndroid Build Coastguard Workertrap image_unmount EXIT
257*2f2c4c7aSAndroid Build Coastguard Worker
258*2f2c4c7aSAndroid Build Coastguard Worker# Copy the patched debootstrap results into the new filesystem
259*2f2c4c7aSAndroid Build Coastguard Workersudo cp -a "${workdir}"/* "${mount}"
260*2f2c4c7aSAndroid Build Coastguard Workersudo rm -rf "${workdir}"
261*2f2c4c7aSAndroid Build Coastguard Worker
262*2f2c4c7aSAndroid Build Coastguard Worker# Unmount the initial ramdisk
263*2f2c4c7aSAndroid Build Coastguard Workersudo umount "${mount}"
264*2f2c4c7aSAndroid Build Coastguard Workertrap initrd_remove EXIT
265*2f2c4c7aSAndroid Build Coastguard Worker
266*2f2c4c7aSAndroid Build Coastguard Workerif [[ "${install_grub}" = 1 ]]; then
267*2f2c4c7aSAndroid Build Coastguard Worker  part_num=0
268*2f2c4c7aSAndroid Build Coastguard Worker  # $1 partition size
269*2f2c4c7aSAndroid Build Coastguard Worker  # $2 gpt partition type
270*2f2c4c7aSAndroid Build Coastguard Worker  # $3 partition name
271*2f2c4c7aSAndroid Build Coastguard Worker  # $4 bypass alignment checks (use on <1MB partitions only)
272*2f2c4c7aSAndroid Build Coastguard Worker  # $5 partition attribute bit to set
273*2f2c4c7aSAndroid Build Coastguard Worker  sgdisk() {
274*2f2c4c7aSAndroid Build Coastguard Worker    part_num=$((part_num+1))
275*2f2c4c7aSAndroid Build Coastguard Worker    [[ -n "${4:-}" ]] && prefix="-a1" || prefix=
276*2f2c4c7aSAndroid Build Coastguard Worker    [[ -n "${5:-}" ]] && suffix="-A:${part_num}:set:$5" || suffix=
277*2f2c4c7aSAndroid Build Coastguard Worker    /sbin/sgdisk ${prefix} \
278*2f2c4c7aSAndroid Build Coastguard Worker      "-n:${part_num}:$1" "-t:${part_num}:$2" "-c:${part_num}:$3" \
279*2f2c4c7aSAndroid Build Coastguard Worker      ${suffix} "${disk}" >/dev/null 2>&1
280*2f2c4c7aSAndroid Build Coastguard Worker  }
281*2f2c4c7aSAndroid Build Coastguard Worker  # If there's a bootloader, we need to make space for the GPT header, GPT
282*2f2c4c7aSAndroid Build Coastguard Worker  # footer and EFI system partition (legacy boot is not supported)
283*2f2c4c7aSAndroid Build Coastguard Worker  # Keep this simple - modern gdisk reserves 1MB for the GPT header and
284*2f2c4c7aSAndroid Build Coastguard Worker  # assumes all partitions are 1MB aligned
285*2f2c4c7aSAndroid Build Coastguard Worker  truncate -s "$((1 + 128 + 10 * 1024 + 1))M" "${disk}"
286*2f2c4c7aSAndroid Build Coastguard Worker  /sbin/sgdisk --zap-all "${disk}" >/dev/null 2>&1
287*2f2c4c7aSAndroid Build Coastguard Worker  # On RockPi devices, steal a bit of space at the start of the disk for
288*2f2c4c7aSAndroid Build Coastguard Worker  # some special bootloader partitions. Some of these have to start/end
289*2f2c4c7aSAndroid Build Coastguard Worker  # at specific offsets as well
290*2f2c4c7aSAndroid Build Coastguard Worker  if [[ "${suite#*-}" = "rockpi" ]]; then
291*2f2c4c7aSAndroid Build Coastguard Worker    # See https://opensource.rock-chips.com/wiki_Boot_option
292*2f2c4c7aSAndroid Build Coastguard Worker    # Keep in sync with rootfs/*-rockpi.sh
293*2f2c4c7aSAndroid Build Coastguard Worker    sgdisk "64:8127"   "8301"        "idbloader" "true"
294*2f2c4c7aSAndroid Build Coastguard Worker    sgdisk "8128:+64"  "8301"        "uboot_env" "true"
295*2f2c4c7aSAndroid Build Coastguard Worker    sgdisk "8M:+4M"    "8301"        "uboot"
296*2f2c4c7aSAndroid Build Coastguard Worker    sgdisk "12M:+4M"   "8301"        "trust"
297*2f2c4c7aSAndroid Build Coastguard Worker    sgdisk "16M:+1M"   "8301"        "misc"
298*2f2c4c7aSAndroid Build Coastguard Worker    sgdisk "17M:+128M" "ef00"        "esp"       ""     "0"
299*2f2c4c7aSAndroid Build Coastguard Worker    sgdisk "145M:0"    "8305"        "rootfs"    ""     "2"
300*2f2c4c7aSAndroid Build Coastguard Worker    system_partition="6"
301*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition="7"
302*2f2c4c7aSAndroid Build Coastguard Worker  else
303*2f2c4c7aSAndroid Build Coastguard Worker    sgdisk "0:+128M"   "ef00"        "esp"       ""     "0"
304*2f2c4c7aSAndroid Build Coastguard Worker    sgdisk "0:0"       "${partguid}" "rootfs"    ""     "2"
305*2f2c4c7aSAndroid Build Coastguard Worker    system_partition="1"
306*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition="2"
307*2f2c4c7aSAndroid Build Coastguard Worker  fi
308*2f2c4c7aSAndroid Build Coastguard Worker
309*2f2c4c7aSAndroid Build Coastguard Worker  # Create an empty EFI system partition; it will be initialized later
310*2f2c4c7aSAndroid Build Coastguard Worker  system_partition_start=$(partx -g -o START -s -n "${system_partition}" "${disk}" | xargs)
311*2f2c4c7aSAndroid Build Coastguard Worker  system_partition_end=$(partx -g -o END -s -n "${system_partition}" "${disk}" | xargs)
312*2f2c4c7aSAndroid Build Coastguard Worker  system_partition_num_sectors=$((${system_partition_end} - ${system_partition_start} + 1))
313*2f2c4c7aSAndroid Build Coastguard Worker  system_partition_num_vfat_blocks=$((${system_partition_num_sectors} / 2))
314*2f2c4c7aSAndroid Build Coastguard Worker  /sbin/mkfs.vfat -n SYSTEM -F 16 --offset=${system_partition_start} "${disk}" ${system_partition_num_vfat_blocks} >/dev/null
315*2f2c4c7aSAndroid Build Coastguard Worker  # Copy the rootfs to just after the EFI system partition
316*2f2c4c7aSAndroid Build Coastguard Worker  rootfs_partition_start=$(partx -g -o START -s -n "${rootfs_partition}" "${disk}" | xargs)
317*2f2c4c7aSAndroid Build Coastguard Worker  rootfs_partition_end=$(partx -g -o END -s -n "${rootfs_partition}" "${disk}" | xargs)
318*2f2c4c7aSAndroid Build Coastguard Worker  rootfs_partition_num_sectors=$((${rootfs_partition_end} - ${rootfs_partition_start} + 1))
319*2f2c4c7aSAndroid Build Coastguard Worker  rootfs_partition_offset=$((${rootfs_partition_start} * 512))
320*2f2c4c7aSAndroid Build Coastguard Worker  rootfs_partition_size=$((${rootfs_partition_num_sectors} * 512))
321*2f2c4c7aSAndroid Build Coastguard Worker  dd if="${initrd}" of="${disk}" bs=512 seek="${rootfs_partition_start}" conv=fsync,notrunc 2>/dev/null
322*2f2c4c7aSAndroid Build Coastguard Worker  /sbin/e2fsck -p -f "${disk}"?offset=${rootfs_partition_offset} || true
323*2f2c4c7aSAndroid Build Coastguard Worker  disksize=$(stat -c %s "${disk}")
324*2f2c4c7aSAndroid Build Coastguard Worker  /sbin/resize2fs "${disk}"?offset=${rootfs_partition_offset} ${rootfs_partition_num_sectors}s
325*2f2c4c7aSAndroid Build Coastguard Worker  truncate -s "${disksize}" "${disk}"
326*2f2c4c7aSAndroid Build Coastguard Worker  /sbin/sgdisk -e "${disk}"
327*2f2c4c7aSAndroid Build Coastguard Worker  /sbin/e2fsck -p -f "${disk}"?offset=${rootfs_partition_offset} || true
328*2f2c4c7aSAndroid Build Coastguard Worker  /sbin/e2fsck -fy "${disk}"?offset=${rootfs_partition_offset} || true
329*2f2c4c7aSAndroid Build Coastguard Workerelse
330*2f2c4c7aSAndroid Build Coastguard Worker  # If there's no bootloader, the initrd is the disk image
331*2f2c4c7aSAndroid Build Coastguard Worker  cp -a "${initrd}" "${disk}"
332*2f2c4c7aSAndroid Build Coastguard Worker  truncate -s 10G "${disk}"
333*2f2c4c7aSAndroid Build Coastguard Worker  /sbin/e2fsck -p -f "${disk}" || true
334*2f2c4c7aSAndroid Build Coastguard Worker  /sbin/resize2fs "${disk}"
335*2f2c4c7aSAndroid Build Coastguard Worker  system_partition=
336*2f2c4c7aSAndroid Build Coastguard Worker  rootfs_partition="raw"
337*2f2c4c7aSAndroid Build Coastguard Workerfi
338*2f2c4c7aSAndroid Build Coastguard Worker
339*2f2c4c7aSAndroid Build Coastguard Worker# Create another fake block device for initrd.img writeout
340*2f2c4c7aSAndroid Build Coastguard Workerraw_initrd=$(mktemp)
341*2f2c4c7aSAndroid Build Coastguard Workerraw_initrd_remove() {
342*2f2c4c7aSAndroid Build Coastguard Worker  rm -f "${raw_initrd}"
343*2f2c4c7aSAndroid Build Coastguard Worker  initrd_remove
344*2f2c4c7aSAndroid Build Coastguard Worker}
345*2f2c4c7aSAndroid Build Coastguard Workertrap raw_initrd_remove EXIT
346*2f2c4c7aSAndroid Build Coastguard Workertruncate -s 64M "${raw_initrd}"
347*2f2c4c7aSAndroid Build Coastguard Worker
348*2f2c4c7aSAndroid Build Coastguard Worker# Get number of cores for qemu. Restrict the maximum value to 8.
349*2f2c4c7aSAndroid Build Coastguard Workerqemucpucores=$(nproc)
350*2f2c4c7aSAndroid Build Coastguard Workerif [[ ${qemucpucores} -gt 8 ]]; then
351*2f2c4c7aSAndroid Build Coastguard Worker  qemucpucores=8
352*2f2c4c7aSAndroid Build Coastguard Workerfi
353*2f2c4c7aSAndroid Build Coastguard Worker
354*2f2c4c7aSAndroid Build Coastguard Worker# Complete the bootstrap process using QEMU and the specified kernel
355*2f2c4c7aSAndroid Build Coastguard Worker${qemu} -machine "${machine}" -cpu "${cpu}" -m 2048 >&2 \
356*2f2c4c7aSAndroid Build Coastguard Worker  -kernel "${kernel}" -initrd "${initrd}" -no-user-config -nodefaults \
357*2f2c4c7aSAndroid Build Coastguard Worker  -no-reboot -display none -nographic -serial stdio -parallel none \
358*2f2c4c7aSAndroid Build Coastguard Worker  -smp "${qemucpucores}",sockets="${qemucpucores}",cores=1,threads=1 \
359*2f2c4c7aSAndroid Build Coastguard Worker  -object rng-random,id=objrng0,filename=/dev/urandom \
360*2f2c4c7aSAndroid Build Coastguard Worker  -device virtio-rng-pci-non-transitional,rng=objrng0,id=rng0,max-bytes=1024,period=2000 \
361*2f2c4c7aSAndroid Build Coastguard Worker  -drive file="${disk}",format=raw,if=none,aio=threads,id=drive-virtio-disk0 \
362*2f2c4c7aSAndroid Build Coastguard Worker  -device virtio-blk-pci-non-transitional,scsi=off,drive=drive-virtio-disk0 \
363*2f2c4c7aSAndroid Build Coastguard Worker  -drive file="${raw_initrd}",format=raw,if=none,aio=threads,id=drive-virtio-disk1 \
364*2f2c4c7aSAndroid Build Coastguard Worker  -device virtio-blk-pci-non-transitional,scsi=off,drive=drive-virtio-disk1 \
365*2f2c4c7aSAndroid Build Coastguard Worker  -chardev file,id=exitcode,path=exitcode \
366*2f2c4c7aSAndroid Build Coastguard Worker  -device pci-serial,chardev=exitcode \
367*2f2c4c7aSAndroid Build Coastguard Worker  -append "root=/dev/ram0 ramdisk_size=524288 init=/root/stage1.sh ${cmdline}"
368*2f2c4c7aSAndroid Build Coastguard Worker[[ -s exitcode ]] && exitcode=$(cat exitcode | tr -d '\r') || exitcode=2
369*2f2c4c7aSAndroid Build Coastguard Workerrm -f exitcode
370*2f2c4c7aSAndroid Build Coastguard Workerif [ "${exitcode}" != "0" ]; then
371*2f2c4c7aSAndroid Build Coastguard Worker  echo "Second stage debootstrap failed (err=${exitcode})"
372*2f2c4c7aSAndroid Build Coastguard Worker  exit "${exitcode}"
373*2f2c4c7aSAndroid Build Coastguard Workerfi
374*2f2c4c7aSAndroid Build Coastguard Worker
375*2f2c4c7aSAndroid Build Coastguard Worker# Fix up any issues from the unclean shutdown
376*2f2c4c7aSAndroid Build Coastguard Workerif [[ ${rootfs_partition} = "raw" ]]; then
377*2f2c4c7aSAndroid Build Coastguard Worker    sudo e2fsck -p -f "${disk}" || true
378*2f2c4c7aSAndroid Build Coastguard Workerelse
379*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition_start=$(partx -g -o START -s -n "${rootfs_partition}" "${disk}" | xargs)
380*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition_end=$(partx -g -o END -s -n "${rootfs_partition}" "${disk}" | xargs)
381*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition_num_sectors=$((${rootfs_partition_end} - ${rootfs_partition_start} + 1))
382*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition_offset=$((${rootfs_partition_start} * 512))
383*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition_tempfile2=$(mktemp)
384*2f2c4c7aSAndroid Build Coastguard Worker    dd if="${disk}" of="${rootfs_partition_tempfile2}" bs=512 skip=${rootfs_partition_start} count=${rootfs_partition_num_sectors}
385*2f2c4c7aSAndroid Build Coastguard Worker    /sbin/e2fsck -p -f "${rootfs_partition_tempfile2}" || true
386*2f2c4c7aSAndroid Build Coastguard Worker    dd if="${rootfs_partition_tempfile2}" of="${disk}" bs=512 seek=${rootfs_partition_start} count=${rootfs_partition_num_sectors} conv=fsync,notrunc
387*2f2c4c7aSAndroid Build Coastguard Worker    rm -f "${rootfs_partition_tempfile2}"
388*2f2c4c7aSAndroid Build Coastguard Worker    /sbin/e2fsck -fy "${disk}"?offset=${rootfs_partition_offset} || true
389*2f2c4c7aSAndroid Build Coastguard Workerfi
390*2f2c4c7aSAndroid Build Coastguard Workerif [[ -n "${system_partition}" ]]; then
391*2f2c4c7aSAndroid Build Coastguard Worker  system_partition_start=$(partx -g -o START -s -n "${system_partition}" "${disk}" | xargs)
392*2f2c4c7aSAndroid Build Coastguard Worker  system_partition_end=$(partx -g -o END -s -n "${system_partition}" "${disk}" | xargs)
393*2f2c4c7aSAndroid Build Coastguard Worker  system_partition_num_sectors=$((${system_partition_end} - ${system_partition_start} + 1))
394*2f2c4c7aSAndroid Build Coastguard Worker  system_partition_offset=$((${system_partition_start} * 512))
395*2f2c4c7aSAndroid Build Coastguard Worker  system_partition_size=$((${system_partition_num_sectors} * 512))
396*2f2c4c7aSAndroid Build Coastguard Worker  system_partition_tempfile=$(mktemp)
397*2f2c4c7aSAndroid Build Coastguard Worker  dd if="${disk}" of="${system_partition_tempfile}" bs=512 skip=${system_partition_start} count=${system_partition_num_sectors}
398*2f2c4c7aSAndroid Build Coastguard Worker  /sbin/fsck.vfat -a "${system_partition_tempfile}" || true
399*2f2c4c7aSAndroid Build Coastguard Worker  dd if="${system_partition_tempfile}" of="${disk}" bs=512 seek=${system_partition_start} count=${system_partition_num_sectors} conv=fsync,notrunc
400*2f2c4c7aSAndroid Build Coastguard Worker  rm -f "${system_partition_tempfile}"
401*2f2c4c7aSAndroid Build Coastguard Workerfi
402*2f2c4c7aSAndroid Build Coastguard Worker
403*2f2c4c7aSAndroid Build Coastguard Worker# New workdir for the initrd extraction
404*2f2c4c7aSAndroid Build Coastguard Workerworkdir="${tmpdir}/initrd"
405*2f2c4c7aSAndroid Build Coastguard Workermkdir "${workdir}"
406*2f2c4c7aSAndroid Build Coastguard Workerchmod 0755 "${workdir}"
407*2f2c4c7aSAndroid Build Coastguard Workersudo chown root:root "${workdir}"
408*2f2c4c7aSAndroid Build Coastguard Worker
409*2f2c4c7aSAndroid Build Coastguard Worker# Change into workdir to repack initramfs
410*2f2c4c7aSAndroid Build Coastguard Workercd "${workdir}"
411*2f2c4c7aSAndroid Build Coastguard Worker
412*2f2c4c7aSAndroid Build Coastguard Worker# Process the initrd to remove kernel-specific metadata
413*2f2c4c7aSAndroid Build Coastguard Workerkernel_version=$(basename $(lz4 -lcd "${raw_initrd}" | sudo cpio -idumv 2>&1 | grep usr/lib/modules/ - | head -n1))
414*2f2c4c7aSAndroid Build Coastguard Workerlz4 -lcd "${raw_initrd}" | sudo cpio -idumv
415*2f2c4c7aSAndroid Build Coastguard Workersudo rm -rf usr/lib/modules
416*2f2c4c7aSAndroid Build Coastguard Workersudo mkdir -p usr/lib/modules
417*2f2c4c7aSAndroid Build Coastguard Worker
418*2f2c4c7aSAndroid Build Coastguard Worker# Debian symlinks /usr/lib to /lib, but we'd prefer the other way around
419*2f2c4c7aSAndroid Build Coastguard Worker# so that it more closely matches what happens in Android initramfs images.
420*2f2c4c7aSAndroid Build Coastguard Worker# This enables 'cat ramdiskA.img ramdiskB.img >ramdiskC.img' to "just work".
421*2f2c4c7aSAndroid Build Coastguard Workersudo rm -f lib
422*2f2c4c7aSAndroid Build Coastguard Workersudo mv usr/lib lib
423*2f2c4c7aSAndroid Build Coastguard Workersudo ln -s /lib usr/lib
424*2f2c4c7aSAndroid Build Coastguard Worker
425*2f2c4c7aSAndroid Build Coastguard Worker# Repack the ramdisk to the final output
426*2f2c4c7aSAndroid Build Coastguard Workerfind * | sudo cpio -H newc -o --quiet | lz4 -lc9 >"${ramdisk}"
427*2f2c4c7aSAndroid Build Coastguard Worker
428*2f2c4c7aSAndroid Build Coastguard Worker# Pack another ramdisk with the combined artifacts, for boot testing
429*2f2c4c7aSAndroid Build Coastguard Workercat "${ramdisk}" "${initramfs}" >"${initrd}"
430*2f2c4c7aSAndroid Build Coastguard Worker
431*2f2c4c7aSAndroid Build Coastguard Worker# Leave workdir to boot-test combined initrd
432*2f2c4c7aSAndroid Build Coastguard Workercd -
433*2f2c4c7aSAndroid Build Coastguard Worker
434*2f2c4c7aSAndroid Build Coastguard Workerrootfs_partition_tempfile=$(mktemp)
435*2f2c4c7aSAndroid Build Coastguard Worker# Mount the new filesystem locally
436*2f2c4c7aSAndroid Build Coastguard Workerif [[ ${rootfs_partition} = "raw" ]]; then
437*2f2c4c7aSAndroid Build Coastguard Worker    sudo mount -o loop -t ext4 "${disk}" "${mount}"
438*2f2c4c7aSAndroid Build Coastguard Workerelse
439*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition_start=$(partx -g -o START -s -n "${rootfs_partition}" "${disk}" | xargs)
440*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition_offset=$((${rootfs_partition_start} * 512))
441*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition_end=$(partx -g -o END -s -n "${rootfs_partition}" "${disk}" | xargs)
442*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition_num_sectors=$((${rootfs_partition_end} - ${rootfs_partition_start} + 1))
443*2f2c4c7aSAndroid Build Coastguard Worker    dd if="${disk}" of="${rootfs_partition_tempfile}" bs=512 skip=${rootfs_partition_start} count=${rootfs_partition_num_sectors}
444*2f2c4c7aSAndroid Build Coastguard Workerfi
445*2f2c4c7aSAndroid Build Coastguard Workerimage_unmount2() {
446*2f2c4c7aSAndroid Build Coastguard Worker  sudo umount "${mount}"
447*2f2c4c7aSAndroid Build Coastguard Worker  raw_initrd_remove
448*2f2c4c7aSAndroid Build Coastguard Worker}
449*2f2c4c7aSAndroid Build Coastguard Workerif [[ ${rootfs_partition} = "raw" ]]; then
450*2f2c4c7aSAndroid Build Coastguard Worker    trap image_unmount2 EXIT
451*2f2c4c7aSAndroid Build Coastguard Workerfi
452*2f2c4c7aSAndroid Build Coastguard Worker
453*2f2c4c7aSAndroid Build Coastguard Worker# Embed the kernel and dtb images now, if requested
454*2f2c4c7aSAndroid Build Coastguard Workerif [[ ${rootfs_partition} = "raw" ]]; then
455*2f2c4c7aSAndroid Build Coastguard Worker    if [[ "${embed_kernel_initrd_dtb}" = "1" ]]; then
456*2f2c4c7aSAndroid Build Coastguard Worker	if [ -n "${dtb}" ]; then
457*2f2c4c7aSAndroid Build Coastguard Worker	    sudo mkdir -p "${mount}/boot/dtb/${dtb_subdir}"
458*2f2c4c7aSAndroid Build Coastguard Worker	    sudo cp -a "${dtb}" "${mount}/boot/dtb/${dtb_subdir}"
459*2f2c4c7aSAndroid Build Coastguard Worker	    sudo chown -R root:root "${mount}/boot/dtb/${dtb_subdir}"
460*2f2c4c7aSAndroid Build Coastguard Worker	fi
461*2f2c4c7aSAndroid Build Coastguard Worker	sudo cp -a "${kernel}" "${mount}/boot/vmlinuz-${kernel_version}"
462*2f2c4c7aSAndroid Build Coastguard Worker	sudo chown root:root "${mount}/boot/vmlinuz-${kernel_version}"
463*2f2c4c7aSAndroid Build Coastguard Worker    fi
464*2f2c4c7aSAndroid Build Coastguard Worker    sudo cp -a "${SCRIPT_DIR}"/rootfs/cron-run-installer-script "${mount}/etc/cron.d/cron-run-installer-script"
465*2f2c4c7aSAndroid Build Coastguard Worker    if [ -e "${extradeb}" ]; then
466*2f2c4c7aSAndroid Build Coastguard Worker	sudo cp -a "${extradeb}" "${mount}/root/extradeb.tar.gz"
467*2f2c4c7aSAndroid Build Coastguard Worker	sudo chown root:root "${mount}/root/extradeb.tar.gz"
468*2f2c4c7aSAndroid Build Coastguard Worker    fi
469*2f2c4c7aSAndroid Build Coastguard Workerelse
470*2f2c4c7aSAndroid Build Coastguard Worker    if [[ "${embed_kernel_initrd_dtb}" = "1" ]]; then
471*2f2c4c7aSAndroid Build Coastguard Worker	if [ -n "${dtb}" ]; then
472*2f2c4c7aSAndroid Build Coastguard Worker	    e2mkdir -G 0 -O 0 "${rootfs_partition_tempfile}":"/boot/dtb/${dtb_subdir}"
473*2f2c4c7aSAndroid Build Coastguard Worker	    e2cp -G 0 -O 0 "${dtb}" "${rootfs_partition_tempfile}":"/boot/dtb/${dtb_subdir}"
474*2f2c4c7aSAndroid Build Coastguard Worker	fi
475*2f2c4c7aSAndroid Build Coastguard Worker	e2cp -G 0 -O 0 "${kernel}" "${rootfs_partition_tempfile}":"/boot/vmlinuz-${kernel_version}"
476*2f2c4c7aSAndroid Build Coastguard Worker    fi
477*2f2c4c7aSAndroid Build Coastguard Worker    e2cp -G 0 -O 0 "${SCRIPT_DIR}"/rootfs/cron-run-installer-script "${rootfs_partition_tempfile}":"/etc/cron.d/cron-run-installer-script"
478*2f2c4c7aSAndroid Build Coastguard Worker    if [ -e "${extradeb}" ]; then
479*2f2c4c7aSAndroid Build Coastguard Worker	e2cp -G 0 -O 0 "${extradeb}" "${rootfs_partition_tempfile}":"/root/extradeb.tar.gz"
480*2f2c4c7aSAndroid Build Coastguard Worker    fi
481*2f2c4c7aSAndroid Build Coastguard Workerfi
482*2f2c4c7aSAndroid Build Coastguard Worker
483*2f2c4c7aSAndroid Build Coastguard Worker# Unmount the initial ramdisk
484*2f2c4c7aSAndroid Build Coastguard Workerif [[ ${rootfs_partition} = "raw" ]]; then
485*2f2c4c7aSAndroid Build Coastguard Worker    sudo umount "${mount}"
486*2f2c4c7aSAndroid Build Coastguard Workerelse
487*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition_start=$(partx -g -o START -s -n "${rootfs_partition}" "${disk}" | xargs)
488*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition_end=$(partx -g -o END -s -n "${rootfs_partition}" "${disk}" | xargs)
489*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition_num_sectors=$((${rootfs_partition_end} - ${rootfs_partition_start} + 1))
490*2f2c4c7aSAndroid Build Coastguard Worker    dd if="${rootfs_partition_tempfile}" of="${disk}" bs=512 seek=${rootfs_partition_start} count=${rootfs_partition_num_sectors} conv=fsync,notrunc
491*2f2c4c7aSAndroid Build Coastguard Workerfi
492*2f2c4c7aSAndroid Build Coastguard Workerrm -f "${rootfs_partition_tempfile}"
493*2f2c4c7aSAndroid Build Coastguard Workertrap raw_initrd_remove EXIT
494*2f2c4c7aSAndroid Build Coastguard Worker
495*2f2c4c7aSAndroid Build Coastguard Worker# Boot test the new system and run stage 3
496*2f2c4c7aSAndroid Build Coastguard Worker${qemu} -machine "${machine}" -cpu "${cpu}" -m 2048 >&2 \
497*2f2c4c7aSAndroid Build Coastguard Worker  -kernel "${kernel}" -initrd "${initrd}" -no-user-config -nodefaults \
498*2f2c4c7aSAndroid Build Coastguard Worker  -no-reboot -display none -nographic -serial stdio -parallel none \
499*2f2c4c7aSAndroid Build Coastguard Worker  -smp "${qemucpucores}",sockets="${qemucpucores}",cores=1,threads=1 \
500*2f2c4c7aSAndroid Build Coastguard Worker  -object rng-random,id=objrng0,filename=/dev/urandom \
501*2f2c4c7aSAndroid Build Coastguard Worker  -device virtio-rng-pci-non-transitional,rng=objrng0,id=rng0,max-bytes=1024,period=2000 \
502*2f2c4c7aSAndroid Build Coastguard Worker  -drive file="${disk}",format=raw,if=none,aio=threads,id=drive-virtio-disk0 \
503*2f2c4c7aSAndroid Build Coastguard Worker  -device virtio-blk-pci-non-transitional,scsi=off,drive=drive-virtio-disk0 \
504*2f2c4c7aSAndroid Build Coastguard Worker  -chardev file,id=exitcode,path=exitcode \
505*2f2c4c7aSAndroid Build Coastguard Worker  -device pci-serial,chardev=exitcode \
506*2f2c4c7aSAndroid Build Coastguard Worker  -netdev user,id=usernet0,ipv6=off \
507*2f2c4c7aSAndroid Build Coastguard Worker  -device virtio-net-pci-non-transitional,netdev=usernet0,id=net0 \
508*2f2c4c7aSAndroid Build Coastguard Worker  -append "root=LABEL=ROOT installer_script=/root/${suite}.sh ${cmdline}"
509*2f2c4c7aSAndroid Build Coastguard Worker[[ -s exitcode ]] && exitcode=$(cat exitcode | tr -d '\r') || exitcode=2
510*2f2c4c7aSAndroid Build Coastguard Workerrm -f exitcode
511*2f2c4c7aSAndroid Build Coastguard Workerif [ "${exitcode}" != "0" ]; then
512*2f2c4c7aSAndroid Build Coastguard Worker  echo "Root filesystem finalization failed (err=${exitcode})"
513*2f2c4c7aSAndroid Build Coastguard Worker  exit "${exitcode}"
514*2f2c4c7aSAndroid Build Coastguard Workerfi
515*2f2c4c7aSAndroid Build Coastguard Worker
516*2f2c4c7aSAndroid Build Coastguard Worker# Fix up any issues from the unclean shutdown
517*2f2c4c7aSAndroid Build Coastguard Workerif [[ ${rootfs_partition} = "raw" ]]; then
518*2f2c4c7aSAndroid Build Coastguard Worker    sudo e2fsck -p -f "${disk}" || true
519*2f2c4c7aSAndroid Build Coastguard Workerelse
520*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition_start=$(partx -g -o START -s -n "${rootfs_partition}" "${disk}" | xargs)
521*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition_end=$(partx -g -o END -s -n "${rootfs_partition}" "${disk}" | xargs)
522*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition_num_sectors=$((${rootfs_partition_end} - ${rootfs_partition_start} + 1))
523*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition_offset=$((${rootfs_partition_start} * 512))
524*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition_tempfile2=$(mktemp)
525*2f2c4c7aSAndroid Build Coastguard Worker    dd if="${disk}" of="${rootfs_partition_tempfile2}" bs=512 skip=${rootfs_partition_start} count=${rootfs_partition_num_sectors}
526*2f2c4c7aSAndroid Build Coastguard Worker    /sbin/e2fsck -p -f "${rootfs_partition_tempfile2}" || true
527*2f2c4c7aSAndroid Build Coastguard Worker    dd if="${rootfs_partition_tempfile2}" of="${disk}" bs=512 seek=${rootfs_partition_start} count=${rootfs_partition_num_sectors} conv=fsync,notrunc
528*2f2c4c7aSAndroid Build Coastguard Worker    rm -f "${rootfs_partition_tempfile2}"
529*2f2c4c7aSAndroid Build Coastguard Worker    /sbin/e2fsck -fy "${disk}"?offset=${rootfs_partition_offset} || true
530*2f2c4c7aSAndroid Build Coastguard Workerfi
531*2f2c4c7aSAndroid Build Coastguard Workerif [[ -n "${system_partition}" ]]; then
532*2f2c4c7aSAndroid Build Coastguard Worker  system_partition_start=$(partx -g -o START -s -n "${system_partition}" "${disk}" | xargs)
533*2f2c4c7aSAndroid Build Coastguard Worker  system_partition_end=$(partx -g -o END -s -n "${system_partition}" "${disk}" | xargs)
534*2f2c4c7aSAndroid Build Coastguard Worker  system_partition_num_sectors=$((${system_partition_end} - ${system_partition_start} + 1))
535*2f2c4c7aSAndroid Build Coastguard Worker  system_partition_offset=$((${system_partition_start} * 512))
536*2f2c4c7aSAndroid Build Coastguard Worker  system_partition_size=$((${system_partition_num_sectors} * 512))
537*2f2c4c7aSAndroid Build Coastguard Worker  system_partition_tempfile=$(mktemp)
538*2f2c4c7aSAndroid Build Coastguard Worker  dd if="${disk}" of="${system_partition_tempfile}" bs=512 skip=${system_partition_start} count=${system_partition_num_sectors}
539*2f2c4c7aSAndroid Build Coastguard Worker  /sbin/fsck.vfat -a "${system_partition_tempfile}" || true
540*2f2c4c7aSAndroid Build Coastguard Worker  dd if="${system_partition_tempfile}" of="${disk}" bs=512 seek=${system_partition_start} count=${system_partition_num_sectors} conv=fsync,notrunc
541*2f2c4c7aSAndroid Build Coastguard Worker  rm -f "${system_partition_tempfile}"
542*2f2c4c7aSAndroid Build Coastguard Workerfi
543*2f2c4c7aSAndroid Build Coastguard Worker
544*2f2c4c7aSAndroid Build Coastguard Worker# Mount the final disk image locally
545*2f2c4c7aSAndroid Build Coastguard Workerif [[ ${rootfs_partition} = "raw" ]]; then
546*2f2c4c7aSAndroid Build Coastguard Worker    sudo mount -o loop -t ext4 "${disk}" "${mount}"
547*2f2c4c7aSAndroid Build Coastguard Workerelse
548*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition_start=$(partx -g -o START -s -n "${rootfs_partition}" "${disk}" | xargs)
549*2f2c4c7aSAndroid Build Coastguard Worker    rootfs_partition_offset=$((${rootfs_partition_start} * 512))
550*2f2c4c7aSAndroid Build Coastguard Worker    sudo mount -o loop,offset=${rootfs_partition_offset} -t ext4 "${disk}" "${mount}"
551*2f2c4c7aSAndroid Build Coastguard Workerfi
552*2f2c4c7aSAndroid Build Coastguard Workerimage_unmount3() {
553*2f2c4c7aSAndroid Build Coastguard Worker  sudo umount "${mount}"
554*2f2c4c7aSAndroid Build Coastguard Worker  raw_initrd_remove
555*2f2c4c7aSAndroid Build Coastguard Worker}
556*2f2c4c7aSAndroid Build Coastguard Workertrap image_unmount3 EXIT
557*2f2c4c7aSAndroid Build Coastguard Worker
558*2f2c4c7aSAndroid Build Coastguard Worker# Fill the rest of the space with zeroes, to optimize compression
559*2f2c4c7aSAndroid Build Coastguard Workersudo dd if=/dev/zero of="${mount}/sparse" bs=1M 2>/dev/null || true
560*2f2c4c7aSAndroid Build Coastguard Workersudo rm -f "${mount}/sparse"
561*2f2c4c7aSAndroid Build Coastguard Worker
562*2f2c4c7aSAndroid Build Coastguard Workerecho "Debian ${suite} for ${arch} filesystem generated at '${disk}'."
563*2f2c4c7aSAndroid Build Coastguard Workerecho "Initial ramdisk generated at '${ramdisk}'."
564