blob: 5563d8a49555d441be8bddf0057644f65f041b87 [file] [log] [blame]
Alistair Strachan2d999852018-06-20 11:08:17 -07001#!/bin/bash
2#
3# Copyright (C) 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18set -e
19
20SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)
21
Maciej Żenczykowski51ab7bd2020-01-29 00:28:24 -080022# Make sure we're in C locale so build inside chroot does not complain
23# about missing files
24unset LANG LANGUAGE \
25 LC_ADDRESS LC_ALL LC_COLLATE LC_CTYPE LC_IDENTIFICATION LC_MEASUREMENT \
26 LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE LC_TIME
27export LC_ALL=C
28
Alistair Strachan2d999852018-06-20 11:08:17 -070029usage() {
Maciej Żenczykowskif92d9bb2020-04-17 13:09:55 -070030 echo -n "usage: $0 [-h] [-s bullseye] [-a i386|amd64|armhf|arm64] "
Alistair Strachan34089b02018-10-25 13:30:21 -070031 echo "[-m http://mirror/debian] [-n net_test.rootfs.`date +%Y%m%d`]"
Alistair Strachan2d999852018-06-20 11:08:17 -070032 exit 1
33}
34
Alistair Strachan34089b02018-10-25 13:30:21 -070035mirror=http://ftp.debian.org/debian
36debootstrap=debootstrap
Maciej Żenczykowskif92d9bb2020-04-17 13:09:55 -070037suite=bullseye
Alistair Strachan34089b02018-10-25 13:30:21 -070038arch=amd64
Alistair Strachan2d999852018-06-20 11:08:17 -070039
Alistair Strachan34089b02018-10-25 13:30:21 -070040while getopts ":hs:a:m:n:" opt; do
Alistair Strachan2d999852018-06-20 11:08:17 -070041 case $opt in
42 h)
43 usage
44 ;;
45 s)
Maciej Żenczykowskif92d9bb2020-04-17 13:09:55 -070046 if [[ "$OPTARG" != "bullseye" ]]; then
Alistair Strachan2d999852018-06-20 11:08:17 -070047 echo "Invalid suite: $OPTARG" >&2
48 usage
49 fi
Maciej Żenczykowskibc960f32019-03-22 17:36:27 -070050 suite="${OPTARG}"
Alistair Strachan2d999852018-06-20 11:08:17 -070051 ;;
Alistair Strachan34089b02018-10-25 13:30:21 -070052 a)
Maciej Żenczykowski03aa0a62019-03-22 13:02:00 -070053 case "${OPTARG}" in
54 i386|amd64|armhf|arm64)
55 arch="${OPTARG}"
56 ;;
57 *)
58 echo "Invalid arch: ${OPTARG}" >&2
59 usage
60 ;;
61 esac
Alistair Strachan34089b02018-10-25 13:30:21 -070062 ;;
63 m)
64 mirror=$OPTARG
65 ;;
Alistair Strachan2d999852018-06-20 11:08:17 -070066 n)
67 name=$OPTARG
68 ;;
69 \?)
70 echo "Invalid option: $OPTARG" >&2
71 usage
72 ;;
73 :)
74 echo "Invalid option: $OPTARG requires an argument" >&2
75 usage
76 ;;
77 esac
78done
79
Maciej Żenczykowskie9e18052019-03-21 16:30:47 -070080if [[ -z "${name}" ]]; then
Maciej Żenczykowski1b384cc2019-03-21 16:33:31 -070081 name=net_test.rootfs.${arch}.${suite}.`date +%Y%m%d`
Maciej Żenczykowskie9e18052019-03-21 16:30:47 -070082fi
Alistair Strachan34089b02018-10-25 13:30:21 -070083
84# Switch to qemu-debootstrap for incompatible architectures
85if [ "$arch" = "arm64" ]; then
86 debootstrap=qemu-debootstrap
87fi
88
Alistair Strachan2d999852018-06-20 11:08:17 -070089# Sometimes it isn't obvious when the script fails
90failure() {
91 echo "Filesystem generation process failed." >&2
92}
93trap failure ERR
94
95# Import the package list for this release
96packages=`cat $SCRIPT_DIR/rootfs/$suite.list | xargs | tr -s ' ' ','`
97
98# For the debootstrap intermediates
Maciej Żenczykowski16767ad2020-04-17 13:11:23 -070099tmpdir=`mktemp -d`
100tmpdir_remove() {
Alistair Strachan2d999852018-06-20 11:08:17 -0700101 echo "Removing temporary files.." >&2
Maciej Żenczykowski16767ad2020-04-17 13:11:23 -0700102 sudo rm -rf "${tmpdir}"
Alistair Strachan2d999852018-06-20 11:08:17 -0700103}
Maciej Żenczykowski16767ad2020-04-17 13:11:23 -0700104trap tmpdir_remove EXIT
105
106workdir="${tmpdir}/_"
107
108mkdir "${workdir}"
109chmod 0755 "${workdir}"
110sudo chown root:root "${workdir}"
Alistair Strachan2d999852018-06-20 11:08:17 -0700111
112# Run the debootstrap first
113cd $workdir
Alistair Strachan34089b02018-10-25 13:30:21 -0700114sudo $debootstrap --arch=$arch --variant=minbase --include=$packages \
115 $suite . $mirror
Alistair Strachan2d999852018-06-20 11:08:17 -0700116# Workarounds for bugs in the debootstrap suite scripts
117for mount in `cat /proc/mounts | cut -d' ' -f2 | grep -e ^$workdir`; do
118 echo "Unmounting mountpoint $mount.." >&2
119 sudo umount $mount
120done
121# Copy the chroot preparation scripts, and enter the chroot
122for file in $suite.sh common.sh net_test.sh; do
123 sudo cp -a $SCRIPT_DIR/rootfs/$file root/$file
124 sudo chown root:root root/$file
125done
126sudo chroot . /root/$suite.sh
127
128# Leave the workdir, to build the filesystem
129cd -
130
131# For the final image mount
132mount=`mktemp -d`
133mount_remove() {
134 rmdir $mount
Maciej Żenczykowski16767ad2020-04-17 13:11:23 -0700135 tmpdir_remove
Alistair Strachan2d999852018-06-20 11:08:17 -0700136}
137trap mount_remove EXIT
138
139# Create a 1G empty ext3 filesystem
140truncate -s 1G $name
141mke2fs -F -t ext3 -L ROOT $name
142
143# Mount the new filesystem locally
144sudo mount -o loop -t ext3 $name $mount
145image_unmount() {
146 sudo umount $mount
147 mount_remove
148}
149trap image_unmount EXIT
150
151# Copy the patched debootstrap results into the new filesystem
152sudo cp -a $workdir/* $mount
153
154# Fill the rest of the space with zeroes, to optimize compression
155sudo dd if=/dev/zero of=$mount/sparse bs=1M 2>/dev/null || true
156sudo rm -f $mount/sparse
157
Alistair Strachan34089b02018-10-25 13:30:21 -0700158echo "Debian $suite for $arch filesystem generated at '$name'."