From ee291edfccd67e97f2f8f4129168279e5f0499b3 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Thu, 4 Mar 2010 16:47:17 -0500 Subject: replace 'cloudconfig' entries in fstab rather than appending (LP: #524562) This marks the comment option of fs_mntops in fstab (man fstab) with cloudconfig for each of the mount options added by cloudconfig. It will search through existing lines, any entry written by cloudconfig will be deleted. LP: #524562 --- cloudinit/CloudConfig.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/cloudinit/CloudConfig.py b/cloudinit/CloudConfig.py index 9eb2d00b..487e9872 100644 --- a/cloudinit/CloudConfig.py +++ b/cloudinit/CloudConfig.py @@ -26,6 +26,8 @@ import os import glob import sys import time +import re +import string per_instance="once-per-instance" cronpre = "/etc/cron.d/cloudinit" @@ -344,15 +346,33 @@ class CloudConfig(): if len(actlist) == 0: return + comment="comment=cloudconfig" + cc_lines = [ ] needswap = False dirs = [ ] - - fstab=file("/etc/fstab","ab") - fstab.write("# cloud-config mounts\n") for line in actlist: - fstab.write('\t'.join(line) + "\n") + # write 'comment' in the fs_mntops, entry, claiming this + line[3]="%s,comment=cloudconfig" % line[3] if line[2] == "swap": needswap = True if line[1].startswith("/"): dirs.append(line[1]) + cc_lines.append('\t'.join(line)) + + fstab_lines = [ ] + fstab=open("/etc/fstab","r+") + ws = re.compile("[%s]+" % string.whitespace) + for line in fstab.read().splitlines(): + try: + toks = ws.split(line) + if toks[3].find(comment) != -1: continue + except: + pass + fstab_lines.append(line) + + fstab_lines.extend(cc_lines) + + fstab.seek(0) + fstab.write("%s\n" % '\n'.join(fstab_lines)) + fstab.truncate() fstab.close() if needswap: -- cgit v1.2.3