blob: e0788c115b4dfb4787107191303177897d366ee7 [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() {
23 echo "usage: $0 [-h] [-s wheezy|stretch] [-n net_test.rootfs.`date +%Y%m%d`]"
24 exit 1
25}
26
27name=net_test.rootfs.`date +%Y%m%d`
28suite=stretch
29
30while getopts ":hs:n:" opt; do
31 case $opt in
32 h)
33 usage
34 ;;
35 s)
36 if [ "$OPTARG" != "wheezy" -a "$OPTARG" != "stretch" ]; then
37 echo "Invalid suite: $OPTARG" >&2
38 usage
39 fi
40 suite=$OPTARG
41 ;;
42 n)
43 name=$OPTARG
44 ;;
45 \?)
46 echo "Invalid option: $OPTARG" >&2
47 usage
48 ;;
49 :)
50 echo "Invalid option: $OPTARG requires an argument" >&2
51 usage
52 ;;
53 esac
54done
55
56# Sometimes it isn't obvious when the script fails
57failure() {
58 echo "Filesystem generation process failed." >&2
59}
60trap failure ERR
61
62# Import the package list for this release
63packages=`cat $SCRIPT_DIR/rootfs/$suite.list | xargs | tr -s ' ' ','`
64
65# For the debootstrap intermediates
66workdir=`mktemp -d`
67workdir_remove() {
68 echo "Removing temporary files.." >&2
69 sudo rm -rf $workdir
70}
71trap workdir_remove EXIT
72
73# Run the debootstrap first
74cd $workdir
75sudo debootstrap --arch=amd64 --variant=minbase --include=$packages \
76 $suite . http://ftp.debian.org/debian
77# Workarounds for bugs in the debootstrap suite scripts
78for mount in `cat /proc/mounts | cut -d' ' -f2 | grep -e ^$workdir`; do
79 echo "Unmounting mountpoint $mount.." >&2
80 sudo umount $mount
81done
82# Copy the chroot preparation scripts, and enter the chroot
83for file in $suite.sh common.sh net_test.sh; do
84 sudo cp -a $SCRIPT_DIR/rootfs/$file root/$file
85 sudo chown root:root root/$file
86done
87sudo chroot . /root/$suite.sh
88
89# Leave the workdir, to build the filesystem
90cd -
91
92# For the final image mount
93mount=`mktemp -d`
94mount_remove() {
95 rmdir $mount
96 workdir_remove
97}
98trap mount_remove EXIT
99
100# Create a 1G empty ext3 filesystem
101truncate -s 1G $name
102mke2fs -F -t ext3 -L ROOT $name
103
104# Mount the new filesystem locally
105sudo mount -o loop -t ext3 $name $mount
106image_unmount() {
107 sudo umount $mount
108 mount_remove
109}
110trap image_unmount EXIT
111
112# Copy the patched debootstrap results into the new filesystem
113sudo cp -a $workdir/* $mount
114
115# Fill the rest of the space with zeroes, to optimize compression
116sudo dd if=/dev/zero of=$mount/sparse bs=1M 2>/dev/null || true
117sudo rm -f $mount/sparse
118
119echo "Debian $suite filesystem generated at '$name'."