diff options
author | Wanlong Gao <gaowanlong@cn.fujitsu.com> | 2012-07-18 09:58:43 +0800 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2012-07-18 09:37:17 +0100 |
commit | 87206e4e9e3b0ca813a4ff7b5fac0eccc07a484a (patch) | |
tree | b1de20359160c0da257e9a541a325c67f9f996c7 | |
parent | 2f89f584c0203023f8a829ff9796dc2c14ddeb68 (diff) | |
download | libguestfs-87206e4e9e3b0ca813a4ff7b5fac0eccc07a484a.tar.gz libguestfs-87206e4e9e3b0ca813a4ff7b5fac0eccc07a484a.tar.xz libguestfs-87206e4e9e3b0ca813a4ff7b5fac0eccc07a484a.zip |
New API: add new api xfs_info
Add xfs_info to show the geometry of the xfs filesystem.
Signed-off-by: Wanlong Gao <gaowanlong@cn.fujitsu.com>
RWMJ:
- Updated po/POTFILES.
- Use xfs_ prefix for all struct fields.
- Return uninitialized fields as -1 / empty string.
- Copyedit the description.
-rw-r--r-- | daemon/Makefile.am | 1 | ||||
-rw-r--r-- | daemon/xfs.c | 355 | ||||
-rw-r--r-- | generator/generator_actions.ml | 23 | ||||
-rw-r--r-- | generator/generator_structs.ml | 30 | ||||
-rw-r--r-- | gobject/Makefile.inc | 2 | ||||
-rw-r--r-- | java/Makefile.inc | 1 | ||||
-rw-r--r-- | java/com/redhat/et/libguestfs/.gitignore | 1 | ||||
-rw-r--r-- | po/POTFILES | 2 | ||||
-rw-r--r-- | src/MAX_PROC_NR | 2 |
9 files changed, 416 insertions, 1 deletions
diff --git a/daemon/Makefile.am b/daemon/Makefile.am index 9e2a633a..afe88741 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -165,6 +165,7 @@ guestfsd_SOURCES = \ utimens.c \ wc.c \ xattr.c \ + xfs.c \ zero.c \ zerofree.c guestfsd_LDADD = \ diff --git a/daemon/xfs.c b/daemon/xfs.c new file mode 100644 index 00000000..3efc14f7 --- /dev/null +++ b/daemon/xfs.c @@ -0,0 +1,355 @@ +/* libguestfs - the guestfsd daemon + * Copyright (C) 2012 Fujitsu Limited. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#include <inttypes.h> +#include <string.h> +#include <unistd.h> + +#include "daemon.h" +#include "actions.h" +#include "optgroups.h" + +int +optgroup_xfs_available (void) +{ + return prog_exists ("mkfs.xfs"); +} + +static char * +split_strdup (char *string) +{ + char *end = string; + while (*end != ' ' && *end != ',' && *end != '\0') end++; + size_t len = end - string; + char *ret = malloc (len + 1); + if (!ret) { + reply_with_perror ("malloc"); + return NULL; + } + strncpy (ret, string, len); + ret[len] = '\0'; + return ret; +} + +static int +parse_uint32 (uint32_t *ret, const char *str) +{ + uint32_t r; + + if (sscanf (str, "%" SCNu32, &r) != 1) { + reply_with_error ("cannot parse numeric field from isoinfo: %s", str); + return -1; + } + + *ret = r; + return 0; +} + +static int +parse_uint64 (uint64_t *ret, const char *str) +{ + uint64_t r; + + if (sscanf (str, "%" SCNu64, &r) != 1) { + reply_with_error ("cannot parse numeric field from isoinfo: %s", str); + return -1; + } + + *ret = r; + return 0; +} + +/* Typical crazy output from the xfs_info command: + * + * meta-data=/dev/sda1 isize=256 agcount=4, agsize=6392 blks + * = sectsz=512 attr=2 + * data = bsize=4096 blocks=25568, imaxpct=25 + * = sunit=0 swidth=0 blks + * naming =version 2 bsize=4096 ascii-ci=0 + * log =internal bsize=4096 blocks=1200, version=2 + * = sectsz=512 sunit=0 blks, lazy-count=1 + * realtime =none extsz=4096 blocks=0, rtextents=0 + * + * We may need to revisit this parsing code if the output changes + * in future. + */ +static guestfs_int_xfsinfo * +parse_xfs_info (char **lines) +{ + guestfs_int_xfsinfo *ret; + char *buf = NULL, *p; + size_t i; + + ret = malloc (sizeof *ret); + if (ret == NULL) { + reply_with_error ("malloc"); + return NULL; + } + + /* Initialize fields to NULL or -1 so the caller can tell which fields + * were updated in the code below. + */ + ret->xfs_mntpoint = NULL; + ret->xfs_inodesize = -1; + ret->xfs_agcount = -1; + ret->xfs_agsize = -1; + ret->xfs_sectsize = -1; + ret->xfs_attr = -1; + ret->xfs_blocksize = -1; + ret->xfs_datablocks = -1; + ret->xfs_imaxpct = -1; + ret->xfs_sunit = -1; + ret->xfs_swidth = -1; + ret->xfs_dirversion = -1; + ret->xfs_dirblocksize = -1; + ret->xfs_cimode = -1; + ret->xfs_logname = NULL; + ret->xfs_logblocksize = -1; + ret->xfs_logblocks = -1; + ret->xfs_logversion = -1; + ret->xfs_logsectsize = -1; + ret->xfs_logsunit = -1; + ret->xfs_lazycount = -1; + ret->xfs_rtname = NULL; + ret->xfs_rtextsize = -1; + ret->xfs_rtblocks = -1; + ret->xfs_rtextents = -1; + + for (i = 0; lines[i] != NULL; ++i) { + if ((p = strstr (lines[i], "meta-data="))) { + ret->xfs_mntpoint = split_strdup (p + 10); + if (ret->xfs_mntpoint == NULL) goto error; + } + if ((p = strstr (lines[i], "isize="))) { + buf = split_strdup (p + 6); + if (buf == NULL) goto error; + if (parse_uint32 (&ret->xfs_inodesize, buf) == -1) + goto error; + free (buf); + } + if ((p = strstr (lines[i], "agcount="))) { + buf = split_strdup (p + 8); + if (buf == NULL) goto error; + if (parse_uint32 (&ret->xfs_agcount, buf) == -1) + goto error; + free (buf); + } + if ((p = strstr (lines[i], "agsize="))) { + buf = split_strdup (p + 7); + if (buf == NULL) goto error; + if (parse_uint32 (&ret->xfs_agsize, buf) == -1) + goto error; + free (buf); + } + if ((p = strstr (lines[i], "sectsz="))) { + buf = split_strdup (p + 7); + if (buf == NULL) goto error; + if (i == 1) { + if (parse_uint32 (&ret->xfs_sectsize, buf) == -1) + goto error; + free (buf); + } else if (i == 6) { + if (parse_uint32 (&ret->xfs_logsectsize, buf) == -1) + goto error; + free (buf); + } else goto error; + } + if ((p = strstr (lines[i], "attr="))) { + buf = split_strdup (p + 5); + if (buf == NULL) goto error; + if (parse_uint32 (&ret->xfs_attr, buf) == -1) + goto error; + free (buf); + } + if ((p = strstr (lines[i], "bsize="))) { + buf = split_strdup (p + 6); + if (buf == NULL) goto error; + if (i == 2) { + if (parse_uint32 (&ret->xfs_blocksize, buf) == -1) + goto error; + free (buf); + } else if (i == 4) { + if (parse_uint32 (&ret->xfs_dirblocksize, buf) == -1) + goto error; + free (buf); + } else if (i == 5) { + if (parse_uint32 (&ret->xfs_logblocksize, buf) == -1) + goto error; + free (buf); + } else goto error; + } + if ((p = strstr (lines[i], "blocks="))) { + buf = split_strdup (p + 7); + if (buf == NULL) goto error; + if (i == 2) { + if (parse_uint64 (&ret->xfs_datablocks, buf) == -1) + goto error; + free (buf); + } else if (i == 5) { + if (parse_uint32 (&ret->xfs_logblocks, buf) == -1) + goto error; + free (buf); + } else if (i == 7) { + if (parse_uint64 (&ret->xfs_rtblocks, buf) == -1) + goto error; + free (buf); + } else goto error; + } + if ((p = strstr (lines[i], "imaxpct="))) { + buf = split_strdup (p + 8); + if (buf == NULL) goto error; + if (parse_uint32 (&ret->xfs_imaxpct, buf) == -1) + goto error; + free (buf); + } + if ((p = strstr (lines[i], "sunit="))) { + buf = split_strdup (p + 6); + if (buf == NULL) goto error; + if (i == 3) { + if (parse_uint32 (&ret->xfs_sunit, buf) == -1) + goto error; + free (buf); + } else if (i == 6) { + if (parse_uint32 (&ret->xfs_logsunit, buf) == -1) + goto error; + free (buf); + } else goto error; + } + if ((p = strstr (lines[i], "swidth="))) { + buf = split_strdup (p + 7); + if (buf == NULL) goto error; + if (parse_uint32 (&ret->xfs_swidth, buf) == -1) + goto error; + free (buf); + } + if ((p = strstr (lines[i], "naming =version "))) { + buf = split_strdup (p + 18); + if (buf == NULL) goto error; + if (parse_uint32 (&ret->xfs_dirversion, buf) == -1) + goto error; + free (buf); + } + if ((p = strstr (lines[i], "ascii-ci="))) { + buf = split_strdup (p + 9); + if (buf == NULL) goto error; + if (parse_uint32 (&ret->xfs_cimode, buf) == -1) + goto error; + free (buf); + } + if ((p = strstr (lines[i], "log ="))) { + ret->xfs_logname = split_strdup (p + 10); + if (ret->xfs_logname == NULL) goto error; + } + if ((p = strstr (lines[i], "version="))) { + buf = split_strdup (p + 8); + if (buf == NULL) goto error; + if (parse_uint32 (&ret->xfs_logversion, buf) == -1) + goto error; + free (buf); + } + if ((p = strstr (lines[i], "lazy-count="))) { + buf = split_strdup (p + 11); + if (buf == NULL) goto error; + if (parse_uint32 (&ret->xfs_lazycount, buf) == -1) + goto error; + free (buf); + } + if ((p = strstr (lines[i], "realtime ="))) { + ret->xfs_rtname = split_strdup (p + 10); + if (ret->xfs_rtname == NULL) goto error; + } + if ((p = strstr (lines[i], "rtextents="))) { + buf = split_strdup (p + 10); + if (buf == NULL) goto error; + if (parse_uint64 (&ret->xfs_rtextents, buf) == -1) + goto error; + free (buf); + } + } + + if (ret->xfs_mntpoint == NULL) { + ret->xfs_mntpoint = strdup (""); + if (ret->xfs_mntpoint == NULL) goto error; + } + if (ret->xfs_logname == NULL) { + ret->xfs_logname = strdup (""); + if (ret->xfs_logname == NULL) goto error; + } + if (ret->xfs_rtname == NULL) { + ret->xfs_rtname = strdup (""); + if (ret->xfs_rtname == NULL) goto error; + } + + return ret; + +error: + free (buf); + free (ret->xfs_mntpoint); + free (ret->xfs_logname); + free (ret->xfs_rtname); + free (ret); + return NULL; +} + +guestfs_int_xfsinfo * +do_xfs_info (const char *path) +{ + int r; + char *buf; + char *out = NULL, *err = NULL; + char **lines = NULL; + guestfs_int_xfsinfo *ret = NULL; + + if (do_is_dir (path)) { + buf = sysroot_path (path); + if (!buf) { + reply_with_perror ("malloc"); + return NULL; + } + } else { + buf = strdup(path); + if (!buf) { + reply_with_perror ("strdup"); + return NULL; + } + } + + r = command (&out, &err, "xfs_info", buf, NULL); + free (buf); + if (r == -1) { + reply_with_error ("%s", err); + goto error; + } + + lines = split_lines (out); + if (lines == NULL) + goto error; + + ret = parse_xfs_info (lines); + +error: + free (err); + free (out); + if (lines) + free_strings (lines); + return ret; +} diff --git a/generator/generator_actions.ml b/generator/generator_actions.ml index 90cc661f..4edf9789 100644 --- a/generator/generator_actions.ml +++ b/generator/generator_actions.ml @@ -8948,6 +8948,29 @@ be returned if you called C<guestfs_list_devices>. To find out the maximum number of devices that could be added, call C<guestfs_max_disks>." }; + { defaults with + name = "xfs_info"; + style = RStruct ("info", "xfsinfo"), [Pathname "path"], []; + proc_nr = Some 337; + optional = Some "xfs"; + tests = [ + InitEmpty, IfAvailable "xfs", TestOutputStruct ( + [["part_disk"; "/dev/sda"; "mbr"]; + ["mkfs"; "xfs"; "/dev/sda1"; ""; "NOARG"; ""; ""]; + ["mount_options"; ""; "/dev/sda1"; "/"]; + ["xfs_info"; "/"]], + [CompareWithInt ("xfs_blocksize", 4096); + ]) + ]; + shortdesc = "get geometry of XFS filesystem"; + longdesc = "\ +C<path> is a mounted XFS filesystem. This command returns the +geometry of the filesystem. + +The returned struct contains geometry information. Missing +fields are returned as C<-1> (for numeric fields) or empty +string." }; + ] (* Non-API meta-commands available only in guestfish. diff --git a/generator/generator_structs.ml b/generator/generator_structs.ml index 024bb3cd..c3339c24 100644 --- a/generator/generator_structs.ml +++ b/generator/generator_structs.ml @@ -226,6 +226,35 @@ let structs = [ "btrfssubvolume_top_level_id", FUInt64; "btrfssubvolume_path", FString; ]; + + (* XFS info descriptor. *) + "xfsinfo", [ + "xfs_mntpoint", FString; + "xfs_inodesize", FUInt32; + "xfs_agcount", FUInt32; + "xfs_agsize", FUInt32; + "xfs_sectsize", FUInt32; + "xfs_attr", FUInt32; + "xfs_blocksize", FUInt32; + "xfs_datablocks", FUInt64; + "xfs_imaxpct", FUInt32; + "xfs_sunit", FUInt32; + "xfs_swidth", FUInt32; + "xfs_dirversion", FUInt32; + "xfs_dirblocksize", FUInt32; + "xfs_cimode", FUInt32; + "xfs_logname", FString; + "xfs_logblocksize", FUInt32; + "xfs_logblocks", FUInt32; + "xfs_logversion", FUInt32; + "xfs_logsectsize", FUInt32; + "xfs_logsunit", FUInt32; + "xfs_lazycount", FUInt32; + "xfs_rtname", FString; + "xfs_rtextsize", FUInt32; + "xfs_rtblocks", FUInt64; + "xfs_rtextents", FUInt64; + ]; ] (* end of structs *) (* For bindings which want camel case *) @@ -243,6 +272,7 @@ let camel_structs = [ "partition", "Partition"; "application", "Application"; "isoinfo", "ISOInfo"; + "xfsinfo", "XFSInfo"; "mdstat", "MDStat"; "btrfssubvolume", "BTRFSSubvolume"; ] diff --git a/gobject/Makefile.inc b/gobject/Makefile.inc index 4de56a96..c73e5df0 100644 --- a/gobject/Makefile.inc +++ b/gobject/Makefile.inc @@ -38,6 +38,7 @@ guestfs_gobject_headers= \ include/guestfs-gobject/struct-isoinfo.h \ include/guestfs-gobject/struct-mdstat.h \ include/guestfs-gobject/struct-btrfssubvolume.h \ + include/guestfs-gobject/struct-xfsinfo.h \ include/guestfs-gobject/optargs-internal_test.h \ include/guestfs-gobject/optargs-add_drive.h \ include/guestfs-gobject/optargs-add_domain.h \ @@ -82,6 +83,7 @@ guestfs_gobject_sources= \ src/struct-isoinfo.c \ src/struct-mdstat.c \ src/struct-btrfssubvolume.c \ + src/struct-xfsinfo.c \ src/optargs-internal_test.c \ src/optargs-add_drive.c \ src/optargs-add_domain.c \ diff --git a/java/Makefile.inc b/java/Makefile.inc index efad2a0c..da8de49b 100644 --- a/java/Makefile.inc +++ b/java/Makefile.inc @@ -35,4 +35,5 @@ java_built_sources = \ com/redhat/et/libguestfs/VG.java \ com/redhat/et/libguestfs/Version.java \ com/redhat/et/libguestfs/XAttr.java \ + com/redhat/et/libguestfs/XFSInfo.java \ com/redhat/et/libguestfs/GuestFS.java diff --git a/java/com/redhat/et/libguestfs/.gitignore b/java/com/redhat/et/libguestfs/.gitignore index 9556d816..10346658 100644 --- a/java/com/redhat/et/libguestfs/.gitignore +++ b/java/com/redhat/et/libguestfs/.gitignore @@ -13,3 +13,4 @@ StatVFS.java VG.java Version.java XAttr.java +XFSInfo.java diff --git a/po/POTFILES b/po/POTFILES index 7f679115..e3986871 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -84,6 +84,7 @@ daemon/upload.c daemon/utimens.c daemon/wc.c daemon/xattr.c +daemon/xfs.c daemon/zero.c daemon/zerofree.c df/df.c @@ -173,6 +174,7 @@ gobject/src/struct-stat.c gobject/src/struct-statvfs.c gobject/src/struct-version.c gobject/src/struct-xattr.c +gobject/src/struct-xfsinfo.c gobject/src/tristate.c inspector/virt-inspector.c java/com_redhat_et_libguestfs_GuestFS.c diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR index e64f24d5..f59a90f3 100644 --- a/src/MAX_PROC_NR +++ b/src/MAX_PROC_NR @@ -1 +1 @@ -336 +337 |