[Libguestfs] [PATCH] inspect: improve detection of Mageia install discs

Richard W.M. Jones rjones at redhat.com
Wed Mar 22 16:31:41 UTC 2017


On Wed, Mar 22, 2017 at 05:16:34PM +0100, Pino Toscano wrote:
> Check for a "product.id" file in an architecture-specific subdirectory
> of the main partition, and use its data to improve the data on the
> media.
> 
> Only Mageia as distribution name is recognized there, since most
> probably this file will not be available on other distros.
> ---
>  lib/inspect-fs-cd.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 76 insertions(+)
> 
> diff --git a/lib/inspect-fs-cd.c b/lib/inspect-fs-cd.c
> index 278386e..9c809b4 100644
> --- a/lib/inspect-fs-cd.c
> +++ b/lib/inspect-fs-cd.c
> @@ -21,6 +21,8 @@
>  #include <stdio.h>
>  #include <stdlib.h>
>  #include <string.h>
> +#include <libintl.h>
> +#include <inttypes.h>
>  
>  #ifdef HAVE_ENDIAN_H
>  #include <endian.h>
> @@ -432,10 +434,72 @@ check_w2k3_installer_root (guestfs_h *g, struct inspect_fs *fs,
>    return 0;
>  }
>  
> +/* Read the data from a product.id-like file.
> + *
> + * This is an old file, mostly used in Mandriva-based systems (still including
> + * Mageia).  A very minimal documentation for it is:
> + * - https://wiki.mageia.org/en/Product_id
> + * - http://wiki.mandriva.com/en/Product_id (old URL, defunct)
> + */
> +static int
> +check_product_id_installer_root (guestfs_h *g, struct inspect_fs *fs,
> +                                 const char *filename)
> +{
> +  int64_t size;
> +  CLEANUP_FREE_STRING_LIST char **lines = NULL;
> +  const char *elem;
> +  char *saveptr;
> +
> +  fs->type = OS_TYPE_LINUX;
> +
> +  /* Don't trust guestfs_head_n not to break with very large files.
> +   * Check the file size is something reasonable first.
> +   */
> +  size = guestfs_filesize (g, filename);
> +  if (size == -1)
> +    /* guestfs_filesize failed and has already set error in handle */
> +    return -1;
> +  if (size > MAX_SMALL_FILE_SIZE) {
> +    error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
> +           filename, size);
> +    return -1;
> +  }
> +
> +  lines = guestfs_head_n (g, 1, filename);
> +  if (lines == NULL)
> +    return -1;

Could the above code be replaced by a call to
guestfs_int_first_line_of_file ?

> +  elem = strtok_r (lines[0], ",", &saveptr);

You can probably use strtok here [I think?] if it's simpler.

Rich.

> +  while (elem) {
> +    const char *equal = strchr (elem, '=');
> +    if (equal == NULL || equal == elem)
> +      return -1;
> +
> +    const char *value = equal + 1;
> +
> +    if (STRPREFIX (elem, "distribution=")) {
> +      if (STREQ (value, "Mageia"))
> +        fs->distro = OS_DISTRO_MAGEIA;
> +    } else if (STRPREFIX (elem, "version=")) {
> +      if (guestfs_int_version_from_x_y_or_x (g, &fs->version, value) == -1)
> +        return -1;
> +    } else if (STRPREFIX (elem, "arch=")) {
> +      fs->arch = safe_strdup (g, value);
> +    }
> +
> +    elem = strtok_r (NULL, ",", &saveptr);
> +  }
> +
> +  /* Not found. */
> +  return 0;
> +}
> +
>  /* The currently mounted device is very likely to be an installer. */
>  int
>  guestfs_int_check_installer_root (guestfs_h *g, struct inspect_fs *fs)
>  {
> +  CLEANUP_FREE_STRING_LIST char **paths = NULL;
> +
>    /* The presence of certain files indicates a live CD.
>     *
>     * XXX Fedora netinst contains a ~120MB squashfs called
> @@ -495,6 +559,18 @@ guestfs_int_check_installer_root (guestfs_h *g, struct inspect_fs *fs)
>        return -1;
>    }
>  
> +  /* Linux with /{i586,x86_64,etc}/product.id (typically found in Mandriva
> +   * and Mageia).  Usually there should be just one around, so we use the
> +   * first one found.
> +   */
> +  paths = guestfs_glob_expand (g, "/*/product.id");
> +  if (paths == NULL)
> +    return -1;
> +  if (paths[0] != NULL) {
> +    if (check_product_id_installer_root (g, fs, paths[0]) == -1)
> +      return -1;
> +  }
> +
>    return 0;
>  }
>  
> -- 
> 2.9.3
> 
> _______________________________________________
> Libguestfs mailing list
> Libguestfs at redhat.com
> https://www.redhat.com/mailman/listinfo/libguestfs

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-top is 'top' for virtual machines.  Tiny program with many
powerful monitoring features, net stats, disk stats, logging, etc.
http://people.redhat.com/~rjones/virt-top




More information about the Libguestfs mailing list