summaryrefslogtreecommitdiff
path: root/usr.sbin/vmd
diff options
context:
space:
mode:
Diffstat (limited to 'usr.sbin/vmd')
-rw-r--r--usr.sbin/vmd/vioqcow2.c13
-rw-r--r--usr.sbin/vmd/vioraw.c7
-rw-r--r--usr.sbin/vmd/virtio.c6
-rw-r--r--usr.sbin/vmd/virtio.h4
-rw-r--r--usr.sbin/vmd/vm.c7
-rw-r--r--usr.sbin/vmd/vmboot.c61
-rw-r--r--usr.sbin/vmd/vmd.h8
7 files changed, 73 insertions, 33 deletions
diff --git a/usr.sbin/vmd/vioqcow2.c b/usr.sbin/vmd/vioqcow2.c
index b348804756c..ada0bafc72d 100644
--- a/usr.sbin/vmd/vioqcow2.c
+++ b/usr.sbin/vmd/vioqcow2.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: vioqcow2.c,v 1.4 2018/09/28 08:29:06 reyk Exp $ */
+/* $OpenBSD: vioqcow2.c,v 1.5 2018/09/28 12:35:32 reyk Exp $ */
/*
* Copyright (c) 2018 Ori Bernstein <ori@eigenstate.org>
@@ -108,7 +108,7 @@ static int qc2_openpath(struct qcdisk *, char *, int);
static int qc2_open(struct qcdisk *, int);
static ssize_t qc2_pread(void *, char *, size_t, off_t);
static ssize_t qc2_pwrite(void *, char *, size_t, off_t);
-static void qc2_close(void *);
+static void qc2_close(void *, int);
/*
* Initializes a raw disk image backing file from an fd.
@@ -269,7 +269,7 @@ qc2_open(struct qcdisk *disk, int fd)
return 0;
error:
- qc2_close(disk);
+ qc2_close(disk, 0);
return -1;
}
@@ -377,14 +377,15 @@ qc2_pwrite(void *p, char *buf, size_t len, off_t off)
}
static void
-qc2_close(void *p)
+qc2_close(void *p, int stayopen)
{
struct qcdisk *disk;
disk = p;
if (disk->base)
- qc2_close(disk->base);
- close(disk->fd);
+ qc2_close(disk->base, stayopen);
+ if (!stayopen)
+ close(disk->fd);
free(disk->l1);
free(disk);
}
diff --git a/usr.sbin/vmd/vioraw.c b/usr.sbin/vmd/vioraw.c
index 621d7a6854d..e02ab67c5dc 100644
--- a/usr.sbin/vmd/vioraw.c
+++ b/usr.sbin/vmd/vioraw.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: vioraw.c,v 1.2 2018/09/19 04:29:21 ccardenas Exp $ */
+/* $OpenBSD: vioraw.c,v 1.3 2018/09/28 12:35:32 reyk Exp $ */
/*
* Copyright (c) 2018 Ori Bernstein <ori@eigenstate.org>
*
@@ -40,9 +40,10 @@ raw_pwrite(void *file, char *buf, size_t len, off_t off)
}
static void
-raw_close(void *file)
+raw_close(void *file, int stayopen)
{
- close(*(int *)file);
+ if (!stayopen)
+ close(*(int *)file);
free(file);
}
diff --git a/usr.sbin/vmd/virtio.c b/usr.sbin/vmd/virtio.c
index 05734cfae34..93490344560 100644
--- a/usr.sbin/vmd/virtio.c
+++ b/usr.sbin/vmd/virtio.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: virtio.c,v 1.69 2018/09/19 04:29:21 ccardenas Exp $ */
+/* $OpenBSD: virtio.c,v 1.70 2018/09/28 12:35:32 reyk Exp $ */
/*
* Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
@@ -2015,9 +2015,9 @@ virtio_shutdown(struct vmd_vm *vm)
int i;
/* ensure that our disks are synced */
- vioscsi->file.close(vioscsi->file.p);
+ vioscsi->file.close(vioscsi->file.p, 0);
for (i = 0; i < nr_vioblk; i++)
- vioblk[i].file.close(vioblk[i].file.p);
+ vioblk[i].file.close(vioblk[i].file.p, 0);
}
int
diff --git a/usr.sbin/vmd/virtio.h b/usr.sbin/vmd/virtio.h
index 2cbb0d627eb..46006916b6a 100644
--- a/usr.sbin/vmd/virtio.h
+++ b/usr.sbin/vmd/virtio.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: virtio.h,v 1.29 2018/09/19 04:29:21 ccardenas Exp $ */
+/* $OpenBSD: virtio.h,v 1.30 2018/09/28 12:35:32 reyk Exp $ */
/*
* Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
@@ -65,7 +65,7 @@ struct virtio_backing {
void *p;
ssize_t (*pread)(void *p, char *buf, size_t len, off_t off);
ssize_t (*pwrite)(void *p, char *buf, size_t len, off_t off);
- void (*close)(void *p);
+ void (*close)(void *p, int);
};
/*
diff --git a/usr.sbin/vmd/vm.c b/usr.sbin/vmd/vm.c
index 99acb3025f2..ef4494d918b 100644
--- a/usr.sbin/vmd/vm.c
+++ b/usr.sbin/vmd/vm.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: vm.c,v 1.39 2018/09/19 04:29:21 ccardenas Exp $ */
+/* $OpenBSD: vm.c,v 1.40 2018/09/28 12:35:32 reyk Exp $ */
/*
* Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
@@ -266,7 +266,8 @@ loadfile_bios(FILE *fp, struct vcpu_reg_state *vrs)
int
start_vm(struct vmd_vm *vm, int fd)
{
- struct vm_create_params *vcp = &vm->vm_params.vmc_params;
+ struct vmop_create_params *vmc = &vm->vm_params;
+ struct vm_create_params *vcp = &vmc->vmc_params;
struct vcpu_reg_state vrs;
int nicfds[VMM_MAX_NICS_PER_VM];
int ret;
@@ -326,7 +327,7 @@ start_vm(struct vmd_vm *vm, int fd)
/* Find and open kernel image */
if ((fp = vmboot_open(vm->vm_kernel,
- vm->vm_disks[0], &vmboot)) == NULL)
+ vm->vm_disks[0], vmc->vmc_disktypes[0], &vmboot)) == NULL)
fatalx("failed to open kernel - exiting");
/* Load kernel image */
diff --git a/usr.sbin/vmd/vmboot.c b/usr.sbin/vmd/vmboot.c
index f17ae24ed34..44ceeb64a7e 100644
--- a/usr.sbin/vmd/vmboot.c
+++ b/usr.sbin/vmd/vmboot.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: vmboot.c,v 1.4 2017/08/29 21:10:20 deraadt Exp $ */
+/* $OpenBSD: vmboot.c,v 1.5 2018/09/28 12:35:32 reyk Exp $ */
/*
* Copyright (c) 2016 Reyk Floeter <reyk@openbsd.org>
@@ -35,6 +35,7 @@
#include "vmd.h"
#include "vmboot.h"
+#include "virtio.h"
int vmboot_bootconf(char *, size_t, struct vmboot_params *);
int vmboot_bootcmd(char *, struct vmboot_params *);
@@ -243,21 +244,22 @@ vmboot_strategy(void *devdata, int rw,
daddr32_t blk, size_t size, void *buf, size_t *rsize)
{
struct vmboot_params *vmboot = devdata;
+ struct virtio_backing *vfp = vmboot->vbp_arg;
ssize_t rlen;
- if (vmboot->vbp_fd == -1)
+ if (vfp == NULL)
return (EIO);
switch (rw) {
case F_READ:
- rlen = pread(vmboot->vbp_fd, buf, size,
+ rlen = vfp->pread(vfp->p, buf, size,
(blk + vmboot->vbp_partoff) * DEV_BSIZE);
if (rlen == -1)
return (errno);
*rsize = (size_t)rlen;
break;
case F_WRITE:
- rlen = pwrite(vmboot->vbp_fd, buf, size,
+ rlen = vfp->pwrite(vfp->p, buf, size,
(blk + vmboot->vbp_partoff) * DEV_BSIZE);
if (rlen == -1)
return (errno);
@@ -383,15 +385,19 @@ vmboot_loadfile(struct open_file *f, char *file, size_t *size)
}
FILE *
-vmboot_open(int kernel_fd, int disk_fd, struct vmboot_params *vmboot)
+vmboot_open(int kernel_fd, int disk_fd, unsigned int disk_type,
+ struct vmboot_params *vmboot)
{
char file[PATH_MAX];
char *buf = NULL;
size_t size;
FILE *fp = NULL;
struct disklabel dl;
+ struct virtio_backing *vfp;
+ off_t sz;
memset(vmboot, 0, sizeof(*vmboot));
+ memset(&vfp, 0, sizeof(vfp));
memset(&dl, 0, sizeof(dl));
/* First open kernel directly if specified by fd */
@@ -401,13 +407,33 @@ vmboot_open(int kernel_fd, int disk_fd, struct vmboot_params *vmboot)
if (disk_fd == -1)
return (NULL);
+ if ((vfp = calloc(1, sizeof(*vfp))) == NULL)
+ goto fail;
+ vmboot->vbp_type = disk_type;
+ vmboot->vbp_arg = vfp;
+
+ switch (vmboot->vbp_type) {
+ case VMDF_RAW:
+ if (virtio_init_raw(vfp, &sz, disk_fd) == -1) {
+ log_debug("%s: could not open raw disk", __func__);
+ goto fail;
+ }
+ break;
+ case VMDF_QCOW2:
+ if (virtio_init_qcow2(vfp, &sz, disk_fd) == -1) {
+ log_debug("%s: could not open qcow2 disk", __func__);
+ goto fail;
+ }
+ break;
+ }
+
vmboot->vbp_fd = disk_fd;
vmboot_file.f_devdata = vmboot;
if ((vmboot->vbp_partoff =
vmboot_findopenbsd(&vmboot_file, 0, &dl)) == -1) {
log_debug("%s: could not find openbsd partition", __func__);
- return (NULL);
+ goto fail;
}
/* Set the default kernel boot device and image path */
@@ -421,7 +447,7 @@ vmboot_open(int kernel_fd, int disk_fd, struct vmboot_params *vmboot)
if ((buf = vmboot_loadfile(&vmboot_file, file, &size)) != NULL) {
if (vmboot_bootconf(buf, size, vmboot) == -1) {
free(buf);
- return (NULL);
+ goto fail;
}
free(buf);
}
@@ -429,11 +455,11 @@ vmboot_open(int kernel_fd, int disk_fd, struct vmboot_params *vmboot)
/* Parse boot device and find partition in disk label */
if ((vmboot->vbp_bootdev =
vmboot_bootdevice(vmboot->vbp_device)) == 0)
- return (NULL);
+ goto fail;
if (B_PARTITION(vmboot->vbp_bootdev) > dl.d_npartitions) {
log_debug("%s: invalid boot partition: %s",
__func__, vmboot->vbp_device);
- return (NULL);
+ goto fail;
}
vmboot->vbp_partoff =
dl.d_partitions[B_PARTITION(vmboot->vbp_bootdev)].p_offset;
@@ -443,25 +469,34 @@ vmboot_open(int kernel_fd, int disk_fd, struct vmboot_params *vmboot)
vmboot->vbp_image, &size)) == NULL) {
log_debug("%s: failed to open kernel %s:%s", __func__,
vmboot->vbp_device, vmboot->vbp_image);
- return (NULL);
+ goto fail;
}
- vmboot->vbp_arg = buf;
+ vmboot->vbp_buf = buf;
if ((fp = fmemopen(buf, size, "r")) == NULL) {
log_debug("%s: failed to open memory stream", __func__);
free(buf);
- vmboot->vbp_arg = NULL;
+ vmboot->vbp_buf = NULL;
} else {
log_debug("%s: kernel %s:%s", __func__,
vmboot->vbp_device, vmboot->vbp_image);
}
return (fp);
+ fail:
+ vmboot_close(fp, vmboot);
+ return (NULL);
}
void
vmboot_close(FILE *fp, struct vmboot_params *vmboot)
{
- fclose(fp);
+ struct virtio_backing *vfp = vmboot->vbp_arg;
+
+ if (fp != NULL)
+ fclose(fp);
+ if (vfp != NULL)
+ vfp->close(vfp->p, 1);
free(vmboot->vbp_arg);
+ free(vmboot->vbp_buf);
}
diff --git a/usr.sbin/vmd/vmd.h b/usr.sbin/vmd/vmd.h
index a791c7388d5..4cf0295d946 100644
--- a/usr.sbin/vmd/vmd.h
+++ b/usr.sbin/vmd/vmd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: vmd.h,v 1.79 2018/09/09 04:09:32 ccardenas Exp $ */
+/* $OpenBSD: vmd.h,v 1.80 2018/09/28 12:35:32 reyk Exp $ */
/*
* Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
@@ -205,7 +205,9 @@ struct vmboot_params {
char vbp_image[PATH_MAX];
uint32_t vbp_bootdev;
uint32_t vbp_howto;
- char *vbp_arg;
+ unsigned int vbp_type;
+ void *vbp_arg;
+ char *vbp_buf;
};
struct vmd_if {
@@ -402,7 +404,7 @@ int config_getif(struct privsep *, struct imsg *);
int config_getcdrom(struct privsep *, struct imsg *);
/* vmboot.c */
-FILE *vmboot_open(int, int, struct vmboot_params *);
+FILE *vmboot_open(int, int, unsigned int, struct vmboot_params *);
void vmboot_close(FILE *, struct vmboot_params *);
/* parse.y */