From: dv Date: Wed, 4 May 2022 23:17:25 +0000 (+0000) Subject: vmctl(8)/vmd(8): convert disk sizes from MB to bytes X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=ead1b14692bf0c0449024b84a6fd3705361b7e77;p=openbsd vmctl(8)/vmd(8): convert disk sizes from MB to bytes Continue converting other parts to storing data in bytes instead of MB. In this case, the logic for disk sizes was being scaled. This fixes issues reported by Martin Vahlensieck where vmctl could no longer create disks larger than 7 MiB after previous commits to change storing memory sizes as bytes. While this keeps the vm memory limit check in vmctl's size parser, it skips the limit check for disks. The error messages adjust accordingly and this removes the double error message logging. Update comments and function types accordingly. ok marlkin@ --- diff --git a/usr.sbin/vmctl/main.c b/usr.sbin/vmctl/main.c index 37fc4dc642a..d5b7f937414 100644 --- a/usr.sbin/vmctl/main.c +++ b/usr.sbin/vmctl/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.69 2022/05/03 21:39:18 dv Exp $ */ +/* $OpenBSD: main.c,v 1.70 2022/05/04 23:17:25 dv Exp $ */ /* * Copyright (c) 2015 Reyk Floeter @@ -401,30 +401,25 @@ parse_network(struct parse_result *res, char *word) return (0); } -int -parse_size(struct parse_result *res, char *word) +void +parse_size(struct parse_result *res, char *word, const char *type) { char result[FMT_SCALED_STRSIZE]; long long val = 0; if (word != NULL) { - if (scan_scaled(word, &val) != 0) { - warn("invalid memory size: %s", word); - return (-1); - } + if (scan_scaled(word, &val) != 0) + err(1, "invalid %s size: %s", type, word); } - if (val < (1024 * 1024)) { - warnx("memory size must be at least 1MB"); - return (-1); - } + if (val < (1024 * 1024)) + errx(1, "%s size must be at least 1MB", type); - if (val > VMM_MAX_VM_MEM_SIZE) { + if (strcmp("memory", type) == 0 && val > VMM_MAX_VM_MEM_SIZE) { if (fmt_scaled(VMM_MAX_VM_MEM_SIZE, result) == 0) - warnx("memory size too large (limit is %s)", result); + errx(1, "memory size too large (limit is %s)", result); else - warnx("memory size too large"); - return (-1); + errx(1, "memory size too large"); } /* Round down to the megabyte. */ @@ -432,12 +427,10 @@ parse_size(struct parse_result *res, char *word) if (res->size != (size_t)val) { if (fmt_scaled(res->size, result) == 0) - warnx("memory size rounded to %s", result); + warnx("%s size rounded to %s", type, result); else - warnx("memory size rounded to %zu bytes", res->size); + warnx("%s size rounded to %zuB", type, res->size); } - - return (0); } int @@ -584,8 +577,7 @@ ctl_create(struct parse_result *res, int argc, char *argv[]) input = optarg; break; case 's': - if (parse_size(res, optarg) != 0) - errx(1, "invalid size: %s", optarg); + parse_size(res, optarg, "disk"); break; default: ctl_usage(res->ctl); @@ -871,8 +863,7 @@ ctl_start(struct parse_result *res, int argc, char *argv[]) case 'm': if (res->size) errx(1, "memory specified multiple times"); - if (parse_size(res, optarg) != 0) - errx(1, "invalid memory size: %s", optarg); + parse_size(res, optarg, "memory"); break; case 'n': if (parse_network(res, optarg) != 0) diff --git a/usr.sbin/vmctl/vmctl.c b/usr.sbin/vmctl/vmctl.c index 9cda15bdc77..2b2d61b123f 100644 --- a/usr.sbin/vmctl/vmctl.c +++ b/usr.sbin/vmctl/vmctl.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vmctl.c,v 1.81 2022/05/04 02:24:26 dv Exp $ */ +/* $OpenBSD: vmctl.c,v 1.82 2022/05/04 23:17:25 dv Exp $ */ /* * Copyright (c) 2014 Mike Larkin @@ -59,7 +59,7 @@ struct imsgbuf *ibuf; * Parameters: * start_id: optional ID of the VM * name: optional name of the VM - * memsize: memory size (MB) of the VM to create + * memsize: memory size (in bytes) of the VM to create * nnics: number of vionet network interfaces to create * nics: switch names of the network interfaces to create * ndisks: number of disk images @@ -925,7 +925,7 @@ open_imagefile(int type, const char *imgfile_path, int flags, * type : format of the image file * imgfile_path: path to the image file to create * base_path : path to the qcow2 base image - * imgsize : size of the image file to create (in MB) + * imgsize : size of the image file to create (in bytes) * format : string identifying the format * * Return: diff --git a/usr.sbin/vmctl/vmctl.h b/usr.sbin/vmctl/vmctl.h index 6d84c7b1c6c..f7016dec085 100644 --- a/usr.sbin/vmctl/vmctl.h +++ b/usr.sbin/vmctl/vmctl.h @@ -1,4 +1,4 @@ -/* $OpenBSD: vmctl.h,v 1.35 2022/05/03 21:39:18 dv Exp $ */ +/* $OpenBSD: vmctl.h,v 1.36 2022/05/04 23:17:25 dv Exp $ */ /* * Copyright (c) 2015 Reyk Floeter @@ -77,7 +77,7 @@ extern struct imsgbuf *ibuf; int vmmaction(struct parse_result *); int parse_ifs(struct parse_result *, char *, int); int parse_network(struct parse_result *, char *); -int parse_size(struct parse_result *, char *); +void parse_size(struct parse_result *, char *, const char *); int parse_disktype(const char *, const char **); int parse_disk(struct parse_result *, char *, int); int parse_vmid(struct parse_result *, char *, int); diff --git a/usr.sbin/vmd/parse.y b/usr.sbin/vmd/parse.y index 90d74e28401..f8aebbe039f 100644 --- a/usr.sbin/vmd/parse.y +++ b/usr.sbin/vmd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.60 2022/05/03 21:39:18 dv Exp $ */ +/* $OpenBSD: parse.y,v 1.61 2022/05/04 23:17:25 dv Exp $ */ /* * Copyright (c) 2007-2016 Reyk Floeter @@ -1279,9 +1279,9 @@ parse_size(char *word, int64_t val) if (size != val) { if (fmt_scaled(size, result) == 0) - log_warnx("memory size rounded to %s", result); + log_debug("memory size rounded to %s", result); else - log_warnx("memory size rounded to %zd bytes", size); + log_debug("memory size rounded to %zd bytes", size); } return ((ssize_t)size); diff --git a/usr.sbin/vmd/vioqcow2.c b/usr.sbin/vmd/vioqcow2.c index 98dafdb0430..015acc203d2 100644 --- a/usr.sbin/vmd/vioqcow2.c +++ b/usr.sbin/vmd/vioqcow2.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vioqcow2.c,v 1.17 2022/01/04 15:21:40 claudio Exp $ */ +/* $OpenBSD: vioqcow2.c,v 1.18 2022/05/04 23:17:25 dv Exp $ */ /* * Copyright (c) 2018 Ori Bernstein @@ -628,17 +628,15 @@ inc_refs(struct qcdisk *disk, off_t off, int newcluster) */ int virtio_qcow2_create(const char *imgfile_path, - const char *base_path, long imgsize) + const char *base_path, uint64_t disksz) { struct qcheader hdr, basehdr; int fd, ret; ssize_t base_len; - uint64_t l1sz, refsz, disksz, initsz, clustersz; + uint64_t l1sz, refsz, 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)) diff --git a/usr.sbin/vmd/vioraw.c b/usr.sbin/vmd/vioraw.c index 27acd94062e..798399affb3 100644 --- a/usr.sbin/vmd/vioraw.c +++ b/usr.sbin/vmd/vioraw.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vioraw.c,v 1.6 2021/06/16 16:55:02 dv Exp $ */ +/* $OpenBSD: vioraw.c,v 1.7 2022/05/04 23:17:25 dv Exp $ */ /* * Copyright (c) 2018 Ori Bernstein * @@ -81,7 +81,7 @@ virtio_raw_init(struct virtio_backing *file, off_t *szp, int *fd, size_t nfd) * * Parameters: * imgfile_path: path to the image file to create - * imgsize : size of the image file to create (in MB) + * imgsize : size of the image file to create (in bytes) * * Return: * EEXIST: The requested image file already exists @@ -89,7 +89,7 @@ virtio_raw_init(struct virtio_backing *file, off_t *szp, int *fd, size_t nfd) * Exxxx : Various other Exxxx errno codes due to other I/O errors */ int -virtio_raw_create(const char *imgfile_path, long imgsize) +virtio_raw_create(const char *imgfile_path, uint64_t imgsize) { int fd, ret; @@ -100,7 +100,7 @@ virtio_raw_create(const char *imgfile_path, long imgsize) return (errno); /* Extend to desired size */ - if (ftruncate(fd, (off_t)imgsize * 1024 * 1024) == -1) { + if (ftruncate(fd, (off_t)imgsize) == -1) { ret = errno; close(fd); unlink(imgfile_path); diff --git a/usr.sbin/vmd/virtio.h b/usr.sbin/vmd/virtio.h index 6573933cf1b..128b469018a 100644 --- a/usr.sbin/vmd/virtio.h +++ b/usr.sbin/vmd/virtio.h @@ -1,4 +1,4 @@ -/* $OpenBSD: virtio.h,v 1.41 2021/07/16 16:21:22 dv Exp $ */ +/* $OpenBSD: virtio.h,v 1.42 2022/05/04 23:17:25 dv Exp $ */ /* * Copyright (c) 2015 Mike Larkin @@ -290,9 +290,9 @@ void viornd_update_qa(void); int viornd_notifyq(void); ssize_t virtio_qcow2_get_base(int, char *, size_t, const char *); -int virtio_qcow2_create(const char *, const char *, long); +int virtio_qcow2_create(const char *, const char *, uint64_t); int virtio_qcow2_init(struct virtio_backing *, off_t *, int*, size_t); -int virtio_raw_create(const char *, long); +int virtio_raw_create(const char *, uint64_t); int virtio_raw_init(struct virtio_backing *, off_t *, int*, size_t); int virtio_blk_io(int, uint16_t, uint32_t *, uint8_t *, void *, uint8_t);