vl: convert -m to QemuOpts

Adds option to -m
 "size" - startup memory amount

For compatibility with legacy CLI if suffix-less number is passed,
it assumes amount in Mb.

Otherwise user is free to use suffixed number using suffixes b,k/K,M,G

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Andreas Färber <afaerber@suse.de>
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
This commit is contained in:
Igor Mammedov 2013-11-27 01:27:35 +01:00 committed by Michael Tokarev
parent e96e5ae880
commit 6e1d3c1c85
2 changed files with 63 additions and 17 deletions

View File

@ -210,10 +210,13 @@ use is discouraged as it may be removed from future versions.
ETEXI ETEXI
DEF("m", HAS_ARG, QEMU_OPTION_m, DEF("m", HAS_ARG, QEMU_OPTION_m,
"-m megs set virtual RAM size to megs MB [default=" "-m [size=]megs\n"
stringify(DEFAULT_RAM_SIZE) "]\n", QEMU_ARCH_ALL) " configure guest RAM\n"
" size: initial amount of guest memory (default: "
stringify(DEFAULT_RAM_SIZE) "MiB)\n",
QEMU_ARCH_ALL)
STEXI STEXI
@item -m @var{megs} @item -m [size=]@var{megs}
@findex -m @findex -m
Set virtual RAM size to @var{megs} megabytes. Default is 128 MiB. Optionally, Set virtual RAM size to @var{megs} megabytes. Default is 128 MiB. Optionally,
a suffix of ``M'' or ``G'' can be used to signify a value in megabytes or a suffix of ``M'' or ``G'' can be used to signify a value in megabytes or

71
vl.c
View File

@ -510,6 +510,20 @@ static QemuOptsList qemu_name_opts = {
}, },
}; };
static QemuOptsList qemu_mem_opts = {
.name = "memory",
.implied_opt_name = "size",
.head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
.merge_lists = true,
.desc = {
{
.name = "size",
.type = QEMU_OPT_SIZE,
},
{ /* end of list */ }
},
};
/** /**
* Get machine options * Get machine options
* *
@ -2955,6 +2969,8 @@ int main(int argc, char **argv, char **envp)
}; };
const char *trace_events = NULL; const char *trace_events = NULL;
const char *trace_file = NULL; const char *trace_file = NULL;
const ram_addr_t default_ram_size = (ram_addr_t)DEFAULT_RAM_SIZE *
1024 * 1024;
atexit(qemu_run_exit_notifiers); atexit(qemu_run_exit_notifiers);
error_set_progname(argv[0]); error_set_progname(argv[0]);
@ -2978,6 +2994,7 @@ int main(int argc, char **argv, char **envp)
qemu_add_opts(&qemu_trace_opts); qemu_add_opts(&qemu_trace_opts);
qemu_add_opts(&qemu_option_rom_opts); qemu_add_opts(&qemu_option_rom_opts);
qemu_add_opts(&qemu_machine_opts); qemu_add_opts(&qemu_machine_opts);
qemu_add_opts(&qemu_mem_opts);
qemu_add_opts(&qemu_smp_opts); qemu_add_opts(&qemu_smp_opts);
qemu_add_opts(&qemu_boot_opts); qemu_add_opts(&qemu_boot_opts);
qemu_add_opts(&qemu_sandbox_opts); qemu_add_opts(&qemu_sandbox_opts);
@ -3002,7 +3019,7 @@ int main(int argc, char **argv, char **envp)
module_call_init(MODULE_INIT_MACHINE); module_call_init(MODULE_INIT_MACHINE);
machine_class = find_default_machine(); machine_class = find_default_machine();
cpu_model = NULL; cpu_model = NULL;
ram_size = 0; ram_size = default_ram_size;
snapshot = 0; snapshot = 0;
cyls = heads = secs = 0; cyls = heads = secs = 0;
translation = BIOS_ATA_TRANSLATION_AUTO; translation = BIOS_ATA_TRANSLATION_AUTO;
@ -3289,20 +3306,48 @@ int main(int argc, char **argv, char **envp)
exit(0); exit(0);
break; break;
case QEMU_OPTION_m: { case QEMU_OPTION_m: {
int64_t value;
uint64_t sz; uint64_t sz;
char *end; const char *mem_str;
value = strtosz(optarg, &end); opts = qemu_opts_parse(qemu_find_opts("memory"),
if (value < 0 || *end) { optarg, 1);
fprintf(stderr, "qemu: invalid ram size: %s\n", optarg); if (!opts) {
exit(1); exit(EXIT_FAILURE);
} }
sz = QEMU_ALIGN_UP((uint64_t)value, 8192);
mem_str = qemu_opt_get(opts, "size");
if (!mem_str) {
error_report("invalid -m option, missing 'size' option");
exit(EXIT_FAILURE);
}
if (!*mem_str) {
error_report("missing 'size' option value");
exit(EXIT_FAILURE);
}
sz = qemu_opt_get_size(opts, "size", ram_size);
/* Fix up legacy suffix-less format */
if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
uint64_t overflow_check = sz;
sz <<= 20;
if ((sz >> 20) != overflow_check) {
error_report("too large 'size' option value");
exit(EXIT_FAILURE);
}
}
/* backward compatibility behaviour for case "-m 0" */
if (sz == 0) {
sz = default_ram_size;
}
sz = QEMU_ALIGN_UP(sz, 8192);
ram_size = sz; ram_size = sz;
if (ram_size != sz) { if (ram_size != sz) {
fprintf(stderr, "qemu: ram size too large\n"); error_report("ram size too large");
exit(1); exit(EXIT_FAILURE);
} }
break; break;
} }
@ -4145,10 +4190,8 @@ int main(int argc, char **argv, char **envp)
exit(1); exit(1);
} }
/* init the memory */ /* store value for the future use */
if (ram_size == 0) { qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size);
ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
}
if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
!= 0) { != 0) {