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