No idea if the code works yet.
Hopefully I can try later. But love the idea. 

Mischa

> On 8 Oct 2018, at 04:31, Ori Bernstein <o...@eigenstate.org> wrote:
> 
> 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.
> 
> diff --git usr.sbin/vmd/config.c usr.sbin/vmd/config.c
> index af12b790002..522bae32501 100644
> --- usr.sbin/vmd/config.c
> +++ usr.sbin/vmd/config.c
> @@ -61,7 +61,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,
> diff --git usr.sbin/vmd/vmd.c usr.sbin/vmd/vmd.c
> index 18a5e0d3d5d..732691b4381 100644
> --- usr.sbin/vmd/vmd.c
> +++ usr.sbin/vmd/vmd.c
> @@ -1169,6 +1169,27 @@ vm_remove(struct vmd_vm *vm, const char *caller)
>    free(vm);
> }
> 
> +static uint32_t
> +claim_vmid(const char *name)
> +{
> +    struct name2id *n2i = NULL;
> +
> +    TAILQ_FOREACH(n2i, env->vmd_known, entry)
> +        if (strcmp(n2i->name, name) == 0)
> +            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;
> +    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 +1321,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) ? claim_vmid(vcp->vcp_name) : id;
> 
>    log_debug("%s: registering vm %d", __func__, vm->vm_vmid);
>    TAILQ_INSERT_TAIL(env->vmd_vms, vm, vm_entry);
> diff --git usr.sbin/vmd/vmd.h usr.sbin/vmd/vmd.h
> index b7c012854e8..86fad536e59 100644
> --- usr.sbin/vmd/vmd.h
> +++ usr.sbin/vmd/vmd.h
> @@ -276,6 +276,13 @@ struct vmd_user {
> };
> TAILQ_HEAD(userlist, vmd_user);
> 
> +struct name2id {
> +    char            name[VMM_MAX_NAME_LEN];
> +    int32_t            id;
> +    TAILQ_ENTRY(name2id)    entry;
> +};
> +TAILQ_HEAD(name2idlist, name2id);
> +
> struct address {
>    struct sockaddr_storage     ss;
>    int             prefixlen;
> @@ -300,6 +307,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;
> 
> -- 
>    Ori Bernstein
> 

Reply via email to