|
|
65aaff |
From e3ebd50abde3b05db86c8965868c866152cd3287 Mon Sep 17 00:00:00 2001
|
|
|
65aaff |
From: "Richard W.M. Jones" <rjones@redhat.com>
|
|
|
65aaff |
Date: Thu, 28 Apr 2022 13:16:54 +0100
|
|
|
65aaff |
Subject: [PATCH] New API: guestfs_device_name returning the drive name
|
|
|
65aaff |
|
|
|
65aaff |
For each drive added, return the name. For example calling this with
|
|
|
65aaff |
index 0 will return the string "/dev/sda". I called it
|
|
|
65aaff |
guestfs_device_name (not drive_name) for consistency with the existing
|
|
|
65aaff |
guestfs_device_index function.
|
|
|
65aaff |
|
|
|
65aaff |
You don't really need to call this function. You can follow the
|
|
|
65aaff |
advice here:
|
|
|
65aaff |
https://libguestfs.org/guestfs.3.html#block-device-naming
|
|
|
65aaff |
and assume that drives are added with predictable names like
|
|
|
65aaff |
"/dev/sda", "/dev/sdb", etc.
|
|
|
65aaff |
|
|
|
65aaff |
However it's useful to expose the internal guestfs_int_drive_name
|
|
|
65aaff |
function since especially handling names beyond index 26 is tricky
|
|
|
65aaff |
(https://rwmj.wordpress.com/2011/01/09/how-are-linux-drives-named-beyond-drive-26-devsdz/)
|
|
|
65aaff |
|
|
|
65aaff |
Fixes: https://github.com/libguestfs/libguestfs/issues/80
|
|
|
65aaff |
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
|
|
|
65aaff |
(cherry picked from commit ac00e603f83802634f1d53b1629aee4670eaf31c)
|
|
|
65aaff |
---
|
|
|
65aaff |
generator/actions_core.ml | 24 +++++++++++++++++++++++-
|
|
|
65aaff |
lib/drives.c | 15 +++++++++++++++
|
|
|
65aaff |
2 files changed, 38 insertions(+), 1 deletion(-)
|
|
|
65aaff |
|
|
|
65aaff |
diff --git a/generator/actions_core.ml b/generator/actions_core.ml
|
|
|
65aaff |
index ce9ee39cc..dc12fdc33 100644
|
|
|
65aaff |
--- a/generator/actions_core.ml
|
|
|
65aaff |
+++ b/generator/actions_core.ml
|
|
|
65aaff |
@@ -737,7 +737,29 @@ returns the index of the device in the list of devices.
|
|
|
65aaff |
Index numbers start from 0. The named device must exist,
|
|
|
65aaff |
for example as a string returned from C<guestfs_list_devices>.
|
|
|
65aaff |
|
|
|
65aaff |
-See also C<guestfs_list_devices>, C<guestfs_part_to_dev>." };
|
|
|
65aaff |
+See also C<guestfs_list_devices>, C<guestfs_part_to_dev>,
|
|
|
65aaff |
+C<guestfs_device_name>." };
|
|
|
65aaff |
+
|
|
|
65aaff |
+ { defaults with
|
|
|
65aaff |
+ name = "device_name"; added = (1, 49, 1);
|
|
|
65aaff |
+ style = RString (RPlainString, "name"), [Int "index"], [];
|
|
|
65aaff |
+ tests = [
|
|
|
65aaff |
+ InitEmpty, Always, TestResult (
|
|
|
65aaff |
+ [["device_name"; "0"]], "STREQ (ret, \"/dev/sda\")"), [];
|
|
|
65aaff |
+ InitEmpty, Always, TestResult (
|
|
|
65aaff |
+ [["device_name"; "1"]], "STREQ (ret, \"/dev/sdb\")"), [];
|
|
|
65aaff |
+ InitEmpty, Always, TestLastFail (
|
|
|
65aaff |
+ [["device_name"; "99"]]), []
|
|
|
65aaff |
+ ];
|
|
|
65aaff |
+ shortdesc = "convert device index to name";
|
|
|
65aaff |
+ longdesc = "\
|
|
|
65aaff |
+This function takes a device index and returns the device
|
|
|
65aaff |
+name. For example index C<0> will return the string C</dev/sda>.
|
|
|
65aaff |
+
|
|
|
65aaff |
+The drive index must have been added to the handle.
|
|
|
65aaff |
+
|
|
|
65aaff |
+See also C<guestfs_list_devices>, C<guestfs_part_to_dev>,
|
|
|
65aaff |
+C<guestfs_device_index>." };
|
|
|
65aaff |
|
|
|
65aaff |
{ defaults with
|
|
|
65aaff |
name = "shutdown"; added = (1, 19, 16);
|
|
|
65aaff |
diff --git a/lib/drives.c b/lib/drives.c
|
|
|
65aaff |
index fd95308d2..a6179fc36 100644
|
|
|
65aaff |
--- a/lib/drives.c
|
|
|
65aaff |
+++ b/lib/drives.c
|
|
|
65aaff |
@@ -31,6 +31,7 @@
|
|
|
65aaff |
#include <netdb.h>
|
|
|
65aaff |
#include <arpa/inet.h>
|
|
|
65aaff |
#include <assert.h>
|
|
|
65aaff |
+#include <errno.h>
|
|
|
65aaff |
#include <libintl.h>
|
|
|
65aaff |
|
|
|
65aaff |
#include "c-ctype.h"
|
|
|
65aaff |
@@ -1084,3 +1085,17 @@ guestfs_impl_device_index (guestfs_h *g, const char *device)
|
|
|
65aaff |
error (g, _("%s: device not found"), device);
|
|
|
65aaff |
return r;
|
|
|
65aaff |
}
|
|
|
65aaff |
+
|
|
|
65aaff |
+char *
|
|
|
65aaff |
+guestfs_impl_device_name (guestfs_h *g, int index)
|
|
|
65aaff |
+{
|
|
|
65aaff |
+ char drive_name[64];
|
|
|
65aaff |
+
|
|
|
65aaff |
+ if (index < 0 || index >= g->nr_drives) {
|
|
|
65aaff |
+ guestfs_int_error_errno (g, EINVAL, _("drive index out of range"));
|
|
|
65aaff |
+ return NULL;
|
|
|
65aaff |
+ }
|
|
|
65aaff |
+
|
|
|
65aaff |
+ guestfs_int_drive_name (index, drive_name);
|
|
|
65aaff |
+ return safe_asprintf (g, "/dev/sd%s", drive_name);
|
|
|
65aaff |
+}
|
|
|
65aaff |
--
|
|
|
65aaff |
2.31.1
|
|
|
65aaff |
|