The UUID and TYPE don't always come out of blkid in the same order,
apparently...

Also, since we put everything on the root filesystem on the target, we should
clean out entries for the other filesystems. The policy is to remove any lines
that mount a file (like /dev/*) into the filesystem, and are not the root FS or
the swap partition.

Signed-off-by: Ben Lipton <[email protected]>
---
Please take note of how this changes the output fstab. I can't decide whether
deleting those lines is bad. Those particular ones aren't useful in Ganeti, but
I'm not sure that this policy is actually correct. It leaves things like proc
intact, but it might still remove too much. Maybe noauto lines should be
allowed to stay, even if not useful? Thoughts?


 instance-p2v-target/fixes/fixlib/fix_fstab.py    |   40 +++++++++++++++------
 instance-p2v-target/test/testdata/fstab_uuid_out |    2 -
 2 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/instance-p2v-target/fixes/fixlib/fix_fstab.py 
b/instance-p2v-target/fixes/fixlib/fix_fstab.py
index 3e652c5..32b15b1 100644
--- a/instance-p2v-target/fixes/fixlib/fix_fstab.py
+++ b/instance-p2v-target/fixes/fixlib/fix_fstab.py
@@ -44,19 +44,26 @@ def FixFstab(fname_in="/target/etc/fstab", 
fname_out="/target/etc/fstab"):
   # information to reconstruct the whole partitioning scheme.
   uuids = {}
   fstypes = {}
+  disk_name = "xvda"
   p = subprocess.Popen(["blkid"], stdout=subprocess.PIPE)
-  for line in p.communicate()[0].split('\n'):
-    parts = re.match("/dev/(xvda[0-9]): UUID=\"([-a-z0-9]+)\" TYPE=\"(.+)\"",
-                     line)
-    if parts:
-      partname, uuid, fstype = parts.groups()
-      uuids[partname] = uuid
-      fstypes[partname] = fstype
 
-  if "xvda1" not in uuids:
-    print uuids
-    raise fixlib.FixError("Could not determine UUID of root filesystem."
-                          " /etc/fstab may need to be edited by hand")
+  devregex = re.compile("/dev/(%s[0-9]):" % disk_name)
+  uuidregex = re.compile("UUID=\"([-a-z0-9]+)\"")
+  typeregex = re.compile("TYPE=\"(.+)\"")
+
+  for line in p.communicate()[0].splitlines():
+    devmatch = devregex.search(line)
+    uuidmatch = uuidregex.search(line)
+    typematch = typeregex.search(line)
+    if devmatch and uuidmatch and typematch:
+      partname = devmatch.group(1)
+      uuids[partname] = uuidmatch.group(1)
+      fstypes[partname] = typematch.group(1)
+
+  if "xvda1" not in uuids or "xvda2" not in uuids:
+    raise fixlib.FixError("Could not determine UUID of root and swap"
+                          " filesystems. Found filesystems were: %s\n"
+                          "/etc/fstab may need to be edited by hand." % uuids)
 
   fstab_file = open(fname_in, "r")
   new_fstab = ""
@@ -67,10 +74,19 @@ def FixFstab(fname_in="/target/etc/fstab", 
fname_out="/target/etc/fstab"):
         parts[0] = "UUID=%s" % uuids["xvda1"]
         parts[2] = fstypes["xvda1"]
         line = "\t".join(parts) + "\n"
+        new_fstab += line
       elif parts[2] == "swap":
         parts[0] = "UUID=%s" % uuids["xvda2"]
         line = "\t".join(parts) + "\n"
-    new_fstab += line
+        new_fstab += line
+      # We only have two "real" filesystems, so skip any other "real" ones. But
+      # there may be some special filesystems that we want to include, so
+      # append any lines that don't mount a real device file.
+      elif parts[0][0] != "/" and parts[0][0:5] != "UUID=":
+        new_fstab += line
+    else:
+      # Keep comments and whitespace
+      new_fstab += line
   fstab_file.close()
   fstab_file = open(fname_out, "w")
   fstab_file.write(new_fstab)
diff --git a/instance-p2v-target/test/testdata/fstab_uuid_out 
b/instance-p2v-target/test/testdata/fstab_uuid_out
index d20dbbf..08b5013 100644
--- a/instance-p2v-target/test/testdata/fstab_uuid_out
+++ b/instance-p2v-target/test/testdata/fstab_uuid_out
@@ -10,5 +10,3 @@ proc            /proc           proc    defaults        0     
  0
 UUID=11111111-1111-1111-1111-111111111111      /       ext3    
errors=remount-ro       0       1
 # swap was on /dev/sda5 during installation
 UUID=22222222-2222-2222-2222-222222222222      none    swap    sw      0       0
-/dev/scd0       /media/cdrom0   udf,iso9660 user,noauto     0       0
-/dev/fd0        /media/floppy0  auto    rw,user,noauto  0       0
-- 
1.7.3.1

Reply via email to