summaryrefslogtreecommitdiff
path: root/cloudinit/util.py
diff options
context:
space:
mode:
authorBen Howard <ben.howard@canonical.com>2013-10-02 15:05:15 -0600
committerBen Howard <ben.howard@canonical.com>2013-10-02 15:05:15 -0600
commitea88f3e1208ba501b50d1f41187b83cf11f15785 (patch)
treea1c1ea966d8ed38670968d809dba88750cc4cd96 /cloudinit/util.py
parented8cb662f72e4e3e03ee0489593b7d114e5618cf (diff)
downloadvyos-cloud-init-ea88f3e1208ba501b50d1f41187b83cf11f15785.tar.gz
vyos-cloud-init-ea88f3e1208ba501b50d1f41187b83cf11f15785.zip
Added ability to define disks via 'ephemeralX.Y'.
Modified cc_mounts to identify whether ephermalX is partitioned. Changed datasources for Azure and SmartOS to use 'ephemeralX.Y' format. Added disk remove functionally
Diffstat (limited to 'cloudinit/util.py')
-rw-r--r--cloudinit/util.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/cloudinit/util.py b/cloudinit/util.py
index 50ca7959..14519586 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -32,6 +32,7 @@ import grp
import gzip
import hashlib
import os
+import os.path
import platform
import pwd
import random
@@ -1826,3 +1827,78 @@ def log_time(logfunc, msg, func, args=None, kwargs=None, get_uptime=False):
except:
pass
return ret
+
+
+def map_partition(alias):
+ """
+ Return partition number for devices like ephemeral0.0 or ephemeral0.1
+
+ Parameters:
+ alaias: the alias, i.e. ephemeral0 or swap0
+ device: the actual device to markup
+
+ Rules:
+ - anything after a . is a parittion
+ - device.0 is the same as device
+ """
+
+ if len(alias.split('.')) == 1:
+ return None
+
+ suffix = alias.split('.')[-1]
+ try:
+ if int(suffix) == 0:
+ return None
+ return int(suffix)
+ except ValueError:
+ pass
+
+ return None
+
+
+def map_device_alias(device, partition=None, alias=None):
+ """
+ Find the name of the partition. While this might seem rather
+ straight forward, its not since some devices are '<device><partition>'
+ while others are '<device>p<partition>'. For example, /dev/xvda3 on EC2
+ will present as /dev/xvda3p1 for the first partition since /dev/xvda3 is
+ a block device.
+
+ The primary use is to map 'ephemeral0.1' in the datasource to a
+ real device name
+ """
+
+ if not device:
+ return None
+
+ if not partition and not alias:
+ raise Exception("partition or alias is required")
+
+ if alias:
+ partition = map_partition(alias)
+
+ # if the partition doesn't map, return the device
+ if not partition:
+ return device
+
+ short_name = device.split('/')[-1]
+ sys_path = "/sys/block/%s" % short_name
+
+ if not os.path.exists(sys_path):
+ return None
+
+ sys_long_path = sys_path + "/" + short_name
+ valid_mappings = [sys_long_path + "%s" % partition,
+ sys_long_path + "p%s" % partition]
+
+ for cdisk in valid_mappings:
+ if not os.path.exists(cdisk):
+ continue
+
+ dev_path = "/dev/%s" % cdisk.split('/')[-1]
+ if os.path.exists(dev_path):
+ return dev_path
+
+ return None
+
+