3
# Copyright (C) 2009-2010 Canonical Ltd.
4
# Copyright (C) 2012 Hewlett-Packard Development Company, L.P.
6
# Author: Scott Moser <scott.moser@canonical.com>
7
# Author: Juerg Haefliger <juerg.haefliger@hp.com>
9
# This program is free software: you can redistribute it and/or modify
10
# it under the terms of the GNU General Public License version 3, as
11
# published by the Free Software Foundation.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
from string import whitespace # pylint: disable=W0402
25
from cloudinit import util
27
# Shortname matches 'sda', 'sda1', 'xvda', 'hda', 'sdb', xvdb, vda, vdd1
28
SHORTNAME_FILTER = r"^[x]{0,1}[shv]d[a-z][0-9]*$"
29
SHORTNAME = re.compile(SHORTNAME_FILTER)
30
WS = re.compile("[%s]+" % (whitespace))
34
# return true if this is a metadata service name
35
if name in ["ami", "root", "swap"]:
37
# names 'ephemeral0' or 'ephemeral1'
38
# 'ebs[0-9]' appears when '--block-device-mapping sdf=snap-d4d90bbc'
39
for enumname in ("ephemeral", "ebs"):
40
if name.startswith(enumname) and name.find(":") == -1:
45
def handle(_name, cfg, cloud, log, _args):
46
# fs_spec, fs_file, fs_vfstype, fs_mntops, fs-freq, fs_passno
47
defvals = [None, None, "auto", "defaults,nobootwait", "0", "2"]
48
defvals = cfg.get("mount_default_fields", defvals)
50
# these are our default set of mounts
51
defmnts = [["ephemeral0", "/mnt", "auto", defvals[3], "0", "2"],
52
["swap", "none", "swap", "sw", "0", "0"]]
56
cfgmnt = cfg["mounts"]
58
for i in range(len(cfgmnt)):
59
# skip something that wasn't a list
60
if not isinstance(cfgmnt[i], list):
61
log.warn("Mount option %s not a list, got a %s instead",
62
(i + 1), util.obj_name(cfgmnt[i]))
65
startname = str(cfgmnt[i][0])
66
log.debug("Attempting to determine the real name of %s", startname)
68
# workaround, allow user to specify 'ephemeral'
69
# rather than more ec2 correct 'ephemeral0'
70
if startname == "ephemeral":
71
cfgmnt[i][0] = "ephemeral0"
72
log.debug(("Adjusted mount option %s "
73
"name from ephemeral to ephemeral0"), (i + 1))
75
if is_mdname(startname):
76
newname = cloud.device_name_to_device(startname)
78
log.debug("Ignoring nonexistant named mount %s", startname)
82
if not newname.startswith("/"):
83
renamed = "/dev/%s" % newname
84
cfgmnt[i][0] = renamed
85
log.debug("Mapped metadata name %s to %s", startname, renamed)
87
if SHORTNAME.match(startname):
88
renamed = "/dev/%s" % startname
89
log.debug("Mapped shortname name %s to %s", startname, renamed)
90
cfgmnt[i][0] = renamed
92
# in case the user did not quote a field (likely fs-freq, fs_passno)
93
# but do not convert None to 'None' (LP: #898365)
94
for j in range(len(cfgmnt[i])):
98
cfgmnt[i][j] = str(cfgmnt[i][j])
100
for i in range(len(cfgmnt)):
101
# fill in values with defaults from defvals above
102
for j in range(len(defvals)):
103
if len(cfgmnt[i]) <= j:
104
cfgmnt[i].append(defvals[j])
105
elif cfgmnt[i][j] is None:
106
cfgmnt[i][j] = defvals[j]
108
# if the second entry in the list is 'None' this
109
# clears all previous entries of that same 'fs_spec'
110
# (fs_spec is the first field in /etc/fstab, ie, that device)
111
if cfgmnt[i][1] is None:
113
if cfgmnt[j][0] == cfgmnt[i][0]:
116
# for each of the "default" mounts, add them only if no other
117
# entry has the same device name
118
for defmnt in defmnts:
119
startname = defmnt[0]
120
devname = cloud.device_name_to_device(startname)
122
log.debug("Ignoring nonexistant named default mount %s", startname)
124
if devname.startswith("/"):
127
defmnt[0] = "/dev/%s" % devname
129
log.debug("Mapped default device %s to %s", startname, defmnt[0])
133
if cfgm[0] == defmnt[0]:
138
log.debug(("Not including %s, already"
139
" previously included"), startname)
141
cfgmnt.append(defmnt)
143
# now, each entry in the cfgmnt list has all fstab values
144
# if the second field is None (not the string, the value) we skip it
148
log.debug("Skipping non-existent device named %s", x[0])
152
if len(actlist) == 0:
153
log.debug("No modifications to fstab needed.")
156
comment = "comment=cloudconfig"
161
# write 'comment' in the fs_mntops, entry, claiming this
162
line[3] = "%s,%s" % (line[3], comment)
163
if line[2] == "swap":
165
if line[1].startswith("/"):
167
cc_lines.append('\t'.join(line))
170
fstab = util.load_file(cloud.paths.join(True, "/etc/fstab"))
171
for line in fstab.splitlines():
173
toks = WS.split(line)
174
if toks[3].find(comment) != -1:
178
fstab_lines.append(line)
180
fstab_lines.extend(cc_lines)
181
contents = "%s\n" % ('\n'.join(fstab_lines))
182
util.write_file(cloud.paths.join(False, "/etc/fstab"), contents)
186
util.subp(("swapon", "-a"))
188
util.logexc(log, "Activating swap via 'swapon -a' failed")
191
real_dir = cloud.paths.join(False, d)
193
util.ensure_dir(real_dir)
195
util.logexc(log, "Failed to make '%s' config-mount", d)
198
util.subp(("mount", "-a"))
200
util.logexc(log, "Activating mounts via 'mount -a' failed")