3131 disklabel_type:
3232 description:
3333 - disklabel type string (eg: 'gpt') to use, overriding the built-in logic in blivet
34+ safe_mode:
35+ description:
36+ - boolean indicating that we should fail rather than implicitly/automatically
37+ removing devices or formatting
3438
3539author:
3640 - David Lehman (dlehman@redhat.com)
116120
117121use_partitions = None # create partitions on pool backing device disks?
118122disklabel_type = None # user-specified disklabel type
123+ safe_mode = None # do not remove any existing devices or formatting
124+ packages_only = None # only set things up enough to get a list of required packages
119125
120126
121127class BlivetAnsibleError (Exception ):
@@ -163,7 +169,6 @@ def _get_format(self):
163169 label = self ._volume ['fs_label' ],
164170 options = self ._volume ['fs_create_options' ])
165171 if not fmt .supported or not fmt .formattable :
166- # FAIL: fs type tools are not available
167172 raise BlivetAnsibleError ("required tools for file system '%s' are missing" % self ._volume ['fs_type' ])
168173
169174 return fmt
@@ -189,32 +194,35 @@ def _resize(self):
189194 try :
190195 size = Size (self ._volume ['size' ])
191196 except Exception :
192- # FAIL: invalid size specification
193197 raise BlivetAnsibleError ("invalid size specification for volume '%s': '%s'" % (self ._volume ['name' ], self ._volume ['size' ]))
194198
195199 if size and self ._device .resizable and self ._device .size != size :
196200 if self ._device .format .resizable :
197201 self ._device .format .update_size_info ()
198202
199203 if not self ._device .min_size <= size <= self ._device .max_size :
200- # FAIL: resize to specified size not possible
201204 raise BlivetAnsibleError ("volume '%s' cannot be resized to '%s'" % (self ._volume ['name' ], size ))
202205
203206 try :
204207 self ._blivet .resize_device (self ._device , size )
205208 except ValueError as e :
206- # FAIL: resize not possible
207209 raise BlivetAnsibleError ("volume '%s' cannot be resized from %s to %s: %s" % (self ._device .name ,
208210 self ._device .size ,
209211 size , str (e )))
210212
211213 def _reformat (self ):
212214 """ Schedule actions as needed to ensure the volume is formatted as specified. """
215+ global packages_only
216+
213217 fmt = self ._get_format ()
214218 if self ._device .format .type == fmt .type :
215219 return
216220
217- if self ._device .format .status :
221+ if safe_mode and (self ._device .format .type is not None or self ._device .format .name != get_format (None ).name ) and \
222+ not packages_only :
223+ raise BlivetAnsibleError ("cannot remove existing formatting on volume '%s' in safe mode" % self ._volume ['name' ])
224+
225+ if self ._device .format .status and not packages_only :
218226 self ._device .format .teardown ()
219227 self ._blivet .format_device (self ._device , fmt )
220228
@@ -255,6 +263,17 @@ def _get_device_id(self):
255263 def _type_check (self ):
256264 return self ._device .is_disk
257265
266+ def _look_up_device (self ):
267+ super (BlivetDiskVolume , self )._look_up_device ()
268+ if not self ._get_device_id ():
269+ raise BlivetAnsibleError ("no disks specified for volume '%s'" % self ._volume ['name' ])
270+ elif not isinstance (self ._volume ['disks' ], list ):
271+ raise BlivetAnsibleError ("volume disks must be specified as a list" )
272+
273+ if self ._device is None :
274+ raise BlivetAnsibleError ("unable to resolve disk specified for volume '%s' (%s)" % (self ._volume ['name' ], self ._volume ['disks' ]))
275+
276+
258277
259278class BlivetPartitionVolume (BlivetVolume ):
260279 def _type_check (self ):
@@ -273,21 +292,18 @@ def _create(self):
273292 parent = self ._blivet .devicetree .resolve_device (self ._volume ['pool' ])
274293
275294 if parent is None :
276- # FAIL: failed to find pool
277295 raise BlivetAnsibleError ("failed to find pool '%s' for volume '%s'" % (self ._blivet_pool ['name' ], self ._volume ['name' ]))
278296
279297 size = Size ("256 MiB" )
280298 try :
281299 device = self ._blivet .new_partition (parents = [parent ], size = size , grow = True , fmt = self ._get_format ())
282300 except Exception :
283- # FAIL: failed to instantiate volume device
284301 raise BlivetAnsibleError ("failed set up volume '%s'" % self ._volume ['name' ])
285302
286303 self ._blivet .create_device (device )
287304 try :
288305 do_partitioning (self ._blivet )
289306 except Exception :
290- # FAIL: partition allocation failed: not enough space?
291307 raise BlivetAnsibleError ("partition allocation failed for volume '%s'" % self ._volume ['name' ])
292308
293309 self ._device = device
@@ -303,18 +319,15 @@ def _create(self):
303319
304320 parent = self ._blivet_pool ._device
305321 if parent is None :
306- # FAIL: failed to find pool
307322 raise BlivetAnsibleError ("failed to find pool '%s' for volume '%s'" % (self ._blivet_pool ['name' ], self ._volume ['name' ]))
308323
309324 try :
310325 size = Size (self ._volume ['size' ])
311326 except Exception :
312- # FAIL: invalid size specification
313327 raise BlivetAnsibleError ("invalid size '%s' specified for volume '%s'" % (self ._volume ['size' ], self ._volume ['name' ]))
314328
315329 fmt = self ._get_format ()
316330 if size > parent .free_space :
317- # FAIL: volume size greater than pool free space
318331 raise BlivetAnsibleError ("specified size for volume '%s' exceeds available space in pool '%s' (%s)" % (size ,
319332 parent .name ,
320333 parent .free_space ))
@@ -323,7 +336,6 @@ def _create(self):
323336 device = self ._blivet .new_lv (name = self ._volume ['name' ],
324337 parents = [parent ], size = size , fmt = fmt )
325338 except Exception :
326- # FAIL: failed to create volume
327339 raise BlivetAnsibleError ("failed to set up volume '%s'" % self ._volume ['name' ])
328340
329341 self ._blivet .create_device (device )
@@ -391,8 +403,7 @@ def _type_check(self): # pylint: disable=no-self-use
391403 def _look_up_disks (self ):
392404 """ Look up the pool's disks in blivet's device tree. """
393405 if not self ._pool ['disks' ]:
394- # FAIL: no disks specified for pool
395- raise BlivetAnsibleError ("no disks specified for pool '%s'" % self ._pool ['name' ]) # sure about this one?
406+ raise BlivetAnsibleError ("no disks specified for pool '%s'" % self ._pool ['name' ])
396407 elif not isinstance (self ._pool ['disks' ], list ):
397408 raise BlivetAnsibleError ("pool disks must be specified as a list" )
398409
@@ -403,7 +414,6 @@ def _look_up_disks(self):
403414 disks .append (device )
404415
405416 if self ._pool ['disks' ] and not disks :
406- # FAIL: failed to find any disks
407417 raise BlivetAnsibleError ("unable to resolve any disks specified for pool '%s' (%s)" % (self ._pool ['name' ], self ._pool ['disks' ]))
408418
409419 self ._disks = disks
@@ -428,8 +438,11 @@ def _create_members(self):
428438 """ Schedule actions as needed to ensure pool member devices exist. """
429439 members = list ()
430440 for disk in self ._disks :
431- if not disk .isleaf :
432- self ._blivet .devicetree .recursive_remove (disk )
441+ if not disk .isleaf or disk .format .type is not None :
442+ if safe_mode and not packages_only :
443+ raise BlivetAnsibleError ("cannot remove existing formatting and/or devices on disk '%s' (pool '%s') in safe mode" % (disk .name , self ._pool ['name' ]))
444+ else :
445+ self ._blivet .devicetree .recursive_remove (disk )
433446
434447 if use_partitions :
435448 label = get_format ("disklabel" , device = disk .path )
@@ -446,7 +459,6 @@ def _create_members(self):
446459 try :
447460 do_partitioning (self ._blivet )
448461 except Exception :
449- # FAIL: problem allocating partitions for pool backing devices
450462 raise BlivetAnsibleError ("failed to allocation partitions for pool '%s'" % self ._pool ['name' ])
451463
452464 return members
@@ -490,7 +502,11 @@ def _look_up_device(self):
490502 def _create (self ):
491503 if self ._device .format .type != "disklabel" or \
492504 self ._device .format .label_type != disklabel_type :
493- self ._blivet .devicetree .recursive_remove (self ._device , remove_device = False )
505+ if safe_mode and not packages_only :
506+ raise BlivetAnsibleError ("cannot remove existing formatting and/or devices on disk '%s' "
507+ "(pool '%s') in safe mode" % (self ._device .name , self ._pool ['name' ]))
508+ else :
509+ self ._blivet .devicetree .recursive_remove (self ._device , remove_device = False )
494510
495511 label = get_format ("disklabel" , device = self ._device .path , label_type = disklabel_type )
496512 self ._blivet .format_device (self ._device , label )
@@ -503,7 +519,6 @@ def _type_check(self):
503519 def _get_format (self ):
504520 fmt = get_format ("lvmpv" )
505521 if not fmt .supported or not fmt .formattable :
506- # FAIL: lvm tools are not available
507522 raise BlivetAnsibleError ("required tools for managing LVM are missing" )
508523
509524 return fmt
@@ -516,15 +531,14 @@ def _create(self):
516531 try :
517532 pool_device = self ._blivet .new_vg (name = self ._pool ['name' ], parents = members )
518533 except Exception :
519- # FAIL: failed to instantiate pool device
520534 raise BlivetAnsibleError ("failed to set up pool '%s'" % self ._pool ['name' ])
521535
522536 self ._blivet .create_device (pool_device )
523537 self ._device = pool_device
524538
525539
526540_BLIVET_POOL_TYPES = {
527- "disk " : BlivetPartitionPool ,
541+ "partition " : BlivetPartitionPool ,
528542 "lvm" : BlivetLVMPool
529543}
530544
@@ -660,6 +674,7 @@ def run_module():
660674 volumes = dict (type = 'list' ),
661675 packages_only = dict (type = 'bool' , required = False , default = False ),
662676 disklabel_type = dict (type = 'str' , required = False , default = None ),
677+ safe_mode = dict (type = 'bool' , required = False , default = True ),
663678 use_partitions = dict (type = 'bool' , required = False , default = True ))
664679
665680 # seed the result dict in the object
@@ -692,6 +707,12 @@ def run_module():
692707 global use_partitions
693708 use_partitions = module .params ['use_partitions' ]
694709
710+ global safe_mode
711+ safe_mode = module .params ['safe_mode' ]
712+
713+ global packages_only
714+ packages_only = module .params ['packages_only' ]
715+
695716 b = Blivet ()
696717 b .reset ()
697718 fstab = FSTab (b )
0 commit comments