From 7ba76b5748ff45157c0a98c784a0450b666a19df Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Fri, 24 Jan 2014 18:56:59 -0500 Subject: [PATCH] cli: Add --memory, deprecates -r/--ram This is a compound option like we use elsewhere, with suboptions for maxmemory and hugepages. --- man/virt-image.pod | 7 ++-- man/virt-install.pod | 29 +++++++------- tests/cli-test-xml/compare/image-boot0.xml | 4 +- .../compare/virtxml-edit-simple-memory.xml | 17 ++++++++ tests/clitest.py | 5 ++- virt-image | 11 +++--- virt-install | 14 +++---- virt-xml | 1 + virtinst/cli.py | 39 ++++++++++++++++++- 9 files changed, 91 insertions(+), 36 deletions(-) create mode 100644 tests/cli-test-xml/compare/virtxml-edit-simple-memory.xml diff --git a/man/virt-image.pod b/man/virt-image.pod index 53f46e9d..0e961994 100644 --- a/man/virt-image.pod +++ b/man/virt-image.pod @@ -52,10 +52,11 @@ General configuration parameters that apply to all types of guest installs. Name of the guest instance -=item -r MEMORY, --ram=MEMORY +=item --memory=MEMORY -Memory to allocate for guest instance in megabytes. Defaults to -C in the XML descriptor. +Memory to allocate for the guest, in megabytes. Defaults to C in the XML descriptor. This deprecates the -r/--ram option. + +See L for more details. =item -u UUID, --uuid=UUID diff --git a/man/virt-install.pod b/man/virt-install.pod index e569de97..37bb7479 100644 --- a/man/virt-install.pod +++ b/man/virt-install.pod @@ -35,7 +35,7 @@ argument, example: virt-install --disk=? =head1 OPTIONS -Most options are not required. Minimum requirements are --name, --ram, +Most options are not required. Minimum requirements are --name, --memory, guest storage (--disk, --filesystem or --nodisks), and an install option. =over 2 @@ -89,11 +89,12 @@ currently active. To re-define an existing guest, use the C tool to shut it down ('virsh shutdown') & delete ('virsh undefine') it prior to running C. -=item -r MEMORY, --ram=MEMORY +=item --memory=MEM[,OPT1=VAL][...] -Memory to allocate for guest instance in megabytes. If the hypervisor does -not have enough free memory, it is usual for it to automatically take memory -away from the host operating system to satisfy this allocation. +Memory to allocate for the guest, in megabytes. Sub options are available, +like 'maxmemory' and 'hugepages'. This deprecates the -r/--ram option. + +Use --memory=? to see a list of all available sub options. Complete details at L =item --arch=ARCH @@ -1433,7 +1434,7 @@ CDROM drive, auto launching a graphical VNC viewer --connect qemu:///system \ --virt-type kvm \ --name demo \ - --ram 500 \ + --memory 500 \ --disk path=/var/lib/libvirt/images/demo.img,size=8 \ --graphics vnc \ --cdrom /dev/cdrom \ @@ -1445,7 +1446,7 @@ booting from PXE, using VNC server/viewer # virt-install \ --connect qemu:///system \ --name demo \ - --ram 500 \ + --memory 500 \ --disk path=/dev/HostVG/DemoVM \ --network network=default \ --virt-type qemu @@ -1459,7 +1460,7 @@ pair: # virt-install \ --connect qemu:///system \ --name demo \ - --ram 500 \ + --memory 500 \ --disk path=/dev/hdc \ --network bridge=eth1 \ --arch ppc64 \ @@ -1471,7 +1472,7 @@ Run a Live CD image under Xen fullyvirt, in diskless environment # virt-install \ --hvm \ --name demo \ - --ram 500 \ + --memory 500 \ --nodisks \ --livecd \ --graphics vnc \ @@ -1483,7 +1484,7 @@ at 512 MB of ram and 2 host cpus: # virt-install \ --connect lxc:/// \ --name httpd_guest \ - --ram 512 \ + --memory 512 \ --vcpus 2 \ --init /usr/bin/httpd @@ -1496,7 +1497,7 @@ The host dir "/home/test" will be mounted at # virt-install \ --connect lxc:/// \ --name container \ - --ram 128 \ + --memory 128 \ --filesystem /home/LXC,/ \ --filesystem /home/test,/mnt \ --init /bin/sh @@ -1508,7 +1509,7 @@ options: # virt-install \ --paravirt \ --name demo \ - --ram 500 \ + --memory 500 \ --file /var/lib/xen/images/demo.img \ --file-size 6 \ --graphics none \ @@ -1519,7 +1520,7 @@ the rest of the options. # virt-install \ --name demo \ - --ram 512 \ + --memory 512 \ --disk /home/user/VMs/mydisk.img \ --import @@ -1527,7 +1528,7 @@ Start serial QEMU ARM VM, which requires specifying a manual kernel. # virt-install \ --name armtest \ - --ram 1024 \ + --memory 1024 \ --arch armv7l --machine vexpress-a9 \ --disk /home/user/VMs/myarmdisk.img \ --boot kernel=/tmp/my-arm-kernel,initrd=/tmp/my-arm-initrd,dtb=/tmp/my-arm-dtb,kernel_args="console=ttyAMA0 rw root=/dev/mmcblk0p3" \ diff --git a/tests/cli-test-xml/compare/image-boot0.xml b/tests/cli-test-xml/compare/image-boot0.xml index fc1c13f4..de201f90 100644 --- a/tests/cli-test-xml/compare/image-boot0.xml +++ b/tests/cli-test-xml/compare/image-boot0.xml @@ -1,8 +1,8 @@ foobar 00000000-1111-2222-3333-444444444444 - 65536 - 65536 + 262144 + 131072 7 /usr/bin/pygrub diff --git a/tests/cli-test-xml/compare/virtxml-edit-simple-memory.xml b/tests/cli-test-xml/compare/virtxml-edit-simple-memory.xml new file mode 100644 index 00000000..572d2718 --- /dev/null +++ b/tests/cli-test-xml/compare/virtxml-edit-simple-memory.xml @@ -0,0 +1,17 @@ +--- Original XML ++++ Altered XML +@@ -4,10 +4,9 @@ + Foo bar baz & + yeah boii < > yeahfoo + +- 409600 +- 204800 ++ 1024000 ++ 512000 + +- + + 9 + + +Domain 'test-many-devices' defined successfully. \ No newline at end of file diff --git a/tests/clitest.py b/tests/clitest.py index 02e5b625..b8cc4ba0 100644 --- a/tests/clitest.py +++ b/tests/clitest.py @@ -604,7 +604,7 @@ c.add_valid("--controller usb,model=ich9-ehci1,address=0:0:4.7,index=1") c.add_valid("--controller usb2 --controller usb3") -c = vinst.add_category("lxc", "--connect %(LXCURI)s --noautoconsole --name foolxc --ram 64") +c = vinst.add_category("lxc", "--connect %(LXCURI)s --noautoconsole --name foolxc --memory 64") c.add_compare("", "default") c.add_compare("--filesystem /source,/", "fs-default") c.add_compare("--init /usr/bin/httpd", "manual-init") @@ -759,6 +759,7 @@ c.add_invalid("--domain test-many-devices --edit 5 --tpm /dev/tpm") # device ed c.add_compare("--domain test --print-xml --edit --vcpus 7", "virtxml-print-xml") # test --print-xml c = vixml.add_category("simple edit diff", "--domain test-many-devices --edit --print-diff --define") +c.add_compare("--memory 500,maxmemory=1000,hugepages=off", "virtxml-edit-simple-memory") c.add_compare("--vcpus 10,maxvcpus=20,cores=5,sockets=4,threads=1", "virtxml-edit-simple-vcpus") c.add_compare("--cpu model=pentium2,+x2apic,forbid=pbe", "virtxml-edit-simple-cpu") c.add_compare("--numatune 1-5,7,mode=strict", "virtxml-edit-simple-numatune") @@ -803,7 +804,7 @@ c.add_valid("--vnc --keymap ja --vncport 5950 --vnclisten 1.2.3.4") # VNC w/ lo c = vimag.add_category("misc", "") c.add_valid("--network=?") # Make sure introspection doesn't blow up -c.add_compare("--name foobar --ram 64 --os-variant winxp --boot 0 %(IMAGE_XML)s", "image-boot0") +c.add_compare("--name foobar --memory 128,maxmemory=256 --os-variant winxp --boot 0 %(IMAGE_XML)s", "image-boot0") c.add_compare("--name foobar --ram 64 --network user,model=e1000 --boot 1 %(IMAGE_XML)s", "image-boot1") c.add_compare("--name foobar --ram 64 --boot 0 %(IMAGE_NOGFX_XML)s", "image-nogfx") c.add_valid("--name test --replace %(IMAGE_XML)s") # Colliding VM name w/ --replace diff --git a/virt-image b/virt-image index 1bdf3694..d32f926b 100755 --- a/virt-image +++ b/virt-image @@ -44,10 +44,8 @@ def parse_args(): geng = parser.add_argument_group(_("General Options")) geng.add_argument("-n", "--name", help=_("Name of the guest instance")) - geng.add_argument("-r", "--ram", type=int, dest="memory", - help=_("Memory to allocate for guest instance in " - "megabytes")) geng.add_argument("-u", "--uuid", help=argparse.SUPPRESS) + cli.add_memory_option(geng, backcompat=True) cli.vcpu_cli_options(geng) cli.add_distro_options(geng) cli.add_old_feature_options(geng) @@ -77,7 +75,7 @@ def main(conn=None): cli.convert_old_features(options) parsermap = cli.build_parser_map(options, - only=["vcpus", "cpu", "network", "graphics", "features"]) + only=["memory", "vcpus", "cpu", "network", "graphics", "features"]) if cli.check_option_introspection(options, parsermap): return 0 @@ -104,19 +102,20 @@ def main(conn=None): guest = conn.caps.build_virtinst_guest(conn, *installer.get_caps_guest()) guest.installer = installer + cli.convert_old_memory(options) cli.convert_old_networks(guest, options, image.domain.interface) cli.convert_old_graphics(guest, options, default_override=bool(image.domain.graphics)) cli.convert_old_cpuset(options) if not options.vcpus: options.vcpus = image.domain.vcpu or "" + if not options.memory and image.domain.memory: + options.memory = image.domain.memory guest.replace = options.replace cli.set_os_variant(guest, options.distro_type, options.distro_variant) cli.get_name(guest, options.name or image.name) - cli.get_memory(guest, options.memory or (image.domain.memory and - int(image.domain.memory))) if options.uuid: guest.uuid = options.uuid diff --git a/virt-install b/virt-install index 3c17bcc6..77527844 100755 --- a/virt-install +++ b/virt-install @@ -385,7 +385,7 @@ def validate_required_options(options, guest): msg += "\n" + cli.name_missing if not options.memory: - msg += "\n" + cli.ram_missing + msg += "\n" + cli.memory_missing if (not guest.os.is_container() and not storage_specified(options.disk, @@ -499,6 +499,7 @@ def build_guest_instance(conn, options, parsermap): guest.installer = build_installer(options, conn, guest.os.os_type) + cli.convert_old_memory(options) convert_old_sound(options) cli.convert_old_networks(guest, options, not options.nonetworks and 1 or 0) cli.convert_old_graphics(guest, options) @@ -514,8 +515,6 @@ def build_guest_instance(conn, options, parsermap): guest.autostart = options.autostart cli.get_name(guest, options.name) - # Memory needs to come before the vcpu setting - cli.get_memory(guest, options.memory) # Guest configuration if options.uuid: @@ -533,6 +532,9 @@ def build_guest_instance(conn, options, parsermap): guest.add_default_usb_controller() guest.add_default_channels() + if cli.is_prompt(): + cli.get_memory(guest, guest.memory and (guest.memory / 1024) or None) + # Do this after setting up all optional parameters, so we report error # about those first. need_storage, need_install = validate_required_options(options, guest) @@ -880,9 +882,8 @@ def parse_args(): geng = parser.add_argument_group(_("General Options")) geng.add_argument("-n", "--name", help=_("Name of the guest instance")) - geng.add_argument("-r", "--ram", type=int, dest="memory", - help=_("Memory to allocate for guest instance in " - "megabytes")) + geng.add_argument("-u", "--uuid", help=argparse.SUPPRESS) + cli.add_memory_option(geng, backcompat=True) cli.vcpu_cli_options(geng) geng.add_argument("--description", help=_("Human readable description of the VM to store in " @@ -960,7 +961,6 @@ def parse_args(): help=_("The CPU architecture to simulate")) virg.add_argument("--machine", help=_("The machine type to emulate")) - virg.add_argument("-u", "--uuid", help=argparse.SUPPRESS) cli.add_old_feature_options(virg) misc = parser.add_argument_group(_("Miscellaneous Options")) diff --git a/virt-xml b/virt-xml index 182f160e..a476a7f0 100755 --- a/virt-xml +++ b/virt-xml @@ -197,6 +197,7 @@ def parse_args(): cli.add_disk_option(g) cli.add_net_option(g) cli.add_gfx_option(g) + cli.add_memory_option(g) cli.vcpu_cli_options(g) cli.add_guest_xml_options(g) cli.add_boot_option(g) diff --git a/virtinst/cli.py b/virtinst/cli.py index 2e92385c..343c4a68 100644 --- a/virtinst/cli.py +++ b/virtinst/cli.py @@ -544,7 +544,7 @@ def disk_prompt(conn, origpath, origsize, origsparse, ####################### name_missing = _("--name is required") -ram_missing = _("--ram amount in MB is required") +memory_missing = _("--memory amount in MB is required") def get_name(guest, name): @@ -555,7 +555,7 @@ def get_name(guest, name): def get_memory(guest, memory): prompt_txt = _("How much RAM should be allocated (in megabytes)?") - err_txt = ram_missing + err_txt = memory_missing def check_memory(mem): mem = int(mem) @@ -568,6 +568,14 @@ def get_memory(guest, memory): func=check_memory) +def convert_old_memory(options): + if options.memory: + return + if not options.oldmemory: + return + options.memory = str(options.oldmemory) + + def convert_old_cpuset(options): if not options.cpuset: return @@ -760,6 +768,16 @@ def add_misc_options(grp, prompt=False, replace=False, help=_("Print debugging information")) +def add_memory_option(grp, backcompat=False): + grp.add_argument("--memory", + help=_("Configure guest memory allocation. Ex:\n" + "--memory 1024 (in megabytes)\n" + "--memory 512,maxmemory=1024,hugepages=on")) + if backcompat: + grp.add_argument("-r", "--ram", type=int, dest="oldmemory", + help=argparse.SUPPRESS) + + def vcpu_cli_options(grp, backcompat=True): grp.add_argument("--vcpus", help=_("Number of vcpus to configure for your guest. Ex:\n" @@ -1287,6 +1305,22 @@ class ParserNumatune(VirtCLIParser): self.set_param("numatune.memory_mode", "mode") +#################### +# --memory parsing # +#################### + +class ParserMemory(VirtCLIParser): + def _init_params(self): + self.remove_first = "memory" + + def set_memory_cb(opts, inst, cliname, val): + ignore = opts + setattr(inst, cliname, int(val) * 1024) + self.set_param("memory", "memory", setter_cb=set_memory_cb) + self.set_param("maxmemory", "maxmemory", setter_cb=set_memory_cb) + self.set_param("hugepage", "hugepages", is_onoff=True) + + ################### # --vcpus parsing # ################### @@ -2085,6 +2119,7 @@ def build_parser_map(options, skip=None, only=None): parserobj.cli_arg_name, parserclass)) parsermap[parserobj.option_variable_name] = parserobj + register_parser("memory", ParserMemory) register_parser("vcpus", ParserVCPU) register_parser("cpu", ParserCPU) register_parser("numatune", ParserNumatune)