---
# System prepended metadata

title: '#libvirt'

---

# libvirt


## disk create
- convert img to qcow2
`qemu-img convert -O qcow2 <example>.img <example>.qcow2
`
- QCOW is a copy-on-write, differential image structure, so create an image relative to the base distribution image
`sudo qemu-img create -f qcow2 -b <img>.qcow2 -F qcow2 <img>.qcow2`
    - userimg have to create by this command 

## vm xml for cloud-init
- adding cloud-init support to libvirt xml
```
<sysinfo type='smbios'>
  <system>
    <entry name='serial'>ds=nocloud</entry>
  </system>
</sysinfo>
```

- adding console and serial 
```
<serial type='pty'>
  <source path='/dev/pts/9'/>
  <target type='isa-serial' port='0'>
    <model name='isa-serial'/>
  </target>
  <alias name='serial0'/>
</serial>
<console type='pty' tty='/dev/pts/9'>
  <source path='/dev/pts/9'/>
  <target type='serial' port='0'/>
  <alias name='serial0'/>
</console>
```

- static port for vnc
```
<graphics type='vnc' port='5960' autoport='no' listen='0.0.0.0'>
  <listen type='address' address='0.0.0.0'/>
</graphics>
```


## cloud-init file (for nocloud)

- user-data
```
#cloud-config

users:
  - name: rogerdeng
    plain_text_passwd: "password"
    sudo: ["ALL=(ALL) NOPASSWD:ALL"]
    groups: sudo
    shell: /bin/bash
    lock_passwd: false
```
:::danger
    plain_text_passwd should modify to passwd
:::

- meta-data
```
instance-id: Cloud10000
local-hostname: Cloud10000
```
- network-config:
```
version: 2
ethernets:
  enp0s3:
    dhcp4: no
    addresses:
      - x.x.x.x/24
    gateway4: x.x.x.x
    nameservers:
      addresses:
        - 8.8.8.8
        - 8.8.4.4
```

- make cloud-init.iso 
`sudo genisoimage -output /var/lib/libvirt/iso/volid.iso -V cidata -r -J user-data meta-data network-config`
    - and insert into vm 

## regular
- set cloud-img name same as <libvirt-domain> and cloud-init.iso -> <libvirt-domain>-cloudInit.iso


## other

:::danger
    kvm img does not contain drivers for video card, vnc will not work
:::