Add an example about how to run hooks
[ext/instance-debootstrap.git] / import
diff --git a/import b/import
index 90fd7ff..2d9b58e 100755 (executable)
--- a/import
+++ b/import
@@ -1,6 +1,6 @@
-#!/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
 
 set -e
 
-TEMP=`getopt -o i:b:s: -n '$0' -- "$@"`
+. common.sh
 
-if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
-
-# Note the quotes around `$TEMP': they are essential!
-eval set -- "$TEMP"
-
-while true; do 
-       case "$1" in
-               -i) instance=$2; shift 2;;
-
-               -b) blockdev=$2; shift 2;;
+# 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
 
-               -s) swapdev=$2; shift 2;;
+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
 
-               --) shift; break;;
+mke2fs -Fjq $filesystem_dev
+root_uuid=$($VOL_ID $filesystem_dev )
 
-               *)  echo "Internal error!"; exit 1;;
-       esac
-done
-if [ -z "$instance" -o -z "$blockdev" -o -z "$swapdev" ]; then
-       echo "Missing -i or -b or -s argument!"
-       exit 1
+if [ -n "$swapdev" ]; then
+  mkswap $swapdev
+  swap_uuid=$($VOL_ID $swapdev || true )
 fi
 
-mkswap $swapdev
-mke2fs -Fjq $blockdev
-swap_uuid=$(/sbin/vol_id --uuid $swapdev )
-root_uuid=$(/sbin/vol_id --uuid $blockdev )
-
 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"
-  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#" \
-    > $TMPDIR/etc/fstab.new
+  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#" \
+          -e "s#^(/dev/sdb|UUID=[a-f0-9-]+)\s+swap\s+.*\$#$SWAP_LINE#" \
+      > $TMPDIR/etc/fstab.new
+  else
+    cat $TMPDIR/etc/fstab | \
+      sed -re "s#^(/dev/sda|UUID=[a-f0-9-]+)\s+/\s+.*\$#$ROOT_LINE#" \
+      > $TMPDIR/etc/fstab.new
+  fi
   mv $TMPDIR/etc/fstab.new  $TMPDIR/etc/fstab
 fi
 
-umount $TMPDIR
-rmdir $TMPDIR
+# execute cleanups
+cleanup
 trap - EXIT
+
+exit 0