]> sourceware.org Git - lvm2.git/commitdiff
cleanup: refactor common code
authorZdenek Kabelac <zkabelac@redhat.com>
Mon, 27 May 2024 13:56:53 +0000 (15:56 +0200)
committerZdenek Kabelac <zkabelac@redhat.com>
Mon, 27 May 2024 14:35:00 +0000 (16:35 +0200)
Refactor shared code from _get_rootvg_dev() between vgchange
and vgimportdevices to get_rootvg_dev_uuid() in toollib.c

tools/toollib.c
tools/toollib.h
tools/vgchange.c
tools/vgimportdevices.c

index 433fd5de1d1ecbbeeec1cc10a2f160bafd98edfe..4738a2f6eae5df160f9da7540663637d53ca920d 100644 (file)
@@ -24,6 +24,7 @@
 #include <signal.h>
 #include <sys/wait.h>
 #include <sys/utsname.h>
+#include <mntent.h>
 
 #define report_log_ret_code(ret_code) report_current_object_cmdlog(REPORT_OBJECT_CMDLOG_NAME, \
                                        ((ret_code) == ECMD_PROCESSED) ? REPORT_OBJECT_CMDLOG_SUCCESS \
@@ -6024,3 +6025,45 @@ do_command:
 bad:
        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;
+}
index 2ab6ee05f4f882df77494a93cbc9a0dc7fa30b04..cce0b2d3d25cc49459ff0ce2e90063fb189b4903 100644 (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_rootvg_dev_uuid(struct cmd_context *cmd, char **dm_uuid_out);
+
 #endif
index 6a170c430304fba30ac79ab7700702d548a087dc..8aed62ce1c15add7eb263edc7bfa7ed80e5008fa 100644 (file)
@@ -17,7 +17,6 @@
 #include "lib/device/device_id.h"
 #include "lib/label/hints.h"
 #include "device_mapper/misc/dm-ioctl.h"
-#include <mntent.h>
 
 struct vgchange_params {
        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
  * ConditionPathExists=!/etc/lvm/devices/system.devices
  */
-
 static void _get_rootvg_dev(struct cmd_context *cmd, char **dm_uuid_out)
 {
        char path[PATH_MAX];
-       char dm_uuid[DM_UUID_LEN];
        struct stat info;
-       FILE *fme = NULL;
-       struct mntent *me;
-       int found = 0;
 
        if (cmd->enable_devices_file || devices_file_exists(cmd))
                return;
@@ -852,36 +846,8 @@ static void _get_rootvg_dev(struct cmd_context *cmd, char **dm_uuid_out)
        if (stat(path, &info) < 0)
                return;
 
-       if (!(fme = setmntent("/etc/mtab", "r")))
-               return;
-
-       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);
+       if (!get_rootvg_dev_uuid(cmd, dm_uuid_out))
+               stack;
 }
 
 static int _vgchange_autoactivation_setup(struct cmd_context *cmd,
index b4925bddc320db32e9f4660cfe3681b49d41a697..5de231eeb45047136f44979f6c47af80dbc6ea14 100644 (file)
@@ -19,7 +19,6 @@
 #include "lib/activate/activate.h"
 /* coverity[unnecessary_header] needed for MuslC */
 #include <sys/file.h>
-#include <mntent.h>
 
 struct vgimportdevices_params {
        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)
 {
        char path[PATH_MAX];
-       char dm_uuid[DM_UUID_LEN];
        struct stat info; 
-       FILE *fme = NULL;
-       struct mntent *me;
-       int found = 0;
 
        /*
         * 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;
        }
 
-       if (!(fme = setmntent("/etc/mtab", "r")))
+       if (!get_rootvg_dev_uuid(cmd, dm_uuid_out))
                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;
 }
 
This page took 0.049384 seconds and 5 git commands to generate.