diff options
author | ori <ori@cvs.openbsd.org> | 2018-11-26 05:44:47 +0000 |
---|---|---|
committer | ori <ori@cvs.openbsd.org> | 2018-11-26 05:44:47 +0000 |
commit | 21660a2650ecb690158fc50421bb139fc92fafce (patch) | |
tree | d8936f050ff1b32d298797f6d2736a1512f1d113 /usr.sbin/vmd | |
parent | 0d487244d2a7fbbd6d9e44619cec5592415ff0c2 (diff) |
Keep a list of known vms, and reuse the VM IDs.
This means that when using '-L', the IP addresses of the VMs are stable.
ok reyk@
Diffstat (limited to 'usr.sbin/vmd')
-rw-r--r-- | usr.sbin/vmd/config.c | 10 | ||||
-rw-r--r-- | usr.sbin/vmd/vmd.c | 30 | ||||
-rw-r--r-- | usr.sbin/vmd/vmd.h | 11 |
3 files changed, 44 insertions, 7 deletions
diff --git a/usr.sbin/vmd/config.c b/usr.sbin/vmd/config.c index 5a3be110df1..c49696cb38d 100644 --- a/usr.sbin/vmd/config.c +++ b/usr.sbin/vmd/config.c @@ -1,4 +1,4 @@ -/* $OpenBSD: config.c,v 1.56 2018/11/24 04:51:55 ori Exp $ */ +/* $OpenBSD: config.c,v 1.57 2018/11/26 05:44:46 ori Exp $ */ /* * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org> @@ -87,7 +87,10 @@ config_init(struct vmd *env) if (what & CONFIG_VMS) { if ((env->vmd_vms = calloc(1, sizeof(*env->vmd_vms))) == NULL) return (-1); + if ((env->vmd_known = calloc(1, sizeof(*env->vmd_known))) == NULL) + return (-1); TAILQ_INIT(env->vmd_vms); + TAILQ_INIT(env->vmd_known); } if (what & CONFIG_SWITCHES) { if ((env->vmd_switches = calloc(1, @@ -109,6 +112,7 @@ void config_purge(struct vmd *env, unsigned int reset) { struct privsep *ps = &env->vmd_ps; + struct name2id *n2i; struct vmd_vm *vm; struct vmd_switch *vsw; unsigned int what; @@ -125,6 +129,10 @@ config_purge(struct vmd *env, unsigned int reset) while ((vm = TAILQ_FIRST(env->vmd_vms)) != NULL) { vm_remove(vm, __func__); } + while ((n2i = TAILQ_FIRST(env->vmd_known)) != NULL) { + TAILQ_REMOVE(env->vmd_known, n2i, entry); + free(n2i); + } env->vmd_nvm = 0; } if (what & CONFIG_SWITCHES && env->vmd_switches != NULL) { diff --git a/usr.sbin/vmd/vmd.c b/usr.sbin/vmd/vmd.c index 9423081df1e..2afc11f63c5 100644 --- a/usr.sbin/vmd/vmd.c +++ b/usr.sbin/vmd/vmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vmd.c,v 1.105 2018/11/21 12:31:47 reyk Exp $ */ +/* $OpenBSD: vmd.c,v 1.106 2018/11/26 05:44:46 ori Exp $ */ /* * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org> @@ -62,6 +62,7 @@ int vmd_check_vmh(struct vm_dump_header *); int vm_instance(struct privsep *, struct vmd_vm **, struct vmop_create_params *, uid_t); int vm_checkinsflag(struct vmop_create_params *, unsigned int, uid_t); +uint32_t vm_claimid(const char *, int); struct vmd *env; @@ -1169,6 +1170,28 @@ vm_remove(struct vmd_vm *vm, const char *caller) free(vm); } +uint32_t +vm_claimid(const char *name, int uid) +{ + struct name2id *n2i = NULL; + + TAILQ_FOREACH(n2i, env->vmd_known, entry) + if (strcmp(n2i->name, name) == 0 && n2i->uid == uid) + return n2i->id; + + if (++env->vmd_nvm == 0) + fatalx("too many vms"); + if ((n2i = calloc(1, sizeof(struct name2id))) == NULL) + fatalx("could not alloc vm name"); + n2i->id = env->vmd_nvm; + n2i->uid = uid; + if (strlcpy(n2i->name, name, sizeof(n2i->name)) >= sizeof(n2i->name)) + fatalx("overlong vm name"); + TAILQ_INSERT_TAIL(env->vmd_known, n2i, entry); + + return n2i->id; +} + int vm_register(struct privsep *ps, struct vmop_create_params *vmc, struct vmd_vm **ret_vm, uint32_t id, uid_t uid) @@ -1300,11 +1323,8 @@ vm_register(struct privsep *ps, struct vmop_create_params *vmc, vm->vm_cdrom = -1; vm->vm_iev.ibuf.fd = -1; - if (++env->vmd_nvm == 0) - fatalx("too many vms"); - /* Assign a new internal Id if not specified */ - vm->vm_vmid = id == 0 ? env->vmd_nvm : id; + vm->vm_vmid = (id == 0) ? vm_claimid(vcp->vcp_name, uid) : id; log_debug("%s: registering vm %d", __func__, vm->vm_vmid); TAILQ_INSERT_TAIL(env->vmd_vms, vm, vm_entry); diff --git a/usr.sbin/vmd/vmd.h b/usr.sbin/vmd/vmd.h index de87e4337ee..c7ee8ecac14 100644 --- a/usr.sbin/vmd/vmd.h +++ b/usr.sbin/vmd/vmd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: vmd.h,v 1.85 2018/11/21 12:31:47 reyk Exp $ */ +/* $OpenBSD: vmd.h,v 1.86 2018/11/26 05:44:46 ori Exp $ */ /* * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org> @@ -290,6 +290,14 @@ struct vmd_user { }; TAILQ_HEAD(userlist, vmd_user); +struct name2id { + char name[VMM_MAX_NAME_LEN]; + int uid; + int32_t id; + TAILQ_ENTRY(name2id) entry; +}; +TAILQ_HEAD(name2idlist, name2id); + struct address { struct sockaddr_storage ss; int prefixlen; @@ -319,6 +327,7 @@ struct vmd { uint32_t vmd_nvm; struct vmlist *vmd_vms; + struct name2idlist *vmd_known; uint32_t vmd_nswitches; struct switchlist *vmd_switches; struct userlist *vmd_users; |