summaryrefslogtreecommitdiff
path: root/net/test/build_rootfs.sh
blob: ce09da10d0b8bde99a5b798207cd3bd14ae30761 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
#
# Copyright (C) 2018 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

set -e

SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)

usage() {
  echo -n "usage: $0 [-h] [-s wheezy|stretch] [-a amd64|arm64] "
  echo "[-m http://mirror/debian] [-n net_test.rootfs.`date +%Y%m%d`]"
  exit 1
}

mirror=http://ftp.debian.org/debian
debootstrap=debootstrap
suite=stretch
arch=amd64

while getopts ":hs:a:m:n:" opt; do
  case $opt in
    h)
      usage
      ;;
    s)
      if [ "$OPTARG" != "wheezy" -a "$OPTARG" != "stretch" ]; then
        echo "Invalid suite: $OPTARG" >&2
        usage
      fi
      suite=$OPTARG
      ;;
    a)
      if [ "$OPTARG" != "amd64" -a "$OPTARG" != "arm64" ]; then
        echo "Invalid arch: $OPTARG" >&2
        usage
      fi
      arch=$OPTARG
      ;;
    m)
      mirror=$OPTARG
      ;;
    n)
      name=$OPTARG
      ;;
    \?)
      echo "Invalid option: $OPTARG" >&2
      usage
      ;;
    :)
      echo "Invalid option: $OPTARG requires an argument" >&2
      usage
      ;;
  esac
done

name=net_test.rootfs.$arch.`date +%Y%m%d`

# Switch to qemu-debootstrap for incompatible architectures
if [ "$arch" = "arm64" ]; then
  debootstrap=qemu-debootstrap
fi

# Sometimes it isn't obvious when the script fails
failure() {
  echo "Filesystem generation process failed." >&2
}
trap failure ERR

# Import the package list for this release
packages=`cat $SCRIPT_DIR/rootfs/$suite.list | xargs | tr -s ' ' ','`

# For the debootstrap intermediates
workdir=`mktemp -d`
workdir_remove() {
  echo "Removing temporary files.." >&2
  sudo rm -rf $workdir
}
trap workdir_remove EXIT

# Run the debootstrap first
cd $workdir
sudo $debootstrap --arch=$arch --variant=minbase --include=$packages \
                  $suite . $mirror
# Workarounds for bugs in the debootstrap suite scripts
for mount in `cat /proc/mounts | cut -d' ' -f2 | grep -e ^$workdir`; do
  echo "Unmounting mountpoint $mount.." >&2
  sudo umount $mount
done
# Copy the chroot preparation scripts, and enter the chroot
for file in $suite.sh common.sh net_test.sh; do
  sudo cp -a $SCRIPT_DIR/rootfs/$file root/$file
  sudo chown root:root root/$file
done
sudo chroot . /root/$suite.sh

# Leave the workdir, to build the filesystem
cd -

# For the final image mount
mount=`mktemp -d`
mount_remove() {
 rmdir $mount
 workdir_remove
}
trap mount_remove EXIT

# Create a 1G empty ext3 filesystem
truncate -s 1G $name
mke2fs -F -t ext3 -L ROOT $name

# Mount the new filesystem locally
sudo mount -o loop -t ext3 $name $mount
image_unmount() {
  sudo umount $mount
  mount_remove
}
trap image_unmount EXIT

# Copy the patched debootstrap results into the new filesystem
sudo cp -a $workdir/* $mount

# Fill the rest of the space with zeroes, to optimize compression
sudo dd if=/dev/zero of=$mount/sparse bs=1M 2>/dev/null || true
sudo rm -f $mount/sparse

echo "Debian $suite for $arch filesystem generated at '$name'."