4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
43 #include <nbdkit-plugin.h>
48 #include "virtual-disk.h"
50 static int create_regions (struct virtual_disk
*disk
);
53 init_virtual_disk (struct virtual_disk
*disk
)
55 memset (disk
, 0, sizeof *disk
);
58 init_regions (&disk
->regions
);
62 create_virtual_disk (struct virtual_disk
*disk
)
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
||
76 disk
->secondary_header
== NULL
) {
77 nbdkit_error ("calloc: %m");
81 /* Create the filesystem. This fills in disk->filesystem_size and
84 if (create_filesystem (disk
) == -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
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)
98 /* Initialize partition table structures. This depends on
99 * disk->regions so must be done last.
101 if (create_partition_table (disk
) == -1)
108 free_virtual_disk (struct virtual_disk
*disk
)
110 free_regions (&disk
->regions
);
111 free (disk
->protective_mbr
);
112 free (disk
->primary_header
);
114 free (disk
->secondary_header
);
119 /* Lay out the final disk. */
121 create_regions (struct virtual_disk
*disk
)
123 /* Protective MBR. */
124 if (append_region_len (&disk
->regions
, "Protective MBR",
126 region_data
, (void *) disk
->protective_mbr
) == -1)
129 /* GPT primary partition table header (LBA 1). */
130 if (append_region_len (&disk
->regions
, "GPT primary header",
132 region_data
, (void *) disk
->primary_header
) == -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)
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)
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)
153 /* GPT secondary PT header (LBA -1). */
154 if (append_region_len (&disk
->regions
, "GPT secondary header",
156 region_data
, (void *) disk
->secondary_header
) == -1)