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