forked from xenserver/host-installer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
disktools.py
1379 lines (1185 loc) · 55 KB
/
disktools.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-License-Identifier: GPL-2.0-only
import constants
import errno
import re, subprocess, types, os, time
from pprint import pprint
from copy import copy, deepcopy
import util
from xcp import logger
class Segment:
"""Segments are areas, e.g. disk partitions or LVM segments, defined by start address and size"""
def __init__(self, start, size):
self.start = start
self.size = size
def end(self):
return self.start + self.size
def __repr__(self):
repr = { 'end' : self.end() }
repr.update(self.__dict__)
return str(repr)
class MoveChunk:
"""MoveChunks represent a move. They contain source and destination addresses"""
def __init__(self, src, dest, size):
self.src = src
self.dest = dest
self.size = size
def __repr__(self):
return str(self.__dict__)
class FreePool:
"""FreePool manages the allotment of segments a pool of free segments, and
divides segments as necessary to fill the requested size exactly"""
def __init__(self, freeSegments, usedThreshold=0):
self.freeSegments = freeSegments
# Instead of altering the free segment list as free space is consumed by takeSegments,
# this class maintains a usedThreshold address. Addresses lower than the threshold
# have already been used, and those at or above it are still available
self.usedThreshold = usedThreshold
def freeSpace(self):
sizeLeft = 0
for seg in self.freeSegments:
freeSize = min(seg.size, seg.end() - self.usedThreshold)
if freeSize > 0:
sizeLeft += freeSize
return sizeLeft
def takeSegments(self, size):
"""Returns a LIST of segments that fill the requested size, and effectively removes
those segments from the free pool by increasing usedThreshold"""
initialFreeSpace = self.freeSpace()
segsToTake = []
sizeLeft = size
for seg in self.freeSegments:
availableStart = max(seg.start, self.usedThreshold)
sizeToTake = min(seg.end() - availableStart, sizeLeft)
if sizeToTake > 0:
takenSegment = Segment(availableStart, sizeToTake)
segsToTake.append(takenSegment)
self.usedThreshold = takenSegment.end()
sizeLeft -= takenSegment.size
assert sizeLeft >= 0 # Underflow implies a logic error
if sizeLeft > 0:
raise Exception("Disk allocation failed - out of space")
assert size == sum([seg.size for seg in segsToTake]) # Check we've allocated the size required
assert size == initialFreeSpace - self.freeSpace() # Check that free space has shrunk by the right amount
return segsToTake
def __repr__(self):
return str(self.__dict__)
class LVMTool:
# Separation character - mustn't appear in anything we expect back from pvs/vgs/lvs
SEP = '#'
# Evacuate this many more extents than pvresize theoretically requires
PVRESIZE_EXTENT_MARGIN = 0
# Volume group prefixes
VG_SWAP_PREFIX = 'VG_XenSwap'
VG_CONFIG_PREFIX = 'VG_XenConfig'
# Prefix used by the SR type 'lvm'
VG_SR_PREFIX = 'VG_XenStorage'
# Prefix for non-'lvm' local storage SR types
VG_OTHER_SR_PREFIX = 'XSLocal'
PVMOVE = ['pvmove']
LVCHANGE = ['lvchange']
LVREMOVE = ['lvremove']
VGCHANGE = ['vgchange']
VGREMOVE = ['vgremove']
PVREMOVE = ['pvremove']
PVRESIZE = ['pvresize']
VGS_INFO = { # For one-per-VG records
'command' : ['/sbin/lvm', 'vgs'],
'arguments' : ['--noheadings', '--nosuffix', '--units', 'b', '--separator', SEP],
'string_options' : ['vg_name'],
'integer_options' : []
}
LVS_SEG_INFO = { # For one-per-LV-segment records
'command' : ['/sbin/lvm', 'lvs'],
'arguments' : ['--noheadings', '--nosuffix', '--units', 'b', '--separator', SEP, '--segments'],
'string_options' : ['seg_pe_ranges'],
'integer_options' : []
}
LVS_INFO = { # For one-per-LV records
'command' : ['/sbin/lvm', 'lvs'],
'arguments' : ['--noheadings', '--nosuffix', '--units', 'b', '--separator', SEP],
'string_options' : ['lv_name', 'vg_name'],
'integer_options' : []
}
PVS_INFO = { # For one-per-PV records
'command' : ['/sbin/lvm', 'pvs'],
'arguments' : ['--noheadings', '--nosuffix', '--units', 'b', '--separator', SEP],
'string_options' : ['pv_name', 'vg_name'],
'integer_options' : ['pe_start', 'pv_size', 'pv_free', 'pv_pe_count', 'dev_size']
}
def __init__(self):
self.readAllInfo()
self.pvsToDelete = []
self.vgsToDelete = []
self.lvsToDelete = []
# moveLists are per device, so self.moveLists might be{ '/dev/sda3': [MoveChunk, MoveChunk, ...], '/dev/sdb3' : ... }
self.moveLists = {}
self.resizeList = []
@classmethod
def cmdWrap(cls, params):
rv, out, err = util.runCmd2(params, True, True)
if rv != 0:
if isinstance(err, (types.ListType, types.TupleType)):
raise Exception("\n".join(err)+"\nError="+str(rv))
else:
raise Exception(str(err)+"\nError="+str(rv))
return out
def readInfo(self, info):
retVal = []
allOptions = info['string_options'] + info['integer_options']
cmd = info['command'] + info['arguments'] + ['--options', ','.join(allOptions)]
out = self.cmdWrap(cmd)
for line in out.strip().split('\n'):
# skip blank lines
if line == '':
continue
try:
# Create a dict of the form 'option_name':value
data = dict(zip(allOptions, line.lstrip().split(self.SEP)))
if len(data) != len(allOptions):
raise Exception("Wrong number of options in reply")
for name in info['integer_options']:
# Convert integer options to integer type
data[name] = int(data[name])
retVal.append(data)
except Exception as e:
logger.log("Discarding corrupt LVM output line '"+str(line)+"'")
logger.log(" Command was '"+str(cmd)+"'")
logger.log(" Error was '"+str(e)+"'")
return retVal
def readAllInfo(self):
self.vgs = self.readInfo(self.VGS_INFO)
self.lvs = self.readInfo(self.LVS_INFO)
self.lvSegs = self.readInfo(self.LVS_SEG_INFO)
self.pvs = self.readInfo(self.PVS_INFO)
# For DM nodes "pvs" incorrectly returns /dev/dm-n, which does not exist.
# Replace occurrences of /dev/dm-n with the correct node under /dev/mapper/
for pv in self.pvs:
name = pv['pv_name']
if name.startswith('/dev/dm-'):
n = int(name[8:])
pv['pv_name'] = getDeviceMapperNode(n)
@classmethod
def decodeSegmentRange(cls, segRange):
# Handle only a single range, e.g. '/dev/sdb3:11001-16158'
matches = re.match(r'([^:]+):([0-9]+)-([0-9]+)$', segRange)
if not matches:
raise Exception("Could not decode segment range from '"+segRange+"'")
# End value is inclusive, so 0-0 is one segment long
return {
'device' : matches.group(1),
'start' : int(matches.group(2)),
'size' : int(matches.group(3)) - int(matches.group(2)) + 1 # +1 because end is inclusive
}
@classmethod
def encodeSegmentRange(cls, device, start, size):
endInclusive = start+size-1
if start < 0 or endInclusive < start:
raise Exception("Invalid segment to encode: "+str(device)+', start='+str(start)+', size='+str(size))
retVal = device+':'+str(start)+'-'+str(endInclusive)
return retVal
def segmentList(self, device):
# PV segments don't record whether the segment is free space or not, so iterate through
# the LV segments for the device instead
segments = []
for lvSeg in self.lvSegs:
segRange = self.decodeSegmentRange(lvSeg['seg_pe_ranges'])
if segRange['device'] == device:
segments.append(Segment(segRange['start'], segRange['size']))
segments.sort(lambda x, y : cmp(x.start, y.start))
return segments
def freeSegmentList(self, device):
pv = self.deviceToPV(device)
usedSegs = self.segmentList(device)
# Add a fake zero-sized end segment to the list, so the unallocated space at the end
# of the volume is a gap between two segments and not a special case
fakeEndSeg = Segment(pv['pv_pe_count'], 0)
usedSegs.append(fakeEndSeg)
freeSegs = []
# Iterate over pairs of consecutive segments
for seg, nextSeg in zip(usedSegs[:-1], usedSegs[1:]):
# ... work out the gap between them ...
gapSize = nextSeg.start - seg.end()
if gapSize > 0:
# ... and add that to the free segment list
freeSegs.append(Segment(seg.end(), gapSize))
return freeSegs
def segmentsToMove(self, device, threshold):
"""Given a device, i.e. a partition containing an LVM volume, and a threshold in extents,
returns the segments that would need to be moved so that all non-free segments are
below that address. Can add just part of a segment if the original straddles the threshold"""
segsToMove = []
for seg in self.segmentList(device):
if seg.end() > threshold:
start = max(seg.start, threshold)
segsToMove.append(Segment(start, seg.end() - start))
return segsToMove
def makeSpaceAfterThreshold(self, device, thresholdExtent):
"""Queues up a set of MoveChunks that will free up space at the end of a PV so that
a pvresize cammand can succeed, and these will lead to pvmove commands at
commit time. Doesn't queue up the pvresize command itself - resizeDevice will do that..
Also safe to call if no pvmoves are necessary"""
pv = self.deviceToPV(device)
# Extents >= thresholdExtent must be freed.
segsToMove = self.segmentsToMove(device, thresholdExtent)
# Calculate the free pool if we haven't already. If we have done it already, we've been
# here before for this device, so use the existing FreePool object as it knows how much
# free space is already used by reallocation
if 'free-pool' not in pv:
pv['free-pool'] = FreePool(self.freeSegmentList(device))
# Take a copy. We'll only commit our modified copy back to pv['free-pool'] if our transaction succeeds
freePool = deepcopy(pv['free-pool'])
moveList = []
for srcSeg in segsToMove:
srcOffset = 0
destSegs = freePool.takeSegments(srcSeg.size)
# destSegs are a tailor-made set of segments to consume srcSeg exactly, and the loop
# beow relies on that
for destSeg in destSegs:
# Divide up the source segments into the destination segments
srcStart = srcSeg.start + srcOffset
destStart = destSeg.start
moveList.append(MoveChunk(srcStart, destStart, destSeg.size))
srcOffset += destSeg.size
assert srcOffset == srcSeg.size # Logic error if not
# Add our moves to the current MoveChunk list for this device, creating the
# dict element if necessary
self.moveLists[device] = self.moveLists.get(device, []) + moveList
pv['free-pool'] = freePool
def deviceToPVOrNone(self, device):
""" Returns the PV record for a given device (partition), or None if there is no PV
for that device."""
for pv in self.pvs:
if pv['pv_name'] == device:
return pv
return None
def deviceToPV(self, device):
pv = self.deviceToPVOrNone(device)
if pv is None:
raise Exception("PV for device '"+str(device)+"' not found")
return pv
def vGContainingLV(self, lvol):
for lv in self.lvs:
if lv['lv_name'] == lvol:
return lv['vg_name']
raise Exception("VG for LV '"+lvol+"' not found")
def deviceSize(self, device):
pv = self.deviceToPV(device)
return pv['pv_size'] # in bytes
def deviceFreeSpace(self, device):
pv = self.deviceToPV(device)
return pv['pv_free'] # in bytes
def resizeDevice(self, device, byteSize):
""" Resizes the PV on a device, moving extents around if necessary
"""
pv = self.deviceToPV(device)
if byteSize > pv['dev_size']:
raise Exception("Size requested for "+str(device)+" ("+str(byteSize)+
") is greater than device size ("+str(pv['dev_size'])+")")
extentBytes = pv['pv_size'] // pv['pv_pe_count'] # Typically 4MiB
# Calculate the threshold in extents beyond which segments must be moved elsewhere.
# Round down, so enough space is freed for pvresize to complete, and allow
# PVRESIZE_EXTENT_MARGIN for extents consumed by LVM metadata
metadataExtents = (pv['pe_start'] + extentBytes - 1) // extentBytes # Round up
thresholdExtent = byteSize // extentBytes - metadataExtents - self.PVRESIZE_EXTENT_MARGIN
self.makeSpaceAfterThreshold(device, thresholdExtent)
self.resizeList.append({'device' : device, 'bytesize' : byteSize})
def testPartition(self, devicePrefix, vgPrefix):
"""Returns the first partition where the device name starts with devicePrefix and
the volume group that it's in starts with vgPrefix"""
retVal = None
for pv in self.pvs:
if pv['pv_name'].startswith(devicePrefix) and pv['vg_name'].startswith(vgPrefix):
retVal = pv['pv_name']
break
return retVal
def configPartition(self, devicePrefix):
"""Returns the PV name for a config partition on the specified WHOLE DEVICE, e.g. '/dev/sda',
or None if none present"""
return self.testPartition(devicePrefix, self.VG_CONFIG_PREFIX)
def swapPartition(self, devicePrefix):
return self.testPartition(devicePrefix, self.VG_SWAP_PREFIX)
def srPartition(self, devicePrefix):
retVal = self.testPartition(devicePrefix, self.VG_SR_PREFIX)
if retVal is None:
retVal = self.testPartition(devicePrefix, self.VG_OTHER_SR_PREFIX)
return retVal
def isPartitionConfig(self, device):
"""Returns True if there is a config partition on the specified PARTITION, e.g. '/dev/sda2',
or False if none present"""
pv = self.deviceToPVOrNone(device)
return pv is not None and pv['vg_name'].startswith(self.VG_CONFIG_PREFIX)
def isPartitionSwap(self, device):
pv = self.deviceToPVOrNone(device)
return pv is not None and pv['vg_name'].startswith(self.VG_SWAP_PREFIX)
def isPartitionSR(self, device):
pv = self.deviceToPVOrNone(device)
return pv is not None and (pv['vg_name'].startswith(self.VG_SR_PREFIX) or \
pv['vg_name'].startswith(self.VG_OTHER_SR_PREFIX))
def deleteDevice(self, device):
"""Deletes PVs, VGs and LVs associated with a device (partition)"""
pvsToDelete = []
vgsToDelete = []
lvsToDelete = []
for pv in self.pvs:
if pv['pv_name'] == device:
pvsToDelete.append(pv['pv_name'])
vgsToDelete.append(pv['vg_name'])
for lv in self.lvs:
if lv['vg_name'] in vgsToDelete:
# lvremove requires a 'path': <VG name>/<LV name>
lvsToDelete.append(lv['vg_name']+'/'+lv['lv_name'])
self.pvsToDelete += pvsToDelete
self.vgsToDelete += vgsToDelete
self.lvsToDelete += lvsToDelete
def activateVG(self, vg):
self.cmdWrap(self.VGCHANGE + ['-ay', vg])
def deactivateVG(self, vg):
self.cmdWrap(self.VGCHANGE + ['-an', vg])
def deactivateAll(self):
"""Makes sure that LVM has unmounted everything so that, e.g. sfdisk can succeed"""
for vg in self.vgs:
# Passing VG names to LVchange is intentional
try:
self.cmdWrap(self.LVCHANGE + ['-an', vg['vg_name']])
except Exception as e:
logger.logException(e)
@classmethod
def executeMoves(cls, progress_callback, device, moveList):
# Call commit instead this method unless you have special requirements
"""Issues pvmove commands to move MoveChunks specified by the MoveList. Doesn't
handle overlapping source and destination segments in a single MoveChunk, but in
a makeSpaceAtEnd scenario those aren't generated"""
sizeStep = 16 # Moving 16 extents takes only slightly more time than moving 1
totalExtents = sum(move.size for move in moveList)
extentsSoFar = 0
for move in moveList:
offset = 0
while offset < move.size:
progress_callback((100 * extentsSoFar) / totalExtents)
chunkSize = min(sizeStep, move.size - offset)
srcRange = cls.encodeSegmentRange(device, move.src + offset, chunkSize)
destRange = cls.encodeSegmentRange(device, move.dest + offset, chunkSize)
cls.cmdWrap(cls.PVMOVE +
[
'--alloc',
'anywhere',
srcRange,
destRange
])
offset += chunkSize
extentsSoFar += chunkSize
def commit(self, progress_callback=lambda _ : ()):
"""Commit the changes queued up by issuing LVM commands, delete our queues as they
succeed, and then reread the new configuration from LVM"""
progress_callback(0)
# Abort pvmoves if any have been left partiially completed by e.g. a crash
self.cmdWrap(self.PVMOVE + ['--abort'])
self.deactivateAll()
progress_callback(1)
# Process delete lists
for lv in self.lvsToDelete:
self.cmdWrap(self.LVREMOVE + [lv])
self.lvsToDelete = []
progress_callback(2)
for vg in self.vgsToDelete:
self.cmdWrap(self.VGREMOVE + [vg])
self.vgsToDelete = []
progress_callback(3)
for pv in self.pvsToDelete:
self.cmdWrap(self.PVREMOVE + ['--force', '--yes', pv])
self.pvsToDelete = []
progress_callback(4)
# Process move lists. Most of the code here is for calculating smoothly
# increasing progress values
totalExtents = 0
for moveList in self.moveLists.values():
totalExtents += sum([ move.size for move in moveList ])
extentsSoFar = 0
for device, moveList in sorted(self.moveLists.items()):
thisSize = sum([ move.size for move in moveList ])
callback = lambda percent : (progress_callback( 5 + (98 - 5) * (extentsSoFar + thisSize * percent / 100) / totalExtents) )
self.executeMoves(callback, device, moveList)
extentsSoFar += thisSize
self.moveLists = {}
# Process resize list
progress_callback(98)
for resize in self.resizeList:
self.cmdWrap(self.PVRESIZE + ['--setphysicalvolumesize', str(resize['bytesize']//1024)+'k', resize['device']])
self.resizeList = []
self.readAllInfo() # Reread the new LVM configuration
progress_callback(99)
self.deactivateAll() # Stop active LVs preventing changes to the partition structure
progress_callback(100)
def dump(self):
pprint(self.__dict__)
def diskDevice(partitionDevice):
matches = re.match(r'(.+)(p?|(-part))\d+$', partitionDevice)
if matches:
return matches.group(1)
matches = re.match(r'(.+\D)\d+$', partitionDevice)
if not matches:
raise Exception("Could not determine disk device for device '"+partitionDevice+"'")
return matches.group(1)
def determineMidfix(device):
DISK_PREFIX = '/dev/'
P_STYLE_DISKS = [ 'cciss', 'ida', 'rd', 'sg', 'i2o', 'amiraid', 'iseries', 'emd', 'carmel', 'mapper/', 'nvme', 'md', 'mmcblk' ]
PART_STYLE_DISKS = [ 'disk/by-id' ]
for key in P_STYLE_DISKS:
if device.startswith(DISK_PREFIX + key):
return '' if re.match(r'.+\D$', device) else 'p'
for key in PART_STYLE_DISKS:
if device.startswith(DISK_PREFIX + key):
return '-part'
return ''
def partitionDevice(device, deviceNum):
return device + determineMidfix(device) + str(deviceNum)
def roundUp(n, mult):
n += mult - 1
return n - n % mult
class PartitionToolBase:
"""
Base class for the DOS and GPT Partition Tool classes.
Contains common code.
"""
BLOCKDEV = '/sbin/blockdev'
DEFAULT_SECTOR_SIZE = 512 # Used if sfdisk won't print its (hardcoded) value
def __init__(self, device):
self.sectorAlignment = 1
self.device = device
self.midfix = determineMidfix(device)
self.readDiskDetails()
self.partitions = self.partitionTable()
self.origPartitions = deepcopy(self.partitions)
def partitionNumber(self, partitionDevice):
matches = re.match(self.device + self.midfix + r'(\d+)$', partitionDevice)
if not matches:
raise Exception("Could not determine partition number for device '"+partitionDevice+"'")
return int(matches.group(1))
# Private methods:
def cmdWrap(self, params):
rv, out, err = util.runCmd2(params, True, True)
if rv != 0:
raise Exception(err)
return out
def _partitionDevice(self, deviceNum):
return self.device + self.midfix + str(deviceNum)
def _partitionNumber(self, partitionDevice):
# sfdisk is inconsistent in naming partitions of by-id devices
matches = re.match(self.device + r'\D*(\d+)$', partitionDevice)
if not matches:
raise Exception("Could not determine partition number for device '"+partitionDevice+"'")
return int(matches.group(1))
def settleUdev(self):
timeout = 30
try:
self.cmdWrap(util.udevsettleCmd() + ['--timeout=%d' % timeout ])
except:
logger.log('udevsettle with %d second timeout failed' % timeout)
def waitForDeviceNodes(self):
# Ensure new device nodes are available before we continue.
# Wait a second to ensure that udev picks up the change event from
# the kernel, then call settle to wait for all events complete.
time.sleep(1)
self.settleUdev()
def writePartitionTable(self, dryrun=False, log=False):
try:
self.writeThisPartitionTable(self.partitions, dryrun, log)
except Exception as e:
try:
# Revert to the original partition table
self.writeThisPartitionTable(self.origPartitions, dryrun)
except Exception as e2:
raise Exception('The new partition table could not be written: '+str(e)+'\nReversion also failed: '+str(e2))
raise Exception('The new partition table could not be written but was reverted successfully: '+str(e))
else:
self.waitForDeviceNodes()
# Public methods from here onward:
def getPartition(self, number, default=None):
return deepcopy(self.partitions.get(number, default))
def createPartition(self, id, sizeBytes=None, number=None, order=None, startBytes=None, active=False):
if number is None:
if len(self.partitions) == 0:
newNumber = 1
else:
newNumber = 1+max(self.partitions.keys())
else:
newNumber = number
if newNumber in self.partitions:
raise Exception('Partition '+str(newNumber)+' already exists')
partitions = [None] + [part for num, part in sorted(self.partitions.items(), key=lambda item: item[1]['start'])]
if startBytes is None:
if order:
if order < 1:
raise Exception("Order cannot be less than 1")
elif order == 1:
startSector = self.sectorFirstUsable
else:
startSector = partitions[order - 1]['start'] + partitions[order - 1]['size']
else:
startSector = partitions[-1]['start'] + partitions[-1]['size']
startSector = roundUp(startSector, self.sectorAlignment)
else:
if startBytes % self.sectorSize != 0:
raise Exception("Partition start ("+str(startBytes)+") is not a multiple of the sector size "+str(self.sectorSize))
startSector = startBytes // self.sectorSize
if sizeBytes is None:
if order:
if order < 1:
raise Exception("Order cannot be less than 1")
elif order > len(partitions):
raise Exception("Order too large")
elif order == len(partitions):
sizeSectors = self.sectorLastUsable + 1 - startSector
else:
sizeSectors = partitions[order]['start'] - startSector
else:
sizeSectors = self.sectorLastUsable + 1 - startSector
else:
if sizeBytes % self.sectorSize != 0:
raise Exception("Partition size ("+str(sizeBytes)+") is not a multiple of the sector size "+str(self.sectorSize))
sizeSectors = sizeBytes // self.sectorSize
if sizeSectors < 0:
self.dump()
raise Exception("Partition size in sectors ("+str(sizeSectors)+") is negative")
self.partitions[newNumber] = {
'start': startSector,
'size': sizeSectors,
'id': id,
'active': active
}
def deletePartition(self, number):
del self.partitions[number]
def deletePartitionIfPresent(self, number):
if number in self.partitions:
self.deletePartition(number)
def deletePartitions(self, numbers):
for number in numbers:
self.deletePartition(number)
def renamePartition(self, srcNumber, destNumber, overwrite=False):
if srcNumber not in self.partitions:
raise Exception('Source partition '+str(srcNumber)+' does not exist')
if srcNumber != destNumber:
if not overwrite and destNumber in self.partitions:
raise Exception('Destination partition '+str(destNumber)+' already exists')
self.partitions[destNumber] = self.partitions[srcNumber]
self.deletePartition(srcNumber)
def partitionSize(self, number):
if number not in self.partitions:
raise Exception('Partition '+str(number)+' does not exist')
return self.getPartition(number)['size'] * self.sectorSize
def partitionStart(self, number):
if number not in self.partitions:
raise Exception('Partition '+str(number)+' does not exist')
return self.getPartition(number)['start'] * self.sectorSize
def partitionEnd(self, number):
return self.partitionStart(number) + self.partitionSize(number)
def partitionID(self, number):
if number not in self.partitions:
raise Exception('Partition '+str(number)+' does not exist')
return self.getPartition(number)['id']
def resizePartition(self, number, sizeBytes):
if number not in self.partitions:
raise Exception('Partition for resize '+str(number)+' does not exists')
if sizeBytes % self.sectorSize != 0:
raise Exception("Partition size ("+str(sizeBytes)+") is not a multiple of the sector size "+str(self.sectorSize))
self.partitions[number]['size'] = sizeBytes // self.sectorSize
def setActiveFlag(self, activeFlag, number):
assert isinstance(activeFlag, types.BooleanType) # Assert that params are the right way around
if not number in self.partitions:
raise Exception('Partition '+str(number)+' does not exist')
self.partitions[number]['active'] = activeFlag
def inactivateDisk(self):
for number, partition in self.partitions.items():
if partition['active']:
self.setActiveFlag(False, number)
def items(self):
# sorted() creates a new list, so you can delete partitions whilst iterating
for number, partition in sorted(self.partitions.items()):
yield number, partition
def commit(self, dryrun=False, log=False):
if log:
self.dump()
self.writePartitionTable(dryrun, log)
if not dryrun:
# Update the revert point so this tool can be used repeatedly
self.origPartitions = deepcopy(self.partitions)
def dump(self):
output = "Sector size : "+str(self.sectorSize) + "\n"
output += "Sector extent : "+str(self.sectorExtent)+" sectors\n"
output += "Sector last usable : "+str(self.sectorLastUsable)+"\n"
output += "Sector first usable : "+str(self.sectorFirstUsable)+"\n"
output += "Partition size and start addresses in sectors:\n"
for number, partition in sorted(self.origPartitions.items()):
output += "Old partition "+str(number)+":"
for k, v in sorted(partition.items()):
output += ' '+k+'='+((k == 'id' and type(v) == int) and hex(v) or str(v))
output += "\n"
for number, partition in sorted(self.partitions.items()):
output += "New partition "+str(number)+":"
for k, v in sorted(partition.items()):
output += ' '+k+'='+((k == 'id' and type(v) == int) and hex(v) or str(v))
output += "\n"
logger.log(output)
class DOSPartitionTool(PartitionToolBase):
ID_LINUX_SWAP = 0x82
ID_LINUX = 0x83
ID_LINUX_LVM = 0x8e
ID_DELL_UTILITY = 0xde
ID_EFI_BOOT = 0xef
# List of hidden Microsoft partition IDs
__HIDDEN_MS_IDS = {
0x11, 0x14, 0x16, 0x17, 0x1b, 0x1c, 0x1e, 0x27
}
SFDISK = '/sbin/sfdisk'
partTableType = constants.PARTITION_DOS
def __readDiskDetails(self):
# Read basic geometry
out = self.cmdWrap([self.SFDISK, '-Lg', self.device])
matches = re.match(r'^[^:]*:\s*(\d+)\s+cylinders,\s*(\d+)\s+heads,\s*(\d+)\s+sectors', out)
if not matches:
raise Exception("Couldn't decode sfdisk output: "+out)
cylinders = int(matches.group(1))
heads = int(matches.group(2))
sectors = int(matches.group(3))
self.sectorExtent = cylinders * heads * sectors
# DOS partition tables have 32bit sector addresses so we may need to truncate sectorExtent
# Actually truncate a bit more because sfdisk has unfathomablely lower limit
self.sectorExtent = min([self.sectorExtent, 0xffe00000]) # 2047G
cylinders = int(self.sectorExtent//(heads * sectors))
self.sectorExtent = cylinders * heads * sectors # Ignore partial cylinder at end
self.sectorFirstUsable = sectors # Some SANs require bootable disks to start on sector boundary
self.sectorLastUsable = self.sectorExtent - 1
# Read sector size
self.sectorSize = int(self.cmdWrap([self.BLOCKDEV, '--getss', self.device]))
def __readDeviceMapperDiskDetails(self):
# DM nodes don't have a geometry and this version of sfdisk will return nothing.
# Later versions return the default geometry below.
heads = 255
sectors = 63
self.sectorSize = int(self.cmdWrap([self.BLOCKDEV, '--getss', self.device]))
out = self.cmdWrap([self.BLOCKDEV, '--getsize64', self.device])
self.sectorExtent = int(out)//self.sectorSize
# DOS partition tables have 32bit sector addresses so we may need to truncate sectorExtent
# Actually truncate a bit more because sfdisk has unfathomablely lower limit
self.sectorExtent = min([self.sectorExtent, 0xffe00000]) # 2047G
cylinders = int(self.sectorExtent//(heads * sectors))
self.sectorExtent = cylinders * heads * sectors # Ignore partial cylinder at end
self.sectorFirstUsable = sectors # Some SANs require bootable disks to start on sector boundary
self.sectorLastUsable = self.sectorExtent - 1
def readDiskDetails(self):
if isDeviceMapperNode(self.device):
self.__readDeviceMapperDiskDetails()
else:
self.__readDiskDetails()
def partitionTable(self):
out = self.cmdWrap([self.SFDISK, '-Ld', self.device])
state = 0
partitions = {}
for line in out.split("\n"):
if line == '' or line[0] == '#':
pass # Skip comments and blank lines
elif state == 0:
if line != 'unit: sectors':
raise Exception("Expecting 'unit: sectors' but got '"+line+"'")
state += 1
elif state == 1:
matches = re.match(r'(.*?)\s*:\s*start=\s*(\d+),\s*size=\s*(\d+),\s*Id=\s*(\w+)\s*(,\s*bootable)?', line)
if matches:
idt = int(matches.group(4), 16) # Base 16
active = (matches.group(5) is not None)
else:
# extended BSD partition?
idt = 0
active = False
matches = re.match(r'(.*?)\s*:\s*start=\s*(\d+),\s*size=\s*(\d+)', line)
if not matches:
raise Exception("Could not decode partition line: '"+line+"'")
size = int(matches.group(3))
if size != 0: # Treat partitions of size 0 as not present
number = self._partitionNumber(matches.group(1))
hidden = idt in self.__HIDDEN_MS_IDS
partitions[number] = {
'start': int(matches.group(2)),
'size': size,
'id': idt,
'active': active,
'hidden': hidden
}
return partitions
def commitActivePartitiontoDisk(self, part_num):
self.settleUdev()
self.cmdWrap([self.SFDISK, '--no-reread', '-A%d' % part_num, self.device]) # BIOS bootable flag set for one and unset for others partition
self.waitForDeviceNodes()
def writeThisPartitionTable(self, table, dryrun=False, log=False):
input = 'unit: sectors\n\n'
# sfdisk doesn't allow us to skip partitions, so invent lines for empty slot
for number in range(1, 1+max([1]+list(table.keys()))):
partition = table.get(number, {
'start': 0,
'size': 0,
'id': 0,
'active': False
})
line = self._partitionDevice(number)+' :'
line += ' start='+str(partition['start'])+','
line += ' size='+str(partition['size'])+','
line += ' Id=%x' % partition['id']
if partition['active']:
line += ', bootable'
input += line+'\n'
if log:
logger.log('Input to sfdisk:\n'+input)
if isDeviceMapperNode(self.device):
# Destroy device mapper partitions before re-writing partition table on mpath device
rv = destroyPartnodes(self.device)
if rv:
raise Exception('Failed to destroy partitions on ' + self.device)
heads = 255
sectors = 63
cylinders = self.sectorExtent//(heads * sectors)
cmd = [self.SFDISK, dryrun and '-Lnu' or '-Lu', '--no-reread', '-f', '-C%d' % cylinders, '-H%d' % heads, '-S%d' % sectors, self.device]
else:
cmd = [self.SFDISK, dryrun and '-LnuS' or '-LuS', '--no-reread', '-f', self.device]
logger.log('sfdisk command: %s' % ' '.join(cmd))
self.settleUdev()
process = subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
close_fds=True,
universal_newlines=True,
)
output = process.communicate(input)
if log:
logger.log('Output from sfdisk:\n'+output[0])
if isDeviceMapperNode(self.device):
# Create partitions using device mapper
rv = createPartnodes(self.device)
if rv:
raise Exception('Failed to create partitions on %s using kpartx ' % self.device)
for number in table.keys():
size = int(self.cmdWrap([self.BLOCKDEV, '--getsize64', '%sp%d' % (self.device, number)]))//self.sectorSize
if size != table[number]['size']:
raise Exception('Failed to create partition %sp%d of size %d' % (self.device, number, table[number]['size']))
else:
if process.returncode != 0:
raise Exception('Partition changes could not be applied: '+str(output[0]))
# CA-35300: sfdisk doesn't return non-zero when the BLKRRPART ioctl fails
if 'BLKRRPART: Device or resource busy' in output:
raise Exception('The disk appears to be in use and partition changes cannot be applied. Reboot and repeat the installation')
# Verify the table
# Ignore warnings about partitions apparently with ends beyond the end of the disk
rc, err = util.runCmd2([self.SFDISK, '-LVquS', self.device], with_stderr=True)
if rc == 1:
lines = err.split('\n')
if len([x for x in lines if x != '' and not x.endswith('extends past end of disk')]) != 0:
raise Exception(err)
elif rc != 0:
raise Exception(err)
def utilityPartitions(self):
# Return list of partition numbers for partitions we should preserve
return [num for num in self.partitions.keys() if self.partitions[num]['id'] == self.ID_DELL_UTILITY]
def convertToGPT(self):
self.cmdWrap(['sgdisk', '--mbrtogpt', self.device])
gpt_tool = GPTPartitionTool(self.device)
# Copy hidden attribute on MS partitions
hidden_args = []
for num, part in self.partitions.items():
if part['id'] in self.__HIDDEN_MS_IDS and gpt_tool.partitions[num]['id'] == gpt_tool.ID_LINUX:
hidden_args.append('--attributes=%d:set:62' % num)
gpt_tool.partitions[num]['hidden'] = True
if hidden_args:
self.cmdWrap([gpt_tool.SGDISK] + hidden_args + [self.device])
return gpt_tool
class GPTPartitionTool(PartitionToolBase):
# These are partition type GUIDs
ID_LINUX_SWAP = "0657FD6D-A4AB-43C4-84E5-0933C84B4F4F"
ID_LINUX = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7"
ID_LINUX_LVM = "E6D6D379-F507-44C2-A23C-238F2A3DF928"
ID_EFI_BOOT = "C12A7328-F81F-11D2-BA4B-00A0C93EC93B"
ID_BIOS_BOOT = "21686148-6449-6E6F-744E-656564454649"
# Lookup used for creating partitions
GUID_to_type_code = {
ID_LINUX_SWAP: '8200',
ID_LINUX: '0700',
ID_LINUX_LVM: '8e00',
ID_EFI_BOOT: 'ef00',
ID_BIOS_BOOT: 'ef02',
}
SGDISK = 'sgdisk'
partTableType = constants.PARTITION_GPT
def readDiskDetails(self):
self.sectorSize = int(self.cmdWrap([self.BLOCKDEV, '--getss', self.device]))
self.sectorExtent = int(self.cmdWrap([self.BLOCKDEV, '--getsize64', self.device])) // self.sectorSize
# size depends on GPT entries (should be 128), their size (128 bytes) and sector size
# first sector is MBR, second GPT header
self.sectorFirstUsable = 2 - (-128*128 // self.sectorSize)
self.sectorLastUsable = self.sectorExtent - self.sectorFirstUsable
self.sectorAlignment = 2 ** 20 // self.sectorSize
def partitionTable(self):
cmd = [self.SGDISK, '--print', self.device]
rv, out, err = util.runCmd2(cmd, True, True)
if rv != 0:
logger.log('Invalid or corrupt partition table found on disk %s. Skipping...' % self.device)
self.waitForDeviceNodes()
return {}
matchWarning = re.compile('Found invalid GPT and valid MBR; converting MBR to GPT format.')
matchHeader = re.compile('Number\s+Start \(sector\)\s+End \(sector\)\s+Size\s+Code\s+Name')
matchPartition = re.compile('^\s+(\d+)\s+(\d+)\s+(\d+)\s+([\d.]+\s+\w+)\s+([0-9A-F]{4})(\s+(.*))?$') # num start end sz typecode name
matchActive = re.compile('.*\(legacy BIOS bootable\)')
matchHidden = re.compile('.*\(hidden\)')
matchId = re.compile('^Partition GUID code: ([0-9A-F\-]+) ')
matchPartUUID = re.compile('^Partition unique GUID: ([0-9A-F\-]+)$')
partitions = {}
lines = out.split('\n')
gotHeader = False
for line in lines:
if not line.strip():
continue
if not gotHeader:
if matchWarning.match(line):
logger.log("Warning: GPTPartitionTool found DOS partition table on device %s" % self.device)
elif matchHeader.match(line):
gotHeader = True
else:
matches = matchPartition.match(line)
if not matches:
raise Exception("Could not parse sgdisk output line: %s" % line)
number = int(matches.group(1))
start = int(matches.group(2))
_end = int(matches.group(3))
size = _end + 1 - start
partlabel = matches.group(7) if matches.group(7) else ''
partitions[number] = {
'start': int(matches.group(2)),
'size': size,
'partlabel': partlabel,