ISCSI: Pick default initiator-name based on the name of the VM
PATCH 1/1 ISCSI: Pick default initiator-name based on the name of the VM
From: Ronnie Sahlberg <[email protected]>
This patch updates the iscsi layer to automatically pick a 'unique' initiator-name based on the name of the vm in case the user has not set an explicit iqn-name to use.
Create a new function qemu_get_vm_name() that returns the name of the VM, if specified.
This way we can thus create default names to use as the initiator name based on the guest session.
If the VM is not named via the '-name' command line argument, the iscsi initiator-name used wiull simply be
iqn.2008-11.org.linux-kvm
If a name for the VM was specified with the '-name' option, iscsi will use a default initiatorname of
iqn.2008-11.org.linux-kvm:<name>
These names are just the default iscsi initiator name that qemu will generate/use only when the user has not set an explicit initiator name via the commandlines or config files.
--- block/iscsi.c | 14 +++++++++++--- qemu-common.h | 1 + qemu-doc.texi | 5 +++++ qemu-options.hx | 8 ++++++++ qemu-tool.c | 5 +++++ vl.c | 5 +++++ 6 files changed, 35 insertions(+), 3 deletions(-)
block/iscsi.c +11 -3
@@ -896,23 +896,31 @@ static char *parse_initiator_name(const char *target)QemuOptsList *list;QemuOpts *opts;const char *name =NULL ; +char *default_name; +const char *iscsi_name =qemu_get_vm_name (); + +if (asprintf (&default_name,"iqn.2008-11.org.linux-kvm%s%s" , + iscsi_name ?":" :"" , + iscsi_name ? iscsi_name :"" ) ==-1 ) { + default_name = (char *)"iqn.2008-11.org.linux-kvm" ; + } list =qemu_find_opts ("iscsi" );if (!list) { -return g_strdup ("iqn.2008-11.org.linux-kvm" ); +return g_strdup (default_name); } opts =qemu_opts_find (list, target);if (opts ==NULL ) { opts =QTAILQ_FIRST (&list->head);if (!opts) { -return g_strdup ("iqn.2008-11.org.linux-kvm" ); +return g_strdup (default_name); } } name =qemu_opt_get (opts,"initiator-name" );if (!name) { -return g_strdup ("iqn.2008-11.org.linux-kvm" ); +return g_strdup (default_name); }return g_strdup (name);
qemu-common.h +1 -0
@@ -376,6 +376,7 @@ bool buffer_is_zero(const void *buf, size_t len);void qemu_progress_init (int enabled,float min_skip);void qemu_progress_end (void );void qemu_progress_print (float delta,int max); +const char *qemu_get_vm_name (void );#define QEMU_FILE_TYPE_BIOS 0#define QEMU_FILE_TYPE_KEYMAP 1
qemu-doc.texi +5 -0
@@ -734,6 +734,11 @@ Various session related parameters can be set via special options, either in a configuration file provided via '-readconfig' or directly on the command line. +If the initiator-name is not specified qemu will use a default name +of 'iqn.2008-11.org.linux-kvm[:<name>'] where <name> is the name of the +virtual machine. + + @example Setting a specific initiator name to use when logging in to the target -iscsi initiator-name=iqn.qemu.test:my-initiator
qemu-options.hx +8 -0
@@ -1897,6 +1897,11 @@ images for the guest storage. Both disk and cdrom images are supported. Syntax for specifying iSCSI LUNs is ``iscsi://<target-ip>[:<port>]/<target-iqn>/<lun>'' +By default qemu will use the iSCSI initiator-name +'iqn.2008-11.org.linux-kvm[:<name>]' but this can also be set from the command +line or a configuration file. + + Example (without authentication): @example qemu-system-i386 -iscsi initiator-name=iqn.2001-04.com.example:my-initiator \
@@ -1926,6 +1931,9 @@ DEF("iscsi", HAS_ARG, QEMU_OPTION_iscsi, " iSCSI session parameters\n", QEMU_ARCH_ALL) STEXI +iSCSI parameters such as username and password can also be specified via +a configuration file. See qemu-doc for more information and examples. + @item NBD QEMU supports NBD (Network Block Devices) both using TCP protocol as well as Unix Domain Sockets.
qemu-tool.c +5 -0
@@ -30,6 +30,11 @@ struct QEMUBHvoid *opaque; }; +const char *qemu_get_vm_name (void ) +{ +return NULL ; +} +Monitor *cur_mon;int monitor_cur_is_qmp (void )
vl.c +5 -0
@@ -293,6 +293,11 @@ static struct { { .driver = "qxl-vga", .flag = &default_vga }, }; +const char *qemu_get_vm_name (void ) +{ +return qemu_name; +} +static void res_free (void ) {if (boot_splash_filedata !=NULL ) {
Signature · 1 line
1.7.3.1
Il 06/08/2012 10:24, [email protected] ha scritto:
block/iscsi.c static char *parse_initiator_name(const char *target) · line 902
Quoted 12 lines
block/iscsi.c +4
-0@@ -896,23 +896,31 @@ static char *parse_initiator_name(const char *target)QemuOptsList *list;QemuOpts *opts;const char *name =NULL ; +char *default_name; +const char *iscsi_name =qemu_get_vm_name (); + + if (asprintf(&default_name, "iqn.2008-11.org.linux-kvm%s%s",
asprintf is not portable, g_strdup_printf is.
Quoted 16 lines
block/iscsi.c +5
-2+ iscsi_name ? ":" : "", + iscsi_name ? iscsi_name : "") == -1) { + default_name = (char *)"iqn.2008-11.org.linux-kvm" ; + } list =qemu_find_opts ("iscsi" );if (!list) { -return g_strdup ("iqn.2008-11.org.linux-kvm" ); +return g_strdup (default_name); } opts =qemu_opts_find (list, target);if (opts ==NULL ) { opts =QTAILQ_FIRST (&list->head);if (!opts) { -return g_strdup ("iqn.2008-11.org.linux-kvm" );
block/iscsi.c static char *parse_initiator_name(const char *target) · line 925
Quoted 10 lines
block/iscsi.c +2
-1+return g_strdup (default_name); } } name =qemu_opt_get (opts,"initiator-name" );if (!name) { -return g_strdup ("iqn.2008-11.org.linux-kvm" ); +return g_strdup (default_name); }
Each of these strdup calls is leaking the original default_name.
block/iscsi.c static char *parse_initiator_name(const char *target) · line 926
Quoted 1 line
block/iscsi.c +0
-0return g_strdup (name);
iscsi_open is never going to free this, but neither is libiscsi.
Putting all this together, we need on top of your patch something like this:
block/iscsi.c +30 -35
@@ -899,31 +899,27 @@ static char *parse_initiator_name(const char *target)char *default_name;const char *iscsi_name =qemu_get_vm_name (); -if (asprintf (&default_name,"iqn.2008-11.org.linux-kvm%s%s" , - iscsi_name ?":" :"" , - iscsi_name ? iscsi_name :"" ) ==-1 ) { - default_name = (char *)"iqn.2008-11.org.linux-kvm" ; - } + default_name =g_strdup_printf (&default_name,"iqn.2008-11.org.linux-kvm%s%s" , + iscsi_name ?":" :"" , + iscsi_name ? iscsi_name :"" ); list =qemu_find_opts ("iscsi" ); -if (!list) { -return g_strdup (default_name); - } - - opts =qemu_opts_find (list, target); -if (opts ==NULL ) { - opts =QTAILQ_FIRST (&list->head); +if (list) { + opts =qemu_opts_find (list, target);if (!opts) { -return g_strdup (default_name); + opts =QTAILQ_FIRST (&list->head); + } +if (opts) { + name =qemu_opt_get (opts,"initiator-name" ); } } - name =qemu_opt_get (opts,"initiator-name" ); -if (!name) { -return g_strdup (default_name); +if (name) { +g_free (default_name); +return g_strdup (name); + }else { +return default_name; } - -return g_strdup (name); }/*
@@ -951,7 +947,7 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags)error_report ("Failed to parse URL : %s %s" , filename,iscsi_get_error (iscsi)); ret = -EINVAL ; - goto failed; + goto out; }memset (iscsilun,0 ,sizeof (IscsiLun));
@@ -962,13 +958,13 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags)if (iscsi ==NULL ) {error_report ("iSCSI: Failed to create iSCSI context." ); ret = -ENOMEM ; - goto failed; + goto out; }if (iscsi_set_targetname (iscsi, iscsi_url->target)) {error_report ("iSCSI: Failed to set target name." ); ret = -EINVAL ; - goto failed; + goto out; }if (iscsi_url->user !=NULL ) {
@@ -977,7 +973,7 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags)if (ret !=0 ) {error_report ("Failed to set initiator username and password" ); ret = -EINVAL ; - goto failed; + goto out; } }
@@ -985,13 +981,13 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags)if (parse_chap (iscsi, iscsi_url->target) !=0 ) {error_report ("iSCSI: Failed to set CHAP user/password" ); ret = -EINVAL ; - goto failed; + goto out; }if (iscsi_set_session_type (iscsi,ISCSI_SESSION_NORMAL ) !=0 ) {error_report ("iSCSI: Failed to set session type to normal." ); ret = -EINVAL ; - goto failed; + goto out; }iscsi_set_header_digest (iscsi,ISCSI_HEADER_DIGEST_NONE_CRC32C );
@@ -1012,7 +1008,7 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags) != 0) {error_report ("iSCSI: Failed to start async connect." ); ret = -EINVAL ; - goto failed; + goto out; }while (!task.complete) {
@@ -1023,11 +1019,7 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags)error_report ("iSCSI: Failed to connect to LUN : %s" ,iscsi_get_error (iscsi)); ret = -EINVAL ; - goto failed; - } - -if (iscsi_url !=NULL ) { -iscsi_destroy_url (iscsi_url); + goto out; }/* Medium changer or tape. We dont have any emulation for this so this must
@@ -1039,19 +1031,22 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags) bs->sg =1 ; } -return 0 ; + ret =0 ; -failed: +out:if (initiator_name !=NULL ) {g_free (initiator_name); }if (iscsi_url !=NULL ) {iscsi_destroy_url (iscsi_url); } -if (iscsi !=NULL ) { -iscsi_destroy_context (iscsi); + +if (ret) { +if (iscsi !=NULL ) { +iscsi_destroy_context (iscsi); + } +memset (iscsilun,0 ,sizeof (IscsiLun)); } -memset (iscsilun,0 ,sizeof (IscsiLun));return ret; }
Can you confirm that this is how the initiator name should be passed, so I can split the above patch and commit it?
Paolo
Paolo,
Quoted 204 lines · Paolo Bonzini
Il 06/08/2012 10:24, [email protected] ha scritto:
block/iscsi.c static char *parse_initiator_name(const char *target) · line 902
Quoted 12 lines
block/iscsi.c +4
-0@@ -896,23 +896,31 @@ static char *parse_initiator_name(const char *target)QemuOptsList *list;QemuOpts *opts;const char *name =NULL ; +char *default_name; +const char *iscsi_name =qemu_get_vm_name (); + + if (asprintf(&default_name, "iqn.2008-11.org.linux-kvm%s%s",asprintf is not portable,
g_strdup_printfis.Quoted 16 lines
block/iscsi.c +5
-2+ iscsi_name ? ":" : "", + iscsi_name ? iscsi_name : "") == -1) { + default_name = (char *)"iqn.2008-11.org.linux-kvm" ; + } list =qemu_find_opts ("iscsi" );if (!list) { -return g_strdup ("iqn.2008-11.org.linux-kvm" ); +return g_strdup (default_name); } opts =qemu_opts_find (list, target);if (opts ==NULL ) { opts =QTAILQ_FIRST (&list->head);if (!opts) { -return g_strdup ("iqn.2008-11.org.linux-kvm" );block/iscsi.c static char *parse_initiator_name(const char *target) · line 925
Quoted 10 lines
block/iscsi.c +2
-1+return g_strdup (default_name); } } name =qemu_opt_get (opts,"initiator-name" );if (!name) { -return g_strdup ("iqn.2008-11.org.linux-kvm" ); +return g_strdup (default_name); }Each of these strdup calls is leaking the original
default_name.block/iscsi.c static char *parse_initiator_name(const char *target) · line 926
Quoted 1 line
block/iscsi.c +0
-0return g_strdup (name);
iscsi_openis never going to free this, but neither is libiscsi.Putting all this together, we need on top of your patch something like this:
block/iscsi.c +30
-35@@ -899,31 +899,27 @@ static char *parse_initiator_name(const char *target)char *default_name;const char *iscsi_name =qemu_get_vm_name (); -if (asprintf (&default_name,"iqn.2008-11.org.linux-kvm%s%s" , - iscsi_name ?":" :"" , - iscsi_name ? iscsi_name :"" ) ==-1 ) { - default_name = (char *)"iqn.2008-11.org.linux-kvm" ; - } + default_name =g_strdup_printf (&default_name,"iqn.2008-11.org.linux-kvm%s%s" , + iscsi_name ?":" :"" , + iscsi_name ? iscsi_name :"" ); list =qemu_find_opts ("iscsi" ); -if (!list) { -return g_strdup (default_name); - } - - opts =qemu_opts_find (list, target); -if (opts ==NULL ) { - opts =QTAILQ_FIRST (&list->head); +if (list) { + opts =qemu_opts_find (list, target);if (!opts) { -return g_strdup (default_name); + opts =QTAILQ_FIRST (&list->head); + } +if (opts) { + name =qemu_opt_get (opts,"initiator-name" ); } } - name =qemu_opt_get (opts,"initiator-name" ); -if (!name) { -return g_strdup (default_name); +if (name) { +g_free (default_name); +return g_strdup (name); + }else { +return default_name; } - -return g_strdup (name); }/* @@ -951,7 +947,7 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags)error_report ("Failed to parse URL : %s %s" , filename,iscsi_get_error (iscsi)); ret = -EINVAL ; - goto failed; + goto out; }memset (iscsilun,0 ,sizeof (IscsiLun));@@ -962,13 +958,13 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags)if (iscsi ==NULL ) {error_report ("iSCSI: Failed to create iSCSI context." ); ret = -ENOMEM ; - goto failed; + goto out; }if (iscsi_set_targetname (iscsi, iscsi_url->target)) {error_report ("iSCSI: Failed to set target name." ); ret = -EINVAL ; - goto failed; + goto out; }if (iscsi_url->user !=NULL ) {@@ -977,7 +973,7 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags)if (ret !=0 ) {error_report ("Failed to set initiator username and password" ); ret = -EINVAL ; - goto failed; + goto out; } }@@ -985,13 +981,13 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags)if (parse_chap (iscsi, iscsi_url->target) !=0 ) {error_report ("iSCSI: Failed to set CHAP user/password" ); ret = -EINVAL ; - goto failed; + goto out; }if (iscsi_set_session_type (iscsi,ISCSI_SESSION_NORMAL ) !=0 ) {error_report ("iSCSI: Failed to set session type to normal." ); ret = -EINVAL ; - goto failed; + goto out; }iscsi_set_header_digest (iscsi,ISCSI_HEADER_DIGEST_NONE_CRC32C );@@ -1012,7 +1008,7 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags) != 0) {error_report ("iSCSI: Failed to start async connect." ); ret = -EINVAL ; - goto failed; + goto out; }while (!task.complete) {@@ -1023,11 +1019,7 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags)error_report ("iSCSI: Failed to connect to LUN : %s" ,iscsi_get_error (iscsi)); ret = -EINVAL ; - goto failed; - } - -if (iscsi_url !=NULL ) { -iscsi_destroy_url (iscsi_url); + goto out; }/* Medium changer or tape. We dont have any emulation for this so this must @@ -1039,19 +1031,22 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags) bs->sg =1 ; } -return 0 ; + ret =0 ; -failed: +out:if (initiator_name !=NULL ) {g_free (initiator_name); }if (iscsi_url !=NULL ) {iscsi_destroy_url (iscsi_url); } -if (iscsi !=NULL ) { -iscsi_destroy_context (iscsi); + +if (ret) { +if (iscsi !=NULL ) { +iscsi_destroy_context (iscsi); + } +memset (iscsilun,0 ,sizeof (IscsiLun)); } -memset (iscsilun,0 ,sizeof (IscsiLun));return ret; }
Quoted 3 lines
Can you confirm that this is how the initiator name should be passed, so I can split the above patch and commit it?
That looks good.
Thanks!
ronnie sahlberg