Allow users to add disk images to their sandbox. Only disk images are supported so far,
but the
parameter is intentionally designed for future changes.
---
bin/virt-sandbox.c | 37 ++++++++++++++++++++++
.../libvirt-sandbox-builder-container.c | 36 ++++++++++++++++++++-
libvirt-sandbox/libvirt-sandbox-builder-machine.c | 31 +++++++++++++++++-
3 files changed, 102 insertions(+), 2 deletions(-)
diff --git a/bin/virt-sandbox.c b/bin/virt-sandbox.c
index 1ab2f8a..aa09f33 100644
--- a/bin/virt-sandbox.c
+++ b/bin/virt-sandbox.c
@@ -63,6 +63,7 @@ int main(int argc, char **argv) {
GMainLoop *loop = NULL;
GError *error = NULL;
gchar *name = NULL;
+ gchar **disks = NULL;
gchar **mounts = NULL;
gchar **includes = NULL;
gchar *includefile = NULL;
@@ -92,6 +93,8 @@ int main(int argc, char **argv) {
N_("name of the sandbox"), "NAME" },
{ "root", 'r', 0, G_OPTION_ARG_STRING, &root,
N_("root directory of the sandbox"), "DIR" },
+ { "disk", ' ', 0, G_OPTION_ARG_STRING_ARRAY, &disks,
+ N_("add a disk in the guest"),
"TYPE:TARGET=SOURCE,FORMAT=FORMAT" },
{ "mount", 'm', 0, G_OPTION_ARG_STRING_ARRAY, &mounts,
N_("mount a filesystem in the guest"), "TYPE:TARGET=SOURCE"
},
{ "include", 'i', 0, G_OPTION_ARG_STRING_ARRAY, &includes,
@@ -182,6 +185,13 @@ int main(int argc, char **argv) {
gvir_sandbox_config_set_username(cfg, "root");
}
+ if (disks &&
+ !gvir_sandbox_config_add_disk_strv(cfg, disks, &error)) {
+ g_printerr(_("Unable to parse disks: %s\n"),
+ error && error->message ? error->message :
_("Unknown failure"));
+ goto cleanup;
+ }
+
if (mounts &&
!gvir_sandbox_config_add_mount_strv(cfg, mounts, &error)) {
g_printerr(_("Unable to parse mounts: %s\n"),
@@ -319,6 +329,33 @@ inheriting the host's root filesystem.
NB. C<DIR> must contain a matching install of the libvirt-sandbox
package. This restriction may be lifted in a future version.
+=item B<--disk TYPE:TARGET=SOURCE,FORMAT=FORMAT>
+
+Sets up a disk inside the sandbox by using B<SOURCE> with target device
B<TARGET>
+and type B<TYPE> and format B<FORMAT>. Example:
file:hda=/var/lib/sandbox/demo/tmp.qcow2,format=qcow2
+Format is an optional parameter.
+
+=over 4
+
+=item B<TYPE>
+
+Type parameter can be set to "file".
+
+=item B<TARGET>
+
+Target parameter can be set to "hda" or any other libvirt supported target
disk
+
+=item B<SOURCE>
+
+Source parameter needs to point a file which must be a one of the valid domain disk
formats supported by qemu.
+
+=item B<FORMAT>
+
+Format parameter must be set to the same disk format as the file passed on source
parameter.
+This parameter is optional and the format can be guessed from the image extension
+
+=back
+
=item B<-m TYPE:DST=SRC>, B<--mount TYPE:DST=SRC>
Sets up a mount inside the sandbox at B<DST> backed by B<SRC>. The
diff --git a/libvirt-sandbox/libvirt-sandbox-builder-container.c
b/libvirt-sandbox/libvirt-sandbox-builder-container.c
index c3a58b2..9839810 100644
--- a/libvirt-sandbox/libvirt-sandbox-builder-container.c
+++ b/libvirt-sandbox/libvirt-sandbox-builder-container.c
@@ -218,7 +218,9 @@ static gboolean
gvir_sandbox_builder_container_construct_devices(GVirSandboxBuil
GVirConfigDomainInterfaceNetwork *iface;
GVirConfigDomainConsole *con;
GVirConfigDomainChardevSourcePty *src;
- GList *tmp = NULL, *mounts = NULL, *networks = NULL;
+ GVirConfigDomainDisk *disk;
+ GVirConfigDomainDiskDriver *diskDriver;
+ GList *tmp = NULL, *mounts = NULL, *networks = NULL, *disks = NULL;
gchar *configdir = g_strdup_printf("%s/config", statedir);
gboolean ret = FALSE;
@@ -226,6 +228,38 @@ static gboolean
gvir_sandbox_builder_container_construct_devices(GVirSandboxBuil
construct_devices(builder, config, statedir, domain, error))
goto cleanup;
+
+ tmp = disks = gvir_sandbox_config_get_disks(config);
+ while(tmp){
+ GVirSandboxConfigDisk *dconfig = GVIR_SANDBOX_CONFIG_DISK(tmp->data);
+
+ if (GVIR_SANDBOX_IS_CONFIG_DISK(dconfig)){
+
+ disk = gvir_config_domain_disk_new();
+ diskDriver = gvir_config_domain_disk_driver_new();
+ gvir_config_domain_disk_set_type(disk,
+
gvir_sandbox_config_disk_get_disk_type(dconfig));
+ gvir_config_domain_disk_driver_set_format(diskDriver,
+
gvir_sandbox_config_disk_get_format(dconfig));
+ gvir_config_domain_disk_driver_set_name(diskDriver, "nbd");
+ gvir_config_domain_disk_set_source(disk,
+
gvir_sandbox_config_disk_get_source(dconfig));
+ gvir_config_domain_disk_set_target_bus(disk,
+ GVIR_CONFIG_DOMAIN_DISK_BUS_IDE);
+ gvir_config_domain_disk_set_target_dev(disk,
+
gvir_sandbox_config_disk_get_target(dconfig));
+ gvir_config_domain_disk_set_driver(disk, diskDriver);
+ gvir_config_domain_add_device(domain,
+ GVIR_CONFIG_DOMAIN_DEVICE(disk));
+ g_object_unref(disk);
+ }
+ tmp = tmp->next;
+ }
+
+ g_list_foreach(disks, (GFunc)g_object_unref, NULL);
+ g_list_free(disks);
+
+
fs = gvir_config_domain_filesys_new();
gvir_config_domain_filesys_set_type(fs, GVIR_CONFIG_DOMAIN_FILESYS_MOUNT);
gvir_config_domain_filesys_set_access_type(fs,
GVIR_CONFIG_DOMAIN_FILESYS_ACCESS_PASSTHROUGH);
diff --git a/libvirt-sandbox/libvirt-sandbox-builder-machine.c
b/libvirt-sandbox/libvirt-sandbox-builder-machine.c
index 35a5816..255c52d 100644
--- a/libvirt-sandbox/libvirt-sandbox-builder-machine.c
+++ b/libvirt-sandbox/libvirt-sandbox-builder-machine.c
@@ -497,12 +497,13 @@ static gboolean
gvir_sandbox_builder_machine_construct_devices(GVirSandboxBuilde
{
GVirConfigDomainFilesys *fs;
GVirConfigDomainDisk *disk;
+ GVirConfigDomainDiskDriver *diskDriver;
GVirConfigDomainInterface *iface;
GVirConfigDomainMemballoon *ball;
GVirConfigDomainConsole *con;
GVirConfigDomainSerial *ser;
GVirConfigDomainChardevSourcePty *src;
- GList *tmp = NULL, *mounts = NULL, *networks = NULL;
+ GList *tmp = NULL, *mounts = NULL, *networks = NULL, *disks = NULL;
size_t nHostBind = 0;
size_t nHostImage = 0;
gchar *configdir = g_strdup_printf("%s/config", statedir);
@@ -512,6 +513,34 @@ static gboolean
gvir_sandbox_builder_machine_construct_devices(GVirSandboxBuilde
construct_devices(builder, config, statedir, domain, error))
goto cleanup;
+ tmp = disks = gvir_sandbox_config_get_disks(config);
+ while(tmp){
+ GVirSandboxConfigDisk *dconfig = GVIR_SANDBOX_CONFIG_DISK(tmp->data);
+
+ if (GVIR_SANDBOX_IS_CONFIG_DISK(dconfig)){
+
+ disk = gvir_config_domain_disk_new();
+ diskDriver = gvir_config_domain_disk_driver_new();
+ gvir_config_domain_disk_set_type(disk,
+
gvir_sandbox_config_disk_get_disk_type(dconfig));
+ gvir_config_domain_disk_driver_set_format(diskDriver,
+
gvir_sandbox_config_disk_get_format(dconfig));
+ gvir_config_domain_disk_set_source(disk,
gvir_sandbox_config_disk_get_source(dconfig));
+ gvir_config_domain_disk_set_target_bus(disk,
+ GVIR_CONFIG_DOMAIN_DISK_BUS_IDE);
+ gvir_config_domain_disk_set_target_dev(disk,
+
gvir_sandbox_config_disk_get_target(dconfig));
+ gvir_config_domain_disk_set_driver(disk, diskDriver);
+ gvir_config_domain_add_device(domain, GVIR_CONFIG_DOMAIN_DEVICE(disk));
+ g_object_unref(disk);
+ }
+ tmp = tmp->next;
+ }
+
+ g_list_foreach(disks, (GFunc)g_object_unref, NULL);
+ g_list_free(disks);
+
+
fs = gvir_config_domain_filesys_new();
gvir_config_domain_filesys_set_type(fs, GVIR_CONFIG_DOMAIN_FILESYS_MOUNT);
gvir_config_domain_filesys_set_access_type(fs,
GVIR_CONFIG_DOMAIN_FILESYS_ACCESS_SQUASH);
--
2.1.0