1 /*
2  * Copyright 2009-2017 Citrix Ltd and other contributors
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; version 2.1 only. with the special
7  * exception on linking described in file LICENSE.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  */
14 
15 #include <stdlib.h>
16 
17 #include <libxl.h>
18 #include <libxl_utils.h>
19 #include <libxlutil.h>
20 
21 #include "xl.h"
22 #include "xl_utils.h"
23 #include "xl_parse.h"
24 
main_blockattach(int argc,char ** argv)25 int main_blockattach(int argc, char **argv)
26 {
27     int opt;
28     uint32_t fe_domid;
29     libxl_device_disk disk;
30     XLU_Config *config = 0;
31 
32     SWITCH_FOREACH_OPT(opt, "", NULL, "block-attach", 2) {
33         /* No options */
34     }
35 
36     if (libxl_domain_qualifier_to_domid(ctx, argv[optind], &fe_domid) < 0) {
37         fprintf(stderr, "%s is an invalid domain identifier\n", argv[optind]);
38         return 1;
39     }
40     optind++;
41 
42     parse_disk_config_multistring
43         (&config, argc-optind, (const char* const*)argv + optind, &disk);
44 
45     if (dryrun_only) {
46         char *json = libxl_device_disk_to_json(ctx, &disk);
47         printf("disk: %s\n", json);
48         free(json);
49         if (ferror(stdout) || fflush(stdout)) { perror("stdout"); exit(-1); }
50         return 0;
51     }
52 
53     if (libxl_device_disk_add(ctx, fe_domid, &disk, 0)) {
54         fprintf(stderr, "libxl_device_disk_add failed.\n");
55         return 1;
56     }
57     return 0;
58 }
59 
main_blocklist(int argc,char ** argv)60 int main_blocklist(int argc, char **argv)
61 {
62     int opt;
63     int i, nb;
64     libxl_device_disk *disks;
65     libxl_diskinfo diskinfo;
66 
67     SWITCH_FOREACH_OPT(opt, "", NULL, "block-list", 1) {
68         /* No options */
69     }
70 
71     printf("%-5s %-3s %-6s %-5s %-6s %-8s %-30s\n",
72            "Vdev", "BE", "handle", "state", "evt-ch", "ring-ref", "BE-path");
73     for (argv += optind, argc -= optind; argc > 0; --argc, ++argv) {
74         uint32_t domid;
75         if (libxl_domain_qualifier_to_domid(ctx, *argv, &domid) < 0) {
76             fprintf(stderr, "%s is an invalid domain identifier\n", *argv);
77             continue;
78         }
79         disks = libxl_device_disk_list(ctx, domid, &nb);
80         if (!disks) {
81             continue;
82         }
83         for (i=0; i<nb; i++) {
84             if (!libxl_device_disk_getinfo(ctx, domid, &disks[i], &diskinfo)) {
85                 /*      Vdev BE   hdl  st   evch rref BE-path*/
86                 printf("%-5d %-3d %-6d %-5d %-6d %-8d %-30s\n",
87                        diskinfo.devid, diskinfo.backend_id, diskinfo.frontend_id,
88                        diskinfo.state, diskinfo.evtch, diskinfo.rref, diskinfo.backend);
89                 libxl_diskinfo_dispose(&diskinfo);
90             }
91         }
92         libxl_device_disk_list_free(disks, nb);
93     }
94     return 0;
95 }
96 
main_blockdetach(int argc,char ** argv)97 int main_blockdetach(int argc, char **argv)
98 {
99     uint32_t domid;
100     int opt, rc = 0;
101     libxl_device_disk disk;
102 
103     SWITCH_FOREACH_OPT(opt, "", NULL, "block-detach", 2) {
104         /* No options */
105     }
106 
107     domid = find_domain(argv[optind]);
108 
109     if (libxl_vdev_to_device_disk(ctx, domid, argv[optind+1], &disk)) {
110         fprintf(stderr, "Error: Device %s not connected.\n", argv[optind+1]);
111         return 1;
112     }
113     rc = libxl_device_disk_remove(ctx, domid, &disk, 0);
114     if (rc) {
115         fprintf(stderr, "libxl_device_disk_remove failed.\n");
116         return 1;
117     }
118     libxl_device_disk_dispose(&disk);
119     return rc;
120 }
121 
122 /*
123  * Local variables:
124  * mode: C
125  * c-basic-offset: 4
126  * indent-tabs-mode: nil
127  * End:
128  */
129