Use UUID-based mounts for create and import
authorGuido Trotter <ultrotter@google.com>
Thu, 14 Aug 2008 10:24:35 +0000 (10:24 +0000)
committerGuido Trotter <ultrotter@google.com>
Thu, 14 Aug 2008 10:24:35 +0000 (10:24 +0000)
With this change we mount our root and swap filesystems in /etc/fstab
based on their UUID rather than on their device name. This makes the
system resistent to device name changes, and makes sure the operating
system will find its devices whether they're seen as scsi, ide, virtio,
or some other new type/name.

This change makes the OS be able to boot under KVM, but also
xen+paravirt_ops and other possibly other hypervisors.

Reviewed-by: imsnah

NEWS
create.in
import

diff --git a/NEWS b/NEWS
index bb8dd89..ccd4eb8 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,10 @@
+Version 0.6 (unreleased)
+-----------
+
+The instance's fstab is now generated with volumes' UUIDs rather than paths.
+This makes it more resilient to changes and use under different
+hypervisors/drivers.
+
 Version 0.5
 -----------
 
index b168e5c..16c16ad 100755 (executable)
--- a/create.in
+++ b/create.in
@@ -71,8 +71,10 @@ if [ -z "$instance" -o -z "$blockdev" -o -z "$swapdev" ]; then
   exit 1
 fi
 
-mkswap $swapdev
+swap_uuid=$(mkswap $swapdev | grep 'UUID' | sed -re 's/.*UUID=//')
 mke2fs -Fjq $blockdev
+root_uuid=$(dumpe2fs -h $blockdev | grep '^Filesystem UUID:' \
+            | sed -re 's/.*UUID:\s*//')
 TMPDIR=`mktemp -d` || exit 1
 trap "umount $TMPDIR; rmdir $TMPDIR" EXIT
 
@@ -129,10 +131,10 @@ echo $instance > $TMPDIR/etc/mailname
 cat > $TMPDIR/etc/fstab <<EOF
 # /etc/fstab: static file system information.
 #
-# <file system> <mount point>   <type>  <options>       <dump>  <pass>
-/dev/sda        /               ext3    defaults        0       1
-/dev/sdb        swap            swap    defaults        0       0
-proc            /proc           proc    defaults        0       0
+# <file system>   <mount point>   <type>  <options>       <dump>  <pass>
+UUID=$root_uuid   /               ext3    defaults        0       1
+UUID=$swap_uuid   swap            swap    defaults        0       0
+proc              /proc           proc    defaults        0       0
 EOF
 
 cat > $TMPDIR/etc/network/interfaces <<EOF
diff --git a/import b/import
index a287644..0f4691b 100755 (executable)
--- a/import
+++ b/import
@@ -44,8 +44,11 @@ if [ -z "$instance" -o -z "$blockdev" -o -z "$swapdev" ]; then
        exit 1
 fi
 
-mkswap $swapdev
+swap_uuid=$(mkswap $swapdev | grep 'UUID' | sed -re 's/.*UUID=//')
 mke2fs -Fjq $blockdev
+root_uuid=$(dumpe2fs -h $blockdev | grep '^Filesystem UUID:' \
+            | sed -re 's/.*UUID:\s*//')
+
 TMPDIR=`mktemp -d` || exit 1
 trap "umount $TMPDIR; rmdir $TMPDIR" EXIT
 
@@ -59,6 +62,18 @@ mount $MOUNT_OPTIONS $blockdev $TMPDIR
 
 ( cd $TMPDIR; restore -r -y -f - )
 rm -f $TMPDIR/etc/udev/rules.d/z25_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
+  mv $TMPDIR/etc/fstab.new  $TMPDIR/etc/fstab
+fi
+
 umount $TMPDIR
 rmdir $TMPDIR
 trap - EXIT