-
Notifications
You must be signed in to change notification settings - Fork 345
/
disks.py
124 lines (107 loc) · 5.06 KB
/
disks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# User interface library functions for filesystem/disk space checking
#
# Copyright (C) 2013 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details. You should have received a copy of the
# GNU General Public License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
# Red Hat Author(s): David Lehman <dlehman@redhat.com>
# Chris Lumens <clumens@redhat.com>
#
from blivet.devices import MultipathDevice, iScsiDiskDevice, FcoeDiskDevice
from pyanaconda.flags import flags
__all__ = ["FakeDiskLabel", "FakeDisk", "getDisks", "isLocalDisk"]
class FakeDiskLabel(object):
def __init__(self, free=0):
self.free = free
class FakeDisk(object):
def __init__(self, name, size=0, free=0, partitioned=True, vendor=None,
model=None, serial=None, removable=False):
self.name = name
self.size = size
self.format = FakeDiskLabel(free=free)
self.partitioned = partitioned
self.vendor = vendor
self.model = model
self.serial = serial
self.removable = removable
@property
def description(self):
return "%s %s" % (self.vendor, self.model)
def getDisks(devicetree, fake=False):
if not fake:
devices = devicetree.devices
if flags.imageInstall:
hidden_images = [d for d in devicetree._hidden \
if d.name in devicetree.diskImages]
devices += hidden_images
else:
devices += devicetree._hidden
disks = []
for d in devices:
if d.isDisk and not d.format.hidden and not d.protected:
# unformatted DASDs are detected with a size of 0, but they should
# still show up as valid disks if this function is called, since we
# can still use them; anaconda will know how to handle them, so they
# don't need to be ignored anymore
if d.type == "dasd":
disks.append(d)
elif d.size > 0 and d.mediaPresent:
disks.append(d)
else:
disks = []
disks.append(FakeDisk("sda", size=300000, free=10000, serial="00001",
vendor="Seagate", model="Monster"))
disks.append(FakeDisk("sdb", size=300000, free=300000, serial="00002",
vendor="Seagate", model="Monster"))
disks.append(FakeDisk("sdc", size=8000, free=2100, removable=True,
vendor="SanDisk", model="Cruzer", serial="00003"))
# Remove duplicate names from the list.
return sorted(set(disks), key=lambda d: d.name)
def isLocalDisk(disk):
return (not isinstance(disk, MultipathDevice)
and not isinstance(disk, iScsiDiskDevice)
and not isinstance(disk, FcoeDiskDevice))
def applyDiskSelection(storage, data, use_names):
onlyuse = use_names[:]
for disk in (d for d in storage.disks if d.name in onlyuse):
onlyuse.extend(d.name for d in disk.ancestors
if d.name not in onlyuse)
data.ignoredisk.onlyuse = onlyuse
data.clearpart.drives = use_names[:]
def getRelatedDisks(storage, disk):
""" Return a list of disks related to disk by way of container devices. """
return set(d for dep in storage.deviceDeps(disk) for d in dep.disks)
def checkDiskSelection(storage, all_disks, selected_disks):
""" Return a list of errors related to a proposed disk selection.
:param :class:`blivet.Blivet` storage: storage data
:param all_disks: all known disks
:type all_disks: list of :class:`blivet.devices.StorageDevice`
:param selected_disks: proposed selected disks
:type selected_disks: list of :class:`blivet.devices.StorageDevice`
:returns: a list of error messages
:rtype: list of str
"""
errors = []
for selected in all_disks:
if selected.name not in selected_disks:
continue
related = getRelatedDisks(storage, selected)
missing = [r.name for r in related if r.name not in selected_disks]
if missing:
errors.append("You selected disk %s, which contains devices that "
"also use disk(s) %s. You must select or de-select "
"these disks as a set." %
(selected.name, ",".join(missing)))
return errors