11__author__ = 'desultory'
2- __version__ = '4.1 .0'
2+ __version__ = '4.2 .0'
33
44from pathlib import Path
55from zenlib .util import check_dict , pretty_print
@@ -245,55 +245,55 @@ def get_dm_info(self) -> dict:
245245
246246@check_dict ('hostonly' , value = True , log_level = 30 , message = "Skipping device mapper autodetection, hostonly mode is disabled." )
247247def _autodetect_dm (self , mountpoint ) -> None :
248- """ Autodetects device mapper root config. """
249- self .logger .debug ("Detecting device mapper info for mountpoint: %s" , mountpoint )
250-
251- source_device = self ['_mounts' ][mountpoint ]['device' ]
252-
253- root_mount_info = self ['_blkid_info' ].get (source_device )
254-
255- if not root_mount_info :
256- root_mount_info = self ['_blkid_info' ].get (f"/dev/{ self ._dm_info [str (mountpoint ).replace ('/dev/' , '' )]['slaves' ][0 ]} " )
248+ """
249+ Autodetects device mapper config given a device.
250+ If no device is passed, it will attempt to autodetect based on the mountpoint.
251+ """
252+ if mountpoint in self ['_mounts' ]:
253+ source_device = self ['_mounts' ][mountpoint ]['device' ]
254+ else :
255+ source_device = "/dev/mapper/" + self ['_dm_info' ][mountpoint .split ('/' )[- 1 ]]['name' ]
257256
258- if not root_mount_info :
259- raise FileNotFoundError ("Unable to find blkdid info for mount point : %s" % mountpoint )
257+ if not source_device or source_device not in self [ '_blkid_info' ] :
258+ raise FileNotFoundError ("[%s] No blkdid info for dm device : %s" % ( mountpoint , source_device ) )
260259
261260 if not source_device .startswith ('/dev/mapper' ) and not source_device .startswith ('/dev/dm-' ):
262- self .logger .debug ("[%s] Mount is not a device mapper mount: %s" % ( source_device , root_mount_info ) )
261+ self .logger .debug ("Mount is not a device mapper mount: %s" % source_device )
263262 return
263+
264264 self .logger .info ("Detected a device mapper mount: %s" % source_device )
265265 source_device = Path (source_device )
266266 major , minor = source_device .stat ().st_rdev >> 8 , source_device .stat ().st_rdev & 0xFF
267267 self .logger .debug ("[%s] Major: %s, Minor: %s" % (source_device , major , minor ))
268268
269269 for name , info in self ['_dm_info' ].items ():
270270 if info ['major' ] == str (major ) and info ['minor' ] == str (minor ):
271- mapped_name = name
271+ dm_num = name
272272 break
273273 else :
274- raise RuntimeError ("Unable to find device mapper device: %s" % source_device )
274+ raise RuntimeError ("[%s] Unable to find device mapper device with maj : %s min: %s " % ( source_device , major , minor ) )
275275
276- if len (self ._dm_info [mapped_name ]['slaves' ]) == 0 :
276+ if len (self ._dm_info [dm_num ]['slaves' ]) == 0 :
277277 raise RuntimeError ("No slaves found for device mapper device, unknown type: %s" % source_device .name )
278- slave_source = self ._dm_info [mapped_name ]['slaves' ][0 ]
278+ slave_source = self ._dm_info [dm_num ]['slaves' ][0 ]
279279
280280 try :
281- dm_mount = self ['_blkid_info' ][f"/dev/{ slave_source } " ]
281+ dm_info = self ['_blkid_info' ][f"/dev/{ slave_source } " ]
282282 except KeyError :
283283 if slave_source in self ['_dm_info' ]:
284- dm_mount = self ['_blkid_info' ][f"/dev/mapper/{ self ._dm_info [slave_source ]['name' ]} " ]
284+ dm_info = self ['_blkid_info' ][f"/dev/mapper/{ self ._dm_info [slave_source ]['name' ]} " ]
285285 else :
286286 raise KeyError ("Unable to find blkid info for device mapper slave: %s" % slave_source )
287- if source_device .name != self ._dm_info [mapped_name ]['name' ] and source_device .name != mapped_name :
288- raise ValueError ("Device mapper device name mismatch: %s != %s" % (source_device .name , self ._dm_info [mapped_name ]['name' ]))
289-
290- self .logger .debug ("[%s] Device mapper info: %s" % (source_device .name , self ._dm_info [mapped_name ]))
291- if dm_mount .get ('type' ) == 'crypto_LUKS' or source_device .name in self .get ('cryptsetup' , {}):
292- return autodetect_root_luks (self , source_device , mapped_name , dm_mount )
293- elif dm_mount .get ('type' ) == 'LVM2_member' :
294- return autodetect_root_lvm (self , source_device , mapped_name , dm_mount )
287+ if source_device .name != self ._dm_info [dm_num ]['name' ] and source_device .name != dm_num :
288+ raise ValueError ("Device mapper device name mismatch: %s != %s" % (source_device .name , self ._dm_info [dm_num ]['name' ]))
289+
290+ self .logger .debug ("[%s] Device mapper info: %s" % (source_device .name , self ._dm_info [dm_num ]))
291+ if dm_info .get ('type' ) == 'crypto_LUKS' or source_device .name in self .get ('cryptsetup' , {}):
292+ return autodetect_luks (self , source_device , dm_num , dm_info )
293+ elif dm_info .get ('type' ) == 'LVM2_member' :
294+ return autodetect_root_lvm (self , source_device , dm_num , dm_info )
295295 else :
296- raise RuntimeError ("Unknown device mapper device type: %s" % dm_mount .get ('type' ))
296+ raise RuntimeError ("Unknown device mapper device type: %s" % dm_info .get ('type' ))
297297
298298
299299@check_dict ('autodetect_root_dm' , value = True , log_level = 10 , message = "Skipping device mapper autodetection, autodetect_root_dm is not set." )
@@ -304,58 +304,57 @@ def autodetect_root_dm(self) -> None:
304304
305305@check_dict ('autodetect_root_lvm' , value = True , log_level = 10 , message = "Skipping LVM autodetection, autodetect_root_lvm is not set." )
306306@check_dict ('hostonly' , value = True , log_level = 30 , message = "Skipping LVM autodetection, hostonly mode is disabled." )
307- def autodetect_root_lvm (self , mount_loc , mapped_name , lvm_mount ) -> None :
307+ def autodetect_root_lvm (self , mount_loc , dm_num , dm_info ) -> None :
308308 """ Autodetects LVM mounts and sets the lvm config. """
309309 if 'ugrd.fs.lvm' not in self ['modules' ]:
310310 self .logger .info ("Autodetected LVM mount, enabling the lvm module." )
311311 self ['modules' ] = 'ugrd.fs.lvm'
312312
313- if uuid := lvm_mount .get ('uuid' ):
313+ if uuid := dm_info .get ('uuid' ):
314314 self .logger .info ("[%s] LVM volume uuid: %s" % (mount_loc .name , uuid ))
315- self ['lvm' ] = {self ._dm_info [mapped_name ]['name' ]: {'uuid' : uuid }}
315+ self ['lvm' ] = {self ._dm_info [dm_num ]['name' ]: {'uuid' : uuid }}
316316 else :
317317 raise ValueError ("Failed to autodetect LVM volume uuid: %s" % mount_loc .name )
318318
319319 # Check if the LVM volume is part of a LUKS volume
320- for slave in self ._dm_info [mapped_name ]['slaves' ]:
320+ for slave in self ._dm_info [dm_num ]['slaves' ]:
321321 _autodetect_dm (self , '/dev/' + slave )
322322
323323
324324@check_dict ('autodetect_root_luks' , value = True , log_level = 10 , message = "Skipping LUKS autodetection, autodetect_root_luks is not set." )
325325@check_dict ('hostonly' , value = True , log_level = 30 , message = "Skipping LUKS autodetection, hostonly mode is disabled." )
326- def autodetect_root_luks (self , mount_loc , mapped_name , luks_mount ) -> None :
326+ def autodetect_luks (self , mount_loc , dm_num , dm_info ) -> None :
327327 """ Autodetects LUKS mounts and sets the cryptsetup config. """
328328 if 'ugrd.crypto.cryptsetup' not in self ['modules' ]:
329329 self .logger .info ("Autodetected LUKS mount, enabling the cryptsetup module: %s" % mount_loc .name )
330330 self ['modules' ] = 'ugrd.crypto.cryptsetup'
331331
332- if 'cryptsetup' in self and any (mount_type in self ['cryptsetup' ].get (self ._dm_info [mapped_name ]['name' ], []) for mount_type in SOURCE_TYPES ):
333- self .logger .warning ("Skipping LUKS autodetection, cryptsetup config already set: %s" % self ['cryptsetup' ][self ._dm_info [mapped_name ]['name' ]])
332+ if 'cryptsetup' in self and any (mount_type in self ['cryptsetup' ].get (self ._dm_info [dm_num ]['name' ], []) for mount_type in SOURCE_TYPES ):
333+ self .logger .warning ("Skipping LUKS autodetection, cryptsetup config already set: %s" % self ['cryptsetup' ][self ._dm_info [dm_num ]['name' ]])
334334 return
335335
336- if len (self ._dm_info [mapped_name ]['slaves' ]) > 1 :
337- self .logger .error ("Device mapper slaves: %s" % self ._dm_info [mapped_name ]['slaves' ])
336+ if len (self ._dm_info [dm_num ]['slaves' ]) > 1 :
337+ self .logger .error ("Device mapper slaves: %s" % self ._dm_info [dm_num ]['slaves' ])
338338 raise RuntimeError ("Multiple slaves found for device mapper device, unknown type: %s" % mount_loc .name )
339339
340- luks_mount = self ['_blkid_info' ][f"/dev/{ self ._dm_info [mapped_name ]['slaves' ][0 ]} " ]
341- self .logger .debug ("[%s] LUKS mount info: %s" % (mapped_name , luks_mount ))
342- if luks_mount .get ('type' ) != 'crypto_LUKS' :
343- if not luks_mount .get ('uuid' ): # No uuid will be defined if there are detached headers
340+ dm_type = dm_info .get ('type' )
341+ if dm_type != 'crypto_LUKS' :
342+ if not dm_info .get ('uuid' ): # No uuid will be defined if there are detached headers
344343 if not self ['cryptsetup' ][mount_loc .name ].get ('header_file' ):
345- raise ValueError ("[%s] Unknown LUKS mount type, if using detached headers, specify 'header_file' : %s" % (mount_loc .name , luks_mount . get ( 'type' ) ))
344+ raise ValueError ("[%s] Unknown LUKS mount type: %s" % (mount_loc .name , dm_type ))
346345 else : # If there is some uuid and it's not LUKS, that's a problem
347- raise RuntimeError ("[%s] Unknown device mapper slave type: %s" % (self ._dm_info [mapped_name ]['slaves' ][0 ], luks_mount . get ( 'type' ) ))
346+ raise RuntimeError ("[%s] Unknown device mapper slave type: %s" % (self ._dm_info [dm_num ]['slaves' ][0 ], dm_type ))
348347
349348 # Configure cryptsetup based on the LUKS mount
350- if uuid := luks_mount .get ('uuid' ):
349+ if uuid := dm_info .get ('uuid' ):
351350 self .logger .info ("[%s] LUKS volume uuid: %s" % (mount_loc .name , uuid ))
352- self ['cryptsetup' ] = {self ._dm_info [mapped_name ]['name' ]: {'uuid' : uuid }}
353- elif partuuid := luks_mount .get ('partuuid' ):
351+ self ['cryptsetup' ] = {self ._dm_info [dm_num ]['name' ]: {'uuid' : uuid }}
352+ elif partuuid := dm_info .get ('partuuid' ):
354353 self .logger .info ("[%s] LUKS volume partuuid: %s" % (mount_loc .name , partuuid ))
355- self ['cryptsetup' ] = {self ._dm_info [mapped_name ]['name' ]: {'partuuid' : partuuid }}
354+ self ['cryptsetup' ] = {self ._dm_info [dm_num ]['name' ]: {'partuuid' : partuuid }}
356355
357356 self .logger .info ("[%s] Configuring cryptsetup for LUKS mount (%s) on: %s\n %s" %
358- (mount_loc .name , self ._dm_info [mapped_name ]['name' ], mapped_name , pretty_print (self ['cryptsetup' ])))
357+ (mount_loc .name , self ._dm_info [dm_num ]['name' ], dm_num , pretty_print (self ['cryptsetup' ])))
359358
360359
361360@check_dict ('autodetect_root' , value = True , log_level = 20 , message = "Skipping root autodetection, autodetect_root is disabled." )
0 commit comments