[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH v2 2/3] qga: add implementation of guest-get-disks for Linux
From: |
Tomáš Golembiovský |
Subject: |
Re: [PATCH v2 2/3] qga: add implementation of guest-get-disks for Linux |
Date: |
Tue, 6 Oct 2020 10:31:41 +0200 |
On Tue, Sep 29, 2020 at 07:22:00PM +0400, Marc-André Lureau wrote:
> Hi
>
> On Mon, Sep 7, 2020 at 1:17 PM Tomáš Golembiovský <tgolembi@redhat.com>
> wrote:
>
> > The command lists all disks (real and virtual) as well as disk
> > partitions. For each disk the list of slave disks is also listed and
> > /dev path is used as a handle so it can be matched with "name" filed of
> >
>
> field
>
> other returned disk entries. For disk partitions the "slave" list is
> > populated with the the parent device for easier tracking of hierarchy.
> >
> > Example output:
> > {
> > "return": [
> > ...
> > {
> > "name": "/dev/dm-0",
> > "partition": false,
> > "slaves": [
> > "/dev/sda2"
> > ],
> > "alias": "luks-7062202e-5b9b-433e-81e8-6628c40da9f7"
> > },
> > {
> > "name": "/dev/sda2",
> > "partition": true,
> > "slaves": [
> > "/dev/sda"
> > ]
> > },
> > {
> > "name": "/dev/sda",
> > "partition": false,
> > "address": {
> > "serial": "SAMSUNG_MZ7LN512HCHP-000L1_S1ZKNXAG822493",
> > "bus-type": "sata",
> > ...
> > "dev": "/dev/sda",
> > "target": 0
> > },
> > "slaves": []
> > },
> > ...
> > ]
> > }
> >
> > Signed-off-by: Tomáš Golembiovský <tgolembi@redhat.com>
> > ---
> > qga/commands-posix.c | 247 +++++++++++++++++++++++++++++++++++++++++--
> > 1 file changed, 240 insertions(+), 7 deletions(-)
> >
> > diff --git a/qga/commands-posix.c b/qga/commands-posix.c
> > index f99731af51..3babc25c09 100644
> > --- a/qga/commands-posix.c
> > +++ b/qga/commands-posix.c
> > @@ -62,6 +62,9 @@ extern char **environ;
> > #endif
> > #endif
> >
> > +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GuestFilesystemInfo,
> > + qapi_free_GuestFilesystemInfo)
> > +
> >
>
> This will now conflict with qapi-gen generated headers.
>
> static void ga_wait_child(pid_t pid, int *status, Error **errp)
> > {
> > pid_t rpid;
> > @@ -1150,6 +1153,21 @@ static void
> > build_guest_fsinfo_for_virtual_device(char const *syspath,
> > closedir(dir);
> > }
> >
> > +static bool is_disk_virtual(const char *devpath, Error **errp)
> > +{
> > + g_autofree char *syspath = realpath(devpath, NULL);
> > +
> > + if (!syspath) {
> > + error_setg_errno(errp, errno, "realpath(\"%s\")", devpath);
> >
> + return false;
> > + }
> > + if (strstr(syspath, "/devices/virtual/block/")) {
> > + return true;
> > + } else {
> > + return false;
> > + }
> >
>
> simply to "return strstr(syspath, "/devices/virtual/block/") != NULL;" ?
> (Or strstr(syspath, "/devices/virtual/block/") ? true : false )
>
> +}
> > +
> > /* Dispatch to functions for virtual/real device */
> > static void build_guest_fsinfo_for_device(char const *devpath,
> > GuestFilesystemInfo *fs,
> > @@ -1168,6 +1186,7 @@ static void build_guest_fsinfo_for_device(char const
> > *devpath,
> >
> > g_debug(" parse sysfs path '%s'", syspath);
> >
> > + /* TODO: use is_disk_virtual() */
> >
>
> just do it, no?
It's great that I put a note there otherwise I might have forgotten to
do it. ;)
>
> if (strstr(syspath, "/devices/virtual/block/")) {
> > build_guest_fsinfo_for_virtual_device(syspath, fs, errp);
> > } else {
> > @@ -1177,6 +1196,225 @@ static void build_guest_fsinfo_for_device(char
> > const *devpath,
> > free(syspath);
> > }
> >
> > +#ifdef CONFIG_LIBUDEV
> > +
> > +/*
> > + * Wrapper around build_guest_fsinfo_for_device() for getting just
> > + * the disk address.
> > + */
> > +static GuestDiskAddress *get_disk_address(const char *syspath, Error
> > **errp)
> > +{
> > + g_autoptr(GuestFilesystemInfo) fs = NULL;
> > +
> > + fs = g_new0(GuestFilesystemInfo, 1);
> >
>
> Heap allocation / auto wasn't really necessary here, but ok.
I used it so that qapi_free_GuestFilesystemInfo() is called on function
exit in all cases. I am not sure if I could do that if `fs` were on the
stack.
>
>
> > + build_guest_fsinfo_for_device(syspath, fs, errp);
> > + if (fs->disk != NULL) {
> > + return g_steal_pointer(&fs->disk->value);
> > + }
> > + return NULL;
> >
>
> Could also be a onliner, but perhaps less readable.
Yeah, I prefer it this way.
>
> +}
> > +
> > +static char *get_alias_for_syspath(const char *syspath)
> > +{
> > + struct udev *udev = NULL;
> > + struct udev_device *udevice = NULL;
> > + char *ret = NULL;
> > +
> > + udev = udev_new();
> >
>
> I would have g_return_val_if_fail(udev != NULL, NULL); here as,
>
> + udevice = udev_device_new_from_syspath(udev, syspath);
> >
>
> udev_device_new_from_syspath() might crash otherwise.
That is probably true. This may require fixes at other places too.
>
>
> > + if (udev == NULL || udevice == NULL) {
> > + g_debug("failed to query udev");
> > + } else {
> > + const char *alias = udev_device_get_property_value(
> > + udevice, "DM_NAME");
> > + if (alias != NULL && *alias != 0) {
> > + ret = g_strdup(alias);
> >
>
> g_strdup() works fine with NULL. Is there "" empty aliases? Why not report
> them too?
NULL means an error and empty alias means there is no alias. I will put
that in a comment there. In QAPI we have alias as optional which seems
preferable rather than always returning empty string.
>
> + }
> > + }
> > +
> > + udev_unref(udev);
> > + udev_device_unref(udevice);
> > + return ret;
> > +}
> > +
> > +static char *get_device_for_syspath(const char *syspath)
> > +{
> > + struct udev *udev = NULL;
> > + struct udev_device *udevice = NULL;
> > + char *ret = NULL;
> > +
> > + udev = udev_new();
> > + udevice = udev_device_new_from_syspath(udev, syspath);
> > + if (udev == NULL || udevice == NULL) {
> >
>
> Same as above
>
> + g_debug("failed to query udev");
> > + } else {
> > + ret = g_strdup(udev_device_get_devnode(udevice));
> > + }
> > + udev_unref(udev);
> > + udev_device_unref(udevice);
> > + return ret;
> > +}
> > +
> >
> +GuestDiskInfoList *qmp_guest_get_disks(Error **errp)
> > +{
> > + GuestDiskInfoList *item, *ret = NULL;
> > + GuestDiskInfo *disk, *partition;
> > + DIR *dp = NULL;
> > + struct dirent *de = NULL;
> > +
> > + g_debug("listing /sys/block directory");
> > + dp = opendir("/sys/block");
> > + if (dp == NULL) {
> > + error_setg_errno(errp, errno, "Can't open directory
> > \"/sys/block\"");
> > + return NULL;
> > + }
> > + while ((de = readdir(dp)) != NULL) {
> > + g_autofree char *disk_dir = NULL, *line = NULL,
> > + *size_dir = NULL, *slaves_dir = NULL;
> > + struct dirent *de_disk, *de_slaves;
> > + DIR *dp_disk = NULL, *dp_slaves = NULL;
> > + FILE *fp = NULL;
> > + size_t n = 0;
> > + Error *local_err = NULL;
> > + if (de->d_type != DT_LNK) {
> > + g_debug(" skipping entry: %s", de->d_name);
> > + continue;
> > + }
> > +
> > + /* Check size and skip zero-sized disks */
> > + g_debug(" checking disk size");
> > + size_dir = g_strdup_printf("/sys/block/%s/size", de->d_name);
> > + fp = fopen(size_dir, "r");
> > + if (!fp) {
> > + g_debug(" failed to read disk size");
> > + continue;
> > + }
> > + if (getline(&line, &n, fp) == -1) {
> > + g_debug(" failed to read disk size");
> >
>
> line: getline(3) "This buffer should be freed by the user program even if
> getline() failed."
That is handled by the g_autofree, or am I missing something? `line`
will get out of scope after every interation (even with continue). Or do
you prefer to have it explicit and free as soon as we don't need it?
>
> + fclose(fp);
> > + continue;
> > + }
> > + fclose(fp);
> > + if (strcmp(line, "0\n") == 0) {
> >
>
> It would be slightly better to defend against NULL crash here, with
> g_strcmp0()
>
> + g_debug(" skipping zero-sized disk");
> > + continue;
> > + }
> > +
> >
>
> line is never freed?
>
> + g_debug(" adding %s", de->d_name);
> > + disk_dir = g_strdup_printf("/sys/block/%s", de->d_name);
> > + disk = g_new0(GuestDiskInfo, 1);
> > + disk->name = get_device_for_syspath(disk_dir);
> > + disk->partition = false;
> > + disk->alias = get_alias_for_syspath(disk_dir);
> > + if (disk->alias != NULL) {
> > + disk->has_alias = true;
> > + }
> >
>
> Could be a single line too
>
> + item = g_new0(GuestDiskInfoList, 1);
> > + item->value = disk;
> > + item->next = ret;
> > + ret = item;
> > +
> > + /* Get address for non-virtual devices */
> > + bool is_virtual = is_disk_virtual(disk_dir, &local_err);
> > + if (local_err != NULL) {
> > + g_debug(" failed to check disk path, ignoring error: %s",
> > + error_get_pretty(local_err));
> > + error_free(local_err);
> > + local_err = NULL;
> >
> + /* Don't try to get the address */
> > + is_virtual = true;
> > + }
> > + if (!is_virtual) {
> > + disk->address = get_disk_address(disk_dir, &local_err);
> > + if (local_err != NULL) {
> > + g_debug(" failed to get device info, ignoring error: %s",
> > + error_get_pretty(local_err));
> > + error_free(local_err);
> > + local_err = NULL;
> > + } else if (disk->address != NULL) {
> > + disk->has_address = true;
> > + }
> > + }
> > +
> > + /* List slave disks */
> > + slaves_dir = g_strdup_printf("%s/slaves", disk_dir);
> > + g_debug(" listing entries in: %s", slaves_dir);
> > + dp_slaves = opendir(slaves_dir);
> > + while ((de_slaves = readdir(dp_slaves)) != NULL) {
> > + g_autofree char *slave_dir = NULL;
> > + char *slave_dev;
> > + strList *slave_item = NULL;
> > +
> > + if ((strcmp(".", de_slaves->d_name) == 0) ||
> > + (strcmp("..", de_slaves->d_name) == 0)) {
> >
> + continue;
> > + }
> > +
> > + /* Add slave disks */
> > + slave_dir = g_strdup_printf("%s/%s",
> > + slaves_dir, de_slaves->d_name);
> > + slave_dev = get_device_for_syspath(slave_dir);
> > + if (slave_dev != NULL) {
> > + g_debug(" adding slave device: %s", slave_dev);
> > + slave_item = g_new0(strList, 1);
> > + slave_item->value = slave_dev;
> > + slave_item->next = disk->slaves;
> > + disk->slaves = slave_item;
> > + }
> > + }
> > + closedir(dp_slaves);
> > +
> > + /*
> > + * Detect partitions subdirectory name is "<parent><number>" or
> > + * "<parent>p<number>"
> > + */
> > + dp_disk = opendir(disk_dir);
> > + while ((de_disk = readdir(dp_disk)) != NULL) {
> > + size_t len;
> > + g_autofree char *partition_dir = NULL;
> > +
> > + if (!(de_disk->d_type & DT_DIR)) {
> > + continue;
> > + }
> > +
> > + len = strlen(de->d_name);
> > + if (!(strncmp(de->d_name, de_disk->d_name, len) == 0 &&
> > + ((*(de_disk->d_name + len) == 'p' &&
> > + isdigit(*(de_disk->d_name + len + 1))) ||
> > + isdigit(*(de_disk->d_name + len))))) {
> > + continue;
> > + }
> > +
> > + partition_dir = g_strdup_printf("%s/%s",
> > + disk_dir, de_disk->d_name);
> > + partition = g_new0(GuestDiskInfo, 1);
> > + partition->name = get_device_for_syspath(partition_dir);
> > + partition->partition = true;
> > + /* Add parent disk as slave for easier tracking of hierarchy
> > */
> > + partition->slaves = g_new0(strList, 1);
> > + partition->slaves->value = g_strdup(disk->name);
> > +
> > + item = g_new0(GuestDiskInfoList, 1);
> > + item->value = partition;
> > + item->next = ret;
> > + ret = item;
> > +
> > + }
> > + closedir(dp_disk);
> > + }
> > + return ret;
> > +}
> > +
> > +#else
> > +
> > +GuestDiskInfoList *qmp_guest_get_disks(Error **errp)
> > +{
> > + error_setg(errp, QERR_UNSUPPORTED);
> > + return NULL;
> > +}
> > +
> > +#endif
> > +
> > /* Return a list of the disk device(s)' info which @mount lies on */
> > static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount,
> > Error **errp)
> > @@ -2809,7 +3047,8 @@ GList *ga_command_blacklist_init(GList *blacklist)
> > const char *list[] = {
> > "guest-get-fsinfo", "guest-fsfreeze-status",
> > "guest-fsfreeze-freeze", "guest-fsfreeze-freeze-list",
> > - "guest-fsfreeze-thaw", "guest-get-fsinfo", NULL};
> > + "guest-fsfreeze-thaw", "guest-get-fsinfo",
> > + "guest-get-disks", NULL};
> > char **p = (char **)list;
> >
> > while (*p) {
> > @@ -3042,9 +3281,3 @@ GuestOSInfo *qmp_guest_get_osinfo(Error **errp)
> >
> > return info;
> > }
> > -
> > -GuestDiskInfoList *qmp_guest_get_disks(Error **errp)
> > -{
> > - error_setg(errp, QERR_UNSUPPORTED);
> > - return NULL;
> > -}
> > --
> > 2.25.0
> >
> >
> >
>
> --
> Marc-André Lureau
--
Tomáš Golembiovský <tgolembi@redhat.com>
- Re: [PATCH v2 2/3] qga: add implementation of guest-get-disks for Linux,
Tomáš Golembiovský <=