blob: 72e9598ae5afef64837614bcbfcea821cbc62db1 [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
22usage() {
Maciej Żenczykowski03aa0a62019-03-22 13:02:00 -070023 echo -n "usage: $0 [-h] [-s wheezy|stretch] [-a i386|amd64|armhf|arm64] "
Alistair Strachan34089b02018-10-25 13:30:21 -070024 echo "[-m http://mirror/debian] [-n net_test.rootfs.`date +%Y%m%d`]"
Alistair Strachan2d999852018-06-20 11:08:17 -070025 exit 1
26}
27
Alistair Strachan34089b02018-10-25 13:30:21 -070028mirror=http://ftp.debian.org/debian
29debootstrap=debootstrap
Alistair Strachan2d999852018-06-20 11:08:17 -070030suite=stretch
Alistair Strachan34089b02018-10-25 13:30:21 -070031arch=amd64
Alistair Strachan2d999852018-06-20 11:08:17 -070032
Alistair Strachan34089b02018-10-25 13:30:21 -070033while getopts ":hs:a:m:n:" opt; do
Alistair Strachan2d999852018-06-20 11:08:17 -070034 case $opt in
35 h)
36 usage
37 ;;
38 s)
39 if [ "$OPTARG" != "wheezy" -a "$OPTARG" != "stretch" ]; then
40 echo "Invalid suite: $OPTARG" >&2
41 usage
42 fi
Maciej Żenczykowskibc960f32019-03-22 17:36:27 -070043 suite="${OPTARG}"
44 if [[ "${suite}" == wheezy ]]; then
45 mirror=http://archive.debian.org/debian
46 fi
Alistair Strachan2d999852018-06-20 11:08:17 -070047 ;;
Alistair Strachan34089b02018-10-25 13:30:21 -070048 a)
Maciej Żenczykowski03aa0a62019-03-22 13:02:00 -070049 case "${OPTARG}" in
50 i386|amd64|armhf|arm64)
51 arch="${OPTARG}"
52 ;;
53 *)
54 echo "Invalid arch: ${OPTARG}" >&2
55 usage
56 ;;
57 esac
Alistair Strachan34089b02018-10-25 13:30:21 -070058 ;;
59 m)
60 mirror=$OPTARG
61 ;;
Alistair Strachan2d999852018-06-20 11:08:17 -070062 n)
63 name=$OPTARG
64 ;;
65 \?)
66 echo "Invalid option: $OPTARG" >&2
67 usage
68 ;;
69 :)
70 echo "Invalid option: $OPTARG requires an argument" >&2
71 usage
72 ;;
73 esac
74done
75
Maciej Żenczykowskie9e18052019-03-21 16:30:47 -070076if [[ -z "${name}" ]]; then
Maciej Żenczykowski1b384cc2019-03-21 16:33:31 -070077 name=net_test.rootfs.${arch}.${suite}.`date +%Y%m%d`
Maciej Żenczykowskie9e18052019-03-21 16:30:47 -070078fi
Alistair Strachan34089b02018-10-25 13:30:21 -070079
80# Switch to qemu-debootstrap for incompatible architectures
81if [ "$arch" = "arm64" ]; then
82 debootstrap=qemu-debootstrap
83fi
84
Alistair Strachan2d999852018-06-20 11:08:17 -070085# Sometimes it isn't obvious when the script fails
86failure() {
87 echo "Filesystem generation process failed." >&2
88}
89trap failure ERR
90
91# Import the package list for this release
92packages=`cat $SCRIPT_DIR/rootfs/$suite.list | xargs | tr -s ' ' ','`
93
94# For the debootstrap intermediates
95workdir=`mktemp -d`
96workdir_remove() {
97 echo "Removing temporary files.." >&2
98 sudo rm -rf $workdir
99}
100trap workdir_remove EXIT
101
102# Run the debootstrap first
103cd $workdir
Alistair Strachan34089b02018-10-25 13:30:21 -0700104sudo $debootstrap --arch=$arch --variant=minbase --include=$packages \
105 $suite . $mirror
Alistair Strachan2d999852018-06-20 11:08:17 -0700106# Workarounds for bugs in the debootstrap suite scripts
107for mount in `cat /proc/mounts | cut -d' ' -f2 | grep -e ^$workdir`; do
108 echo "Unmounting mountpoint $mount.." >&2
109 sudo umount $mount
110done
111# Copy the chroot preparation scripts, and enter the chroot
112for file in $suite.sh common.sh net_test.sh; do
113 sudo cp -a $SCRIPT_DIR/rootfs/$file root/$file
114 sudo chown root:root root/$file
115done
116sudo chroot . /root/$suite.sh
117
118# Leave the workdir, to build the filesystem
119cd -
120
121# For the final image mount
122mount=`mktemp -d`
123mount_remove() {
124 rmdir $mount
125 workdir_remove
126}
127trap mount_remove EXIT
128
129# Create a 1G empty ext3 filesystem
130truncate -s 1G $name
131mke2fs -F -t ext3 -L ROOT $name
132
133# Mount the new filesystem locally
134sudo mount -o loop -t ext3 $name $mount
135image_unmount() {
136 sudo umount $mount
137 mount_remove
138}
139trap image_unmount EXIT
140
141# Copy the patched debootstrap results into the new filesystem
142sudo cp -a $workdir/* $mount
143
144# Fill the rest of the space with zeroes, to optimize compression
145sudo dd if=/dev/zero of=$mount/sparse bs=1M 2>/dev/null || true
146sudo rm -f $mount/sparse
147
Alistair Strachan34089b02018-10-25 13:30:21 -0700148echo "Debian $suite for $arch filesystem generated at '$name'."