New API: guestfs_device_name returning the drive name

For each drive added, return the name.  For example calling this with
index 0 will return the string "/dev/sda".  I called it
guestfs_device_name (not drive_name) for consistency with the existing
guestfs_device_index function.

You don't really need to call this function.  You can follow the
advice here:
https://libguestfs.org/guestfs.3.html#block-device-naming
and assume that drives are added with predictable names like
"/dev/sda", "/dev/sdb", etc.

However it's useful to expose the internal guestfs_int_drive_name
function since especially handling names beyond index 26 is tricky
(https://rwmj.wordpress.com/2011/01/09/how-are-linux-drives-named-beyond-drive-26-devsdz/)

Fixes: https://github.com/libguestfs/libguestfs/issues/80
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
This commit is contained in:
Richard W.M. Jones
2022-04-28 13:16:54 +01:00
parent f9babf8c04
commit ac00e603f8
2 changed files with 38 additions and 1 deletions

View File

@@ -31,6 +31,7 @@
#include <netdb.h>
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <libintl.h>
#include "c-ctype.h"
@@ -1084,3 +1085,17 @@ guestfs_impl_device_index (guestfs_h *g, const char *device)
error (g, _("%s: device not found"), device);
return r;
}
char *
guestfs_impl_device_name (guestfs_h *g, int index)
{
char drive_name[64];
if (index < 0 || index >= g->nr_drives) {
guestfs_int_error_errno (g, EINVAL, _("drive index out of range"));
return NULL;
}
guestfs_int_drive_name (index, drive_name);
return safe_asprintf (g, "/dev/sd%s", drive_name);
}