~registry/pykickstart/trunk

« back to all changes in this revision

Viewing changes to pykickstart/commands/btrfs.py

  • Committer: Chris Lumens
  • Date: 2014-10-22 14:19:44 UTC
  • Revision ID: git-v1:982498c0647c4bc3b4460209d4a76698c0eb6f8c
Add a note that the repo has moved.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# Chris Lumens <clumens@redhat.com>
3
 
# David Lehman <dlehman@redhat.com>
4
 
#
5
 
# Copyright 2005, 2006, 2007, 2011 Red Hat, Inc.
6
 
#
7
 
# This copyrighted material is made available to anyone wishing to use, modify,
8
 
# copy, or redistribute it subject to the terms and conditions of the GNU
9
 
# General Public License v.2.  This program is distributed in the hope that it
10
 
# will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the
11
 
# implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
 
# See the GNU General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU General Public License along with
15
 
# this program; if not, write to the Free Software Foundation, Inc., 51
16
 
# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  Any Red Hat
17
 
# trademarks that are incorporated in the source code or documentation are not
18
 
# subject to the GNU General Public License and may only be used or replicated
19
 
# with the express permission of Red Hat, Inc. 
20
 
#
21
 
from pykickstart.base import BaseData, KickstartCommand
22
 
from pykickstart.errors import KickstartValueError, formatErrorMsg
23
 
from pykickstart.options import KSOptionParser
24
 
 
25
 
import gettext
26
 
import warnings
27
 
_ = lambda x: gettext.ldgettext("pykickstart", x)
28
 
 
29
 
class F17_BTRFSData(BaseData):
30
 
    removedKeywords = BaseData.removedKeywords
31
 
    removedAttrs = BaseData.removedAttrs
32
 
 
33
 
    def __init__(self, *args, **kwargs):
34
 
        BaseData.__init__(self, *args, **kwargs)
35
 
        self.format = kwargs.get("format", True)
36
 
        self.preexist = kwargs.get("preexist", False)
37
 
        self.label = kwargs.get("label", "")
38
 
        self.mountpoint = kwargs.get("mountpoint", "")
39
 
        self.devices = kwargs.get("devices", [])
40
 
        self.dataLevel = kwargs.get("data", None)
41
 
        self.metaDataLevel = kwargs.get("metadata", None)
42
 
 
43
 
        # subvolume-specific
44
 
        self.subvol = kwargs.get("subvol", False)
45
 
        self.name = kwargs.get("name", None)        # required
46
 
 
47
 
    def __eq__(self, y):
48
 
        if not y:
49
 
            return False
50
 
 
51
 
        return self.mountpoint == y.mountpoint
52
 
 
53
 
    def __ne__(self, y):
54
 
        return not self == y
55
 
 
56
 
    def _getArgsAsStr(self):
57
 
        retval = ""
58
 
        if not self.format:
59
 
            retval += " --noformat"
60
 
        if self.preexist:
61
 
            retval += " --useexisting"
62
 
        if self.label:
63
 
            retval += " --label=%s" % self.label
64
 
        if self.dataLevel:
65
 
            retval += " --data=%s" % self.dataLevel
66
 
        if self.metaDataLevel:
67
 
            retval += " --metadata=%s" % self.metaDataLevel
68
 
        if self.subvol:
69
 
            retval += " --subvol --name=%s" % self.name
70
 
 
71
 
        return retval
72
 
 
73
 
    def __str__(self):
74
 
        retval = BaseData.__str__(self)
75
 
        retval += "btrfs %s" % self.mountpoint
76
 
        retval += self._getArgsAsStr()
77
 
        return retval + " " + " ".join(self.devices) + "\n"
78
 
 
79
 
class F17_BTRFS(KickstartCommand):
80
 
    removedKeywords = KickstartCommand.removedKeywords
81
 
    removedAttrs = KickstartCommand.removedAttrs
82
 
 
83
 
    def __init__(self, writePriority=132, *args, **kwargs):
84
 
        KickstartCommand.__init__(self, writePriority, *args, **kwargs)
85
 
        self.op = self._getParser()
86
 
 
87
 
        # A dict of all the RAID levels we support.  This means that if we
88
 
        # support more levels in the future, subclasses don't have to
89
 
        # duplicate too much.
90
 
        self.levelMap = { "RAID0": "raid0", "0": "raid0",
91
 
                          "RAID1": "raid1", "1": "raid1",
92
 
                          "RAID10": "raid10", "10": "raid10",
93
 
                          "single": "single" }
94
 
 
95
 
        self.btrfsList = kwargs.get("btrfsList", [])
96
 
 
97
 
    def __str__(self):
98
 
        retval = ""
99
 
        for btr in self.btrfsList:
100
 
            retval += btr.__str__()
101
 
 
102
 
        return retval
103
 
 
104
 
    def _getParser(self):
105
 
        # Have to be a little more complicated to set two values.
106
 
        def btrfs_cb (option, opt_str, value, parser):
107
 
            parser.values.format = False
108
 
            parser.values.preexist = True
109
 
 
110
 
        def level_cb (option, opt_str, value, parser):
111
 
            if value in self.levelMap:
112
 
                parser.values.ensure_value(option.dest, self.levelMap[value])
113
 
 
114
 
        op = KSOptionParser()
115
 
        op.add_option("--noformat", action="callback", callback=btrfs_cb,
116
 
                      dest="format", default=True, nargs=0)
117
 
        op.add_option("--useexisting", action="callback", callback=btrfs_cb,
118
 
                      dest="preexist", default=False, nargs=0)
119
 
 
120
 
        # label, data, metadata
121
 
        op.add_option("--label", dest="label", default="")
122
 
        op.add_option("--data", dest="dataLevel", action="callback",
123
 
                      callback=level_cb, type="string", nargs=1)
124
 
        op.add_option("--metadata", dest="metaDataLevel", action="callback",
125
 
                      callback=level_cb, type="string", nargs=1)
126
 
 
127
 
        #
128
 
        # subvolumes
129
 
        #
130
 
        op.add_option("--subvol", dest="subvol", action="store_true",
131
 
                      default=False)
132
 
 
133
 
        # parent must be a device spec (LABEL, UUID, &c)
134
 
        op.add_option("--parent", dest="parent", default="")
135
 
        op.add_option("--name", dest="name", default="")
136
 
 
137
 
        return op
138
 
 
139
 
    def parse(self, args):
140
 
        (opts, extra) = self.op.parse_args(args=args, lineno=self.lineno)
141
 
        data = self.handler.BTRFSData()
142
 
        self._setToObj(self.op, opts, data)
143
 
        data.lineno = self.lineno
144
 
 
145
 
        if len(extra) == 0:
146
 
            raise KickstartValueError(formatErrorMsg(self.lineno, msg=_("btrfs must be given a mountpoint")))
147
 
 
148
 
        if len(extra) == 1 and not data.subvol:
149
 
            raise KickstartValueError(formatErrorMsg(self.lineno, msg=_("btrfs must be given a list of partitions")))
150
 
        elif len(extra) == 1:
151
 
            raise KickstartValueError(formatErrorMsg(self.lineno, msg=_("btrfs subvol requires specification of parent volume")))
152
 
 
153
 
        if data.subvol and not data.name:
154
 
            raise KickstartValueError(formatErrorMsg(self.lineno, msg=_("btrfs subvolume requires a name")))
155
 
 
156
 
        data.mountpoint = extra[0]
157
 
        data.devices = extra[1:]
158
 
 
159
 
        # Check for duplicates in the data list.
160
 
        if data in self.dataList():
161
 
            warnings.warn(_("A btrfs volume with the mountpoint %s has already been defined.") % data.label)
162
 
 
163
 
        return data
164
 
 
165
 
    def dataList(self):
166
 
        return self.btrfsList