Update Red Hat Copyright Notices
[nbdkit.git] / plugins / linuxdisk / virtual-disk.c
blobbb062937cbed350b427e4ebafa3cf1659790abbc
1 /* nbdkit
2 * Copyright Red Hat
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdbool.h>
38 #include <stdint.h>
39 #include <inttypes.h>
40 #include <string.h>
41 #include <unistd.h>
43 #include <nbdkit-plugin.h>
45 #include "random.h"
46 #include "regions.h"
48 #include "virtual-disk.h"
50 static int create_regions (struct virtual_disk *disk);
52 void
53 init_virtual_disk (struct virtual_disk *disk)
55 memset (disk, 0, sizeof *disk);
56 disk->fd = -1;
58 init_regions (&disk->regions);
61 int
62 create_virtual_disk (struct virtual_disk *disk)
64 size_t i;
66 /* Allocate the partition table structures. We can't fill them in
67 * until we have created the disk layout.
69 disk->protective_mbr = calloc (1, SECTOR_SIZE);
70 disk->primary_header = calloc (1, SECTOR_SIZE);
71 disk->pt = calloc (1, 32*SECTOR_SIZE);
72 disk->secondary_header = calloc (1, SECTOR_SIZE);
73 if (disk->protective_mbr == NULL ||
74 disk->primary_header == NULL ||
75 disk->pt == NULL ||
76 disk->secondary_header == NULL) {
77 nbdkit_error ("calloc: %m");
78 return -1;
81 /* Create the filesystem. This fills in disk->filesystem_size and
82 * disk->id.
84 if (create_filesystem (disk) == -1)
85 return -1;
87 /* Create a random GUID used as "Unique partition GUID". However
88 * this doesn't follow GUID conventions so in theory could make an
89 * invalid value.
91 for (i = 0; i < 16; ++i)
92 disk->guid[i] = xrandom (&random_state) & 0xff;
94 /* Create the virtual disk regions. */
95 if (create_regions (disk) == -1)
96 return -1;
98 /* Initialize partition table structures. This depends on
99 * disk->regions so must be done last.
101 if (create_partition_table (disk) == -1)
102 return -1;
104 return 0;
107 void
108 free_virtual_disk (struct virtual_disk *disk)
110 free_regions (&disk->regions);
111 free (disk->protective_mbr);
112 free (disk->primary_header);
113 free (disk->pt);
114 free (disk->secondary_header);
115 if (disk->fd >= 0)
116 close (disk->fd);
119 /* Lay out the final disk. */
120 static int
121 create_regions (struct virtual_disk *disk)
123 /* Protective MBR. */
124 if (append_region_len (&disk->regions, "Protective MBR",
125 SECTOR_SIZE, 0, 0,
126 region_data, (void *) disk->protective_mbr) == -1)
127 return -1;
129 /* GPT primary partition table header (LBA 1). */
130 if (append_region_len (&disk->regions, "GPT primary header",
131 SECTOR_SIZE, 0, 0,
132 region_data, (void *) disk->primary_header) == -1)
133 return -1;
135 /* GPT primary PT (LBA 2..33). */
136 if (append_region_len (&disk->regions, "GPT primary PT",
137 32*SECTOR_SIZE, 0, 0,
138 region_data, (void *) disk->pt) == -1)
139 return -1;
141 /* Partition containing the filesystem. Align it to 2048 sectors. */
142 if (append_region_len (&disk->regions, "Filesystem",
143 disk->filesystem_size, 2048*SECTOR_SIZE, 0,
144 region_file, 0 /* unused */) == -1)
145 return -1;
147 /* GPT secondary PT (LBA -33..-2). */
148 if (append_region_len (&disk->regions, "GPT secondary PT",
149 32*SECTOR_SIZE, SECTOR_SIZE, 0,
150 region_data, (void *) disk->pt) == -1)
151 return -1;
153 /* GPT secondary PT header (LBA -1). */
154 if (append_region_len (&disk->regions, "GPT secondary header",
155 SECTOR_SIZE, 0, 0,
156 region_data, (void *) disk->secondary_header) == -1)
157 return -1;
159 return 0;