Alistair Strachan | 2d99985 | 2018-06-20 11:08:17 -0700 | [diff] [blame] | 1 | #!/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 | |
| 18 | set -e |
| 19 | |
| 20 | SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P) |
| 21 | |
| 22 | usage() { |
Maciej Żenczykowski | 03aa0a6 | 2019-03-22 13:02:00 -0700 | [diff] [blame^] | 23 | echo -n "usage: $0 [-h] [-s wheezy|stretch] [-a i386|amd64|armhf|arm64] " |
Alistair Strachan | 34089b0 | 2018-10-25 13:30:21 -0700 | [diff] [blame] | 24 | echo "[-m http://mirror/debian] [-n net_test.rootfs.`date +%Y%m%d`]" |
Alistair Strachan | 2d99985 | 2018-06-20 11:08:17 -0700 | [diff] [blame] | 25 | exit 1 |
| 26 | } |
| 27 | |
Alistair Strachan | 34089b0 | 2018-10-25 13:30:21 -0700 | [diff] [blame] | 28 | mirror=http://ftp.debian.org/debian |
| 29 | debootstrap=debootstrap |
Alistair Strachan | 2d99985 | 2018-06-20 11:08:17 -0700 | [diff] [blame] | 30 | suite=stretch |
Alistair Strachan | 34089b0 | 2018-10-25 13:30:21 -0700 | [diff] [blame] | 31 | arch=amd64 |
Alistair Strachan | 2d99985 | 2018-06-20 11:08:17 -0700 | [diff] [blame] | 32 | |
Alistair Strachan | 34089b0 | 2018-10-25 13:30:21 -0700 | [diff] [blame] | 33 | while getopts ":hs:a:m:n:" opt; do |
Alistair Strachan | 2d99985 | 2018-06-20 11:08:17 -0700 | [diff] [blame] | 34 | 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 Żenczykowski | bc960f3 | 2019-03-22 17:36:27 -0700 | [diff] [blame] | 43 | suite="${OPTARG}" |
| 44 | if [[ "${suite}" == wheezy ]]; then |
| 45 | mirror=http://archive.debian.org/debian |
| 46 | fi |
Alistair Strachan | 2d99985 | 2018-06-20 11:08:17 -0700 | [diff] [blame] | 47 | ;; |
Alistair Strachan | 34089b0 | 2018-10-25 13:30:21 -0700 | [diff] [blame] | 48 | a) |
Maciej Żenczykowski | 03aa0a6 | 2019-03-22 13:02:00 -0700 | [diff] [blame^] | 49 | 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 Strachan | 34089b0 | 2018-10-25 13:30:21 -0700 | [diff] [blame] | 58 | ;; |
| 59 | m) |
| 60 | mirror=$OPTARG |
| 61 | ;; |
Alistair Strachan | 2d99985 | 2018-06-20 11:08:17 -0700 | [diff] [blame] | 62 | 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 |
| 74 | done |
| 75 | |
Maciej Żenczykowski | e9e1805 | 2019-03-21 16:30:47 -0700 | [diff] [blame] | 76 | if [[ -z "${name}" ]]; then |
Maciej Żenczykowski | 1b384cc | 2019-03-21 16:33:31 -0700 | [diff] [blame] | 77 | name=net_test.rootfs.${arch}.${suite}.`date +%Y%m%d` |
Maciej Żenczykowski | e9e1805 | 2019-03-21 16:30:47 -0700 | [diff] [blame] | 78 | fi |
Alistair Strachan | 34089b0 | 2018-10-25 13:30:21 -0700 | [diff] [blame] | 79 | |
| 80 | # Switch to qemu-debootstrap for incompatible architectures |
| 81 | if [ "$arch" = "arm64" ]; then |
| 82 | debootstrap=qemu-debootstrap |
| 83 | fi |
| 84 | |
Alistair Strachan | 2d99985 | 2018-06-20 11:08:17 -0700 | [diff] [blame] | 85 | # Sometimes it isn't obvious when the script fails |
| 86 | failure() { |
| 87 | echo "Filesystem generation process failed." >&2 |
| 88 | } |
| 89 | trap failure ERR |
| 90 | |
| 91 | # Import the package list for this release |
| 92 | packages=`cat $SCRIPT_DIR/rootfs/$suite.list | xargs | tr -s ' ' ','` |
| 93 | |
| 94 | # For the debootstrap intermediates |
| 95 | workdir=`mktemp -d` |
| 96 | workdir_remove() { |
| 97 | echo "Removing temporary files.." >&2 |
| 98 | sudo rm -rf $workdir |
| 99 | } |
| 100 | trap workdir_remove EXIT |
| 101 | |
| 102 | # Run the debootstrap first |
| 103 | cd $workdir |
Alistair Strachan | 34089b0 | 2018-10-25 13:30:21 -0700 | [diff] [blame] | 104 | sudo $debootstrap --arch=$arch --variant=minbase --include=$packages \ |
| 105 | $suite . $mirror |
Alistair Strachan | 2d99985 | 2018-06-20 11:08:17 -0700 | [diff] [blame] | 106 | # Workarounds for bugs in the debootstrap suite scripts |
| 107 | for mount in `cat /proc/mounts | cut -d' ' -f2 | grep -e ^$workdir`; do |
| 108 | echo "Unmounting mountpoint $mount.." >&2 |
| 109 | sudo umount $mount |
| 110 | done |
| 111 | # Copy the chroot preparation scripts, and enter the chroot |
| 112 | for 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 |
| 115 | done |
| 116 | sudo chroot . /root/$suite.sh |
| 117 | |
| 118 | # Leave the workdir, to build the filesystem |
| 119 | cd - |
| 120 | |
| 121 | # For the final image mount |
| 122 | mount=`mktemp -d` |
| 123 | mount_remove() { |
| 124 | rmdir $mount |
| 125 | workdir_remove |
| 126 | } |
| 127 | trap mount_remove EXIT |
| 128 | |
| 129 | # Create a 1G empty ext3 filesystem |
| 130 | truncate -s 1G $name |
| 131 | mke2fs -F -t ext3 -L ROOT $name |
| 132 | |
| 133 | # Mount the new filesystem locally |
| 134 | sudo mount -o loop -t ext3 $name $mount |
| 135 | image_unmount() { |
| 136 | sudo umount $mount |
| 137 | mount_remove |
| 138 | } |
| 139 | trap image_unmount EXIT |
| 140 | |
| 141 | # Copy the patched debootstrap results into the new filesystem |
| 142 | sudo cp -a $workdir/* $mount |
| 143 | |
| 144 | # Fill the rest of the space with zeroes, to optimize compression |
| 145 | sudo dd if=/dev/zero of=$mount/sparse bs=1M 2>/dev/null || true |
| 146 | sudo rm -f $mount/sparse |
| 147 | |
Alistair Strachan | 34089b0 | 2018-10-25 13:30:21 -0700 | [diff] [blame] | 148 | echo "Debian $suite for $arch filesystem generated at '$name'." |