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     static struct option opts[] = {
100         {"force", 0, 0, 'f'},
101         COMMON_LONG_OPTS
102     };
103     uint32_t domid;
104     int opt, rc = 0;
105     libxl_device_disk disk;
106     bool force = false;
107 
108     SWITCH_FOREACH_OPT(opt, "f", opts, "block-detach", 2) {
109     case 'f':
110         force = true;
111         break;
112     default:
113         break;
114     }
115 
116     domid = find_domain(argv[optind]);
117 
118     if (libxl_vdev_to_device_disk(ctx, domid, argv[optind+1], &disk)) {
119         fprintf(stderr, "Error: Device %s not connected.\n", argv[optind+1]);
120         return 1;
121     }
122 
123     if (disk.specification == LIBXL_DISK_SPECIFICATION_VIRTIO) {
124         fprintf(stderr, "Hotunplug isn't supported for specification virtio\n");
125         return 1;
126     }
127 
128     rc = !force ? libxl_device_disk_safe_remove(ctx, domid, &disk, 0) :
129         libxl_device_disk_destroy(ctx, domid, &disk, 0);
130     if (rc) {
131         fprintf(stderr, "libxl_device_disk_%s failed.\n",
132                 !force ? "safe_remove" : "destroy");
133         return 1;
134     }
135     libxl_device_disk_dispose(&disk);
136     return rc;
137 }
138 
139 /*
140  * Local variables:
141  * mode: C
142  * c-basic-offset: 4
143  * indent-tabs-mode: nil
144  * End:
145  */
146