device usage based on devices file
The LVM devices file lists devices that lvm can use. The default
file is /etc/lvm/devices/system.devices, and the lvmdevices(8)
command is used to add or remove device entries. If the file
does not exist, or if lvm.conf includes use_devicesfile=0, then
lvm will not use a devices file. When the devices file is in use,
the regex filter is not used, and the filter settings in lvm.conf
or on the command line are ignored.
LVM records devices in the devices file using hardware-specific
IDs, such as the WWID, and attempts to use subsystem-specific
IDs for virtual device types. These device IDs are also written
in the VG metadata. When no hardware or virtual ID is available,
lvm falls back using the unstable device name as the device ID.
When devnames are used, lvm performs extra scanning to find
devices if their devname changes, e.g. after reboot.
When proper device IDs are used, an lvm command will not look
at devices outside the devices file, but when devnames are used
as a fallback, lvm will scan devices outside the devices file
to locate PVs on renamed devices. A config setting
search_for_devnames can be used to control the scanning for
renamed devname entries.
Related to the devices file, the new command option
--devices <devnames> allows a list of devices to be specified for
the command to use, overriding the devices file. The listed
devices act as a sort of devices file in terms of limiting which
devices lvm will see and use. Devices that are not listed will
appear to be missing to the lvm command.
Multiple devices files can be kept in /etc/lvm/devices, which
allows lvm to be used with different sets of devices, e.g.
system devices do not need to be exposed to a specific application,
and the application can use lvm on its own set of devices that are
not exposed to the system. The option --devicesfile <filename> is
used to select the devices file to use with the command. Without
the option set, the default system devices file is used.
Setting --devicesfile "" causes lvm to not use a devices file.
An existing, empty devices file means lvm will see no devices.
The new command vgimportdevices adds PVs from a VG to the devices
file and updates the VG metadata to include the device IDs.
vgimportdevices -a will import all VGs into the system devices file.
LVM commands run by dmeventd not use a devices file by default,
and will look at all devices on the system. A devices file can
be created for dmeventd (/etc/lvm/devices/dmeventd.devices) If
this file exists, lvm commands run by dmeventd will use it.
Internal implementaion:
- device_ids_read - read the devices file
. add struct dev_use (du) to cmd->use_devices for each devices file entry
- dev_cache_scan - get /dev entries
. add struct device (dev) to dev_cache for each device on the system
- device_ids_match - match devices file entries to /dev entries
. match each du on cmd->use_devices to a dev in dev_cache, using device ID
. on match, set du->dev, dev->id, dev->flags MATCHED_USE_ID
- label_scan - read lvm headers and metadata from devices
. filters are applied, those that do not need data from the device
. filter-deviceid skips devs without MATCHED_USE_ID, i.e.
skips /dev entries that are not listed in the devices file
. read lvm label from dev
. filters are applied, those that use data from the device
. read lvm metadata from dev
. add info/vginfo structs for PVs/VGs (info is "lvmcache")
- device_ids_find_renamed_devs - handle devices with unstable devname ID
where devname changed
. this step only needed when devs do not have proper device IDs,
and their dev names change, e.g. after reboot sdb becomes sdc.
. detect incorrect match because PVID in the devices file entry
does not match the PVID found when the device was read above
. undo incorrect match between du and dev above
. search system devices for new location of PVID
. update devices file with new devnames for PVIDs on renamed devices
. label_scan the renamed devs
- continue with command processing
2020-06-23 21:25:41 +03:00
/*
* Copyright ( C ) 2020 Red Hat , Inc . All rights reserved .
*
* This file is part of LVM2 .
*
* This copyrighted material is made available to anyone wishing to use ,
* modify , copy , or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License v .2 .1 .
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program ; if not , write to the Free Software Foundation ,
* Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
*/
# include "tools.h"
# include "lib/cache/lvmcache.h"
# include "lib/device/device_id.h"
struct vgimportdevices_params {
uint32_t added_devices ;
} ;
static int _vgimportdevices_single ( struct cmd_context * cmd ,
const char * vg_name ,
struct volume_group * vg ,
struct processing_handle * handle )
{
struct vgimportdevices_params * vp = ( struct vgimportdevices_params * ) handle - > custom_handle ;
struct pv_list * pvl ;
struct physical_volume * pv ;
int update_vg = 1 ;
int updated_pvs = 0 ;
const char * idtypestr = NULL ; /* deviceidtype_ARG ? */
dm_list_iterate_items ( pvl , & vg - > pvs ) {
if ( is_missing_pv ( pvl - > pv ) | | ! pvl - > pv - > dev ) {
log_error ( " Not importing devices for VG %s with missing PV %32s. " ,
vg - > name , ( const char * ) & pvl - > pv - > id . uuid ) ;
goto bad ;
}
}
/*
* We want to allow importing devices of foreign and shared
* VGs , but we do not want to update device_ids in those VGs .
*
* If - - foreign is set , then foreign VGs will be passed
* to this function ; add devices but don ' t update vg .
* shared VGs are passed to this function ; add devices
* and do not update .
*/
if ( vg_is_foreign ( vg ) | | vg_is_shared ( vg ) )
update_vg = 0 ;
dm_list_iterate_items ( pvl , & vg - > pvs ) {
pv = pvl - > pv ;
if ( ! idtypestr & & pv - > device_id_type )
idtypestr = pv - > device_id_type ;
device_id_add ( cmd , pv - > dev , ( const char * ) & pvl - > pv - > id . uuid , idtypestr , NULL ) ;
vp - > added_devices + + ;
/* We could skip update if the device_id has not changed. */
if ( ! update_vg )
continue ;
updated_pvs + + ;
}
if ( updated_pvs ) {
if ( ! vg_write ( vg ) | | ! vg_commit ( vg ) )
goto_bad ;
}
return ECMD_PROCESSED ;
bad :
return ECMD_FAILED ;
}
/*
* This command always scans all devices on the system ,
* any pre - existing devices_file does not limit the scope .
*
* This command adds the VG ' s devices to whichever
* devices_file is set in config or command line .
* If devices_file doesn ' t exist , it ' s created .
*
* If devices_file is " " then this file will scan all devices
* and show the devices that it would otherwise have added to
* the devices_file . The VG is not updated with device_ids .
*
* This command updates the VG metadata to add device_ids
* ( if the metadata is missing them ) , unless an option is
* set to skip that , e . g . - - nodeviceidupdate ?
*
* If the VG found has a foreign system ID then an error
* will be printed . To import devices from a foreign VG :
* vgimportdevices - - foreign - a
* vgimportdevices - - foreign VG
*
* If there are duplicate VG names it will do nothing .
*
* If there are duplicate PVIDs related to VG it will do nothing ,
* the user would need to add the PVs they want with lvmdevices - - add .
*
* vgimportdevices - a ( no vg arg ) will import all accesible VGs .
*/
int vgimportdevices ( struct cmd_context * cmd , int argc , char * * argv )
{
struct vgimportdevices_params vp = { 0 } ;
struct processing_handle * handle ;
2021-03-09 17:51:11 +03:00
int ret = ECMD_FAILED ;
device usage based on devices file
The LVM devices file lists devices that lvm can use. The default
file is /etc/lvm/devices/system.devices, and the lvmdevices(8)
command is used to add or remove device entries. If the file
does not exist, or if lvm.conf includes use_devicesfile=0, then
lvm will not use a devices file. When the devices file is in use,
the regex filter is not used, and the filter settings in lvm.conf
or on the command line are ignored.
LVM records devices in the devices file using hardware-specific
IDs, such as the WWID, and attempts to use subsystem-specific
IDs for virtual device types. These device IDs are also written
in the VG metadata. When no hardware or virtual ID is available,
lvm falls back using the unstable device name as the device ID.
When devnames are used, lvm performs extra scanning to find
devices if their devname changes, e.g. after reboot.
When proper device IDs are used, an lvm command will not look
at devices outside the devices file, but when devnames are used
as a fallback, lvm will scan devices outside the devices file
to locate PVs on renamed devices. A config setting
search_for_devnames can be used to control the scanning for
renamed devname entries.
Related to the devices file, the new command option
--devices <devnames> allows a list of devices to be specified for
the command to use, overriding the devices file. The listed
devices act as a sort of devices file in terms of limiting which
devices lvm will see and use. Devices that are not listed will
appear to be missing to the lvm command.
Multiple devices files can be kept in /etc/lvm/devices, which
allows lvm to be used with different sets of devices, e.g.
system devices do not need to be exposed to a specific application,
and the application can use lvm on its own set of devices that are
not exposed to the system. The option --devicesfile <filename> is
used to select the devices file to use with the command. Without
the option set, the default system devices file is used.
Setting --devicesfile "" causes lvm to not use a devices file.
An existing, empty devices file means lvm will see no devices.
The new command vgimportdevices adds PVs from a VG to the devices
file and updates the VG metadata to include the device IDs.
vgimportdevices -a will import all VGs into the system devices file.
LVM commands run by dmeventd not use a devices file by default,
and will look at all devices on the system. A devices file can
be created for dmeventd (/etc/lvm/devices/dmeventd.devices) If
this file exists, lvm commands run by dmeventd will use it.
Internal implementaion:
- device_ids_read - read the devices file
. add struct dev_use (du) to cmd->use_devices for each devices file entry
- dev_cache_scan - get /dev entries
. add struct device (dev) to dev_cache for each device on the system
- device_ids_match - match devices file entries to /dev entries
. match each du on cmd->use_devices to a dev in dev_cache, using device ID
. on match, set du->dev, dev->id, dev->flags MATCHED_USE_ID
- label_scan - read lvm headers and metadata from devices
. filters are applied, those that do not need data from the device
. filter-deviceid skips devs without MATCHED_USE_ID, i.e.
skips /dev entries that are not listed in the devices file
. read lvm label from dev
. filters are applied, those that use data from the device
. read lvm metadata from dev
. add info/vginfo structs for PVs/VGs (info is "lvmcache")
- device_ids_find_renamed_devs - handle devices with unstable devname ID
where devname changed
. this step only needed when devs do not have proper device IDs,
and their dev names change, e.g. after reboot sdb becomes sdc.
. detect incorrect match because PVID in the devices file entry
does not match the PVID found when the device was read above
. undo incorrect match between du and dev above
. search system devices for new location of PVID
. update devices file with new devnames for PVIDs on renamed devices
. label_scan the renamed devs
- continue with command processing
2020-06-23 21:25:41 +03:00
if ( arg_is_set ( cmd , foreign_ARG ) )
cmd - > include_foreign_vgs = 1 ;
cmd - > include_shared_vgs = 1 ;
/* So that we can warn about this. */
cmd - > handles_missing_pvs = 1 ;
if ( ! lock_global ( cmd , " ex " ) )
return ECMD_FAILED ;
/*
* Prepare devices file preemptively because the error path for this
* case from process_each is not as clean .
*/
if ( ! setup_devices_file ( cmd ) ) {
log_error ( " Failed to set up devices file. " ) ;
return ECMD_FAILED ;
}
if ( ! cmd - > enable_devices_file ) {
log_error ( " Devices file not enabled. " ) ;
return ECMD_FAILED ;
}
if ( ! devices_file_exists ( cmd ) & & ! devices_file_touch ( cmd ) ) {
log_error ( " Failed to create devices file. " ) ;
return ECMD_FAILED ;
}
/*
* The hint file is associated with the default / system devices file ,
* so don ' t clear hints when using a different - - devicesfile .
*/
if ( ! cmd - > devicesfile )
clear_hint_file ( cmd ) ;
if ( ! ( handle = init_processing_handle ( cmd , NULL ) ) ) {
log_error ( " Failed to initialize processing handle. " ) ;
goto out ;
}
handle - > custom_handle = & vp ;
/*
* import is a case where we do not want to be limited by an existing
* devices file because we want to search outside the devices file for
* new devs to add to it , but we do want devices file entries on
* use_devices so we can update and write out that list .
*
* Ususally when devices file is enabled , we use filter - deviceid and
* skip filter - regex . In this import case it ' s reversed , and we skip
* filter - deviceid and use filter - regex .
*/
cmd - > filter_deviceid_skip = 1 ;
cmd - > filter_regex_with_devices_file = 1 ;
cmd - > create_edit_devices_file = 1 ;
/*
* For each VG :
* device_id_add ( ) each PV in the VG
* update device_ids in the VG ( potentially )
*
* process_each_vg - > label_scan - > setup_devices
* setup_devices sees create_edit_devices_file is 1 ,
* so it does lock_devices_file ( EX ) , then it creates / reads
* the devices file , then each device_id_add happens
* above , and then device_ids_write happens below .
*/
ret = process_each_vg ( cmd , argc , argv , NULL , NULL , READ_FOR_UPDATE ,
0 , handle , _vgimportdevices_single ) ;
if ( ret = = ECMD_FAILED )
goto out ;
if ( ! vp . added_devices ) {
log_print ( " No devices to add. " ) ;
goto out ;
}
if ( ! device_ids_write ( cmd ) ) {
log_print ( " Failed to update devices file. " ) ;
ret = ECMD_FAILED ;
goto out ;
}
log_print ( " Added %u devices to devices file. " , vp . added_devices ) ;
out :
destroy_processing_handle ( cmd , handle ) ;
return ret ;
}