1 /*
2  * viriscsi.c: helper APIs for managing iSCSI
3  *
4  * Copyright (C) 2007-2014 Red Hat, Inc.
5  * Copyright (C) 2007-2008 Daniel P. Berrange
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library.  If not, see
19  * <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #include <config.h>
24 
25 #include "viriscsi.h"
26 
27 #include "viralloc.h"
28 #include "vircommand.h"
29 #include "virerror.h"
30 #include "virfile.h"
31 #include "virlog.h"
32 #include "virrandom.h"
33 #include "virstring.h"
34 
35 #define VIR_FROM_THIS VIR_FROM_NONE
36 
37 VIR_LOG_INIT("util.iscsi");
38 
39 
40 static int
41 virISCSIScanTargetsInternal(const char *portal,
42                             const char *ifacename,
43                             bool persist,
44                             size_t *ntargetsret,
45                             char ***targetsret);
46 
47 
48 struct virISCSISessionData {
49     char *session;
50     const char *devpath;
51 };
52 
53 
54 static int
virISCSIExtractSession(char ** const groups,void * opaque)55 virISCSIExtractSession(char **const groups,
56                        void *opaque)
57 {
58     struct virISCSISessionData *data = opaque;
59 
60     if (!data->session &&
61         STREQ(groups[1], data->devpath)) {
62         data->session = g_strdup(groups[0]);
63         return 0;
64     }
65     return 0;
66 }
67 
68 
69 char *
virISCSIGetSession(const char * devpath,bool probe)70 virISCSIGetSession(const char *devpath,
71                    bool probe)
72 {
73     /*
74      * # iscsiadm --mode session
75      * tcp: [1] 192.168.122.170:3260,1 demo-tgt-b
76      * tcp: [2] 192.168.122.170:3260,1 demo-tgt-a
77      *
78      * Pull out 2nd and 4th fields
79      */
80     const char *regexes[] = {
81         "^tcp:\\s+\\[(\\S+)\\]\\s+\\S+\\s+(\\S+).*$"
82     };
83     int vars[] = {
84         2,
85     };
86     struct virISCSISessionData cbdata = {
87         .session = NULL,
88         .devpath = devpath,
89     };
90     int exitstatus = 0;
91     g_autofree char *error = NULL;
92 
93     g_autoptr(virCommand) cmd = virCommandNewArgList(ISCSIADM, "--mode",
94                                                        "session", NULL);
95     virCommandSetErrorBuffer(cmd, &error);
96 
97     if (virCommandRunRegex(cmd,
98                            1,
99                            regexes,
100                            vars,
101                            virISCSIExtractSession,
102                            &cbdata, NULL, &exitstatus) < 0)
103         return NULL;
104 
105     if (cbdata.session == NULL && !probe)
106         virReportError(VIR_ERR_INTERNAL_ERROR,
107                        _("cannot find iscsiadm session: %s"),
108                        NULLSTR(error));
109 
110     return cbdata.session;
111 }
112 
113 
114 
115 #define IQN_FOUND 1
116 #define IQN_MISSING 0
117 #define IQN_ERROR -1
118 
119 static int
virStorageBackendIQNFound(const char * initiatoriqn,char ** ifacename)120 virStorageBackendIQNFound(const char *initiatoriqn,
121                           char **ifacename)
122 {
123     int ret = IQN_ERROR;
124     char *line = NULL;
125     g_autofree char *outbuf = NULL;
126     g_autofree char *iface = NULL;
127     g_autofree char *iqn = NULL;
128     g_autoptr(virCommand) cmd = virCommandNewArgList(ISCSIADM,
129                                                        "--mode", "iface", NULL);
130 
131     *ifacename = NULL;
132 
133     virCommandSetOutputBuffer(cmd, &outbuf);
134     if (virCommandRun(cmd, NULL) < 0)
135         goto cleanup;
136 
137     /* Example of data we are dealing with:
138      * default tcp,<empty>,<empty>,<empty>,<empty>
139      * iser iser,<empty>,<empty>,<empty>,<empty>
140      * libvirt-iface-253db048 tcp,<empty>,<empty>,<empty>,iqn.2017-03.com.user:client
141      */
142 
143     line = outbuf;
144     while (line && *line) {
145         char *current = line;
146         char *newline;
147         char *next;
148         size_t i;
149 
150         if (!(newline = strchr(line, '\n')))
151             break;
152 
153         *newline = '\0';
154 
155         VIR_FREE(iface);
156         VIR_FREE(iqn);
157 
158         /* Find the first space, copy everything up to that point into
159          * iface and move past it to continue processing */
160         if (!(next = strchr(current, ' ')))
161             goto error;
162 
163         iface = g_strndup(current, next - current);
164 
165         current = next + 1;
166 
167         /* There are five comma separated fields after iface and we only
168          * care about the last one, so we need to skip four commas and
169          * copy whatever's left into iqn */
170         for (i = 0; i < 4; i++) {
171             if (!(next = strchr(current, ',')))
172                 goto error;
173             current = next + 1;
174         }
175 
176         iqn = g_strdup(current);
177 
178         if (STREQ(iqn, initiatoriqn)) {
179             *ifacename = g_steal_pointer(&iface);
180 
181             VIR_DEBUG("Found interface '%s' with IQN '%s'", *ifacename, iqn);
182             break;
183         }
184 
185         line = newline + 1;
186     }
187 
188     ret = *ifacename ? IQN_FOUND : IQN_MISSING;
189 
190  cleanup:
191     if (ret == IQN_MISSING)
192         VIR_DEBUG("Could not find interface with IQN '%s'", iqn);
193 
194     return ret;
195 
196  error:
197     virReportError(VIR_ERR_INTERNAL_ERROR,
198                    _("malformed output of %s: %s"),
199                    ISCSIADM, line);
200     goto cleanup;
201 }
202 
203 
204 static int
virStorageBackendCreateIfaceIQN(const char * initiatoriqn,char ** ifacename)205 virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
206                                 char **ifacename)
207 {
208     int exitstatus = -1;
209     g_autofree char *iface_name = NULL;
210     g_autofree char *temp_ifacename = NULL;
211     g_autoptr(virCommand) cmd = NULL;
212 
213     temp_ifacename = g_strdup_printf("libvirt-iface-%08llx",
214                                      (unsigned long long)virRandomBits(32));
215 
216     VIR_DEBUG("Attempting to create interface '%s' with IQN '%s'",
217               temp_ifacename, initiatoriqn);
218 
219     cmd = virCommandNewArgList(ISCSIADM,
220                                "--mode", "iface",
221                                "--interface", temp_ifacename,
222                                "--op", "new",
223                                NULL);
224     /* Note that we ignore the exitstatus.  Older versions of iscsiadm
225      * tools returned an exit status of > 0, even if they succeeded.
226      * We will just rely on whether the interface got created
227      * properly. */
228     if (virCommandRun(cmd, &exitstatus) < 0) {
229         virReportError(VIR_ERR_INTERNAL_ERROR,
230                        _("Failed to run command '%s' to create new iscsi interface"),
231                        ISCSIADM);
232         return -1;
233     }
234     virCommandFree(cmd);
235 
236     cmd = virCommandNewArgList(ISCSIADM,
237                                "--mode", "iface",
238                                "--interface", temp_ifacename,
239                                "--op", "update",
240                                "--name", "iface.initiatorname",
241                                "--value",
242                                initiatoriqn,
243                                NULL);
244     /* Note that we ignore the exitstatus.  Older versions of iscsiadm tools
245      * returned an exit status of > 0, even if they succeeded.  We will just
246      * rely on whether iface file got updated properly. */
247     if (virCommandRun(cmd, &exitstatus) < 0) {
248         virReportError(VIR_ERR_INTERNAL_ERROR,
249                        _("Failed to run command '%s' to update iscsi interface with IQN '%s'"),
250                        ISCSIADM, initiatoriqn);
251         return -1;
252     }
253 
254     /* Check again to make sure the interface was created. */
255     if (virStorageBackendIQNFound(initiatoriqn, &iface_name) != IQN_FOUND) {
256         VIR_DEBUG("Failed to find interface '%s' with IQN '%s' "
257                   "after attempting to create it",
258                   &temp_ifacename[0], initiatoriqn);
259         return -1;
260     } else {
261         VIR_DEBUG("Interface '%s' with IQN '%s' was created successfully",
262                   iface_name, initiatoriqn);
263     }
264 
265     *ifacename = g_steal_pointer(&iface_name);
266 
267     return 0;
268 }
269 
270 
271 static int
virISCSIConnection(const char * portal,const char * initiatoriqn,const char * target,const char ** extraargv)272 virISCSIConnection(const char *portal,
273                    const char *initiatoriqn,
274                    const char *target,
275                    const char **extraargv)
276 {
277     const char *const baseargv[] = {
278         ISCSIADM,
279         "--mode", "node",
280         "--portal", portal,
281         "--targetname", target,
282         NULL
283     };
284     g_autoptr(virCommand) cmd = NULL;
285     g_autofree char *ifacename = NULL;
286 
287     cmd = virCommandNewArgs(baseargv);
288     virCommandAddArgSet(cmd, extraargv);
289 
290     if (initiatoriqn) {
291         switch (virStorageBackendIQNFound(initiatoriqn, &ifacename)) {
292         case IQN_FOUND:
293             VIR_DEBUG("ifacename: '%s'", ifacename);
294             break;
295         case IQN_MISSING:
296             if (virStorageBackendCreateIfaceIQN(initiatoriqn, &ifacename) != 0)
297                 return -1;
298             /*
299              * iscsiadm doesn't let you send commands to the Interface IQN,
300              * unless you've first issued a 'sendtargets' command to the
301              * portal. Without the sendtargets all that is received is a
302              * "iscsiadm: No records found". However, we must ensure that
303              * the command is issued over interface name we invented above
304              * and that targets are made persistent.
305              */
306             if (virISCSIScanTargetsInternal(portal, ifacename,
307                                             true, NULL, NULL) < 0)
308                 return -1;
309 
310             break;
311         case IQN_ERROR:
312         default:
313             return -1;
314         }
315         virCommandAddArgList(cmd, "--interface", ifacename, NULL);
316     }
317 
318     if (virCommandRun(cmd, NULL) < 0)
319         return -1;
320 
321     return 0;
322 }
323 
324 
325 int
virISCSIConnectionLogin(const char * portal,const char * initiatoriqn,const char * target)326 virISCSIConnectionLogin(const char *portal,
327                         const char *initiatoriqn,
328                         const char *target)
329 {
330     const char *extraargv[] = { "--login", NULL };
331     return virISCSIConnection(portal, initiatoriqn, target, extraargv);
332 }
333 
334 
335 int
virISCSIConnectionLogout(const char * portal,const char * initiatoriqn,const char * target)336 virISCSIConnectionLogout(const char *portal,
337                          const char *initiatoriqn,
338                          const char *target)
339 {
340     const char *extraargv[] = { "--logout", NULL };
341     return virISCSIConnection(portal, initiatoriqn, target, extraargv);
342 }
343 
344 
345 int
virISCSIRescanLUNs(const char * session)346 virISCSIRescanLUNs(const char *session)
347 {
348     g_autoptr(virCommand) cmd = virCommandNewArgList(ISCSIADM,
349                                                        "--mode", "session",
350                                                        "-r", session,
351                                                        "-R",
352                                                        NULL);
353     return virCommandRun(cmd, NULL);
354 }
355 
356 
357 struct virISCSITargetList {
358     size_t ntargets;
359     char **targets;
360 };
361 
362 
363 static int
virISCSIGetTargets(char ** const groups,void * data)364 virISCSIGetTargets(char **const groups,
365                    void *data)
366 {
367     struct virISCSITargetList *list = data;
368     g_autofree char *target = NULL;
369 
370     target = g_strdup(groups[1]);
371 
372     VIR_APPEND_ELEMENT(list->targets, list->ntargets, target);
373 
374     return 0;
375 }
376 
377 
378 static int
virISCSIScanTargetsInternal(const char * portal,const char * ifacename,bool persist,size_t * ntargetsret,char *** targetsret)379 virISCSIScanTargetsInternal(const char *portal,
380                             const char *ifacename,
381                             bool persist,
382                             size_t *ntargetsret,
383                             char ***targetsret)
384 {
385     /**
386      *
387      * The output of sendtargets is very simple, just two columns,
388      * portal then target name
389      *
390      * 192.168.122.185:3260,1 iqn.2004-04.com:fedora14:iscsi.demo0.bf6d84
391      * 192.168.122.185:3260,1 iqn.2004-04.com:fedora14:iscsi.demo1.bf6d84
392      * 192.168.122.185:3260,1 iqn.2004-04.com:fedora14:iscsi.demo2.bf6d84
393      * 192.168.122.185:3260,1 iqn.2004-04.com:fedora14:iscsi.demo3.bf6d84
394      */
395     const char *regexes[] = {
396         "^\\s*(\\S+)\\s+(\\S+)\\s*$"
397     };
398     int vars[] = { 2 };
399     struct virISCSITargetList list;
400     size_t i;
401     g_autoptr(virCommand) cmd = virCommandNewArgList(ISCSIADM,
402                                                        "--mode", "discovery",
403                                                        "--type", "sendtargets",
404                                                        "--portal", portal,
405                                                        NULL);
406 
407     if (!persist) {
408         virCommandAddArgList(cmd,
409                              "--op", "nonpersistent",
410                              NULL);
411     }
412 
413     if (ifacename) {
414         virCommandAddArgList(cmd,
415                              "--interface", ifacename,
416                              NULL);
417     }
418 
419     memset(&list, 0, sizeof(list));
420 
421     if (virCommandRunRegex(cmd,
422                            1,
423                            regexes,
424                            vars,
425                            virISCSIGetTargets,
426                            &list, NULL, NULL) < 0)
427         return -1;
428 
429     if (ntargetsret && targetsret) {
430         *ntargetsret = list.ntargets;
431         *targetsret = list.targets;
432     } else {
433         for (i = 0; i < list.ntargets; i++)
434             VIR_FREE(list.targets[i]);
435         VIR_FREE(list.targets);
436     }
437 
438     return 0;
439 }
440 
441 
442 /**
443  * virISCSIScanTargets:
444  * @portal: iSCSI portal
445  * @initiatoriqn: Initiator IQN
446  * @persists: whether scanned targets should be saved
447  * @ntargets: number of items in @targetsret array
448  * @targets: array of targets
449  *
450  * For given @portal issue sendtargets command. Optionally,
451  * @initiatoriqn can be set to override default configuration.
452  * The targets are stored into @targets array and the size of
453  * the array is stored into @ntargets.
454  *
455  * If @persist is true, then targets returned by iSCSI portal are
456  * made persistent on the host (their config is saved).
457  *
458  * Returns: 0 on success,
459  *         -1 otherwise (with error reported)
460  */
461 int
virISCSIScanTargets(const char * portal,const char * initiatoriqn,bool persist,size_t * ntargets,char *** targets)462 virISCSIScanTargets(const char *portal,
463                     const char *initiatoriqn,
464                     bool persist,
465                     size_t *ntargets,
466                     char ***targets)
467 {
468     g_autofree char *ifacename = NULL;
469 
470     if (ntargets)
471         *ntargets = 0;
472     if (targets)
473         *targets = NULL;
474 
475     if (initiatoriqn) {
476         switch ((virStorageBackendIQNFound(initiatoriqn, &ifacename))) {
477         case IQN_FOUND:
478             break;
479 
480         case IQN_MISSING:
481             virReportError(VIR_ERR_OPERATION_FAILED,
482                            _("no iSCSI interface defined for IQN %s"),
483                            initiatoriqn);
484             G_GNUC_FALLTHROUGH;
485         case IQN_ERROR:
486         default:
487             return -1;
488         }
489     }
490 
491     return virISCSIScanTargetsInternal(portal, ifacename,
492                                        persist, ntargets, targets);
493 }
494 
495 
496 /*
497  * virISCSINodeNew:
498  * @portal: address for iSCSI target
499  * @target: IQN and specific LUN target
500  *
501  * Usage of nonpersistent discovery in virISCSIScanTargets is useful primarily
502  * only when the target IQN is not known; however, since we have the target IQN
503  * usage of the "--op new" can be done. This avoids problems if "--op delete"
504  * had been used wiping out the static nodes determined by the scanning of
505  * all targets.
506  *
507  * NB: If an iSCSI node record is already created for this portal and
508  * target, subsequent "--op new" commands do not return an error.
509  *
510  * Returns 0 on success, -1 w/ error message on error
511  */
512 int
virISCSINodeNew(const char * portal,const char * target)513 virISCSINodeNew(const char *portal,
514                 const char *target)
515 {
516     g_autoptr(virCommand) cmd = NULL;
517     int status;
518 
519     cmd = virCommandNewArgList(ISCSIADM,
520                                "--mode", "node",
521                                "--portal", portal,
522                                "--targetname", target,
523                                "--op", "new",
524                                NULL);
525 
526     if (virCommandRun(cmd, &status) < 0) {
527         virReportError(VIR_ERR_INTERNAL_ERROR,
528                        _("Failed new node mode for target '%s'"),
529                        target);
530         return -1;
531     }
532 
533     if (status != 0) {
534         virReportError(VIR_ERR_INTERNAL_ERROR,
535                        _("%s failed new mode for target '%s' with status '%d'"),
536                        ISCSIADM, target, status);
537         return -1;
538     }
539 
540     return 0;
541 }
542 
543 
544 int
virISCSINodeUpdate(const char * portal,const char * target,const char * name,const char * value)545 virISCSINodeUpdate(const char *portal,
546                    const char *target,
547                    const char *name,
548                    const char *value)
549 {
550     g_autoptr(virCommand) cmd = NULL;
551     int status;
552 
553     cmd = virCommandNewArgList(ISCSIADM,
554                                "--mode", "node",
555                                "--portal", portal,
556                                "--target", target,
557                                "--op", "update",
558                                "--name", name,
559                                "--value", value,
560                                NULL);
561 
562     /* Ignore non-zero status.  */
563     if (virCommandRun(cmd, &status) < 0) {
564         virReportError(VIR_ERR_INTERNAL_ERROR,
565                        _("Failed to update '%s' of node mode for target '%s'"),
566                        name, target);
567         return -1;
568     }
569 
570     return 0;
571 }
572