1 /*
2  * storage_conf.h: config handling for storage driver
3  *
4  * Copyright (C) 2006-2008, 2010-2016 Red Hat, Inc.
5  * Copyright (C) 2006-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 #pragma once
23 
24 #include "internal.h"
25 #include "storage_encryption_conf.h"
26 #include "storage_source_conf.h"
27 #include "virbitmap.h"
28 #include "virthread.h"
29 #include "device_conf.h"
30 #include "object_event.h"
31 #include "storage_adapter_conf.h"
32 #include "virenum.h"
33 #include "virxml.h"
34 
35 
36 
37 int
38 virStoragePoolOptionsPoolTypeSetXMLNamespace(int type,
39                                              virXMLNamespace *ns);
40 
41 int
42 virStoragePoolOptionsFormatPool(virBuffer *buf,
43                                 int type);
44 
45 int
46 virStoragePoolOptionsFormatVolume(virBuffer *buf,
47                                   int type);
48 /*
49  * How the volume's data is stored on underlying
50  * physical devices - can potentially span many
51  * devices in LVM case.
52  */
53 typedef struct _virStorageVolSourceExtent virStorageVolSourceExtent;
54 struct _virStorageVolSourceExtent {
55     char *path;
56     unsigned long long start;
57     unsigned long long end;
58 };
59 
60 typedef struct _virStorageVolSource virStorageVolSource;
61 struct _virStorageVolSource {
62     size_t nextent;
63     virStorageVolSourceExtent *extents;
64 
65     int partType; /* virStorageVolTypeDisk, only used by disk
66                    * backend for partition type creation */
67 };
68 
69 typedef enum {
70     VIR_STORAGE_VOL_DEF_REFRESH_ALLOCATION_DEFAULT,  /* compute actual allocation */
71     VIR_STORAGE_VOL_DEF_REFRESH_ALLOCATION_CAPACITY, /* use logical capacity */
72     VIR_STORAGE_VOL_DEF_REFRESH_ALLOCATION_LAST,
73 } virStorageVolDefRefreshAllocation;
74 
75 VIR_ENUM_DECL(virStorageVolDefRefreshAllocation);
76 
77 typedef struct _virStorageVolDef virStorageVolDef;
78 struct _virStorageVolDef {
79     char *name;
80     char *key;
81     int type; /* virStorageVolType */
82 
83     bool building;
84     unsigned int in_use;
85 
86     virStorageVolSource source;
87     virStorageSource target;
88 };
89 
90 typedef struct _virStorageVolDefList virStorageVolDefList;
91 
92 VIR_ENUM_DECL(virStorageVol);
93 
94 typedef enum {
95     VIR_STORAGE_POOL_DIR,      /* Local directory */
96     VIR_STORAGE_POOL_FS,       /* Local filesystem */
97     VIR_STORAGE_POOL_NETFS,    /* Networked filesystem - eg NFS, GFS, etc */
98     VIR_STORAGE_POOL_LOGICAL,  /* Logical volume groups / volumes */
99     VIR_STORAGE_POOL_DISK,     /* Disk partitions */
100     VIR_STORAGE_POOL_ISCSI,    /* iSCSI targets */
101     VIR_STORAGE_POOL_ISCSI_DIRECT, /* iSCSI targets using libiscsi */
102     VIR_STORAGE_POOL_SCSI,     /* SCSI HBA */
103     VIR_STORAGE_POOL_MPATH,    /* Multipath devices */
104     VIR_STORAGE_POOL_RBD,      /* RADOS Block Device */
105     VIR_STORAGE_POOL_SHEEPDOG, /* Sheepdog device */
106     VIR_STORAGE_POOL_GLUSTER,  /* Gluster device */
107     VIR_STORAGE_POOL_ZFS,      /* ZFS */
108     VIR_STORAGE_POOL_VSTORAGE, /* Virtuozzo Storage */
109 
110     VIR_STORAGE_POOL_LAST,
111 } virStoragePoolType;
112 
113 VIR_ENUM_DECL(virStoragePool);
114 
115 typedef enum {
116     VIR_STORAGE_DEVICE_TYPE_DISK = 0x00,
117     VIR_STORAGE_DEVICE_TYPE_ROM = 0x05,
118 
119     VIR_STORAGE_DEVICE_TYPE_LAST,
120 } virStoragePoolDeviceType;
121 
122 
123 /*
124  * For remote pools, info on how to reach the host
125  */
126 typedef struct _virStoragePoolSourceHost virStoragePoolSourceHost;
127 struct _virStoragePoolSourceHost {
128     char *name;
129     int port;
130 };
131 
132 
133 /*
134  * For MSDOS partitions, the free area is important when
135  * creating logical partitions
136  */
137 typedef enum {
138     VIR_STORAGE_FREE_NONE = 0,
139     VIR_STORAGE_FREE_NORMAL,
140     VIR_STORAGE_FREE_LOGICAL,
141     VIR_STORAGE_FREE_LAST
142 } virStorageFreeType;
143 
144 /*
145  * Available extents on the underlying storage
146  */
147 typedef struct _virStoragePoolSourceDeviceExtent virStoragePoolSourceDeviceExtent;
148 struct _virStoragePoolSourceDeviceExtent {
149     unsigned long long start;
150     unsigned long long end;
151     int type; /* virStorageFreeType */
152 };
153 
154 /*
155  * Pools can be backed by one or more devices, and some
156  * allow us to track free space on underlying devices.
157  */
158 typedef struct _virStoragePoolSourceDevice virStoragePoolSourceDevice;
159 struct _virStoragePoolSourceDevice {
160     int nfreeExtent;
161     virStoragePoolSourceDeviceExtent *freeExtents;
162     char *path;
163     int format; /* Pool specific source format */
164     virTristateBool part_separator;
165 
166     /* When the source device is a physical disk,
167      * the geometry data is needed
168      */
169     struct _geometry {
170         int cylinders;
171         int heads;
172         int sectors;
173     } geometry;
174 };
175 
176 typedef struct _virStoragePoolFeatures virStoragePoolFeatures;
177 struct _virStoragePoolFeatures {
178     virTristateBool cow;
179 };
180 
181 
182 typedef struct _virStoragePoolSource virStoragePoolSource;
183 struct _virStoragePoolSource {
184     /* An optional (maybe multiple) host(s) */
185     size_t nhost;
186     virStoragePoolSourceHost *hosts;
187 
188     /* And either one or more devices ... */
189     size_t ndevice;
190     virStoragePoolSourceDevice *devices;
191 
192     /* Or a directory */
193     char *dir;
194 
195     /* Or an adapter */
196     virStorageAdapter adapter;
197 
198     /* Or a name */
199     char *name;
200 
201     /* Initiator IQN */
202     virStorageSourceInitiatorDef initiator;
203 
204     /* Authentication information */
205     virStorageAuthDef *auth;
206 
207     /* Vendor of the source */
208     char *vendor;
209 
210     /* Product name of the source */
211     char *product;
212 
213     /* Pool type specific format such as filesystem type,
214      * or lvm version, etc.
215      */
216     int format;
217 
218     /* Protocol version value for netfs */
219     unsigned int protocolVer;
220 };
221 
222 typedef struct _virStoragePoolTarget virStoragePoolTarget;
223 struct _virStoragePoolTarget {
224     char *path; /* Optional local filesystem mapping */
225     virStoragePerms perms; /* Default permissions for volumes */
226 };
227 
228 
229 typedef struct _virStorageVolDefRefresh virStorageVolDefRefresh;
230 struct _virStorageVolDefRefresh {
231   int allocation; /* virStorageVolDefRefreshAllocation */
232 };
233 
234 
235 typedef struct _virStoragePoolDefRefresh virStoragePoolDefRefresh;
236 struct _virStoragePoolDefRefresh {
237   virStorageVolDefRefresh volume;
238 };
239 
240 
241 typedef struct _virStoragePoolDef virStoragePoolDef;
242 struct _virStoragePoolDef {
243     char *name;
244     unsigned char uuid[VIR_UUID_BUFLEN];
245     int type; /* virStoragePoolType */
246 
247     virStoragePoolDefRefresh *refresh;
248 
249     unsigned long long allocation; /* bytes */
250     unsigned long long capacity; /* bytes */
251     unsigned long long available; /* bytes */
252 
253     virStoragePoolFeatures features;
254     virStoragePoolSource source;
255     virStoragePoolTarget target;
256 
257     /* Pool backend specific XML namespace data */
258     void *namespaceData;
259     virXMLNamespace ns;
260 };
261 
262 typedef struct _virStoragePoolSourceList virStoragePoolSourceList;
263 struct _virStoragePoolSourceList {
264     int type;
265     unsigned int nsources;
266     virStoragePoolSource *sources;
267 };
268 
269 void
270 virStoragePoolSourceListFree(virStoragePoolSourceList *list);
271 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virStoragePoolSourceList, virStoragePoolSourceListFree);
272 
273 
274 virStoragePoolDef *
275 virStoragePoolDefParseXML(xmlXPathContextPtr ctxt);
276 
277 virStoragePoolDef *
278 virStoragePoolDefParseString(const char *xml,
279                              unsigned int flags);
280 
281 virStoragePoolDef *
282 virStoragePoolDefParseFile(const char *filename);
283 
284 virStoragePoolDef *
285 virStoragePoolDefParseNode(xmlDocPtr xml,
286                            xmlNodePtr root);
287 
288 char *
289 virStoragePoolDefFormat(virStoragePoolDef *def);
290 
291 typedef enum {
292     /* do not require volume capacity at all */
293     VIR_VOL_XML_PARSE_NO_CAPACITY  = 1 << 0,
294     /* do not require volume capacity if the volume has a backing store */
295     VIR_VOL_XML_PARSE_OPT_CAPACITY = 1 << 1,
296 } virStorageVolDefParseFlags;
297 
298 virStorageVolDef *
299 virStorageVolDefParseString(virStoragePoolDef *pool,
300                             const char *xml,
301                             unsigned int flags);
302 
303 virStorageVolDef *
304 virStorageVolDefParseFile(virStoragePoolDef *pool,
305                           const char *filename,
306                           unsigned int flags);
307 
308 virStorageVolDef *
309 virStorageVolDefParseNode(virStoragePoolDef *pool,
310                           xmlDocPtr xml,
311                           xmlNodePtr root,
312                           unsigned int flags);
313 
314 char *
315 virStorageVolDefFormat(virStoragePoolDef *pool,
316                        virStorageVolDef *def);
317 
318 int
319 virStoragePoolSaveState(const char *stateFile,
320                         virStoragePoolDef *def);
321 
322 int
323 virStoragePoolSaveConfig(const char *configFile,
324                          virStoragePoolDef *def);
325 
326 void
327 virStorageVolDefFree(virStorageVolDef *def);
328 
329 void
330 virStoragePoolSourceClear(virStoragePoolSource *source);
331 
332 void
333 virStoragePoolSourceDeviceClear(virStoragePoolSourceDevice *dev);
334 
335 void
336 virStoragePoolSourceFree(virStoragePoolSource *source);
337 
338 void
339 virStoragePoolDefFree(virStoragePoolDef *def);
340 
341 virStoragePoolSource *
342 virStoragePoolDefParseSourceString(const char *srcSpec,
343                                    int pool_type);
344 
345 virStoragePoolSource *
346 virStoragePoolSourceListNewSource(virStoragePoolSourceList *list);
347 
348 char *
349 virStoragePoolSourceListFormat(virStoragePoolSourceList *def);
350 
351 typedef enum {
352     VIR_STORAGE_POOL_FS_AUTO = 0,
353     VIR_STORAGE_POOL_FS_EXT2,
354     VIR_STORAGE_POOL_FS_EXT3,
355     VIR_STORAGE_POOL_FS_EXT4,
356     VIR_STORAGE_POOL_FS_UFS,
357     VIR_STORAGE_POOL_FS_ISO,
358     VIR_STORAGE_POOL_FS_UDF,
359     VIR_STORAGE_POOL_FS_GFS,
360     VIR_STORAGE_POOL_FS_GFS2,
361     VIR_STORAGE_POOL_FS_VFAT,
362     VIR_STORAGE_POOL_FS_HFSPLUS,
363     VIR_STORAGE_POOL_FS_XFS,
364     VIR_STORAGE_POOL_FS_OCFS2,
365     VIR_STORAGE_POOL_FS_VMFS,
366     VIR_STORAGE_POOL_FS_LAST,
367 } virStoragePoolFormatFileSystem;
368 VIR_ENUM_DECL(virStoragePoolFormatFileSystem);
369 
370 typedef enum {
371     VIR_STORAGE_POOL_NETFS_AUTO = 0,
372     VIR_STORAGE_POOL_NETFS_NFS,
373     VIR_STORAGE_POOL_NETFS_GLUSTERFS,
374     VIR_STORAGE_POOL_NETFS_CIFS,
375     VIR_STORAGE_POOL_NETFS_LAST,
376 } virStoragePoolFormatFileSystemNet;
377 VIR_ENUM_DECL(virStoragePoolFormatFileSystemNet);
378 
379 typedef enum {
380     VIR_STORAGE_POOL_DISK_UNKNOWN = 0,
381     VIR_STORAGE_POOL_DISK_DOS = 1,
382     VIR_STORAGE_POOL_DISK_DVH,
383     VIR_STORAGE_POOL_DISK_GPT,
384     VIR_STORAGE_POOL_DISK_MAC,
385     VIR_STORAGE_POOL_DISK_BSD,
386     VIR_STORAGE_POOL_DISK_PC98,
387     VIR_STORAGE_POOL_DISK_SUN,
388     VIR_STORAGE_POOL_DISK_LVM2,
389     VIR_STORAGE_POOL_DISK_LAST,
390 } virStoragePoolFormatDisk;
391 VIR_ENUM_DECL(virStoragePoolFormatDisk);
392 
393 typedef enum {
394     VIR_STORAGE_POOL_LOGICAL_UNKNOWN = 0,
395     VIR_STORAGE_POOL_LOGICAL_LVM2 = 1,
396     VIR_STORAGE_POOL_LOGICAL_LAST,
397 } virStoragePoolFormatLogical;
398 VIR_ENUM_DECL(virStoragePoolFormatLogical);
399 
400 /*
401  * XXX: these are basically partition types.
402  *
403  * fdisk has a bazillion partition ID types parted has
404  * practically none, and splits the * info across 3
405  * different attributes.
406  *
407  * So this is a semi-generic set
408  */
409 typedef enum {
410     VIR_STORAGE_VOL_DISK_NONE = 0,
411     VIR_STORAGE_VOL_DISK_LINUX,
412     VIR_STORAGE_VOL_DISK_FAT16,
413     VIR_STORAGE_VOL_DISK_FAT32,
414     VIR_STORAGE_VOL_DISK_LINUX_SWAP,
415     VIR_STORAGE_VOL_DISK_LINUX_LVM,
416     VIR_STORAGE_VOL_DISK_LINUX_RAID,
417     VIR_STORAGE_VOL_DISK_EXTENDED,
418     VIR_STORAGE_VOL_DISK_LAST,
419 } virStorageVolFormatDisk;
420 VIR_ENUM_DECL(virStorageVolFormatDisk);
421 
422 typedef enum {
423     VIR_STORAGE_VOL_DISK_TYPE_NONE = 0,
424     VIR_STORAGE_VOL_DISK_TYPE_PRIMARY,
425     VIR_STORAGE_VOL_DISK_TYPE_LOGICAL,
426     VIR_STORAGE_VOL_DISK_TYPE_EXTENDED,
427     VIR_STORAGE_VOL_DISK_TYPE_LAST,
428 } virStorageVolTypeDisk;
429 
430 /*
431  * Mapping of Parted fs-types MUST be kept in the
432  * same order as virStorageVolFormatDisk
433  */
434 typedef enum {
435     VIR_STORAGE_PARTED_FS_TYPE_NONE = 0,
436     VIR_STORAGE_PARTED_FS_TYPE_LINUX,
437     VIR_STORAGE_PARTED_FS_TYPE_FAT16,
438     VIR_STORAGE_PARTED_FS_TYPE_FAT32,
439     VIR_STORAGE_PARTED_FS_TYPE_LINUX_SWAP,
440     VIR_STORAGE_PARTED_FS_TYPE_LINUX_LVM,
441     VIR_STORAGE_PARTED_FS_TYPE_LINUX_RAID,
442     VIR_STORAGE_PARTED_FS_TYPE_EXTENDED,
443     VIR_STORAGE_PARTED_FS_TYPE_LAST,
444 } virStoragePartedFsType;
445 VIR_ENUM_DECL(virStoragePartedFs);
446 
447 #define VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ACTIVE \
448                 (VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE | \
449                  VIR_CONNECT_LIST_STORAGE_POOLS_INACTIVE)
450 
451 #define VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_PERSISTENT \
452                 (VIR_CONNECT_LIST_STORAGE_POOLS_PERSISTENT | \
453                  VIR_CONNECT_LIST_STORAGE_POOLS_TRANSIENT)
454 
455 #define VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_AUTOSTART \
456                 (VIR_CONNECT_LIST_STORAGE_POOLS_AUTOSTART | \
457                  VIR_CONNECT_LIST_STORAGE_POOLS_NO_AUTOSTART)
458 
459 #define VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_POOL_TYPE \
460                 (VIR_CONNECT_LIST_STORAGE_POOLS_DIR      | \
461                  VIR_CONNECT_LIST_STORAGE_POOLS_FS       | \
462                  VIR_CONNECT_LIST_STORAGE_POOLS_NETFS    | \
463                  VIR_CONNECT_LIST_STORAGE_POOLS_LOGICAL  | \
464                  VIR_CONNECT_LIST_STORAGE_POOLS_DISK     | \
465                  VIR_CONNECT_LIST_STORAGE_POOLS_ISCSI    | \
466                  VIR_CONNECT_LIST_STORAGE_POOLS_SCSI     | \
467                  VIR_CONNECT_LIST_STORAGE_POOLS_MPATH    | \
468                  VIR_CONNECT_LIST_STORAGE_POOLS_RBD      | \
469                  VIR_CONNECT_LIST_STORAGE_POOLS_SHEEPDOG | \
470                  VIR_CONNECT_LIST_STORAGE_POOLS_GLUSTER  | \
471                  VIR_CONNECT_LIST_STORAGE_POOLS_ZFS      | \
472                  VIR_CONNECT_LIST_STORAGE_POOLS_VSTORAGE | \
473                  VIR_CONNECT_LIST_STORAGE_POOLS_ISCSI_DIRECT)
474 
475 #define VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ALL \
476                 (VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ACTIVE     | \
477                  VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_PERSISTENT | \
478                  VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_AUTOSTART  | \
479                  VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_POOL_TYPE)
480 
481 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virStoragePoolSource, virStoragePoolSourceFree);
482 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virStoragePoolDef, virStoragePoolDefFree);
483 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virStorageVolDef, virStorageVolDefFree);
484