4

How can I create a virtual machine from the CLI?

Creating a Virtual Machine

First, download an ISO cd image of some OS you want to run. For Ubuntu, you can find these at:

    http://www.ubuntu.com/getubuntu/download 

Double click on the name of the host. The Status column should read Active

Right click on the name of the host, and select New
This will start a wizard to guide you through the rest of your VM creation
    Enter your virtual machine details

        Name: foo

        Choose Local install media (ISO image or CDROM), or you can use another method if you know what you're doing

        Forward 
    Locate your install media

        Use ISO image

        Browse to find the ISO you downloaded earlier

        Optional: Select the matching OS Type

        Optional: Select the matching Version

        Forward

Primarily just for my own edification.

2
  • 1
    Just use virsh? What problem do you have with it? Nov 14, 2017 at 9:30
  • @EgorVasilyev well, creating a config file from scratch on the CLI based on an ISO...have no idea how to do that.
    – Thufir
    Nov 14, 2017 at 10:04

3 Answers 3

5

Just use:

virt-install \
--name vm_name \
--ram=2048 \
--vcpus=2 \
--disk pool=guest_images,size=30,bus=virtio,format=qcow2 \
--cdrom /var/iso/debian.iso \
--network bridge=kvmbr0,model=virtio \
--graphics vnc,listen=0.0.0.0,password=Qwerty1234 \
--boot cdrom,hd,menu=on

Where /var/iso/debian.iso - path to iso image

guest_images - disk pool, you need to create it before vm

3
  • and how do you run the "install" for the above debian?
    – Thufir
    Nov 14, 2017 at 10:17
  • 1
    @Thufir, what do you mean? Installation will start automatically because i use boot order with cdrom first. And after this i can connect via VNC to manage installation process Nov 14, 2017 at 10:20
  • Too bad I can't figure out how to get virt-install onto my Ubuntu machine! Dec 10, 2019 at 21:18
1

I often use qemu-kvm the old way with this kind of qemu command :

qemu-img create mydisk.img 10G

qemu-system-x86_64 -boot d -cdrom image.iso -m 512 -hda mydisk.img

virsh will launch qemu-kvm. But it need some config file. Sometime it's easier to bypass all this xml lines and just launch the vm.

0
thufir@dur:~$ 
thufir@dur:~$ 
thufir@dur:~$ virsh connect qemu:///system

thufir@dur:~$ 
thufir@dur:~$ virsh list
 Id    Name                           State
----------------------------------------------------
 1     asteriskNOW                    running

thufir@dur:~$ 
thufir@dur:~$ virsh dumpxml AsteriskNow > guest.xml
error: failed to get domain 'AsteriskNow'
error: Domain not found: no domain with matching name 'AsteriskNow'
thufir@dur:~$ 
thufir@dur:~$ virsh dumpxml 1 > guest.xml
thufir@dur:~$ 
thufir@dur:~$ cat guest.xml 
<domain type='kvm' id='1'>
  <name>asteriskNOW</name>
  <uuid>e57fb835-cdfa-4234-b0c2-72b17178646a</uuid>
  <memory unit='KiB'>997376</memory>
  <currentMemory unit='KiB'>997376</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <resource>
    <partition>/machine</partition>
  </resource>
  <os>
    <type arch='x86_64' machine='pc-i440fx-artful'>hvm</type>
    <boot dev='cdrom'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <vmport state='off'/>
  </features>
  <cpu mode='custom' match='exact' check='full'>
    <model fallback='forbid'>Nehalem</model>
    <feature policy='require' name='vme'/>
    <feature policy='require' name='x2apic'/>
    <feature policy='require' name='hypervisor'/>
  </cpu>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/bin/kvm-spice</emulator>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/home/thufir/Downloads/AsteriskNow-1013-current-64.iso'/>
      <backingStore/>
      <target dev='hda' bus='ide'/>
      <readonly/>
      <alias name='ide0-0-0'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <alias name='usb'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <alias name='usb'/>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <alias name='usb'/>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <alias name='usb'/>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'>
      <alias name='pci.0'/>
    </controller>
    <controller type='ide' index='0'>
      <alias name='ide'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <controller type='virtio-serial' index='0'>
      <alias name='virtio-serial0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </controller>
    <interface type='network'>
      <mac address='52:54:00:99:ac:da'/>
      <source network='default' bridge='virbr0'/>
      <target dev='vnet0'/>
      <model type='rtl8139'/>
      <alias name='net0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <source path='/dev/pts/2'/>
      <target port='0'/>
      <alias name='serial0'/>
    </serial>
    <console type='pty' tty='/dev/pts/2'>
      <source path='/dev/pts/2'/>
      <target type='serial' port='0'/>
      <alias name='serial0'/>
    </console>
    <channel type='spicevmc'>
      <target type='virtio' name='com.redhat.spice.0' state='disconnected'/>
      <alias name='channel0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    </channel>
    <input type='mouse' bus='ps2'>
      <alias name='input0'/>
    </input>
    <input type='keyboard' bus='ps2'>
      <alias name='input1'/>
    </input>
    <graphics type='spice' port='5900' autoport='yes' listen='127.0.0.1'>
      <listen type='address' address='127.0.0.1'/>
      <image compression='off'/>
    </graphics>
    <sound model='ich6'>
      <alias name='sound0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </sound>
    <video>
      <model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>
      <alias name='video0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <redirdev bus='usb' type='spicevmc'>
      <alias name='redir0'/>
      <address type='usb' bus='0' port='1'/>
    </redirdev>
    <redirdev bus='usb' type='spicevmc'>
      <alias name='redir1'/>
      <address type='usb' bus='0' port='2'/>
    </redirdev>
    <memballoon model='virtio'>
      <alias name='balloon0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
    </memballoon>
  </devices>
  <seclabel type='dynamic' model='apparmor' relabel='yes'>
    <label>libvirt-e57fb835-cdfa-4234-b0c2-72b17178646a</label>
    <imagelabel>libvirt-e57fb835-cdfa-4234-b0c2-72b17178646a</imagelabel>
  </seclabel>
  <seclabel type='dynamic' model='dac' relabel='yes'>
    <label>+64055:+130</label>
    <imagelabel>+64055:+130</imagelabel>
  </seclabel>
</domain>

thufir@dur:~$ 
thufir@dur:~$ 

and then

thufir@dur:~$ 
thufir@dur:~$ virsh list
 Id    Name                           State
----------------------------------------------------

thufir@dur:~$ 
thufir@dur:~$ virsh create guest.xml 
Domain asteriskNOW created from guest.xml

thufir@dur:~$ 
thufir@dur:~$ virsh list
 Id    Name                           State
----------------------------------------------------
 2     asteriskNOW                    running

thufir@dur:~$ 

although I cheated by using virt-manager to create the initial guest...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .