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