#!/bin/bash # Copyright (C) 2007, 2008, 2009, 2012 Google Inc. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # 02110-1301, USA. set -e # minimum device size is 256 MB, but we use 255 to account for # potential rounding declare -ri MIN_DEV_SIZE=$((255*1048576)) . common.sh if [ "$GENERATE_CACHE" = "yes" -a ! -d "$CACHE_DIR" ]; then mkdir -p "$CACHE_DIR" fi DPKG_ARCH=${ARCH:-`dpkg --print-architecture`} CACHE_FILE="$CACHE_DIR/cache-${SUITE}-${DPKG_ARCH}.tar" # If the target device is not a real block device we'll first losetup it. # This is needed for file disks. if [ ! -b $blockdev ]; then ORIGINAL_BLOCKDEV=$blockdev blockdev=$(losetup -sf $blockdev) CLEANUP+=("losetup -d $blockdev") fi DEVICE_SIZE=$(blockdev --getsize64 $blockdev) if [ "$DEVICE_SIZE" -lt $MIN_DEV_SIZE ]; then echo "Device size is too small ($((DEVICE_SIZE/1048576)) MB)" 1>&2 echo "Required size is at least 256MB" 1>&2 exit 1 fi if [ "$PARTITION_STYLE" = "none" ]; then filesystem_dev=$blockdev elif [ "$PARTITION_STYLE" = "msdos" ]; then # Create one big partition, and make it bootable format_disk0 $blockdev filesystem_dev=$(map_disk0 $blockdev) CLEANUP+=("unmap_disk0 $blockdev") else echo "Unknown partition style $PARTITION_STYLE" 1>&2 exit 1 fi mke2fs -Fjqt $OSP_FILESYSTEM $filesystem_dev root_uuid=$($VOL_ID $filesystem_dev ) if [ -n "$swapdev" ]; then mkswap $swapdev swap_uuid=$($VOL_ID $swapdev || true ) fi TMPDIR=`mktemp -d` || exit 1 CLEANUP+=("rmdir $TMPDIR") mount $filesystem_dev $TMPDIR CLEANUP+=("umount $TMPDIR") # remove the cache file if it's old (> 2 weeks) and writable by the owner (the # default due to the standard umask) if [ "$CLEAN_CACHE" -a -d "$CACHE_DIR" ]; then find "$CACHE_DIR" -name 'cache-*.tar' -type f \ -daystart -mtime "+${CLEAN_CACHE}" -print0 | \ xargs -r0 rm -f fi if [ -f "$CACHE_FILE" ]; then tar xf "$CACHE_FILE" -C $TMPDIR else if [ "$PROXY" ]; then export http_proxy="$PROXY" export https_proxy="$PROXY" fi # INCLUDE will be empty if EXTRA_PKGS is null/empty, otherwise we # build the full parameter format from it INCLUDE=${EXTRA_PKGS:+"--include=$EXTRA_PKGS"} COMP=${COMPONENTS:+"--components=$COMPONENTS"} debootstrap \ --arch "$DPKG_ARCH" \ $INCLUDE \ $COMP \ "$SUITE" $TMPDIR $MIRROR # remove the downloaded debs, as they are no longer needed find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \ xargs -r0 rm -f # remove the persistent-net rules, otherwise it will remember the node's # interfaces as eth0, eth1, ... rm -f "$TMPDIR/etc/udev/rules.d/z25_persistent-net.rules" if [ "$GENERATE_CACHE" = "yes" ]; then TMP_CACHE=`mktemp "${CACHE_FILE}.XXXXXX"` tar cf "$TMP_CACHE" -C $TMPDIR . mv -f "$TMP_CACHE" "$CACHE_FILE" fi fi # reset the root password chroot $TMPDIR passwd -d root cp -p /etc/hosts $TMPDIR/etc/hosts cp -p /etc/resolv.conf $TMPDIR/etc/resolv.conf echo $instance > $TMPDIR/etc/hostname echo $instance > $TMPDIR/etc/mailname cat > $TMPDIR/etc/fstab < UUID=$root_uuid / ext3 defaults 0 1 proc /proc proc defaults 0 0 EOF [ -n "$swapdev" -a -n "$swap_uuid" ] && cat >> $TMPDIR/etc/fstab < $TMPDIR/etc/network/interfaces <> $TMPDIR/etc/inittab elif [ -e $TMPDIR/etc/init ]; then # ubuntu (eg. karmic) cat > $TMPDIR/etc/init/${SERIAL_PORT}.conf < $TMPDIR/etc/event.d/${SERIAL_PORT}.conf <