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() { |
Alistair Strachan | 34089b0 | 2018-10-25 13:30:21 -0700 | [diff] [blame^] | 23 | 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 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 |
| 43 | suite=$OPTARG |
| 44 | ;; |
Alistair Strachan | 34089b0 | 2018-10-25 13:30:21 -0700 | [diff] [blame^] | 45 | 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 Strachan | 2d99985 | 2018-06-20 11:08:17 -0700 | [diff] [blame] | 55 | 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 |
| 67 | done |
| 68 | |
Alistair Strachan | 34089b0 | 2018-10-25 13:30:21 -0700 | [diff] [blame^] | 69 | name=net_test.rootfs.$arch.`date +%Y%m%d` |
| 70 | |
| 71 | # Switch to qemu-debootstrap for incompatible architectures |
| 72 | if [ "$arch" = "arm64" ]; then |
| 73 | debootstrap=qemu-debootstrap |
| 74 | fi |
| 75 | |
Alistair Strachan | 2d99985 | 2018-06-20 11:08:17 -0700 | [diff] [blame] | 76 | # Sometimes it isn't obvious when the script fails |
| 77 | failure() { |
| 78 | echo "Filesystem generation process failed." >&2 |
| 79 | } |
| 80 | trap failure ERR |
| 81 | |
| 82 | # Import the package list for this release |
| 83 | packages=`cat $SCRIPT_DIR/rootfs/$suite.list | xargs | tr -s ' ' ','` |
| 84 | |
| 85 | # For the debootstrap intermediates |
| 86 | workdir=`mktemp -d` |
| 87 | workdir_remove() { |
| 88 | echo "Removing temporary files.." >&2 |
| 89 | sudo rm -rf $workdir |
| 90 | } |
| 91 | trap workdir_remove EXIT |
| 92 | |
| 93 | # Run the debootstrap first |
| 94 | cd $workdir |
Alistair Strachan | 34089b0 | 2018-10-25 13:30:21 -0700 | [diff] [blame^] | 95 | sudo $debootstrap --arch=$arch --variant=minbase --include=$packages \ |
| 96 | $suite . $mirror |
Alistair Strachan | 2d99985 | 2018-06-20 11:08:17 -0700 | [diff] [blame] | 97 | # Workarounds for bugs in the debootstrap suite scripts |
| 98 | for mount in `cat /proc/mounts | cut -d' ' -f2 | grep -e ^$workdir`; do |
| 99 | echo "Unmounting mountpoint $mount.." >&2 |
| 100 | sudo umount $mount |
| 101 | done |
| 102 | # Copy the chroot preparation scripts, and enter the chroot |
| 103 | for 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 |
| 106 | done |
| 107 | sudo chroot . /root/$suite.sh |
| 108 | |
| 109 | # Leave the workdir, to build the filesystem |
| 110 | cd - |
| 111 | |
| 112 | # For the final image mount |
| 113 | mount=`mktemp -d` |
| 114 | mount_remove() { |
| 115 | rmdir $mount |
| 116 | workdir_remove |
| 117 | } |
| 118 | trap mount_remove EXIT |
| 119 | |
| 120 | # Create a 1G empty ext3 filesystem |
| 121 | truncate -s 1G $name |
| 122 | mke2fs -F -t ext3 -L ROOT $name |
| 123 | |
| 124 | # Mount the new filesystem locally |
| 125 | sudo mount -o loop -t ext3 $name $mount |
| 126 | image_unmount() { |
| 127 | sudo umount $mount |
| 128 | mount_remove |
| 129 | } |
| 130 | trap image_unmount EXIT |
| 131 | |
| 132 | # Copy the patched debootstrap results into the new filesystem |
| 133 | sudo cp -a $workdir/* $mount |
| 134 | |
| 135 | # Fill the rest of the space with zeroes, to optimize compression |
| 136 | sudo dd if=/dev/zero of=$mount/sparse bs=1M 2>/dev/null || true |
| 137 | sudo rm -f $mount/sparse |
| 138 | |
Alistair Strachan | 34089b0 | 2018-10-25 13:30:21 -0700 | [diff] [blame^] | 139 | echo "Debian $suite for $arch filesystem generated at '$name'." |