-#!/bin/sh
+#!/bin/bash
-# Copyright (C) 2007 Google Inc.
+# Copyright (C) 2007, 2008, 2009 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
. common.sh
-mke2fs -Fjq $blockdev
-root_uuid=$(/sbin/vol_id --uuid $blockdev )
+# 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
+
+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"
+ exit 1
+fi
+
+mke2fs -Fjq $filesystem_dev
+root_uuid=$($VOL_ID $filesystem_dev )
+
if [ -n "$swapdev" ]; then
mkswap $swapdev
- swap_uuid=$(/sbin/vol_id --uuid $swapdev )
+ swap_uuid=$($VOL_ID $swapdev || true )
fi
TMPDIR=`mktemp -d` || exit 1
-trap "umount $TMPDIR; rmdir $TMPDIR" EXIT
+CLEANUP+=("rmdir $TMPDIR")
-# If it's not a block device try to mount it via loopback device.
-# This is needed for file disks.
-MOUNT_OPTIONS=""
-if [ ! -b $blockdev ]; then
- MOUNT_OPTIONS="$MOUNT_OPTIONS -o loop"
-fi
-mount $MOUNT_OPTIONS $blockdev $TMPDIR
+mount $filesystem_dev $TMPDIR
+CLEANUP+=("umount $TMPDIR")
( cd $TMPDIR; restore -r -y -f - )
-rm -f $TMPDIR/etc/udev/rules.d/z25_persistent-net.rules
+rm -f $TMPDIR/etc/udev/rules.d/z*_persistent-net.rules
# Fix /etc/fstab with the new volumes' UUIDs
if [ -e $TMPDIR/etc/fstab ]; then
ROOT_LINE="UUID=$root_uuid / ext3 defaults 0 1"
- if [ -n "$swapdev" ]; then
+ if [ -n "$swapdev" -a -n "$swap_uuid" ]; then
SWAP_LINE="UUID=$swap_uuid swap swap defaults 0 0"
cat $TMPDIR/etc/fstab | \
sed -re "s#^(/dev/sda|UUID=[a-f0-9-]+)\s+/\s+.*\$#$ROOT_LINE#" \
- -re "s#^(/dev/sdb|UUID=[a-f0-9-]+)\s+swap\s+.*\$#$SWAP_LINE#" \
+ -e "s#^(/dev/sdb|UUID=[a-f0-9-]+)\s+swap\s+.*\$#$SWAP_LINE#" \
> $TMPDIR/etc/fstab.new
else
cat $TMPDIR/etc/fstab | \
mv $TMPDIR/etc/fstab.new $TMPDIR/etc/fstab
fi
-umount $TMPDIR
-rmdir $TMPDIR
+# execute cleanups
+cleanup
trap - EXIT
+
+exit 0