summaryrefslogtreecommitdiff
path: root/cloudinit/util.py
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2012-02-16 13:31:19 -0500
committerScott Moser <smoser@ubuntu.com>2012-02-16 13:31:19 -0500
commit725ea78fe96b3282f67d67ddd6079f24f055c746 (patch)
treeef13b8e71a7dc8bf2ef908336337b7359384304d /cloudinit/util.py
parent4213f7c28c2b6788938b1504e5dec7fcda2b8806 (diff)
downloadvyos-cloud-init-725ea78fe96b3282f67d67ddd6079f24f055c746.tar.gz
vyos-cloud-init-725ea78fe96b3282f67d67ddd6079f24f055c746.zip
initial version of DataSourceConfigDrive
Diffstat (limited to 'cloudinit/util.py')
-rw-r--r--cloudinit/util.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/cloudinit/util.py b/cloudinit/util.py
index e6489648..b53aa5fe 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -32,6 +32,7 @@ import re
import socket
import sys
import time
+import tempfile
import traceback
import urlparse
@@ -630,3 +631,82 @@ def close_stdin():
return
with open(os.devnull) as fp:
os.dup2(fp.fileno(), sys.stdin.fileno())
+
+def find_devs_with(criteria):
+ """
+ find devices matching given criteria (via blkid)
+ criteria can be *one* of:
+ TYPE=<filesystem>
+ LABEL=<label>
+ UUID=<uuid>
+ """
+ try:
+ (out,err) = subp(['blkid','-t%s' % criteria,'-odevice'])
+ except subprocess.CalledProcessError as exc:
+ return([])
+ return(out.splitlines())
+
+
+class mountFailedError(Exception):
+ pass
+
+def mount_callback_umount(device, callback, data=None):
+ """
+ mount the device, call method 'callback' passing the directory
+ in which it was mounted, then unmount. Return whatever 'callback'
+ returned. If data != None, also pass data to callback.
+ """
+
+ def _cleanup(mountpoint, tmpd):
+ if umount:
+ try:
+ subp(["umount", '-l', umount])
+ except subprocess.CalledProcessError as exc:
+ raise
+ if tmpd:
+ os.rmdir(tmpd)
+
+ # go through mounts to see if it was already mounted
+ fp = open("/proc/mounts")
+ mounts = fp.readlines()
+ fp.close()
+
+ tmpd = None
+
+ mounted = {}
+ for mpline in mounts:
+ (dev, mp, fstype, _opts, _freq, _passno) = mpline.split()
+ mp = mp.replace("\\040", " ")
+ mounted[dev] = (dev, fstype, mp, False)
+
+ umount = False
+ if device in mounted:
+ mountpoint = mounted[device][2]
+ else:
+ tmpd = tempfile.mkdtemp()
+
+ mountcmd = ["mount", "-o", "ro", device, tmpd]
+
+ try:
+ (out, err) = subp(mountcmd)
+ umount = tmpd
+ except subprocess.CalledProcessError as exc:
+ _cleanup(umount, tmpd)
+ print exc.output[1]
+ raise mountFailedError(exc.output[1])
+
+ mountpoint = tmpd
+
+ try:
+ if data == None:
+ ret = callback(mountpoint)
+ else:
+ ret = callback(mountpoint, data)
+
+ except Exception as exc:
+ _cleanup(umount, tmpd)
+ raise exc
+
+ _cleanup(umount, tmpd)
+
+ return(ret)