@@ -193,61 +193,67 @@ def _get_dm_devices(self, f_major=None, f_minor=None) -> dict:
193193 return dm_devices
194194
195195
196- @check_dict ('autodetect_root' , value = True , log_level = 10 , message = "Skipping root autodetection, autodetect_root is not set." )
197- @check_dict ({'mounts' : {'root' : 'source' }}, unset = True , log_level = 30 , message = "Skipping root autodetection, root source is already set." )
198- def autodetect_root (self ) -> None :
199- """ Sets self['mounts']['root']['source'] based on the host mount. """
200- root_mount_info = _get_blkid_info (self , _get_mounts_source_device (self , '/' ))
201- self .logger .debug ("Detected root mount info: %s" % root_mount_info )
202-
196+ @check_dict ('autodetect_root_luks' , value = True , log_level = 10 , message = "Skipping LUKS autodetection, autodetect_root_luks is not set." )
197+ def _autodetect_root_luks (self , root_mount_info : dict ) -> None :
203198 # Check if the mount is under /dev/mapper or starts with /dev/dm-
204- if root_mount_info ['name' ].startswith ('/dev/mapper' ) or root_mount_info ['name' ].startswith ('/dev/dm-' ):
205- mount_loc = Path (root_mount_info ['name' ]).resolve ()
206- self .logger .debug ("Detected a device mapper mount: %s" % mount_loc )
207-
208- major , minor = mount_loc .stat ().st_rdev >> 8 , mount_loc .stat ().st_rdev & 0xFF
209- self .logger .debug ("[%s] Major: %s, Minor: %s" % (mount_loc , major , minor ))
210- dm_info = _get_dm_devices (self , major , minor )
211-
212- if len (dm_info ) > 1 : # there should only be one device mapper device associated with the mount
213- self .logger .error ("Device mapper devices: %s" % dm_info )
214- raise RuntimeError ("Multiple device mapper devices found for: %s" % mount_loc )
215-
216- mapped_name , dm_info = dm_info .popitem ()
217-
218- if mount_loc .name != dm_info ['name' ] and mount_loc .name != mapped_name :
219- raise ValueError ("Device mapper device name mismatch: %s != %s" % (mount_loc .name , dm_info ['name' ]))
199+ if not root_mount_info ['name' ].startswith ('/dev/mapper' ) and not root_mount_info ['name' ].startswith ('/dev/dm-' ):
200+ self .logger .debug ("Root mount is not a device mapper mount: %s" % root_mount_info ['name' ])
201+ return
202+ mount_loc = Path (root_mount_info ['name' ]).resolve ()
203+ self .logger .debug ("Detected a device mapper mount: %s" % mount_loc )
204+
205+ major , minor = mount_loc .stat ().st_rdev >> 8 , mount_loc .stat ().st_rdev & 0xFF
206+ self .logger .debug ("[%s] Major: %s, Minor: %s" % (mount_loc , major , minor ))
207+ dm_info = _get_dm_devices (self , major , minor )
208+
209+ if len (dm_info ) > 1 : # there should only be one device mapper device associated with the mount
210+ self .logger .error ("Device mapper devices: %s" % dm_info )
211+ raise RuntimeError ("Multiple device mapper devices found for: %s" % mount_loc )
212+
213+ mapped_name , dm_info = dm_info .popitem ()
214+
215+ if mount_loc .name != dm_info ['name' ] and mount_loc .name != mapped_name :
216+ raise ValueError ("Device mapper device name mismatch: %s != %s" % (mount_loc .name , dm_info ['name' ]))
217+
218+ if len (dm_info ['holders' ]) > 0 :
219+ self .logger .error ("Device mapper holders: %s" % dm_info ['holders' ])
220+ raise RuntimeError ("LUKS volumes should not have holders, potential LVM volume: %s" % mount_loc .name )
221+
222+ if len (dm_info ['slaves' ]) == 0 :
223+ raise RuntimeError ("No slaves found for device mapper device, unknown type: %s" % mount_loc .name )
224+ elif len (dm_info ['slaves' ]) > 1 :
225+ self .logger .error ("Device mapper slaves: %s" % dm_info ['slaves' ])
226+ raise RuntimeError ("Multiple slaves found for device mapper device, unknown type: %s" % mount_loc .name )
227+
228+ luks_mount = _get_blkid_info (self , Path ('/dev/' + dm_info ['slaves' ][0 ]))
229+ if luks_mount .get ('type' ) != 'crypto_LUKS' :
230+ if not luks_mount .get ('uuid' ):
231+ self .logger .error ("[%s] Unknown device mapper slave type: %s" % (dm_info ['slaves' ][0 ], luks_mount .get ('type' )))
232+ else :
233+ raise RuntimeError ("[%s] Unknown device mapper slave type: %s" % (dm_info ['slaves' ][0 ], luks_mount .get ('type' )))
220234
221- if len ( dm_info [ 'holders' ]) > 0 :
222- self .logger .error ( "Device mapper holders : %s" % dm_info [ 'holders ' ])
223- raise RuntimeError ( "LUKS volumes should not have holders, potential LVM volume: %s" % mount_loc . name )
235+ if 'ugrd.crypto.cryptsetup' not in self [ 'modules' ] :
236+ self .logger .info ( "Autodetected LUKS mount, enabling the cryptsetup module : %s" % luks_mount [ 'name ' ])
237+ self [ 'modules' ] = 'ugrd.crypto.cryptsetup'
224238
225- if len (dm_info ['slaves' ]) == 0 :
226- raise RuntimeError ("No slaves found for device mapper device, unknown type: %s" % mount_loc .name )
227- elif len (dm_info ['slaves' ]) > 1 :
228- self .logger .error ("Device mapper slaves: %s" % dm_info ['slaves' ])
229- raise RuntimeError ("Multiple slaves found for device mapper device, unknown type: %s" % mount_loc .name )
239+ if uuid := luks_mount .get ('uuid' ):
240+ self .logger .info ("[%s] Detected LUKS volume uuid: %s" % (mount_loc .name , uuid ))
241+ self ['cryptsetup' ] = {dm_info ['name' ]: {'uuid' : uuid }}
242+ elif partuuid := luks_mount .get ('partuuid' ):
243+ self .logger .info ("[%s] Detected LUKS volume partuuid: %s" % (mount_loc .name , partuuid ))
244+ self ['cryptsetup' ] = {dm_info ['name' ]: {'partuuid' : partuuid }}
230245
231- luks_mount = _get_blkid_info (self , Path ('/dev/' + dm_info ['slaves' ][0 ]))
232- if luks_mount .get ('type' ) != 'crypto_LUKS' :
233- if not luks_mount .get ('uuid' ):
234- self .logger .error ("[%s] Unknown device mapper slave type: %s" % (dm_info ['slaves' ][0 ], luks_mount .get ('type' )))
235- else :
236- raise RuntimeError ("[%s] Unknown device mapper slave type: %s" % (dm_info ['slaves' ][0 ], luks_mount .get ('type' )))
246+ self .logger .info ("[%s] Configuring cryptsetup for LUKS mount (%s) on: %s\n %s" %
247+ (mount_loc .name , dm_info ['name' ], luks_mount ['name' ], pretty_print (self ['cryptsetup' ])))
237248
238- if 'ugrd.crypto.cryptsetup' not in self ['modules' ]:
239- self .logger .info ("Autodetected LUKS mount, enabling the cryptsetup module: %s" % luks_mount ['name' ])
240- self ['modules' ] = 'ugrd.crypto.cryptsetup'
241249
242- if uuid := luks_mount .get ('uuid' ):
243- self .logger .info ("[%s] Detected LUKS volume uuid: %s" % (mount_loc .name , uuid ))
244- self ['cryptsetup' ] = {dm_info ['name' ]: {'uuid' : uuid }}
245- elif partuuid := luks_mount .get ('partuuid' ):
246- self .logger .info ("[%s] Detected LUKS volume partuuid: %s" % (mount_loc .name , partuuid ))
247- self ['cryptsetup' ] = {dm_info ['name' ]: {'partuuid' : partuuid }}
248-
249- self .logger .info ("[%s] Configuring cryptsetup for LUKS mount (%s) on: %s\n %s" %
250- (mount_loc .name , dm_info ['name' ], luks_mount ['name' ], pretty_print (self ['cryptsetup' ])))
250+ @check_dict ('autodetect_root' , value = True , log_level = 10 , message = "Skipping root autodetection, autodetect_root is not set." )
251+ @check_dict ({'mounts' : {'root' : 'source' }}, unset = True , log_level = 30 , message = "Skipping root autodetection, root source is already set." )
252+ def autodetect_root (self ) -> None :
253+ """ Sets self['mounts']['root']['source'] based on the host mount. """
254+ root_mount_info = _get_blkid_info (self , _get_mounts_source_device (self , '/' ))
255+ self .logger .debug ("Detected root mount info: %s" % root_mount_info )
256+ _autodetect_root_luks (self , root_mount_info )
251257
252258 mount_info = {'root' : {'type' : 'auto' , 'base_mount' : False }}
253259
0 commit comments