/
guestfish_lib.py
815 lines (651 loc) · 27.7 KB
/
guestfish_lib.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
from string import *
import guestfs
import math
import sys
class PartitionInfo:
def __init__(self, num, partition, sector_start, sector_end, size, unit):
self.num = num
self.partition = partition
self.sector_start = sector_start
self.sector_end = sector_end
self.size = size
self.unit = unit
def __str__(self):
str_repr = "NUM: " + str(self.num) + "\t" + \
"PARTITION: " + self.partition + "\t" + \
"SECTOR_START: " + str(self.sector_start) + "\t" + \
"SECTOR_END: " + str(self.sector_end) + "\t" + \
"SIZE: " + str(self.size) + "\t" + \
"UNIT: " + self.unit
return str_repr
class PartitionInfoList:
def __init__(self, device, sector_size, gfs_part_list):
self.num_of_partitions = len(gfs_part_list)
self.partitions = []
for p in gfs_part_list:
part_num = p.get("part_num")
part_name = device + str(part_num)
sector_start = math.ceil(p.get("part_start") / sector_size)
sector_end = math.floor(p.get("part_end") / sector_size)
size = p.get("part_size")
unit = "Byte"
p_info = PartitionInfo(part_num, part_name, sector_start, sector_end, size, unit)
self.partitions.append(p_info)
def __str__(self):
str_rep = "NUM OF PARTITIONS: " + str(self.num_of_partitions) + "\n"
for p in self.partitions:
str_rep = str_rep + str(p) + "\n"
return str_rep
class FileSystemInfo:
def __init__(self, filesystem, fstype):
self.filesystem = filesystem
self.fstype = fstype
def __str__(self):
str_repr = "FILESYSTEM: " + str(self.filesystem) + "\t" + \
"FSTYPE: " + self.fstype
return str_repr
class FileSystemInfoList:
def __init__(self, gfs_filesystem_list):
self.num_of_filesystens = len(gfs_filesystem_list)
self.filesystems = []
for fs in gfs_filesystem_list:
filesystem = fs.strip()
fstype = gfs_filesystem_list[fs].strip()
fs_info = FileSystemInfo(filesystem, fstype)
self.filesystems.append(fs_info)
def __str__(self):
str_rep = "NUM OF FILESYSTEMS: " + str(self.num_of_filesystens) + "\n"
for fs in self.filesystems:
str_rep = str_rep + str(fs) + "\n"
return str_rep
class PartitionListResult:
def __init__(self, code, description, partition_info_list = None):
self.code = code
self.description = description
self.partition_info_list = partition_info_list
def __str__(self):
str_rep = "CODE: " + str(self.code) + "\t" + "DESCRIPTION: " + self.description + "\n" + str(self.partition_info_list)
return str_rep
class PartitionAddResult:
def __init__(self, code, description):
self.code = code
self.description = description
def __str__(self):
str_rep = "CODE: " + str(self.code) + "\t" + "DESCRIPTION: " + self.description
return str_rep
class PartitionDelResult:
def __init__(self, code, description):
self.code = code
self.description = description
def __str__(self):
str_rep = "CODE: " + str(self.code) + "\t" + "DESCRIPTION: " + self.description
return str_rep
class PartitionResizeResult:
def __init__(self, code, description):
self.code = code
self.description = description
def __str__(self):
str_rep = "CODE: " + str(self.code) + "\t" + "DESCRIPTION: " + self.description
return str_rep
class VGAddResult:
def __init__(self, code, description):
self.code = code
self.description = description
def __str__(self):
str_rep = "CODE: " + str(self.code) + "\t" + "DESCRIPTION: " + self.description
return str_rep
class VGDelResult:
def __init__(self, code, description):
self.code = code
self.description = description
def __str__(self):
str_rep = "CODE: " + str(self.code) + "\t" + "DESCRIPTION: " + self.description
return str_rep
class LVAddResult:
def __init__(self, code, description):
self.code = code
self.description = description
def __str__(self):
str_rep = "CODE: " + str(self.code) + "\t" + "DESCRIPTION: " + self.description
return str_rep
class LVDelResult:
def __init__(self, code, description):
self.code = code
self.description = description
def __str__(self):
str_rep = "CODE: " + str(self.code) + "\t" + "DESCRIPTION: " + self.description
return str_rep
class FSCreateResult:
def __init__(self, code, description):
self.code = code
self.description = description
def __str__(self):
str_rep = "CODE: " + str(self.code) + "\t" + "DESCRIPTION: " + self.description
return str_rep
class FSDelResult:
def __init__(self, code, description):
self.code = code
self.description = description
def __str__(self):
str_rep = "CODE: " + str(self.code) + "\t" + "DESCRIPTION: " + self.description
return str_rep
class ExInvalidStorageType(Exception):
ERR_INVALID_STORAGE_TYPE = "{0} is not a supported storage unit type."
def __init__(self, unit):
message = self.ERR_INVALID_STORAGE_TYPE.format(unit)
super(Exception, self).__init__(message)
class ExPartitionListError(Exception):
def __init__(self, message):
super(Exception, self).__init__(message)
class ExPartitionAddError(Exception):
def __init__(self, message):
super(Exception, self).__init__(message)
class ExPartitionDoesNotExistError(Exception):
ERR_PARTITION_DOES_NOT_EXIST = "Partition {0} does not exist"
def __init__(self, partition):
message = self.ERR_PARTITION_DOES_NOT_EXIST.format(partition)
super(Exception, self).__init__(message)
class ExPartitionExistsError(Exception):
ERR_PARTITION_EXISTS = "Partition {0} exists"
def __init__(self, partition):
message = self.ERR_PARTITION_EXISTS.format(partition)
super(Exception, self).__init__(message)
class ExPartitionDelError(Exception):
def __init__(self, message):
super(Exception, self).__init__(message)
class ExPartitionResizeError(Exception):
def __init__(self, message):
super(Exception, self).__init__(message)
class ExUnrecognizedDiskLabelError(Exception):
ERR_UNRECOGNIZED_DISK_LABEL_ERROR = "Device {0} does not have a partition table."
def __init__(self, device):
message = self.ERR_UNRECOGNIZED_DISK_LABEL_ERROR.format(device)
super(Exception, self).__init__(message)
class ExVolumeGroupAddError(Exception):
def __init__(self, message):
super(Exception, self).__init__(message)
class ExVolumeGroupDelError(Exception):
def __init__(self, message):
super(Exception, self).__init__(message)
class ExVolumeGroupDoesNotExistError(Exception):
ERR_VG_DOES_NOT_EXIST = "VG {0} does not exist"
def __init__(self, partition):
message = self.ERR_VG_DOES_NOT_EXIST.format(partition)
super(Exception, self).__init__(message)
class ExVolumeGroupExistsError(Exception):
ERR_VG_EXISTS = "VG {0} exists"
def __init__(self, partition):
message = self.ERR_VG_EXISTS.format(partition)
super(Exception, self).__init__(message)
class ExLogicalVolumeAddError(Exception):
def __init__(self, message):
super(Exception, self).__init__(message)
class ExLogicalVolumeDelError(Exception):
def __init__(self, message):
super(Exception, self).__init__(message)
class ExLogicalVolumeDoesNotExistError(Exception):
ERR_LV_DOES_NOT_EXIST = "LV {0} does not exist"
def __init__(self, partition):
message = self.ERR_LV_DOES_NOT_EXIST.format(partition)
super(Exception, self).__init__(message)
class ExLogicalVolumeExistsError(Exception):
ERR_LV_EXISTS = "LV {0} exists"
def __init__(self, partition):
message = self.ERR_LV_EXISTS.format(partition)
super(Exception, self).__init__(message)
class ExFilesystemExistsError(Exception):
ERR_FS_EXISTS = "FS {0} exists"
def __init__(self, fs):
message = self.ERR_FS_EXISTS.format(fs)
super(Exception, self).__init__(message)
class ExFilesystemDoesNotExistError(Exception):
ERR_FS_DOES_NOT_EXIST = "FS {0} does not exist"
def __init__(self, fs):
message = self.ERR_FS_DOES_NOT_EXIST.format(fs)
super(Exception, self).__init__(message)
class ExDeviceDoesNotExistError(Exception):
ERR_DEV_DOES_NOT_EXIST = "Device {0} does not exist"
def __init__(self, dev):
message = self.ERR_DEV_DOES_NOT_EXIST.format(dev)
super(Exception, self).__init__(message)
class ExFilesystemCreateError(Exception):
def __init__(self, message):
super(Exception, self).__init__(message)
class ExFilesystemDelError(Exception):
def __init__(self, message):
super(Exception, self).__init__(message)
class ExFilesystemResizeError(Exception):
def __init__(self, message):
super(Exception, self).__init__(message)
class ExCannotListFilesystemsError(Exception):
def __init__(self, message):
super(Exception, self).__init__(message)
class ExMountDirExistError(Exception):
ERR_MOUNTDIR_EXISTS = "Mountpoint {0} exists."
def __init__(self, mountpoint):
message = self.ERR_MOUNTDIR_EXISTS.format(mountpoint)
super(Exception, self).__init__(message)
class GFSLibUtil:
# Initialize GFSUtil
def __init__(self, cloud_image_paths):
self.cloud_image_paths = cloud_image_paths
self.gfs = guestfs.GuestFS(python_return_dict = True)
self.num_of_cloud_images = len(cloud_image_paths)
for cloud_image_path in cloud_image_paths:
self.gfs.add_drive_opts(cloud_image_path, format = "qcow2", readonly = 0)
self.gfs.launch()
# Release resources on object deletion
def __del__(self):
self.gfs.close()
# Get Sector Size
def get_sector_size(self, device):
# Set Cloud Image device
cloud_device_name = self.convert_device_to_cloud_device(device, False)
# Get the sector size
sector_size = self.gfs.blockdev_getss(cloud_device_name)
return sector_size
# Convert Virtual Disk Device to Cloud Image device
# The device parameter is in the naming conventions of Virtual Disk e.g. /dev/vda
# Guestfish mounts the image as a separate partition and the naming changes to /dev/sda so we need to transform the value.
def convert_device_to_cloud_device(self, device, is_part):
if self.num_of_cloud_images == 1:
if is_part:
cloud_device_name = "/dev/" + device.replace("v", "s", 1)
else:
cloud_device_name = "/dev/sda"
else:
cloud_device_name = "/dev/" + device.replace("v", "s", 1)
return cloud_device_name
# Convert a vol group and logical volume to a device name
# e.g. lv00 and vg00 = /dev/vg00/lv00
def convert_lv_vg_to_device(self, lv, vg):
lv_device = "/dev/" + vg + "/" + lv
return lv_device
# Convert Storage Units to Bytes
# TODO Check for ArithmeticError in the size
def convert_storage_units_to_bytes(self, size, unit):
bytes = 0
if unit == "Byte":
bytes = size
elif unit == "KiB":
bytes = size * 1024
elif unit == "MiB":
bytes = size * (1024 *1024)
elif unit == "GiB":
bytes = size * (1024 * 1024 * 1024)
else:
raise ExInvalidStorageType(unit)
return bytes
# Convert Storage Units to MebiBytes
# TODO Check for ArithmeticError in the size
def convert_storage_units_to_mebibytes(self, size, unit):
mibs = 0
if unit == "Byte":
mibs = size / 1024 / 1024
elif unit == "KiB":
mibs = size / 1024
elif unit == "MiB":
mibs = size
elif unit == "GiB":
mibs = size * 1024
else:
raise ExInvalidStorageType(unit)
return mibs
# List Partitions
def list_partitions(self, device):
try:
# Set Cloud Image device
cloud_device_name = self.convert_device_to_cloud_device(device, False)
# Get the sector Size
sector_size = self.get_sector_size(device)
# List the partitions
partition_list = self.gfs.part_list(cloud_device_name)
# Create and return the result object
partition_info_list = PartitionInfoList(device, sector_size, partition_list)
return partition_info_list
except Exception as ex:
if 'unrecognised disk label' in str(ex):
raise ExUnrecognizedDiskLabelError(device)
else:
raise ExPartitionListError(ex)
def get_partition_type(self, device):
try:
cloud_device_name = self.convert_device_to_cloud_device(device, False)
part_type = self.gfs.part_get_parttype(cloud_device_name)
return part_type
except Exception as ex:
raise
def initialize_partition_table(self, device, partition_type):
cloud_device_name = self.convert_device_to_cloud_device(device, False)
self.gfs.part_init(cloud_device_name, partition_type)
def list_filesystems(self):
try:
filesystem_list = self.gfs.list_filesystems()
fs_info_list = FileSystemInfoList(filesystem_list)
return fs_info_list
except Exception as ex:
raise ExCannotListFilesystemsError(ex)
# Add Partition
def add_partition(self, device, size, unit):
try:
# Set Cloud Image device
cloud_device_name = self.convert_device_to_cloud_device(device, False)
# Get the sector Size
sector_size = self.get_sector_size(device)
# Get existing partitions
existing_partitions = self.list_partitions(device)
# Initialize sector vars
start_sector_of_new_partition = 0
last_sector_of_new_partition = 0
# Check if disk has any partitions to set the correct start sector
if existing_partitions.num_of_partitions == 0:
# Set start sector to 1
start_sector_of_new_partition = 1
# Calculate last sector
last_sector_of_new_partition = math.ceil(self.convert_storage_units_to_bytes(size, unit) / sector_size)
else:
# Get end sector of last partition
last_sector_of_last_partition = existing_partitions.partitions[-1].sector_end
# Add 1 sector to create the new start position
start_sector_of_new_partition = last_sector_of_last_partition + 1
# Calculate last sector
last_sector_of_new_partition = start_sector_of_new_partition + math.ceil(self.convert_storage_units_to_bytes(size, unit) / sector_size)
# Create new partition
# TODO parameterize on partition type prlogex
self.gfs.part_add(cloud_device_name, "p", start_sector_of_new_partition, last_sector_of_new_partition)
# TODO Add created partition info in the result type
return PartitionAddResult(0,"OK")
except ExUnrecognizedDiskLabelError as ex_disk_label_error:
raise
except ExPartitionListError as ex_part_list:
raise
except Exception as ex:
raise ExPartitionAddError(ex)
def delete_partition(self, device, partition_num):
try:
# Get existing partitions
existing_partitions = self.list_partitions(device)
# Set Cloud Image device
cloud_device_name = self.convert_device_to_cloud_device(device, False)
partition_exists = False
# Check if partition number exists
for p in existing_partitions.partitions:
if p.num == partition_num:
partition_exists = True
break
else:
partition_exists = False
if partition_exists:
self.gfs.part_del(cloud_device_name, partition_num)
# TODO Do we need any more information in the result type of the delete operation
return PartitionDelResult(0,"OK")
else:
raise ExPartitionDoesNotExistError(partitionExMountDirExistError_num)
# TODO Do we need to raise the ExPartitionListError or wrap it in an ExPartitionDelError
except ExPartitionListError as ex_part_list:
raise
except ExPartitionDoesNotExistError as ex_part_not_exists:
raise
except Exception as ex:
raise ExPartitionDelError(ex)
# Resize Partition
def resize_partition(self, device, partition_num, size, unit):
try:
# Set Cloud Image device
cloud_device_name = self.convert_device_to_cloud_device(device, False)
# Get the sector Size
sector_size = self.get_sector_size(device)
# Get existing partitions
existing_partitions = self.list_partitions(device)
last_sector_of_partition_to_resized = 0
partition_size = 0
partition_unit = None
# Get end sector of the partition to resize
for p in existing_partitions.partitions:
if p.num == partition_num:
partition_exists = True
last_sector_of_partition_to_resized = p.sector_end
partition_size = p.size
partition_unit = p.unit
break
else:
partition_exists = False
if partition_exists:
# Convert size in Bytes
new_size_in_bytes = self.convert_storage_units_to_bytes(size, unit)
# Calculate the extra space to add to partition
current_size_in_bytes = self.convert_storage_units_to_bytes(partition_size, partition_unit)
extra_space_in_bytes_needed = new_size_in_bytes - current_size_in_bytes
# Calculate Sector for the new size
new_size_in_sectors = extra_space_in_bytes_needed / sector_size
# Calculate new end sector
new_end_sector = math.ceil(last_sector_of_partition_to_resized + new_size_in_sectors)
# Resize Partition
self.gfs.part_resize(cloud_device_name, partition_num, new_end_sector)
return PartitionDelResult(0,"OK")
else:
raise ExPartitionDoesNotExistError(partition_num)
# TODO Add resized partition info in the result type
return PartitionResizeResult(0,"OK")
except ExPartitionListError as ex_part_list:
raise
except ExPartitionDoesNotExistError as ex_part_not_exists:
raise
except Exception as ex:
raise ExPartitionResizeError(ex)
# Add Volume Group
def add_vg(self, vg, pvs):
try:
# Convert pvs to cloud notation. Guestfish uses /dev/sd* rather than /dev/vd*
cloud_pvs = []
for pv in pvs:
cloud_pv = self.convert_device_to_cloud_device(pv, True)
cloud_pvs.append(cloud_pv)
self.gfs.vgcreate(vg, cloud_pvs)
# TODO return more info
return VGAddResult(0, "OK")
except Exception as ex:
if 'already exists' in str(ex):
raise ExVolumeGroupExistsError(vg)
else:
raise ExVolumeGroupAddError(ex)
# Delete Volume Group
def delete_vg(self, vg):
try:
self.gfs.vgremove(vg)
# TODO return more info
return VGDelResult(0, "OK")
except Exception as ex:
if 'not found' in str(ex):
raise ExVolumeGroupDoesNotExistError(vg)
else:
raise ExVolumeGroupDelError(ex)
# Add Logical Volume
def add_lv(self, lv, vg, size, unit):
try:
size_in_mibs = self.convert_storage_units_to_mebibytes(size, unit)
self.gfs.lvcreate(lv, vg, size_in_mibs)
# TODO return more info
return LVAddResult(0, "OK")
except Exception as ex:
if 'already exists' in str(ex):
raise ExLogicalVolumeExistsError(vg)
else:
raise ExLogicalVolumeAddError(ex)
# Delete Logical Volume
def delete_lv(self, lv, vg):
try:
lv_device = self.convert_lv_vg_to_device(lv, lg)
self.gfs.lvremove(lv_device)
# TODO return more info
return LVDelResult(0, "OK")
except Exception as ex:
if 'not found' in str(ex):
raise ExLogicalVolumeDoesNotExistError(vg)
else:
raise ExLogicalVolumeDelError(ex)
# Create Filesystem
def create_fs(self, device, device_type, fstype, force, use_image_mkfs_xfs, mountpoint):
try:
cloud_device = ""
if device_type == 'standard':
cloud_device = self.convert_device_to_cloud_device(device, True)
else:
# LVM Device
cloud_device = "/dev/" + device
if force:
self.gfs.wipefs(cloud_device)
# Get the filesystems
fs_info_list = self.list_filesystems()
# Check if filesystem exists
for fs_info in fs_info_list.filesystems:
if fs_info.filesystem == cloud_device and fs_info.fstype != 'unknown':
raise ExFilesystemExistsError(cloud_device)
if fstype == 'swap':
self.gfs.mkswap(cloud_device)
self.gfs.swapon_device(cloud_device)
else:
# Hack. GuestFS uses the native xfs binary which can be different from the Guest Image one and cause incompatibilities
# If this is the case use sh and the cloud image binary
if use_image_mkfs_xfs:
self.gfs.mount("/dev/sda1","/")
mkfs_xfs_cmd = "mkfs.xfs " + cloud_device
self.gfs.sh(mkfs_xfs_cmd)
self.gfs.umount("/")
else:
self.gfs.mkfs(fstype, cloud_device)
# Mount the filesystem
fs_freq = ""
fs_passno = ""
fs_mntops = ""
fstab_device = "/dev/" + device
# Mount root filesystem of the cloud image
self.gfs.mount("/dev/sda1","/")
if fstype == 'swap':
fs_freq = "0"
fs_passno = "0"
fs_mntops = "defaults"
else:
fs_freq = "1"
fs_passno = "2"
fs_mntops = "defaults"
# Create the mountpoint
self.gfs.mkdir(mountpoint)
# Add fstab entry
self.gfs.write_append("/etc/fstab", "{0}\t{1}\t{2}\t{3}\t{4} {5}\n".format(fstab_device, mountpoint, fstype, fs_mntops, fs_freq, fs_passno))
# Unmount root filesystem
self.gfs.umount("/")
result = FSCreateResult(0,"OK")
return result
except ExFilesystemExistsError as ex_fs_exists_er:
raise
except Exception as ex:
if 'expecting a device name' in str(ex):
raise ExDeviceDoesNotExistError(device)
elif 'File exists' in str(ex):
raise ExMountDirExistError(mountpoint)
else:
raise ExFilesystemCreateError(ex)
# Delete Filesystem
def delete_fs(self, device, device_type):
try:
cloud_device = ""
if device_type == 'standard':
cloud_device = self.convert_device_to_cloud_device(device, True)
else:
cloud_device = device
# Get the filesystems
fs_info_list = self.list_filesystems()
# Check if filesystem exists
for fs_info in fs_info_list.filesystems:
if fs_info.filesystem == cloud_device and fs_info.fstype == 'unknown':
raise ExFilesystemDoesNotExistError(cloud_device)
self.gfs.wipefs(cloud_device)
result = FSDelResult(0,"OK")
return result
except ExFilesystemDoesNotExistError as ex_fs_does_not_exist_er:
raise
except Exception as ex:
if 'expecting a device name' in str(ex):
raise ExDeviceDoesNotExistError(device)
else:
raise ExFilesystemDelError(ex)
# Resize Filesystem
def resize_fs(self, device, device_type, fstype, use_image_mkfs_xfs, mountpoint):
try:
cloud_device = ""
if device_type == 'standard':
cloud_device = self.convert_device_to_cloud_device(device, True)
else:
# LVM Device
cloud_device = "/dev/" + device
# Get the filesystems
fs_info_list = self.list_filesystems()
# Check if filesystem exists
for fs_info in fs_info_list.filesystems:
if fs_info.filesystem == cloud_device and fs_info.fstype == 'unknown':
raise ExFilesystemDoesNotExistError(cloud_device)
self.gfs.mount(cloud_device, mountpoint)
if fstype == "xfs":
if use_image_mkfs_xfs:
mkfs_xfs_cmd = "xfs_growfs " + mountpoint
self.gfs.sh(mkfs_xfs_cmd)
else:
self.gfs.xfs_growfs(mountpoint)
else:
self.gfs.resize2fs(mountpoint)
self.gfs.umount(mountpoint)
result = FSCreateResult(0,"OK")
return result
except ExFilesystemDoesNotExistError as ex_fs_exists_er:
raise
except Exception as ex:
if 'expecting a device name' in str(ex):
raise ExDeviceDoesNotExistError(device)
elif 'File exists' in str(ex):
raise ExMountDirExistError(mountpoint)
else:
raise ExFilesystemResizeError(ex)
def main():
tryme()
def tryme():
print("Enter operation: \n 1. List \n 2. Add \n 3. Delete \n 4. Resize \n 5. Init Partition Table \n 6. Create VG One Disk \n 7. Create VG Two Disks \n 8. Delete VG \n 9. Create FS \n 10. Delete FS \n 11. Resize FS \n")
type = int(input())
gfs_util = GFSLibUtil(["/kvm/large_vda.qcow2"])
part = "vdb"
part_add_num = 2
if type == 1:
parts = gfs_util.list_partitions(part)
print(parts)
if type == 2:
result = gfs_util.add_partition(part,part_add_num,"GiB")
print(result)
if type == 3:
result = gfs_util.delete_partition(part, 2)
print(result)
if type == 4:
result = gfs_util.resize_partition(part, 1, 16, "GiB")
print(result)
if type == 5:
result = gfs_util.initialize_partition_table(part, "mbr")
print(result)
if type == 6:
result = gfs_util.add_vg("vg00", ['vda2'])
print(result)
if type == 7:
result = gfs_util.add_vg("vg01", ['vda3','vdb1'])
print(result)
if type == 8:
result = gfs_util.delete_vg("vg00")
print(result)
if type == 9:
result = gfs_util.create_fs("vda3", "standard", "xfs", False, True)
print(result)
if type == 10:
result = gfs_util.delete_fs("vda3", "standard")
print(result)
if type == 11:
result = gfs_util.resize_fs("vda1", "standard", "xfs", False, "/")
print(result)
if __name__ == '__main__':
main()