1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

cleanup: refactor common code

Refactor shared code from _get_rootvg_dev() between vgchange
and vgimportdevices to get_rootvg_dev_uuid() in toollib.c
This commit is contained in:
Zdenek Kabelac 2024-05-27 15:56:53 +02:00
parent 2e6e752c3a
commit 88e0060aa8
4 changed files with 48 additions and 67 deletions

View File

@ -24,6 +24,7 @@
#include <signal.h> #include <signal.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/utsname.h> #include <sys/utsname.h>
#include <mntent.h>
#define report_log_ret_code(ret_code) report_current_object_cmdlog(REPORT_OBJECT_CMDLOG_NAME, \ #define report_log_ret_code(ret_code) report_current_object_cmdlog(REPORT_OBJECT_CMDLOG_NAME, \
((ret_code) == ECMD_PROCESSED) ? REPORT_OBJECT_CMDLOG_SUCCESS \ ((ret_code) == ECMD_PROCESSED) ? REPORT_OBJECT_CMDLOG_SUCCESS \
@ -6024,3 +6025,45 @@ do_command:
bad: bad:
return 0; return 0;
} }
int get_rootvg_dev_uuid(struct cmd_context *cmd, char **dm_uuid_out)
{
char dm_uuid[DM_UUID_LEN];
struct stat info;
FILE *fme = NULL;
struct mntent *me;
int found = 0;
if (!(fme = setmntent("/etc/mtab", "r")))
return_0;
while ((me = getmntent(fme))) {
if ((me->mnt_dir[0] == '/') && (me->mnt_dir[1] == '\0')) {
found = 1;
break;
}
}
endmntent(fme);
if (!found)
return_0;
if (stat(me->mnt_dir, &info) < 0)
return_0;
if (!device_get_uuid(cmd, MAJOR(info.st_dev), MINOR(info.st_dev), dm_uuid, sizeof(dm_uuid)))
return_0;
log_debug("Found root dm_uuid %s", dm_uuid);
/* UUID_PREFIX = "LVM-" */
if (strncmp(dm_uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
return_0;
if (strlen(dm_uuid) < sizeof(UUID_PREFIX) - 1 + ID_LEN)
return_0;
*dm_uuid_out = dm_pool_strdup(cmd->mem, dm_uuid);
return 1;
}

View File

@ -244,4 +244,6 @@ int lvremove_single(struct cmd_context *cmd, struct logical_volume *lv,
int get_lvt_enum(struct logical_volume *lv); int get_lvt_enum(struct logical_volume *lv);
int get_rootvg_dev_uuid(struct cmd_context *cmd, char **dm_uuid_out);
#endif #endif

View File

@ -17,7 +17,6 @@
#include "lib/device/device_id.h" #include "lib/device/device_id.h"
#include "lib/label/hints.h" #include "lib/label/hints.h"
#include "device_mapper/misc/dm-ioctl.h" #include "device_mapper/misc/dm-ioctl.h"
#include <mntent.h>
struct vgchange_params { struct vgchange_params {
int lock_start_count; int lock_start_count;
@ -833,15 +832,10 @@ static int _vgchange_single(struct cmd_context *cmd, const char *vg_name,
* ExecStart=/usr/sbin/vgimportdevices --rootvg --auto * ExecStart=/usr/sbin/vgimportdevices --rootvg --auto
* ConditionPathExists=!/etc/lvm/devices/system.devices * ConditionPathExists=!/etc/lvm/devices/system.devices
*/ */
static void _get_rootvg_dev(struct cmd_context *cmd, char **dm_uuid_out) static void _get_rootvg_dev(struct cmd_context *cmd, char **dm_uuid_out)
{ {
char path[PATH_MAX]; char path[PATH_MAX];
char dm_uuid[DM_UUID_LEN];
struct stat info; struct stat info;
FILE *fme = NULL;
struct mntent *me;
int found = 0;
if (cmd->enable_devices_file || devices_file_exists(cmd)) if (cmd->enable_devices_file || devices_file_exists(cmd))
return; return;
@ -852,36 +846,8 @@ static void _get_rootvg_dev(struct cmd_context *cmd, char **dm_uuid_out)
if (stat(path, &info) < 0) if (stat(path, &info) < 0)
return; return;
if (!(fme = setmntent("/etc/mtab", "r"))) if (!get_rootvg_dev_uuid(cmd, dm_uuid_out))
return; stack;
while ((me = getmntent(fme))) {
if ((me->mnt_dir[0] == '/') && (me->mnt_dir[1] == '\0')) {
found = 1;
break;
}
}
endmntent(fme);
if (!found)
return;
if (stat(me->mnt_dir, &info) < 0)
return;
if (!device_get_uuid(cmd, MAJOR(info.st_dev), MINOR(info.st_dev), dm_uuid, sizeof(dm_uuid)))
return;
log_debug("Found root dm_uuid %s", dm_uuid);
/* UUID_PREFIX = "LVM-" */
if (strncmp(dm_uuid, UUID_PREFIX, 4))
return;
if (strlen(dm_uuid) < 4 + ID_LEN)
return;
*dm_uuid_out = dm_pool_strdup(cmd->mem, dm_uuid);
} }
static int _vgchange_autoactivation_setup(struct cmd_context *cmd, static int _vgchange_autoactivation_setup(struct cmd_context *cmd,

View File

@ -19,7 +19,6 @@
#include "lib/activate/activate.h" #include "lib/activate/activate.h"
/* coverity[unnecessary_header] needed for MuslC */ /* coverity[unnecessary_header] needed for MuslC */
#include <sys/file.h> #include <sys/file.h>
#include <mntent.h>
struct vgimportdevices_params { struct vgimportdevices_params {
uint32_t added_devices; uint32_t added_devices;
@ -102,11 +101,7 @@ static int _vgimportdevices_single(struct cmd_context *cmd,
static int _get_rootvg_dev(struct cmd_context *cmd, char **dm_uuid_out, int *skip) static int _get_rootvg_dev(struct cmd_context *cmd, char **dm_uuid_out, int *skip)
{ {
char path[PATH_MAX]; char path[PATH_MAX];
char dm_uuid[DM_UUID_LEN];
struct stat info; struct stat info;
FILE *fme = NULL;
struct mntent *me;
int found = 0;
/* /*
* When --auto is set, the command does nothing * When --auto is set, the command does nothing
@ -135,34 +130,9 @@ static int _get_rootvg_dev(struct cmd_context *cmd, char **dm_uuid_out, int *ski
cmd->device_ids_auto_import = 1; cmd->device_ids_auto_import = 1;
} }
if (!(fme = setmntent("/etc/mtab", "r"))) if (!get_rootvg_dev_uuid(cmd, dm_uuid_out))
return_0; return_0;
while ((me = getmntent(fme))) {
if ((me->mnt_dir[0] == '/') && (me->mnt_dir[1] == '\0')) {
found = 1;
break;
}
}
endmntent(fme);
if (!found)
return_0;
if (stat(me->mnt_dir, &info) < 0)
return_0;
if (!device_get_uuid(cmd, MAJOR(info.st_dev), MINOR(info.st_dev), dm_uuid, sizeof(dm_uuid)))
return_0;
/* UUID_PREFIX = "LVM-" */
if (strncmp(dm_uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1))
return_0;
if (strlen(dm_uuid) < sizeof(UUID_PREFIX) - 1 + ID_LEN)
return_0;
*dm_uuid_out = dm_pool_strdup(cmd->mem, dm_uuid);
return 1; return 1;
} }