summaryrefslogtreecommitdiffstats
path: root/usr.sbin/vmctl
diff options
context:
space:
mode:
authorreyk <reyk@openbsd.org>2018-11-26 10:39:30 +0000
committerreyk <reyk@openbsd.org>2018-11-26 10:39:30 +0000
commit62df93ee544d569504e48e1d02765ea2ac7f87cf (patch)
tree45d977c66d977e44f462200b72f593f97cc7e3b1 /usr.sbin/vmctl
parentKeep a list of known vms, and reuse the VM IDs. (diff)
downloadwireguard-openbsd-62df93ee544d569504e48e1d02765ea2ac7f87cf.tar.xz
wireguard-openbsd-62df93ee544d569504e48e1d02765ea2ac7f87cf.zip
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')
-rw-r--r--usr.sbin/vmctl/main.c4
-rw-r--r--usr.sbin/vmctl/vmctl.c187
-rw-r--r--usr.sbin/vmctl/vmctl.h5
3 files changed, 8 insertions, 188 deletions
diff --git a/usr.sbin/vmctl/main.c b/usr.sbin/vmctl/main.c
index da84b5d9e5a..d0efcaf3c8c 100644
--- a/usr.sbin/vmctl/main.c
+++ b/usr.sbin/vmctl/main.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: main.c,v 1.47 2018/10/19 10:12:39 reyk Exp $ */
+/* $OpenBSD: main.c,v 1.48 2018/11/26 10:39:30 reyk Exp $ */
/*
* Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
@@ -658,7 +658,7 @@ ctl_convert(const char *srcfile, const char *dstfile, int dsttype, size_t dstsiz
}
/* align to megabytes */
- dst.size = ALIGN(dstsize, 1048576);
+ dst.size = ALIGNSZ(dstsize, 1048576);
if ((ret = create_imagefile(dst.type, dst.disk, NULL,
dst.size / 1048576, &format)) != 0) {
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);
-}
diff --git a/usr.sbin/vmctl/vmctl.h b/usr.sbin/vmctl/vmctl.h
index 2948a4fc0ce..37aeac34ce2 100644
--- a/usr.sbin/vmctl/vmctl.h
+++ b/usr.sbin/vmctl/vmctl.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: vmctl.h,v 1.27 2018/10/19 10:12:39 reyk Exp $ */
+/* $OpenBSD: vmctl.h,v 1.28 2018/11/26 10:39:30 reyk Exp $ */
/*
* Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
@@ -71,9 +71,6 @@ struct ctl_command {
struct imsgbuf *ibuf;
-#define ALIGN(sz, align) ((sz + align - 1) & ~(align - 1))
-#define MIN(a,b) (((a)<(b))?(a):(b))
-
/* main.c */
int vmmaction(struct parse_result *);
int parse_ifs(struct parse_result *, char *, int);