blob: ce09da10d0b8bde99a5b798207cd3bd14ae30761 [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() {
Alistair Strachan34089b02018-10-25 13:30:21 -070023 echo -n "usage: $0 [-h] [-s wheezy|stretch] [-a amd64|arm64] "
24 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
43 suite=$OPTARG
44 ;;
Alistair Strachan34089b02018-10-25 13:30:21 -070045 a)
46 if [ "$OPTARG" != "amd64" -a "$OPTARG" != "arm64" ]; then
47 echo "Invalid arch: $OPTARG" >&2
48 usage
49 fi
50 arch=$OPTARG
51 ;;
52 m)
53 mirror=$OPTARG
54 ;;
Alistair Strachan2d999852018-06-20 11:08:17 -070055 n)
56 name=$OPTARG
57 ;;
58 \?)
59 echo "Invalid option: $OPTARG" >&2
60 usage
61 ;;
62 :)
63 echo "Invalid option: $OPTARG requires an argument" >&2
64 usage
65 ;;
66 esac
67done
68
Alistair Strachan34089b02018-10-25 13:30:21 -070069name=net_test.rootfs.$arch.`date +%Y%m%d`
70
71# Switch to qemu-debootstrap for incompatible architectures
72if [ "$arch" = "arm64" ]; then
73 debootstrap=qemu-debootstrap
74fi
75
Alistair Strachan2d999852018-06-20 11:08:17 -070076# Sometimes it isn't obvious when the script fails
77failure() {
78 echo "Filesystem generation process failed." >&2
79}
80trap failure ERR
81
82# Import the package list for this release
83packages=`cat $SCRIPT_DIR/rootfs/$suite.list | xargs | tr -s ' ' ','`
84
85# For the debootstrap intermediates
86workdir=`mktemp -d`
87workdir_remove() {
88 echo "Removing temporary files.." >&2
89 sudo rm -rf $workdir
90}
91trap workdir_remove EXIT
92
93# Run the debootstrap first
94cd $workdir
Alistair Strachan34089b02018-10-25 13:30:21 -070095sudo $debootstrap --arch=$arch --variant=minbase --include=$packages \
96 $suite . $mirror
Alistair Strachan2d999852018-06-20 11:08:17 -070097# Workarounds for bugs in the debootstrap suite scripts
98for mount in `cat /proc/mounts | cut -d' ' -f2 | grep -e ^$workdir`; do
99 echo "Unmounting mountpoint $mount.." >&2
100 sudo umount $mount
101done
102# Copy the chroot preparation scripts, and enter the chroot
103for file in $suite.sh common.sh net_test.sh; do
104 sudo cp -a $SCRIPT_DIR/rootfs/$file root/$file
105 sudo chown root:root root/$file
106done
107sudo chroot . /root/$suite.sh
108
109# Leave the workdir, to build the filesystem
110cd -
111
112# For the final image mount
113mount=`mktemp -d`
114mount_remove() {
115 rmdir $mount
116 workdir_remove
117}
118trap mount_remove EXIT
119
120# Create a 1G empty ext3 filesystem
121truncate -s 1G $name
122mke2fs -F -t ext3 -L ROOT $name
123
124# Mount the new filesystem locally
125sudo mount -o loop -t ext3 $name $mount
126image_unmount() {
127 sudo umount $mount
128 mount_remove
129}
130trap image_unmount EXIT
131
132# Copy the patched debootstrap results into the new filesystem
133sudo cp -a $workdir/* $mount
134
135# Fill the rest of the space with zeroes, to optimize compression
136sudo dd if=/dev/zero of=$mount/sparse bs=1M 2>/dev/null || true
137sudo rm -f $mount/sparse
138
Alistair Strachan34089b02018-10-25 13:30:21 -0700139echo "Debian $suite for $arch filesystem generated at '$name'."