summaryrefslogtreecommitdiff
path: root/usr.sbin/vmctl/vmctl.c
diff options
context:
space:
mode:
authorReyk Floeter <reyk@cvs.openbsd.org>2018-11-26 10:39:31 +0000
committerReyk Floeter <reyk@cvs.openbsd.org>2018-11-26 10:39:31 +0000
commitf3955aae333e097afa317635834fba91744cc973 (patch)
tree439592930316be8e10cb911a8d5f5dea3ace9cc7 /usr.sbin/vmctl/vmctl.c
parent21660a2650ecb690158fc50421bb139fc92fafce (diff)
Move the {qcow2,raw} create functions from vmctl into vmd/vio{qcow2,raw}.c
This way they are in the appropriate place and code can be shared with vmd. Ok ori@ mlarkin@ ccardenas@
Diffstat (limited to 'usr.sbin/vmctl/vmctl.c')
-rw-r--r--usr.sbin/vmctl/vmctl.c187
1 files changed, 5 insertions, 182 deletions
diff --git a/usr.sbin/vmctl/vmctl.c b/usr.sbin/vmctl/vmctl.c
index 1feca4754d4..66200a6ab28 100644
--- a/usr.sbin/vmctl/vmctl.c
+++ b/usr.sbin/vmctl/vmctl.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: vmctl.c,v 1.62 2018/10/19 10:12:39 reyk Exp $ */
+/* $OpenBSD: vmctl.c,v 1.63 2018/11/26 10:39:30 reyk Exp $ */
/*
* Copyright (c) 2014 Mike Larkin <mlarkin@openbsd.org>
@@ -851,10 +851,10 @@ open_imagefile(int type, const char *imgfile_path, int flags,
return (-1);
}
}
- ret = virtio_init_qcow2(file, sz, basefd, nfd);
+ ret = virtio_qcow2_init(file, sz, basefd, nfd);
break;
default:
- ret = virtio_init_raw(file, sz, &fd, 1);
+ ret = virtio_raw_init(file, sz, &fd, 1);
break;
}
@@ -893,191 +893,14 @@ create_imagefile(int type, const char *imgfile_path, const char *base_path,
switch (type) {
case VMDF_QCOW2:
*format = "qcow2";
- ret = create_qc2_imagefile(imgfile_path, base_path, imgsize);
+ ret = virtio_qcow2_create(imgfile_path, base_path, imgsize);
break;
default:
*format = "raw";
- ret = create_raw_imagefile(imgfile_path, imgsize);
+ ret = virtio_raw_create(imgfile_path, imgsize);
break;
}
return (ret);
}
-/*
- * create_raw_imagefile
- *
- * Create an empty imagefile with the specified path and size.
- *
- * Parameters:
- * imgfile_path: path to the image file to create
- * imgsize : size of the image file to create (in MB)
- *
- * Return:
- * EEXIST: The requested image file already exists
- * 0 : Image file successfully created
- * Exxxx : Various other Exxxx errno codes due to other I/O errors
- */
-int
-create_raw_imagefile(const char *imgfile_path, long imgsize)
-{
- int fd, ret;
-
- /* Refuse to overwrite an existing image */
- fd = open(imgfile_path, O_RDWR | O_CREAT | O_TRUNC | O_EXCL,
- S_IRUSR | S_IWUSR);
- if (fd == -1)
- return (errno);
-
- /* Extend to desired size */
- if (ftruncate(fd, (off_t)imgsize * 1024 * 1024) == -1) {
- ret = errno;
- close(fd);
- unlink(imgfile_path);
- return (ret);
- }
-
- ret = close(fd);
- return (ret);
-}
-
-/*
- * create_qc2_imagefile
- *
- * Create an empty qcow2 imagefile with the specified path and size.
- *
- * Parameters:
- * imgfile_path: path to the image file to create
- * imgsize : size of the image file to create (in MB)
- *
- * Return:
- * EEXIST: The requested image file already exists
- * 0 : Image file successfully created
- * Exxxx : Various other Exxxx errno codes due to other I/O errors
- */
-int
-create_qc2_imagefile(const char *imgfile_path,
- const char *base_path, long imgsize)
-{
- struct qcheader {
- char magic[4];
- uint32_t version;
- uint64_t backingoff;
- uint32_t backingsz;
- uint32_t clustershift;
- uint64_t disksz;
- uint32_t cryptmethod;
- uint32_t l1sz;
- uint64_t l1off;
- uint64_t refoff;
- uint32_t refsz;
- uint32_t snapcount;
- uint64_t snapsz;
- /* v3 additions */
- uint64_t incompatfeatures;
- uint64_t compatfeatures;
- uint64_t autoclearfeatures;
- uint32_t reforder;
- uint32_t headersz;
- } __packed hdr, basehdr;
- int fd, ret;
- ssize_t base_len;
- uint64_t l1sz, refsz, disksz, initsz, clustersz;
- uint64_t l1off, refoff, v, i, l1entrysz, refentrysz;
- uint16_t refs;
-
- disksz = 1024 * 1024 * imgsize;
-
- if (base_path) {
- fd = open(base_path, O_RDONLY);
- if (read(fd, &basehdr, sizeof(basehdr)) != sizeof(basehdr))
- err(1, "failure to read base image header");
- close(fd);
- if (strncmp(basehdr.magic,
- VM_MAGIC_QCOW, strlen(VM_MAGIC_QCOW)) != 0)
- errx(1, "base image is not a qcow2 file");
- if (!disksz)
- disksz = betoh64(basehdr.disksz);
- else if (disksz != betoh64(basehdr.disksz))
- errx(1, "base size does not match requested size");
- }
- if (!base_path && !disksz)
- errx(1, "missing disk size");
-
- clustersz = (1<<16);
- l1off = ALIGN(sizeof(hdr), clustersz);
-
- l1entrysz = clustersz * clustersz / 8;
- l1sz = (disksz + l1entrysz - 1) / l1entrysz;
-
- refoff = ALIGN(l1off + 8*l1sz, clustersz);
- refentrysz = clustersz * clustersz * clustersz / 2;
- refsz = (disksz + refentrysz - 1) / refentrysz;
-
- initsz = ALIGN(refoff + refsz*clustersz, clustersz);
- base_len = base_path ? strlen(base_path) : 0;
-
- memcpy(hdr.magic, VM_MAGIC_QCOW, strlen(VM_MAGIC_QCOW));
- hdr.version = htobe32(3);
- hdr.backingoff = htobe64(base_path ? sizeof(hdr) : 0);
- hdr.backingsz = htobe32(base_len);
- hdr.clustershift = htobe32(16);
- hdr.disksz = htobe64(disksz);
- hdr.cryptmethod = htobe32(0);
- hdr.l1sz = htobe32(l1sz);
- hdr.l1off = htobe64(l1off);
- hdr.refoff = htobe64(refoff);
- hdr.refsz = htobe32(refsz);
- hdr.snapcount = htobe32(0);
- hdr.snapsz = htobe64(0);
- hdr.incompatfeatures = htobe64(0);
- hdr.compatfeatures = htobe64(0);
- hdr.autoclearfeatures = htobe64(0);
- hdr.reforder = htobe32(4);
- hdr.headersz = htobe32(sizeof(hdr));
-
- /* Refuse to overwrite an existing image */
- fd = open(imgfile_path, O_RDWR | O_CREAT | O_TRUNC | O_EXCL,
- S_IRUSR | S_IWUSR);
- if (fd == -1)
- return (errno);
-
- /* Write out the header */
- if (write(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
- goto error;
-
- /* Add the base image */
- if (base_path && write(fd, base_path, base_len) != base_len)
- goto error;
-
- /* Extend to desired size, and add one refcount cluster */
- if (ftruncate(fd, (off_t)initsz + clustersz) == -1)
- goto error;
-
- /*
- * Paranoia: if our disk image takes more than one cluster
- * to refcount the initial image, fail.
- */
- if (initsz/clustersz > clustersz/2) {
- errno = ERANGE;
- goto error;
- }
-
- /* Add a refcount block, and refcount ourselves. */
- v = htobe64(initsz);
- if (pwrite(fd, &v, 8, refoff) != 8)
- goto error;
- for (i = 0; i < initsz/clustersz + 1; i++) {
- refs = htobe16(1);
- if (pwrite(fd, &refs, 2, initsz + 2*i) != 2)
- goto error;
- }
-
- ret = close(fd);
- return (ret);
-error:
- ret = errno;
- close(fd);
- unlink(imgfile_path);
- return (errno);
-}