This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
/
devtree.py
198 lines (168 loc) · 7.07 KB
/
devtree.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import os
import subprocess
import blivet
from blivet.size import Size
from blivet.partitioning import do_partitioning
class DeviceTreeError(Exception):
pass
def expr2size(expr):
"""
Convert a NixOS device size type to a blivet.size value.
"""
sizes = {s.lower(): s for s in [
"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB",
"KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB",
]}
size_acc = Size(0)
for unit, sizeval in expr.items():
size_acc += Size(str(sizeval) + " " + sizes[unit])
return size_acc
class DeviceTree(object):
def __init__(self):
self._blivet = blivet.Blivet()
self._blivet.reset()
def get_device_by_script(self, devname):
"""
Returns a fuction that executes a script with 'devname' as its first
argument and returns the first line indicating a path as a blivet
device.
"""
def _find_dev(script, incomplete=False):
matches = subprocess.check_output([script, devname]).splitlines()
if len(matches) > 0:
first = matches[0].decode('utf-8')
return self._blivet.devicetree.get_device_by_path(
os.path.realpath(first),
incomplete=incomplete
)
else:
return None
return _find_dev
def get_device_by_physical_pos(self, pos, incomplete=False):
"""
Match physical disks based on a position in the order of enumeration
from the kernel. Note that virtual devices such as /dev/loop* are
excluded from this.
"""
for device in self._blivet.devicetree._devices:
is_incomplete = not getattr(device, 'complete', True)
if not incomplete and is_incomplete:
continue
if device.type != "disk":
continue
pos -= 1
if pos == 0:
return device
return None
def get_device_by_id(self, devid, incomplete=False):
"""
Get a device based on it's ID in /dev/disk/by-id.
Note that this is different from blivet's get_device_by_id() because
blivet's function searches based on a blivet-specific device id as an
integer rather than in /dev/disk/by-id.
"""
path = os.path.realpath(os.path.join("/dev/disk/by-id", devid))
return self._blivet.devicetree.get_device_by_path(
path, incomplete=incomplete
)
def match_device(self, devname, expr):
"""
Match a device 'devname' based on the NixOS storage.disk.*.match
configuration specified by 'expr'.
The return value is a blivet device or None if no device has been
found.
"""
matchers = {
'id': self.get_device_by_id,
'label': self._blivet.devicetree.get_device_by_label,
'name': self._blivet.devicetree.get_device_by_name,
'path': self._blivet.devicetree.get_device_by_path,
'sysfsPath': self._blivet.devicetree.get_device_by_sysfs_path,
'uuid': self._blivet.devicetree.get_device_by_uuid,
'script': self.get_device_by_script(devname),
'physicalPos': self.get_device_by_physical_pos,
}
match_fun = matchers['name']
match_on = devname
for name, fun in matchers.items():
if expr.get(name) is None:
continue
match_fun = fun
match_on = expr[name]
break
return match_fun(match_on, incomplete=expr['allowIncomplete'])
def devspec2tuple(self, devspec):
"""
Take device specification from a Nix expression (which is a dict) and
return it as a tuple so it's hashable.
"""
return (devspec['type'], devspec['name'])
def populate(self, expr, for_mounting=False):
"""
Feed the blivet device tree with the various options from the Nix
expression in 'expr'.
"""
storagetree = {}
if not for_mounting:
for name, attrs in expr['storage']['disk'].items():
disk = self.match_device(name, attrs['match'])
if disk is None:
msg = "Could find a device for disk {}.".format(name)
raise DeviceTreeError(msg)
storagetree[('disk', name)] = disk
for name, attrs in expr['storage']['partition'].items():
parent_spec = self.devspec2tuple(attrs['targetDevice'])
parent = storagetree.get(parent_spec)
if parent is None and not for_mounting:
msg = "Couldn't find a device specification for {}."
raise DeviceTreeError(msg.format('.'.join(parent_spec)))
if parent.format.type is None:
self._blivet.initialize_disk(parent)
part_attrs = {'name': name, 'parents': [parent]}
if attrs['size'] == "fill":
part_attrs['grow'] = True
else:
part_attrs['size'] = expr2size(attrs['size'])
part = self._blivet.new_partition(**part_attrs)
self._blivet.create_device(part)
storagetree[('partition', name)] = part
for name, attrs in expr['storage']['btrfs'].items():
parents = [storagetree.get(self.devspec2tuple(devspec))
for devspec in attrs['devices']]
for parent in parents:
fmt = blivet.formats.get_format(
"btrfs", device=parent.path
)
self._blivet.format_device(parent, fmt)
btrfs = self._blivet.new_btrfs(
name=name, parents=parents, data_level=attrs['data'],
metadata_level=attrs['metadata']
)
self._blivet.create_device(btrfs)
storagetree[('btrfs', name)] = btrfs
for mountpoint, attrs in expr['fileSystems'].items():
uuid = attrs['storage']['uuid']
if for_mounting:
device = self._blivet.devicetree.get_device_by_uuid(uuid)
if device is not None:
device.format.mountpoint = mountpoint
continue
if attrs['storage']['type'] == 'btrfs':
continue
target = storagetree.get(self.devspec2tuple(attrs['storage']))
fmt = blivet.formats.get_format(attrs['fsType'],
device=target.path,
uuid=uuid)
label = attrs.get('label')
if label is not None:
fmt.label = label
self._blivet.format_device(target, fmt)
@property
def devices(self):
return self._blivet.devicetree.devices
def realize(self):
do_partitioning(self._blivet)
self._blivet.do_it()
def mount(self, sysroot):
blivet.flags.installer_mode = True
self._blivet.fsset.mount_filesystems(root_path=sysroot)