Index: stable/10/usr.bin/mkimg/gpt.c =================================================================== --- stable/10/usr.bin/mkimg/gpt.c (revision 287121) +++ stable/10/usr.bin/mkimg/gpt.c (revision 287122) @@ -1,308 +1,310 @@ /*- * Copyright (c) 2014 Juniper Networks, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 THE AUTHOR 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 __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include "image.h" #include "mkimg.h" #include "scheme.h" #ifndef GPT_ENT_TYPE_FREEBSD_NANDFS #define GPT_ENT_TYPE_FREEBSD_NANDFS \ {0x74ba7dd9,0xa689,0x11e1,0xbd,0x04,{0x00,0xe0,0x81,0x28,0x6a,0xcf}} #endif static uuid_t gpt_uuid_efi = GPT_ENT_TYPE_EFI; static uuid_t gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD; static uuid_t gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT; static uuid_t gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS; static uuid_t gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP; static uuid_t gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS; static uuid_t gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM; static uuid_t gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS; static uuid_t gpt_uuid_mbr = GPT_ENT_TYPE_MBR; +static uuid_t gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA; static struct mkimg_alias gpt_aliases[] = { { ALIAS_EFI, ALIAS_PTR2TYPE(&gpt_uuid_efi) }, { ALIAS_FREEBSD, ALIAS_PTR2TYPE(&gpt_uuid_freebsd) }, { ALIAS_FREEBSD_BOOT, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_boot) }, { ALIAS_FREEBSD_NANDFS, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_nandfs) }, { ALIAS_FREEBSD_SWAP, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_swap) }, { ALIAS_FREEBSD_UFS, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_ufs) }, { ALIAS_FREEBSD_VINUM, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_vinum) }, { ALIAS_FREEBSD_ZFS, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_zfs) }, { ALIAS_MBR, ALIAS_PTR2TYPE(&gpt_uuid_mbr) }, + { ALIAS_NTFS, ALIAS_PTR2TYPE(&gpt_uuid_ms_basic_data) }, { ALIAS_NONE, 0 } /* Keep last! */ }; /* CRC32 code derived from work by Gary S. Brown. */ static const uint32_t crc32_tab[] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d }; static uint32_t crc32(const void *buf, size_t sz) { const uint8_t *p = (const uint8_t *)buf; uint32_t crc = ~0U; while (sz--) crc = crc32_tab[(crc ^ *p++) & 0xff] ^ (crc >> 8); return (crc ^ ~0U); } static void gpt_uuid_enc(void *buf, const uuid_t *uuid) { uint8_t *p = buf; int i; le32enc(p, uuid->time_low); le16enc(p + 4, uuid->time_mid); le16enc(p + 6, uuid->time_hi_and_version); p[8] = uuid->clock_seq_hi_and_reserved; p[9] = uuid->clock_seq_low; for (i = 0; i < _UUID_NODE_LEN; i++) p[10 + i] = uuid->node[i]; } static u_int gpt_tblsz(void) { u_int ents; ents = secsz / sizeof(struct gpt_ent); return ((nparts + ents - 1) / ents); } static lba_t gpt_metadata(u_int where, lba_t blk) { if (where == SCHEME_META_IMG_START || where == SCHEME_META_IMG_END) { blk += gpt_tblsz(); blk += (where == SCHEME_META_IMG_START) ? 2 : 1; } return (round_block(blk)); } static int gpt_write_pmbr(lba_t blks, void *bootcode) { u_char *pmbr; uint32_t secs; int error; secs = (blks > UINT32_MAX) ? UINT32_MAX : (uint32_t)blks; pmbr = malloc(secsz); if (pmbr == NULL) return (errno); if (bootcode != NULL) { memcpy(pmbr, bootcode, DOSPARTOFF); memset(pmbr + DOSPARTOFF, 0, secsz - DOSPARTOFF); } else memset(pmbr, 0, secsz); pmbr[DOSPARTOFF + 2] = 2; pmbr[DOSPARTOFF + 4] = 0xee; pmbr[DOSPARTOFF + 5] = 0xff; pmbr[DOSPARTOFF + 6] = 0xff; pmbr[DOSPARTOFF + 7] = 0xff; le32enc(pmbr + DOSPARTOFF + 8, 1); le32enc(pmbr + DOSPARTOFF + 12, secs); le16enc(pmbr + DOSMAGICOFFSET, DOSMAGIC); error = image_write(0, pmbr, 1); free(pmbr); return (error); } static struct gpt_ent * gpt_mktbl(u_int tblsz) { uuid_t uuid; struct gpt_ent *tbl, *ent; struct part *part; int c, idx; tbl = calloc(tblsz, secsz); if (tbl == NULL) return (NULL); STAILQ_FOREACH(part, &partlist, link) { ent = tbl + part->index; gpt_uuid_enc(&ent->ent_type, ALIAS_TYPE2PTR(part->type)); mkimg_uuid(&uuid); gpt_uuid_enc(&ent->ent_uuid, &uuid); le64enc(&ent->ent_lba_start, part->block); le64enc(&ent->ent_lba_end, part->block + part->size - 1); if (part->label != NULL) { idx = 0; while ((c = part->label[idx]) != '\0') { le16enc(ent->ent_name + idx, c); idx++; } } } return (tbl); } static int gpt_write_hdr(struct gpt_hdr *hdr, uint64_t self, uint64_t alt, uint64_t tbl) { uint32_t crc; le64enc(&hdr->hdr_lba_self, self); le64enc(&hdr->hdr_lba_alt, alt); le64enc(&hdr->hdr_lba_table, tbl); hdr->hdr_crc_self = 0; crc = crc32(hdr, offsetof(struct gpt_hdr, padding)); le64enc(&hdr->hdr_crc_self, crc); return (image_write(self, hdr, 1)); } static int gpt_write(lba_t imgsz, void *bootcode) { uuid_t uuid; struct gpt_ent *tbl; struct gpt_hdr *hdr; uint32_t crc; u_int tblsz; int error; /* PMBR */ error = gpt_write_pmbr(imgsz, bootcode); if (error) return (error); /* GPT table(s) */ tblsz = gpt_tblsz(); tbl = gpt_mktbl(tblsz); if (tbl == NULL) return (errno); error = image_write(2, tbl, tblsz); if (error) goto out; error = image_write(imgsz - (tblsz + 1), tbl, tblsz); if (error) goto out; /* GPT header(s) */ hdr = malloc(secsz); if (hdr == NULL) { error = errno; goto out; } memset(hdr, 0, secsz); memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)); le32enc(&hdr->hdr_revision, GPT_HDR_REVISION); le32enc(&hdr->hdr_size, offsetof(struct gpt_hdr, padding)); le64enc(&hdr->hdr_lba_start, 2 + tblsz); le64enc(&hdr->hdr_lba_end, imgsz - tblsz - 2); mkimg_uuid(&uuid); gpt_uuid_enc(&hdr->hdr_uuid, &uuid); le32enc(&hdr->hdr_entries, nparts); le32enc(&hdr->hdr_entsz, sizeof(struct gpt_ent)); crc = crc32(tbl, nparts * sizeof(struct gpt_ent)); le32enc(&hdr->hdr_crc_table, crc); error = gpt_write_hdr(hdr, 1, imgsz - 1, 2); if (!error) error = gpt_write_hdr(hdr, imgsz - 1, 1, imgsz - tblsz - 1); free(hdr); out: free(tbl); return (error); } static struct mkimg_scheme gpt_scheme = { .name = "gpt", .description = "GUID Partition Table", .aliases = gpt_aliases, .metadata = gpt_metadata, .write = gpt_write, .nparts = 4096, .labellen = 36, .bootcode = 512, .maxsecsz = 4096 }; SCHEME_DEFINE(gpt_scheme); Index: stable/10/usr.bin/mkimg/image.c =================================================================== --- stable/10/usr.bin/mkimg/image.c (revision 287121) +++ stable/10/usr.bin/mkimg/image.c (revision 287122) @@ -1,724 +1,724 @@ /*- * Copyright (c) 2014 Juniper Networks, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 THE AUTHOR 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 __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include "image.h" #include "mkimg.h" struct chunk { STAILQ_ENTRY(chunk) ch_list; size_t ch_size; /* Size of chunk in bytes. */ lba_t ch_block; /* Block address in image. */ union { struct { off_t ofs; /* Offset in backing file. */ int fd; /* FD of backing file. */ } file; struct { void *ptr; /* Pointer to data in memory */ } mem; } ch_u; u_int ch_type; #define CH_TYPE_ZEROES 0 /* Chunk is a gap (no data). */ #define CH_TYPE_FILE 1 /* File-backed chunk. */ #define CH_TYPE_MEMORY 2 /* Memory-backed chunk */ }; static STAILQ_HEAD(chunk_head, chunk) image_chunks; static u_int image_nchunks; static char image_swap_file[PATH_MAX]; static int image_swap_fd = -1; static u_int image_swap_pgsz; static off_t image_swap_size; static lba_t image_size; static int is_empty_sector(void *buf) { uint64_t *p = buf; size_t n, max; assert(((uintptr_t)p & 3) == 0); max = secsz / sizeof(uint64_t); for (n = 0; n < max; n++) { if (p[n] != 0UL) return (0); } return (1); } /* * Swap file handlng. */ static off_t image_swap_alloc(size_t size) { off_t ofs; size_t unit; unit = (secsz > image_swap_pgsz) ? secsz : image_swap_pgsz; assert((unit & (unit - 1)) == 0); size = (size + unit - 1) & ~(unit - 1); ofs = image_swap_size; image_swap_size += size; if (ftruncate(image_swap_fd, image_swap_size) == -1) { image_swap_size = ofs; ofs = -1LL; } return (ofs); } /* * Image chunk handling. */ static struct chunk * image_chunk_find(lba_t blk) { static struct chunk *last = NULL; struct chunk *ch; ch = (last != NULL && last->ch_block <= blk) ? last : STAILQ_FIRST(&image_chunks); while (ch != NULL) { if (ch->ch_block <= blk && (lba_t)(ch->ch_block + (ch->ch_size / secsz)) > blk) { last = ch; break; } ch = STAILQ_NEXT(ch, ch_list); } return (ch); } static size_t image_chunk_grow(struct chunk *ch, size_t sz) { size_t dsz, newsz; newsz = ch->ch_size + sz; if (newsz > ch->ch_size) { ch->ch_size = newsz; return (0); } /* We would overflow -- create new chunk for remainder. */ dsz = SIZE_MAX - ch->ch_size; assert(dsz < sz); ch->ch_size = SIZE_MAX; return (sz - dsz); } static struct chunk * image_chunk_memory(struct chunk *ch, lba_t blk) { struct chunk *new; void *ptr; ptr = calloc(1, secsz); if (ptr == NULL) return (NULL); if (ch->ch_block < blk) { new = malloc(sizeof(*new)); if (new == NULL) { free(ptr); return (NULL); } memcpy(new, ch, sizeof(*new)); ch->ch_size = (blk - ch->ch_block) * secsz; new->ch_block = blk; new->ch_size -= ch->ch_size; STAILQ_INSERT_AFTER(&image_chunks, ch, new, ch_list); image_nchunks++; ch = new; } if (ch->ch_size > secsz) { new = malloc(sizeof(*new)); if (new == NULL) { free(ptr); return (NULL); } memcpy(new, ch, sizeof(*new)); ch->ch_size = secsz; new->ch_block++; new->ch_size -= secsz; STAILQ_INSERT_AFTER(&image_chunks, ch, new, ch_list); image_nchunks++; } ch->ch_type = CH_TYPE_MEMORY; ch->ch_u.mem.ptr = ptr; return (ch); } static int image_chunk_skipto(lba_t to) { struct chunk *ch; lba_t from; size_t sz; ch = STAILQ_LAST(&image_chunks, chunk, ch_list); from = (ch != NULL) ? ch->ch_block + (ch->ch_size / secsz) : 0LL; assert(from <= to); /* Nothing to do? */ if (from == to) return (0); /* Avoid bugs due to overflows. */ if ((uintmax_t)(to - from) > (uintmax_t)(SIZE_MAX / secsz)) return (EFBIG); sz = (to - from) * secsz; if (ch != NULL && ch->ch_type == CH_TYPE_ZEROES) { sz = image_chunk_grow(ch, sz); if (sz == 0) return (0); from = ch->ch_block + (ch->ch_size / secsz); } ch = malloc(sizeof(*ch)); if (ch == NULL) return (ENOMEM); memset(ch, 0, sizeof(*ch)); ch->ch_block = from; ch->ch_size = sz; ch->ch_type = CH_TYPE_ZEROES; STAILQ_INSERT_TAIL(&image_chunks, ch, ch_list); image_nchunks++; return (0); } static int image_chunk_append(lba_t blk, size_t sz, off_t ofs, int fd) { struct chunk *ch; ch = STAILQ_LAST(&image_chunks, chunk, ch_list); if (ch != NULL && ch->ch_type == CH_TYPE_FILE) { if (fd == ch->ch_u.file.fd && blk == (lba_t)(ch->ch_block + (ch->ch_size / secsz)) && ofs == (off_t)(ch->ch_u.file.ofs + ch->ch_size)) { sz = image_chunk_grow(ch, sz); if (sz == 0) return (0); blk = ch->ch_block + (ch->ch_size / secsz); ofs = ch->ch_u.file.ofs + ch->ch_size; } } ch = malloc(sizeof(*ch)); if (ch == NULL) return (ENOMEM); memset(ch, 0, sizeof(*ch)); ch->ch_block = blk; ch->ch_size = sz; ch->ch_type = CH_TYPE_FILE; ch->ch_u.file.ofs = ofs; ch->ch_u.file.fd = fd; STAILQ_INSERT_TAIL(&image_chunks, ch, ch_list); image_nchunks++; return (0); } static int image_chunk_copyin(lba_t blk, void *buf, size_t sz, off_t ofs, int fd) { uint8_t *p = buf; int error; error = 0; sz = (sz + secsz - 1) & ~(secsz - 1); while (!error && sz > 0) { if (is_empty_sector(p)) error = image_chunk_skipto(blk + 1); else error = image_chunk_append(blk, secsz, ofs, fd); blk++; p += secsz; sz -= secsz; ofs += secsz; } return (error); } /* * File mapping support. */ static void * image_file_map(int fd, off_t ofs, size_t sz) { void *ptr; size_t unit; int flags, prot; unit = (secsz > image_swap_pgsz) ? secsz : image_swap_pgsz; assert((unit & (unit - 1)) == 0); flags = MAP_NOCORE | MAP_NOSYNC | MAP_SHARED; /* Allow writing to our swap file only. */ prot = PROT_READ | ((fd == image_swap_fd) ? PROT_WRITE : 0); sz = (sz + unit - 1) & ~(unit - 1); ptr = mmap(NULL, sz, prot, flags, fd, ofs); return ((ptr == MAP_FAILED) ? NULL : ptr); } static int image_file_unmap(void *buffer, size_t sz) { size_t unit; unit = (secsz > image_swap_pgsz) ? secsz : image_swap_pgsz; sz = (sz + unit - 1) & ~(unit - 1); munmap(buffer, sz); return (0); } /* * Input/source file handling. */ static int image_copyin_stream(lba_t blk, int fd, uint64_t *sizep) { char *buffer; uint64_t bytesize; off_t swofs; size_t iosz; ssize_t rdsz; int error; /* * This makes sure we're doing I/O in multiples of the page * size as well as of the sector size. 2MB is the minimum * by virtue of secsz at least 512 bytes and the page size * at least 4K bytes. */ iosz = secsz * image_swap_pgsz; bytesize = 0; do { swofs = image_swap_alloc(iosz); if (swofs == -1LL) return (errno); buffer = image_file_map(image_swap_fd, swofs, iosz); if (buffer == NULL) return (errno); rdsz = read(fd, buffer, iosz); if (rdsz > 0) error = image_chunk_copyin(blk, buffer, rdsz, swofs, image_swap_fd); else if (rdsz < 0) error = errno; else error = 0; image_file_unmap(buffer, iosz); /* XXX should we relinguish unused swap space? */ if (error) return (error); bytesize += rdsz; blk += (rdsz + secsz - 1) / secsz; } while (rdsz > 0); if (sizep != NULL) *sizep = bytesize; return (0); } static int image_copyin_mapped(lba_t blk, int fd, uint64_t *sizep) { off_t cur, data, end, hole, pos; void *buf; uint64_t bytesize; size_t iosz, sz; int error; /* * We'd like to know the size of the file and we must * be able to seek in order to mmap(2). If this isn't * possible, then treat the file as a stream/pipe. */ end = lseek(fd, 0L, SEEK_END); if (end == -1L) return (image_copyin_stream(blk, fd, sizep)); /* * We need the file opened for the duration and our * caller is going to close the file. Make a dup(2) * so that control the faith of the descriptor. */ fd = dup(fd); if (fd == -1) return (errno); iosz = secsz * image_swap_pgsz; bytesize = 0; cur = pos = 0; error = 0; while (!error && cur < end) { hole = lseek(fd, cur, SEEK_HOLE); if (hole == -1) hole = end; data = lseek(fd, cur, SEEK_DATA); if (data == -1) data = end; /* * Treat the entire file as data if sparse files * are not supported by the underlying file system. */ if (hole == end && data == end) data = cur; if (cur == hole && data > hole) { hole = pos; pos = data & ~((uint64_t)secsz - 1); blk += (pos - hole) / secsz; error = image_chunk_skipto(blk); bytesize += pos - hole; cur = data; } else if (cur == data && hole > data) { data = pos; pos = (hole + secsz - 1) & ~((uint64_t)secsz - 1); while (data < pos) { sz = (pos - data > (off_t)iosz) ? iosz : (size_t)(pos - data); buf = image_file_map(fd, data, sz); if (buf != NULL) { error = image_chunk_copyin(blk, buf, sz, data, fd); image_file_unmap(buf, sz); } else error = errno; blk += sz / secsz; bytesize += sz; data += sz; } cur = hole; } else { /* * I don't know what this means or whether it * can happen at all... */ error = EDOOFUS; break; } } if (error) close(fd); if (!error && sizep != NULL) *sizep = bytesize; return (error); } int image_copyin(lba_t blk, int fd, uint64_t *sizep) { struct stat sb; int error; error = image_chunk_skipto(blk); if (!error) { if (fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode)) error = image_copyin_stream(blk, fd, sizep); else error = image_copyin_mapped(blk, fd, sizep); } return (error); } /* * Output/sink file handling. */ int image_copyout(int fd) { int error; error = image_copyout_region(fd, 0, image_size); if (!error) error = image_copyout_done(fd); return (error); } int image_copyout_done(int fd) { off_t ofs; int error; ofs = lseek(fd, 0L, SEEK_CUR); if (ofs == -1) return (0); error = (ftruncate(fd, ofs) == -1) ? errno : 0; return (error); } static int image_copyout_memory(int fd, size_t size, void *ptr) { if (write(fd, ptr, size) == -1) return (errno); return (0); } -static int -image_copyout_zeroes(int fd, size_t size) +int +image_copyout_zeroes(int fd, size_t count) { static uint8_t *zeroes = NULL; size_t sz; int error; - if (lseek(fd, (off_t)size, SEEK_CUR) != -1) + if (lseek(fd, (off_t)count, SEEK_CUR) != -1) return (0); /* * If we can't seek, we must write. */ if (zeroes == NULL) { zeroes = calloc(1, secsz); if (zeroes == NULL) return (ENOMEM); } - while (size > 0) { - sz = (size > secsz) ? secsz : size; + while (count > 0) { + sz = (count > secsz) ? secsz : count; error = image_copyout_memory(fd, sz, zeroes); if (error) return (error); - size -= sz; + count -= sz; } return (0); } static int image_copyout_file(int fd, size_t size, int ifd, off_t iofs) { void *buf; size_t iosz, sz; int error; iosz = secsz * image_swap_pgsz; while (size > 0) { sz = (size > iosz) ? iosz : size; buf = image_file_map(ifd, iofs, sz); if (buf == NULL) return (errno); error = image_copyout_memory(fd, sz, buf); image_file_unmap(buf, sz); if (error) return (error); size -= sz; iofs += sz; } return (0); } int image_copyout_region(int fd, lba_t blk, lba_t size) { struct chunk *ch; size_t ofs, sz; int error; size *= secsz; while (size > 0) { ch = image_chunk_find(blk); if (ch == NULL) return (EINVAL); ofs = (blk - ch->ch_block) * secsz; sz = ch->ch_size - ofs; sz = ((lba_t)sz < size) ? sz : (size_t)size; switch (ch->ch_type) { case CH_TYPE_ZEROES: error = image_copyout_zeroes(fd, sz); break; case CH_TYPE_FILE: error = image_copyout_file(fd, sz, ch->ch_u.file.fd, ch->ch_u.file.ofs + ofs); break; case CH_TYPE_MEMORY: error = image_copyout_memory(fd, sz, ch->ch_u.mem.ptr); break; default: return (EDOOFUS); } size -= sz; blk += sz / secsz; } return (0); } int image_data(lba_t blk, lba_t size) { struct chunk *ch; lba_t lim; while (1) { ch = image_chunk_find(blk); if (ch == NULL) return (0); if (ch->ch_type != CH_TYPE_ZEROES) return (1); lim = ch->ch_block + (ch->ch_size / secsz); if (lim >= blk + size) return (0); size -= lim - blk; blk = lim; } /*NOTREACHED*/ } lba_t image_get_size(void) { return (image_size); } int image_set_size(lba_t blk) { int error; error = image_chunk_skipto(blk); if (!error) image_size = blk; return (error); } int image_write(lba_t blk, void *buf, ssize_t len) { struct chunk *ch; while (len > 0) { if (!is_empty_sector(buf)) { ch = image_chunk_find(blk); if (ch == NULL) return (ENXIO); /* We may not be able to write to files. */ if (ch->ch_type == CH_TYPE_FILE) return (EINVAL); if (ch->ch_type == CH_TYPE_ZEROES) { ch = image_chunk_memory(ch, blk); if (ch == NULL) return (ENOMEM); } assert(ch->ch_type == CH_TYPE_MEMORY); memcpy(ch->ch_u.mem.ptr, buf, secsz); } blk++; buf = (char *)buf + secsz; len--; } return (0); } static void image_cleanup(void) { struct chunk *ch; while ((ch = STAILQ_FIRST(&image_chunks)) != NULL) { switch (ch->ch_type) { case CH_TYPE_FILE: /* We may be closing the same file multiple times. */ if (ch->ch_u.file.fd != -1) close(ch->ch_u.file.fd); break; case CH_TYPE_MEMORY: free(ch->ch_u.mem.ptr); break; default: break; } STAILQ_REMOVE_HEAD(&image_chunks, ch_list); free(ch); } if (image_swap_fd != -1) close(image_swap_fd); unlink(image_swap_file); } int image_init(void) { const char *tmpdir; STAILQ_INIT(&image_chunks); image_nchunks = 0; image_swap_size = 0; image_swap_pgsz = getpagesize(); if (atexit(image_cleanup) == -1) return (errno); if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0') tmpdir = _PATH_TMP; snprintf(image_swap_file, sizeof(image_swap_file), "%s/mkimg-XXXXXX", tmpdir); image_swap_fd = mkstemp(image_swap_file); if (image_swap_fd == -1) return (errno); return (0); } Index: stable/10/usr.bin/mkimg/image.h =================================================================== --- stable/10/usr.bin/mkimg/image.h (revision 287121) +++ stable/10/usr.bin/mkimg/image.h (revision 287122) @@ -1,44 +1,45 @@ /*- * Copyright (c) 2014 Juniper Networks, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 THE AUTHOR 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. * * $FreeBSD$ */ #ifndef _MKIMG_IMAGE_H_ #define _MKIMG_IMAGE_H_ typedef int64_t lba_t; int image_copyin(lba_t blk, int fd, uint64_t *sizep); int image_copyout(int fd); int image_copyout_done(int fd); int image_copyout_region(int fd, lba_t blk, lba_t size); +int image_copyout_zeroes(int fd, size_t count); int image_data(lba_t blk, lba_t size); lba_t image_get_size(void); int image_init(void); int image_set_size(lba_t blk); int image_write(lba_t blk, void *buf, ssize_t len); #endif /* _MKIMG_IMAGE_H_ */ Index: stable/10/usr.bin/mkimg/mbr.c =================================================================== --- stable/10/usr.bin/mkimg/mbr.c (revision 287121) +++ stable/10/usr.bin/mkimg/mbr.c (revision 287122) @@ -1,123 +1,124 @@ /*- * Copyright (c) 2014 Juniper Networks, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 THE AUTHOR 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 __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include "image.h" #include "mkimg.h" #include "scheme.h" #ifndef DOSPTYP_FAT32 #define DOSPTYP_FAT32 0x0b #endif #ifndef DOSPTYP_EFI #define DOSPTYP_EFI 0xef #endif static struct mkimg_alias mbr_aliases[] = { { ALIAS_EBR, ALIAS_INT2TYPE(DOSPTYP_EXT) }, { ALIAS_EFI, ALIAS_INT2TYPE(DOSPTYP_EFI) }, { ALIAS_FAT32, ALIAS_INT2TYPE(DOSPTYP_FAT32) }, { ALIAS_FREEBSD, ALIAS_INT2TYPE(DOSPTYP_386BSD) }, + { ALIAS_NTFS, ALIAS_INT2TYPE(DOSPTYP_NTFS) }, { ALIAS_NONE, 0 } /* Keep last! */ }; static lba_t mbr_metadata(u_int where, lba_t blk) { blk += (where == SCHEME_META_IMG_START) ? 1 : 0; return (round_track(blk)); } static void mbr_chs(u_char *cylp, u_char *hdp, u_char *secp, lba_t lba) { u_int cyl, hd, sec; mkimg_chs(lba, 1023, &cyl, &hd, &sec); *cylp = cyl; *hdp = hd; *secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0); } static int mbr_write(lba_t imgsz __unused, void *bootcode) { u_char *mbr; struct dos_partition *dpbase, *dp; struct part *part; lba_t size; int error; mbr = malloc(secsz); if (mbr == NULL) return (ENOMEM); if (bootcode != NULL) { memcpy(mbr, bootcode, DOSPARTOFF); memset(mbr + DOSPARTOFF, 0, secsz - DOSPARTOFF); } else memset(mbr, 0, secsz); le16enc(mbr + DOSMAGICOFFSET, DOSMAGIC); dpbase = (void *)(mbr + DOSPARTOFF); STAILQ_FOREACH(part, &partlist, link) { size = round_track(part->size); dp = dpbase + part->index; dp->dp_flag = (part->index == 0 && bootcode != NULL) ? 0x80 : 0; mbr_chs(&dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect, part->block); dp->dp_typ = ALIAS_TYPE2INT(part->type); mbr_chs(&dp->dp_ecyl, &dp->dp_ehd, &dp->dp_esect, part->block + size - 1); le32enc(&dp->dp_start, part->block); le32enc(&dp->dp_size, size); } error = image_write(0, mbr, 1); free(mbr); return (error); } static struct mkimg_scheme mbr_scheme = { .name = "mbr", .description = "Master Boot Record", .aliases = mbr_aliases, .metadata = mbr_metadata, .write = mbr_write, .bootcode = 512, .nparts = NDOSPART, .maxsecsz = 4096 }; SCHEME_DEFINE(mbr_scheme); Index: stable/10/usr.bin/mkimg/mkimg.1 =================================================================== --- stable/10/usr.bin/mkimg/mkimg.1 (revision 287121) +++ stable/10/usr.bin/mkimg/mkimg.1 (revision 287122) @@ -1,250 +1,331 @@ .\" Copyright (c) 2013, 2014 Juniper Networks, Inc. .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. 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. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 THE AUTHOR 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. .\" .\" $FreeBSD$ .\" -.Dd February 22, 2015 +.Dd August 7, 2015 .Dt MKIMG 1 .Os .Sh NAME .Nm mkimg .Nd "utility to make disk images" .Sh SYNOPSIS .Nm .Op Fl H Ar heads .Op Fl P Ar blksz .Op Fl S Ar secsz .Op Fl T Ar tracksz .Op Fl b Ar bootcode .Op Fl c Ar capacity .Op Fl f Ar format .Op Fl o Ar outfile .Op Fl v .Op Fl y .Op Fl s Ar scheme Op Fl p Ar partition ... .Nm .Ar --formats | --schemes | --version .Sh DESCRIPTION The .Nm utility creates a disk image from the raw partition contents specified with the .Ar partition argument(s) and using the partitioning scheme specified with the .Ar scheme argument. The disk image is written to .Ar stdout by default or the file specified with the .Ar outfile argument. The image file is a raw disk image by default, but the format of the image file can be specified with the .Ar format argument. .Pp The disk image can be made bootable by specifying the scheme-specific boot block contents with the .Ar bootcode argument and, depending on the scheme, with a boot partition. The contents of such a boot partition is provided like any other partition and the .Nm utility does not treat it any differently from other partitions. .Pp Some partitioning schemes need a disk geometry and for those the .Nm utility accepts the .Ar tracksz and .Ar heads arguments, specifying the number of sectors per track and the number of heads per cylinder (resp.) .Pp Both the logical and physical sector size can be specified and for that the .Nm utility accepts the .Ar secsz and .Ar blksz arguments. The .Ar secsz argument is used to specify the logical sector size. This is the sector size reported by a disk when queried for its capacity. Modern disks use a larger sector size internally, referred to as block size by the .Nm utility and this can be specified by the .Ar blksz argument. The .Nm utility will use the (physical) block size to determine the start of partitions and to round the size of the disk image. .Pp The .Fl c option can be used to specify a minimal capacity for the disk image. Use this option without the .Fl s and .Fl p options to create an empty disk image with the given (virtual) size. An empty partition table can be written to the disk when specifying a partitioning scheme with the .Fl s option, but without specifying any partitions. When the size required to for all the partitions is larger than the given capacity, then the disk image will be larger than the capacity given. .Pp The .Fl v option increases the level of output that the .Nm utility prints. .Pp The .Fl y option is used for testing purposes only and is not to be used in production. When present, the .Nm utility will generate predictable values for Universally Unique Identifiers (UUIDs) and time stamps so that consecutive runs of the .Nm utility will create images that are identical. .Pp A set of long options exist to query about the .Nm -utilty itself. +utility itself. Options in this set should be given by themselves because the .Nm utility exits immediately after providing the requested information. The version of the .Nm utility is printed when the .Ar --version option is given. The list of supported output formats is printed when the .Ar --formats option is given and the list of supported partitioning schemes is printed when the .Ar --schemes option is given. Both the format and scheme lists a space-separated lists for easy handling in scripts. .Pp For a more descriptive list of supported partitioning schemes or supported output format, or for a detailed description of how to specify partitions, run the .Nm utility without any arguments. This will print a usage message with all the necessary details. +.Sh DISK FORMATS +The +.Nm +utility supports a number of output file formats. +A short description of these is given below. +.Ss QCOW and QCOW2 +QCOW stands for "QEMU Copy On Write". +It's a sparse file format akin to VHD and VMDK and QCOW represents the +first version. +QCOW2 represents version 2 of the file format. +Version 2 is not backward compatible with version 1 and adds support for +snapshots among other things. +The QCOW file formats are natively supported by QEMU and Xen. +To write QCOW, specify +.Fl f Ar qcow +on the command line. +To write version 2 QCOW, specify +.Fl f Ar qcow2 +on the command line. +The preferred file extension is ".qcow" and ".qcow2" for QCOW and QCOW2 +(resp.), but ".qcow" is sometimes used for version 2 files as well. +.Ss RAW file format +This file format is a sector by sector representation of an actual disk. +There is no extra information that describes or relates to the format +itself. The size of the file is the size of the (virtual) disk. +This file format is suitable for being copyied onto a disk with utilities +like +.Nm dd . +To write a raw disk file, either omit the +.Fl f +option, or specify +.Fl f Ar raw +on the command line. +The preferred file extension is one of ".img" or ".raw", but there's no +real convention for it. +.Ss Dynamic VHD and Fixed VHD +Microsoft's "Virtual Hard Disk" file formats. +The dynamic format is a sparse format akin to QCOW and VMDK. +The fixed format is effectively a raw format with a footer appended to the +file and as such it's often indistinguishable from the raw format. +The fixed file format has been added to support Microsoft's Azure platform +and due to inconsistencies in interpretation of the footer is not compatible +with utilities like +.Nm qemu +when it is specifically instructed to interpreted the file as a VHD file. +By default +.Nm qemu +will treat the file as a raw disk file, which mostly works fine. +To have +.Nm +create a dynamic VHD file, specify +.Fl f Ar vhd +on the command line. +To create a fixed VHD file for use by Azure, specify +.Fl f Ar vhdf +on the command line. +The preferred file extension is ".vhd". +.Ss VMDK +VMware's "Virtual Machine Disk" file format. +It's a sparse file format akin to QCOW and VHD and supported by many +virtualization solutions. +To create a VMDK file, specify +.Fl f Ar vmdk +on the command line. +The preferred file extension is ".vmdk". +.Pp +Not all virtualization solutions support all file formats, but often those +virtualization environments have utilities to convert from one format to +another. +Note however that conversion may require that the virtual disk size is +changed to match the constraints of the output format and this may invalidate +the contents of the disk image. +For example, the GUID Partition Table (GPT) scheme has a header in the last +sector on the disk. +When changing the disk size, the GPT must be changed so that the last header +is moved accordingly. +This is typically not part of the conversion process. +If possible, use an output format specifically for the environment in which +the file is intended to be used. .Sh ENVIRONMENT .Bl -tag -width "TMPDIR" -compact .It Ev TMPDIR Directory to put temporary files in; default is .Pa /tmp . .El .Sh EXAMPLES To create a bootable disk image that is partitioned using the GPT scheme and containing a root file system that was previously created using .Xr makefs and also containing a swap partition, run the .Nm utility as follows: .Dl % mkimg -s gpt -b /boot/pmbr -p freebsd-boot:=/boot/gptboot \ -p freebsd-ufs:=root-file-system.ufs -p freebsd-swap::1G \ -o gpt.img .Pp The command line given above results in a raw image file. This is because no output format was given. To create a VMDK image for example, add the .Fl f Ar vmdk argument to the .Nm utility and name the output file accordingly. .Pp A nested partitioning scheme is created by running the .Nm utility twice. The output of the first will be fed as the contents of a partition to the second. This can be done using a temporary file, like so: .Dl % mkimg -s bsd -b /boot/boot -p freebsd-ufs:=root-file-system.ufs \ -p freebsd-swap::1G -o /tmp/bsd.img .Dl % mkimg -s mbr -b /boot/mbr -p freebsd:=/tmp/bsd.img -o mbr-bsd.img .Pp Alternatively, the .Nm utility can be run in a cascaded fashion, whereby the output of the first is fed directly into the second. To do this, run the .Nm utility as follows: .Dl % mkimg -s mbr -b /boot/mbr -p freebsd:-'mkimg -s bsd -b /boot/boot \ -p freebsd-ufs:=root-file-system.ufs -p freebsd-swap::1G' -o mbr-bsd.img .Pp To accomodate the need to have partitions named or numbered in a certain way, the .Nm utility allows for the specification of empty partitions. For example, to create an image that is compatible with partition layouts found in .Pa /etc/disktab , the 'd' partition often needs to be skipped. This is accomplished by inserting an unused partition after the first 2 partition specifications. It is worth noting at this time that the BSD scheme will automatically skip the 'c' partition by virtue of it referring to the entire disk. To create an image that is compatible with the qp120at disk, use the .Nm utility as follows: .Dl % mkimg -s bsd -b /boot/boot -p freebsd-ufs:=root-file-system.ufs \ -p freebsd-swap::20M -p- -p- -p- -p- -p freebsd-ufs:=usr-file-system.ufs \ -o bsd.img .Pp For partitioning schemes that feature partition labels, the .Nm utility supports assigning labels to the partitions specified. In the following example the file system partition is labeled as 'backup': .Dl % mkimg -s gpt -p freebsd-ufs/backup:=file-system.ufs -o gpt.img .Sh SEE ALSO +.Xr dd 1 , .Xr gpart 8 , .Xr makefs 8 , .Xr mdconfig 8 , .Xr newfs 8 .Sh HISTORY The .Nm utility first appeared in .Fx 10.1 . .Sh AUTHORS The .Nm -utility and manpage were written by Marcel Moolenaar +utility and manpage were written by +.An Marcel Moolenaar Aq Mt marcelm@juniper.net . Index: stable/10/usr.bin/mkimg/scheme.c =================================================================== --- stable/10/usr.bin/mkimg/scheme.c (revision 287121) +++ stable/10/usr.bin/mkimg/scheme.c (revision 287122) @@ -1,187 +1,188 @@ /*- * Copyright (c) 2013,2014 Juniper Networks, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 THE AUTHOR 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 __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include "image.h" #include "mkimg.h" #include "scheme.h" static struct { const char *name; enum alias alias; } scheme_alias[] = { { "ebr", ALIAS_EBR }, { "efi", ALIAS_EFI }, { "fat32", ALIAS_FAT32 }, { "freebsd", ALIAS_FREEBSD }, { "freebsd-boot", ALIAS_FREEBSD_BOOT }, { "freebsd-nandfs", ALIAS_FREEBSD_NANDFS }, { "freebsd-swap", ALIAS_FREEBSD_SWAP }, { "freebsd-ufs", ALIAS_FREEBSD_UFS }, { "freebsd-vinum", ALIAS_FREEBSD_VINUM }, { "freebsd-zfs", ALIAS_FREEBSD_ZFS }, { "mbr", ALIAS_MBR }, + { "ntfs", ALIAS_NTFS }, { NULL, ALIAS_NONE } /* Keep last! */ }; static struct mkimg_scheme *scheme; static void *bootcode; static enum alias scheme_parse_alias(const char *name) { u_int idx; idx = 0; while (scheme_alias[idx].name != NULL) { if (strcasecmp(scheme_alias[idx].name, name) == 0) return (scheme_alias[idx].alias); idx++; } return (ALIAS_NONE); } int scheme_select(const char *spec) { struct mkimg_scheme *s, **iter; SET_FOREACH(iter, schemes) { s = *iter; if (strcasecmp(spec, s->name) == 0) { scheme = s; return (0); } } return (EINVAL); } struct mkimg_scheme * scheme_selected(void) { return (scheme); } int scheme_bootcode(int fd) { struct stat sb; if (scheme == NULL || scheme->bootcode == 0) return (ENXIO); if (fstat(fd, &sb) == -1) return (errno); if (sb.st_size > scheme->bootcode) return (EFBIG); bootcode = malloc(scheme->bootcode); if (bootcode == NULL) return (ENOMEM); memset(bootcode, 0, scheme->bootcode); if (read(fd, bootcode, sb.st_size) != sb.st_size) { free(bootcode); bootcode = NULL; return (errno); } return (0); } int scheme_check_part(struct part *p) { struct mkimg_alias *iter; enum alias alias; assert(scheme != NULL); /* Check the partition type alias */ alias = scheme_parse_alias(p->alias); if (alias == ALIAS_NONE) return (EINVAL); iter = scheme->aliases; while (iter->alias != ALIAS_NONE) { if (alias == iter->alias) break; iter++; } if (iter->alias == ALIAS_NONE) return (EINVAL); p->type = iter->type; /* Validate the optional label. */ if (p->label != NULL) { if (strlen(p->label) > scheme->labellen) return (EINVAL); } return (0); } u_int scheme_max_parts(void) { return ((scheme == NULL) ? 0 : scheme->nparts); } u_int scheme_max_secsz(void) { return ((scheme == NULL) ? INT_MAX+1U : scheme->maxsecsz); } lba_t scheme_metadata(u_int where, lba_t start) { return ((scheme == NULL) ? start : scheme->metadata(where, start)); } int scheme_write(lba_t end) { return ((scheme == NULL) ? 0 : scheme->write(end, bootcode)); } Index: stable/10/usr.bin/mkimg/scheme.h =================================================================== --- stable/10/usr.bin/mkimg/scheme.h (revision 287121) +++ stable/10/usr.bin/mkimg/scheme.h (revision 287122) @@ -1,90 +1,91 @@ /*- * Copyright (c) 2013,2014 Juniper Networks, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 THE AUTHOR 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. * * $FreeBSD$ */ #ifndef _MKIMG_SCHEME_H_ #define _MKIMG_SCHEME_H_ #include enum alias { ALIAS_NONE, /* Keep first! */ /* start */ ALIAS_EBR, ALIAS_EFI, ALIAS_FAT32, ALIAS_FREEBSD, ALIAS_FREEBSD_BOOT, ALIAS_FREEBSD_NANDFS, ALIAS_FREEBSD_SWAP, ALIAS_FREEBSD_UFS, ALIAS_FREEBSD_VINUM, ALIAS_FREEBSD_ZFS, ALIAS_MBR, + ALIAS_NTFS, /* end */ ALIAS_COUNT /* Keep last! */ }; struct mkimg_alias { u_int alias; uintptr_t type; #define ALIAS_PTR2TYPE(p) (uintptr_t)(p) #define ALIAS_INT2TYPE(i) (i) #define ALIAS_TYPE2PTR(p) (void *)(p) #define ALIAS_TYPE2INT(i) (i) }; struct mkimg_scheme { const char *name; const char *description; struct mkimg_alias *aliases; lba_t (*metadata)(u_int, lba_t); #define SCHEME_META_IMG_START 1 #define SCHEME_META_IMG_END 2 #define SCHEME_META_PART_BEFORE 3 #define SCHEME_META_PART_AFTER 4 int (*write)(lba_t, void *); u_int nparts; u_int labellen; u_int bootcode; u_int maxsecsz; }; SET_DECLARE(schemes, struct mkimg_scheme); #define SCHEME_DEFINE(nm) DATA_SET(schemes, nm) int scheme_select(const char *); struct mkimg_scheme *scheme_selected(void); int scheme_bootcode(int fd); int scheme_check_part(struct part *); u_int scheme_max_parts(void); u_int scheme_max_secsz(void); lba_t scheme_metadata(u_int, lba_t); int scheme_write(lba_t); #endif /* _MKIMG_SCHEME_H_ */ Index: stable/10/usr.bin/mkimg/vhd.c =================================================================== --- stable/10/usr.bin/mkimg/vhd.c (revision 287121) +++ stable/10/usr.bin/mkimg/vhd.c (revision 287122) @@ -1,414 +1,432 @@ /*- * Copyright (c) 2014, 2015 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 THE AUTHOR 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 __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include "image.h" #include "format.h" #include "mkimg.h" #ifndef __has_extension #define __has_extension(x) 0 #endif /* * General notes: * o File is in network byte order. * o The timestamp is seconds since 1/1/2000 12:00:00 AM UTC * * This file is divided in 3 parts: * 1. Common definitions * 2. Dynamic VHD support * 3. Fixed VHD support */ /* * PART 1: Common definitions */ #define VHD_SECTOR_SIZE 512 #define VHD_BLOCK_SIZE (4096 * VHD_SECTOR_SIZE) /* 2MB blocks */ struct vhd_geom { uint16_t cylinders; uint8_t heads; uint8_t sectors; }; struct vhd_footer { uint64_t cookie; #define VHD_FOOTER_COOKIE 0x636f6e6563746978 uint32_t features; #define VHD_FEATURES_TEMPORARY 0x01 #define VHD_FEATURES_RESERVED 0x02 uint32_t version; #define VHD_VERSION 0x00010000 uint64_t data_offset; uint32_t timestamp; uint32_t creator_tool; #define VHD_CREATOR_TOOL 0x2a696d67 /* FreeBSD mkimg */ uint32_t creator_version; #define VHD_CREATOR_VERSION 0x00020000 uint32_t creator_os; #define VHD_CREATOR_OS 0x5769326b /* Wi2k */ uint64_t original_size; uint64_t current_size; struct vhd_geom geometry; uint32_t disk_type; #define VHD_DISK_TYPE_FIXED 2 #define VHD_DISK_TYPE_DYNAMIC 3 #define VHD_DISK_TYPE_DIFF 4 uint32_t checksum; uuid_t id; uint8_t saved_state; uint8_t _reserved[427]; }; #if __has_extension(c_static_assert) _Static_assert(sizeof(struct vhd_footer) == VHD_SECTOR_SIZE, "Wrong size for footer"); #endif static uint32_t vhd_checksum(void *buf, size_t sz) { uint8_t *p = buf; uint32_t sum; size_t ofs; sum = 0; for (ofs = 0; ofs < sz; ofs++) sum += p[ofs]; return (~sum); } static void vhd_geometry(uint64_t image_size, struct vhd_geom *geom) { lba_t imgsz; long cth; imgsz = image_size / VHD_SECTOR_SIZE; /* Respect command line options if possible. */ if (nheads > 1 && nheads < 256 && nsecs > 1 && nsecs < 256 && ncyls < 65536) { geom->cylinders = (ncyls != 0) ? ncyls : imgsz / (nheads * nsecs); geom->heads = nheads; geom->sectors = nsecs; return; } if (imgsz > 65536 * 16 * 255) imgsz = 65536 * 16 * 255; if (imgsz >= 65535 * 16 * 63) { geom->cylinders = imgsz / (16 * 255); geom->heads = 16; geom->sectors = 255; return; } geom->sectors = 17; cth = imgsz / 17; geom->heads = (cth + 1023) / 1024; if (geom->heads < 4) geom->heads = 4; if (cth >= (geom->heads * 1024) || geom->heads > 16) { geom->heads = 16; geom->sectors = 31; cth = imgsz / 31; } if (cth >= (geom->heads * 1024)) { geom->heads = 16; geom->sectors = 63; cth = imgsz / 63; } geom->cylinders = cth / geom->heads; } +static uint64_t +vhd_resize(uint64_t origsz) +{ + struct vhd_geom geom; + uint64_t newsz; + + /* + * Round the image size to the pre-determined geometry that + * matches the image size. This circular dependency implies + * that we need to loop to handle boundary conditions. + * The first time, newsz equals origsz and the geometry will + * typically yield a new size that's smaller. We keep adding + * cylinder's worth of sectors to the new size until its + * larger or equal or origsz. But during those iterations, + * the geometry can change, so we need to account for that. + */ + newsz = origsz; + while (1) { + vhd_geometry(newsz, &geom); + newsz = (int64_t)geom.cylinders * geom.heads * + geom.sectors * VHD_SECTOR_SIZE; + if (newsz >= origsz) + break; + newsz += geom.heads * geom.sectors * VHD_SECTOR_SIZE; + } + return (newsz); +} + static uint32_t vhd_timestamp(void) { time_t t; if (!unit_testing) { t = time(NULL); return (t - 0x386d4380); } return (0x01234567); } static void vhd_uuid_enc(void *buf, const uuid_t *uuid) { uint8_t *p = buf; int i; be32enc(p, uuid->time_low); be16enc(p + 4, uuid->time_mid); be16enc(p + 6, uuid->time_hi_and_version); p[8] = uuid->clock_seq_hi_and_reserved; p[9] = uuid->clock_seq_low; for (i = 0; i < _UUID_NODE_LEN; i++) p[10 + i] = uuid->node[i]; } static void vhd_make_footer(struct vhd_footer *footer, uint64_t image_size, uint32_t disk_type, uint64_t data_offset) { uuid_t id; memset(footer, 0, sizeof(*footer)); be64enc(&footer->cookie, VHD_FOOTER_COOKIE); be32enc(&footer->features, VHD_FEATURES_RESERVED); be32enc(&footer->version, VHD_VERSION); be64enc(&footer->data_offset, data_offset); be32enc(&footer->timestamp, vhd_timestamp()); be32enc(&footer->creator_tool, VHD_CREATOR_TOOL); be32enc(&footer->creator_version, VHD_CREATOR_VERSION); be32enc(&footer->creator_os, VHD_CREATOR_OS); be64enc(&footer->original_size, image_size); be64enc(&footer->current_size, image_size); vhd_geometry(image_size, &footer->geometry); be16enc(&footer->geometry.cylinders, footer->geometry.cylinders); be32enc(&footer->disk_type, disk_type); mkimg_uuid(&id); vhd_uuid_enc(&footer->id, &id); be32enc(&footer->checksum, vhd_checksum(footer, sizeof(*footer))); } /* * PART 2: Dynamic VHD support * * Notes: * o File layout: * copy of disk footer * dynamic disk header * block allocation table (BAT) * data blocks * disk footer */ struct vhd_dyn_header { uint64_t cookie; #define VHD_HEADER_COOKIE 0x6378737061727365 uint64_t data_offset; uint64_t table_offset; uint32_t version; uint32_t max_entries; uint32_t block_size; uint32_t checksum; uuid_t parent_id; uint32_t parent_timestamp; char _reserved1[4]; uint16_t parent_name[256]; /* UTF-16 */ struct { uint32_t code; uint32_t data_space; uint32_t data_length; uint32_t _reserved; uint64_t data_offset; } parent_locator[8]; char _reserved2[256]; }; #if __has_extension(c_static_assert) _Static_assert(sizeof(struct vhd_dyn_header) == VHD_SECTOR_SIZE * 2, "Wrong size for header"); #endif static int vhd_dyn_resize(lba_t imgsz) { uint64_t imagesz; - imagesz = imgsz * secsz; - imagesz = (imagesz + VHD_BLOCK_SIZE - 1) & ~(VHD_BLOCK_SIZE - 1); + imagesz = vhd_resize(imgsz * secsz); return (image_set_size(imagesz / secsz)); } static int vhd_dyn_write(int fd) { struct vhd_footer footer; struct vhd_dyn_header header; - uint64_t imgsz; + uint64_t imgsz, rawsz; lba_t blk, blkcnt, nblks; uint32_t *bat; void *bitmap; size_t batsz; uint32_t sector; int bat_entries, error, entry; - imgsz = image_get_size() * secsz; - bat_entries = imgsz / VHD_BLOCK_SIZE; + rawsz = image_get_size() * secsz; + imgsz = (rawsz + VHD_BLOCK_SIZE - 1) & ~(VHD_BLOCK_SIZE - 1); - vhd_make_footer(&footer, imgsz, VHD_DISK_TYPE_DYNAMIC, sizeof(footer)); + vhd_make_footer(&footer, rawsz, VHD_DISK_TYPE_DYNAMIC, sizeof(footer)); if (sparse_write(fd, &footer, sizeof(footer)) < 0) return (errno); + bat_entries = imgsz / VHD_BLOCK_SIZE; memset(&header, 0, sizeof(header)); be64enc(&header.cookie, VHD_HEADER_COOKIE); be64enc(&header.data_offset, ~0ULL); be64enc(&header.table_offset, sizeof(footer) + sizeof(header)); be32enc(&header.version, VHD_VERSION); be32enc(&header.max_entries, bat_entries); be32enc(&header.block_size, VHD_BLOCK_SIZE); be32enc(&header.checksum, vhd_checksum(&header, sizeof(header))); if (sparse_write(fd, &header, sizeof(header)) < 0) return (errno); batsz = bat_entries * sizeof(uint32_t); batsz = (batsz + VHD_SECTOR_SIZE - 1) & ~(VHD_SECTOR_SIZE - 1); bat = malloc(batsz); if (bat == NULL) return (errno); memset(bat, 0xff, batsz); blkcnt = VHD_BLOCK_SIZE / secsz; sector = (sizeof(footer) + sizeof(header) + batsz) / VHD_SECTOR_SIZE; for (entry = 0; entry < bat_entries; entry++) { blk = entry * blkcnt; if (image_data(blk, blkcnt)) { be32enc(&bat[entry], sector); sector += (VHD_BLOCK_SIZE / VHD_SECTOR_SIZE) + 1; } } if (sparse_write(fd, bat, batsz) < 0) { free(bat); return (errno); } free(bat); bitmap = malloc(VHD_SECTOR_SIZE); if (bitmap == NULL) return (errno); memset(bitmap, 0xff, VHD_SECTOR_SIZE); blk = 0; blkcnt = VHD_BLOCK_SIZE / secsz; error = 0; - nblks = image_get_size(); + nblks = rawsz / secsz; while (blk < nblks) { if (!image_data(blk, blkcnt)) { blk += blkcnt; continue; } if (sparse_write(fd, bitmap, VHD_SECTOR_SIZE) < 0) { error = errno; break; } + /* Handle partial last block */ + if (blk + blkcnt > nblks) + blkcnt = nblks - blk; error = image_copyout_region(fd, blk, blkcnt); if (error) break; blk += blkcnt; } free(bitmap); - if (blk != nblks) + if (error) return (error); - + error = image_copyout_zeroes(fd, imgsz - rawsz); + if (error) + return (error); if (sparse_write(fd, &footer, sizeof(footer)) < 0) return (errno); return (0); } static struct mkimg_format vhd_dyn_format = { .name = "vhd", .description = "Virtual Hard Disk", .resize = vhd_dyn_resize, .write = vhd_dyn_write, }; FORMAT_DEFINE(vhd_dyn_format); /* * PART 3: Fixed VHD */ static int vhd_fix_resize(lba_t imgsz) { - struct vhd_geom geom; - int64_t imagesz; + uint64_t imagesz; + imagesz = vhd_resize(imgsz * secsz); /* - * Round the image size to the pre-determined geometry that - * matches the image size. This circular dependency implies - * that we need to loop to handle boundary conditions. - */ - imgsz *= secsz; - imagesz = imgsz; - while (1) { - vhd_geometry(imagesz, &geom); - imagesz = (int64_t)geom.cylinders * geom.heads * - geom.sectors * VHD_SECTOR_SIZE; - if (imagesz >= imgsz) - break; - imagesz += geom.heads * geom.sectors * VHD_SECTOR_SIZE; - } - /* * Azure demands that images are a whole number of megabytes. */ imagesz = (imagesz + 0xfffffULL) & ~0xfffffULL; return (image_set_size(imagesz / secsz)); } static int vhd_fix_write(int fd) { struct vhd_footer footer; - uint64_t imgsz; + uint64_t imagesz; int error; error = image_copyout(fd); - if (!error) { - imgsz = image_get_size() * secsz; - vhd_make_footer(&footer, imgsz, VHD_DISK_TYPE_FIXED, ~0ULL); - if (sparse_write(fd, &footer, sizeof(footer)) < 0) - error = errno; - } + if (error) + return (error); + + imagesz = image_get_size() * secsz; + vhd_make_footer(&footer, imagesz, VHD_DISK_TYPE_FIXED, ~0ULL); + error = (sparse_write(fd, &footer, sizeof(footer)) < 0) ? errno : 0; return (error); } static struct mkimg_format vhd_fix_format = { - .name = "vhdf", - .description = "Fixed Virtual Hard Disk", - .resize = vhd_fix_resize, - .write = vhd_fix_write, + .name = "vhdf", + .description = "Fixed Virtual Hard Disk", + .resize = vhd_fix_resize, + .write = vhd_fix_write, }; FORMAT_DEFINE(vhd_fix_format); Index: stable/10 =================================================================== --- stable/10 (revision 287121) +++ stable/10 (revision 287122) Property changes on: stable/10 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head:r284883,286215,286395,286417,286419,286660