~ubuntu-branches/ubuntu/saucy/cloud-init/saucy

« back to all changes in this revision

Viewing changes to cloudinit/config/cc_mounts.py

  • Committer: Scott Moser
  • Date: 2012-07-06 21:24:18 UTC
  • mfrom: (1.1.26)
  • Revision ID: smoser@ubuntu.com-20120706212418-zwcpwxsdodi0pms3
New upstream snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vi: ts=4 expandtab
 
2
#
 
3
#    Copyright (C) 2009-2010 Canonical Ltd.
 
4
#    Copyright (C) 2012 Hewlett-Packard Development Company, L.P.
 
5
#
 
6
#    Author: Scott Moser <scott.moser@canonical.com>
 
7
#    Author: Juerg Haefliger <juerg.haefliger@hp.com>
 
8
#
 
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.
 
12
#
 
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.
 
17
#
 
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/>.
 
20
 
 
21
from string import whitespace  # pylint: disable=W0402
 
22
 
 
23
import re
 
24
 
 
25
from cloudinit import util
 
26
 
 
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))
 
31
 
 
32
 
 
33
def is_mdname(name):
 
34
    # return true if this is a metadata service name
 
35
    if name in ["ami", "root", "swap"]:
 
36
        return True
 
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:
 
41
            return True
 
42
    return False
 
43
 
 
44
 
 
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)
 
49
 
 
50
    # these are our default set of mounts
 
51
    defmnts = [["ephemeral0", "/mnt", "auto", defvals[3], "0", "2"],
 
52
               ["swap", "none", "swap", "sw", "0", "0"]]
 
53
 
 
54
    cfgmnt = []
 
55
    if "mounts" in cfg:
 
56
        cfgmnt = cfg["mounts"]
 
57
 
 
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]))
 
63
            continue
 
64
 
 
65
        startname = str(cfgmnt[i][0])
 
66
        log.debug("Attempting to determine the real name of %s", startname)
 
67
 
 
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))
 
74
 
 
75
        if is_mdname(startname):
 
76
            newname = cloud.device_name_to_device(startname)
 
77
            if not newname:
 
78
                log.debug("Ignoring nonexistant named mount %s", startname)
 
79
                cfgmnt[i][1] = None
 
80
            else:
 
81
                renamed = newname
 
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)
 
86
        else:
 
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
 
91
 
 
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])):
 
95
            if j is None:
 
96
                continue
 
97
            else:
 
98
                cfgmnt[i][j] = str(cfgmnt[i][j])
 
99
 
 
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]
 
107
 
 
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:
 
112
            for j in range(i):
 
113
                if cfgmnt[j][0] == cfgmnt[i][0]:
 
114
                    cfgmnt[j][1] = None
 
115
 
 
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)
 
121
        if devname is None:
 
122
            log.debug("Ignoring nonexistant named default mount %s", startname)
 
123
            continue
 
124
        if devname.startswith("/"):
 
125
            defmnt[0] = devname
 
126
        else:
 
127
            defmnt[0] = "/dev/%s" % devname
 
128
 
 
129
        log.debug("Mapped default device %s to %s", startname, defmnt[0])
 
130
 
 
131
        cfgmnt_has = False
 
132
        for cfgm in cfgmnt:
 
133
            if cfgm[0] == defmnt[0]:
 
134
                cfgmnt_has = True
 
135
                break
 
136
 
 
137
        if cfgmnt_has:
 
138
            log.debug(("Not including %s, already"
 
139
                       " previously included"), startname)
 
140
            continue
 
141
        cfgmnt.append(defmnt)
 
142
 
 
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
 
145
    actlist = []
 
146
    for x in cfgmnt:
 
147
        if x[1] is None:
 
148
            log.debug("Skipping non-existent device named %s", x[0])
 
149
        else:
 
150
            actlist.append(x)
 
151
 
 
152
    if len(actlist) == 0:
 
153
        log.debug("No modifications to fstab needed.")
 
154
        return
 
155
 
 
156
    comment = "comment=cloudconfig"
 
157
    cc_lines = []
 
158
    needswap = False
 
159
    dirs = []
 
160
    for line in actlist:
 
161
        # write 'comment' in the fs_mntops, entry,  claiming this
 
162
        line[3] = "%s,%s" % (line[3], comment)
 
163
        if line[2] == "swap":
 
164
            needswap = True
 
165
        if line[1].startswith("/"):
 
166
            dirs.append(line[1])
 
167
        cc_lines.append('\t'.join(line))
 
168
 
 
169
    fstab_lines = []
 
170
    fstab = util.load_file(cloud.paths.join(True, "/etc/fstab"))
 
171
    for line in fstab.splitlines():
 
172
        try:
 
173
            toks = WS.split(line)
 
174
            if toks[3].find(comment) != -1:
 
175
                continue
 
176
        except:
 
177
            pass
 
178
        fstab_lines.append(line)
 
179
 
 
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)
 
183
 
 
184
    if needswap:
 
185
        try:
 
186
            util.subp(("swapon", "-a"))
 
187
        except:
 
188
            util.logexc(log, "Activating swap via 'swapon -a' failed")
 
189
 
 
190
    for d in dirs:
 
191
        real_dir = cloud.paths.join(False, d)
 
192
        try:
 
193
            util.ensure_dir(real_dir)
 
194
        except:
 
195
            util.logexc(log, "Failed to make '%s' config-mount", d)
 
196
 
 
197
    try:
 
198
        util.subp(("mount", "-a"))
 
199
    except:
 
200
        util.logexc(log, "Activating mounts via 'mount -a' failed")