mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-21 22:53:37 +00:00
New internal API: internal_parse_mountable
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -437,6 +437,7 @@ Makefile.in
|
||||
/tests/guests/ubuntu.img
|
||||
/tests/guests/windows.img
|
||||
/tests/mount-local/test-parallel-mount-local
|
||||
/tests/mountable/test-internal-parse-mountable
|
||||
/tests/parallel/test-parallel
|
||||
/tests/regressions/rhbz501893
|
||||
/tests/regressions/rhbz790721
|
||||
|
||||
@@ -36,6 +36,7 @@ endif
|
||||
|
||||
# Tests - order is important.
|
||||
if ENABLE_APPLIANCE
|
||||
SUBDIRS += tests/mountable
|
||||
SUBDIRS += tests/qemu
|
||||
SUBDIRS += tests/guests
|
||||
SUBDIRS += tests/c-api
|
||||
|
||||
@@ -1546,6 +1546,7 @@ AC_CONFIG_FILES([Makefile
|
||||
tests/lvm/Makefile
|
||||
tests/md/Makefile
|
||||
tests/mount-local/Makefile
|
||||
tests/mountable/Makefile
|
||||
tests/ntfsclone/Makefile
|
||||
tests/parallel/Makefile
|
||||
tests/protocol/Makefile
|
||||
|
||||
@@ -140,6 +140,7 @@ guestfsd_SOURCES = \
|
||||
mktemp.c \
|
||||
modprobe.c \
|
||||
mount.c \
|
||||
mountable.c \
|
||||
names.c \
|
||||
ntfs.c \
|
||||
ntfsclone.c \
|
||||
|
||||
@@ -34,12 +34,6 @@
|
||||
|
||||
/* Mountables */
|
||||
|
||||
typedef enum {
|
||||
MOUNTABLE_DEVICE, /* A bare device */
|
||||
MOUNTABLE_BTRFSVOL, /* A btrfs subvolume: device + volume */
|
||||
MOUNTABLE_PATH /* An already mounted path: device = path */
|
||||
} mountable_type_t;
|
||||
|
||||
typedef struct {
|
||||
mountable_type_t type;
|
||||
const char *device;
|
||||
|
||||
59
daemon/mountable.c
Normal file
59
daemon/mountable.c
Normal file
@@ -0,0 +1,59 @@
|
||||
/* libguestfs - the guestfsd daemon
|
||||
* Copyright (C) 2009 Red Hat Inc.
|
||||
*
|
||||
* 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 <string.h>
|
||||
|
||||
#include "daemon.h"
|
||||
#include "actions.h"
|
||||
#include "guestfs_protocol.h"
|
||||
|
||||
guestfs_int_internal_mountable *
|
||||
do_internal_parse_mountable (const mountable_t *mountable)
|
||||
{
|
||||
guestfs_int_internal_mountable *ret = calloc (1, sizeof *ret);
|
||||
if (ret == NULL) {
|
||||
reply_with_perror ("calloc");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret->im_type = mountable->type;
|
||||
if (mountable->device) {
|
||||
ret->im_device = strdup (mountable->device);
|
||||
if (!ret->im_device) {
|
||||
reply_with_perror ("strdup");
|
||||
free (ret);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (mountable->volume) {
|
||||
ret->im_volume = strdup (mountable->volume);
|
||||
if (!ret->im_volume) {
|
||||
reply_with_perror ("strdup");
|
||||
free (ret->im_device);
|
||||
free (ret);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -10726,6 +10726,15 @@ you are better to use C<guestfs_mv> instead." };
|
||||
This returns C<true> if and only if C<device> refers to a whole block
|
||||
device. That is, not a partition or a logical device." };
|
||||
|
||||
{ defaults with
|
||||
name = "internal_parse_mountable";
|
||||
style = RStruct ("mountable", "internal_mountable"), [Mountable "mountable"], [];
|
||||
visibility = VInternal;
|
||||
proc_nr = Some 396;
|
||||
shortdesc = "parse a mountable string";
|
||||
longdesc = "\
|
||||
Parse a mountable string." };
|
||||
|
||||
]
|
||||
|
||||
(* Non-API meta-commands available only in guestfish.
|
||||
|
||||
@@ -360,6 +360,16 @@ let structs = [
|
||||
"hivex_value_h", FInt64;
|
||||
];
|
||||
s_camel_name = "HivexValue" };
|
||||
{ defaults with
|
||||
s_name = "internal_mountable";
|
||||
s_internal = true;
|
||||
s_cols = [
|
||||
"im_type", FInt32;
|
||||
"im_device", FString;
|
||||
"im_volume", FString;
|
||||
];
|
||||
s_camel_name = "InternalMountable";
|
||||
};
|
||||
] (* end of structs *)
|
||||
|
||||
let lookup_struct name =
|
||||
|
||||
@@ -64,6 +64,7 @@ daemon/mknod.c
|
||||
daemon/mktemp.c
|
||||
daemon/modprobe.c
|
||||
daemon/mount.c
|
||||
daemon/mountable.c
|
||||
daemon/names.c
|
||||
daemon/ntfs.c
|
||||
daemon/ntfsclone.c
|
||||
|
||||
@@ -1 +1 @@
|
||||
395
|
||||
396
|
||||
|
||||
@@ -66,4 +66,16 @@
|
||||
#define xdr_uint32_t xdr_u_int32_t
|
||||
#endif
|
||||
|
||||
/* The type field of a parsed mountable.
|
||||
*
|
||||
* This is used both by mountable_t in the daemon, and
|
||||
* struct guestfs_int_mountable_internal in the library.
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
MOUNTABLE_DEVICE, /* A bare device */
|
||||
MOUNTABLE_BTRFSVOL, /* A btrfs subvolume: device + volume */
|
||||
MOUNTABLE_PATH /* An already mounted path: device = path */
|
||||
} mountable_type_t;
|
||||
|
||||
#endif /* GUESTFS_INTERNAL_ALL_H_ */
|
||||
|
||||
36
tests/mountable/Makefile.am
Normal file
36
tests/mountable/Makefile.am
Normal file
@@ -0,0 +1,36 @@
|
||||
# libguestfs
|
||||
# Copyright (C) 2012 Red Hat Inc.
|
||||
#
|
||||
# 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 $(top_srcdir)/subdir-rules.mk
|
||||
|
||||
TESTS_ENVIRONMENT = $(top_builddir)/run --test
|
||||
|
||||
TESTS=test-internal-parse-mountable
|
||||
check_PROGRAMS = test-internal-parse-mountable
|
||||
|
||||
test_internal_parse_mountable_SOURCES = test-internal-parse-mountable.c
|
||||
test_internal_parse_mountable_CPPFLAGS = \
|
||||
-DGUESTFS_WARN_DEPRECATED=1 \
|
||||
-DGUESTFS_PRIVATE=1 \
|
||||
-I$(top_srcdir)/gnulib/lib -I$(top_builddir)/gnulib/lib \
|
||||
-I$(top_srcdir)/src -I$(top_builddir)/src
|
||||
test_internal_parse_mountable_CFLAGS = \
|
||||
$(WARN_CFLAGS) $(WERROR_CFLAGS) \
|
||||
$(GPROF_CFLAGS) $(GCOV_CFLAGS)
|
||||
test_internal_parse_mountable_LDADD = \
|
||||
$(top_builddir)/src/libguestfs.la \
|
||||
$(top_builddir)/gnulib/lib/libgnu.la
|
||||
117
tests/mountable/test-internal-parse-mountable.c
Normal file
117
tests/mountable/test-internal-parse-mountable.c
Normal file
@@ -0,0 +1,117 @@
|
||||
/* libguestfs
|
||||
* Copyright (C) 2012 Red Hat Inc.
|
||||
*
|
||||
* 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 <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "guestfs.h"
|
||||
#include "guestfs-internal-all.h"
|
||||
|
||||
#define IMG "test.img"
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
int fd = open (IMG, O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
||||
if (fd == -1) {
|
||||
perror ("open " IMG);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int r = posix_fallocate (fd, 0, 1024*1024*1024);
|
||||
if (r != 0) {
|
||||
fprintf (stderr, "posix_fallocate " IMG " 1G: %s\n", strerror (r));
|
||||
unlink (IMG);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (close (fd) == -1) {
|
||||
perror ("close " IMG);
|
||||
unlink (IMG);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
guestfs_h *g = guestfs_create ();
|
||||
if (g == NULL) {
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (guestfs_add_drive_opts (g, IMG,
|
||||
GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
|
||||
GUESTFS_ADD_DRIVE_OPTS_READONLY, 1,
|
||||
-1) == -1) {
|
||||
error:
|
||||
guestfs_close (g);
|
||||
unlink (IMG);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (guestfs_launch (g) == -1) goto error;
|
||||
|
||||
if (guestfs_part_disk (g, "/dev/sda", "mbr") == -1) goto error;
|
||||
|
||||
if (guestfs_pvcreate (g, "/dev/sda1") == -1) goto error;
|
||||
|
||||
const char *pvs[] = { "/dev/sda1", NULL };
|
||||
if (guestfs_vgcreate (g, "VG", (char **) pvs) == -1) goto error;
|
||||
|
||||
if (guestfs_lvcreate (g, "LV", "VG", 900) == -1) goto error;
|
||||
|
||||
const char *devices[] = { "/dev/VG/LV", NULL };
|
||||
if (guestfs_mkfs_btrfs (g, (char * const *)devices, -1) == -1) goto error;
|
||||
|
||||
if (guestfs_mount (g, "/dev/VG/LV", "/") == -1) goto error;
|
||||
|
||||
if (guestfs_btrfs_subvolume_create (g, "/sv") == -1) goto error;
|
||||
|
||||
struct guestfs_internal_mountable *mountable =
|
||||
guestfs_internal_parse_mountable (g, "/dev/VG/LV");
|
||||
if (mountable == NULL) goto error;
|
||||
|
||||
if (mountable->im_type != MOUNTABLE_DEVICE ||
|
||||
!STREQ ("/dev/VG/LV", mountable->im_device))
|
||||
{
|
||||
fprintf (stderr, "incorrectly parsed /dev/VG/LV");
|
||||
goto error;
|
||||
}
|
||||
|
||||
guestfs_free_internal_mountable (mountable);
|
||||
|
||||
mountable =
|
||||
guestfs_internal_parse_mountable (g, "btrfsvol:/dev/VG/LV/sv");
|
||||
if (mountable == NULL) goto error;
|
||||
|
||||
if (mountable->im_type != MOUNTABLE_BTRFSVOL ||
|
||||
!STREQ ("/dev/VG/LV", mountable->im_device) ||
|
||||
!STREQ ("sv", mountable->im_volume))
|
||||
{
|
||||
fprintf (stderr, "incorrectly parsed /dev/VG/LV/sv");
|
||||
goto error;
|
||||
}
|
||||
guestfs_free_internal_mountable (mountable);
|
||||
|
||||
guestfs_close (g);
|
||||
unlink (IMG);
|
||||
|
||||
exit (EXIT_SUCCESS);
|
||||
}
|
||||
Reference in New Issue
Block a user