virt-install with cloud-init

If you have not heard about cloud-init then you should definately search for it and learn about cloud-config as well. You can find the documentation for cloud-config here. It is used for configuring VMs when running in cloud environments. Specially useful to inject the ssh keys or set the root password but you can do anything in the cloud-config as there is a section where you can run bash commands. While this is useful for cloud instances, you can make use of this feature when running your vm with qemu or libvirtd. All you need to do is either attach a iso disc with user-data and meta-data files in it or even simpler, you can use virt-install command. The command that I use is as follows

virt-install --import --name fedora --memory 2048 --vcpus 1 \
--disk  ./a.qcow2,format=qcow2,bus=virtio  \
--cloud-init=root-password-generate=on,disable=on,meta-data=./meta-data,user-data=./user-data \
--network bridge=virbr0,model=virtio --os-type=linux --graphics spice \
--autoconsole none --virt-type kvm  --os-variant fedora35

The various parameters are

  • import - import existing disk
  • name - name for the vm
  • memory, vcpus - memory and vcpus to use for the vm
  • disk - a.qcow2 in the above command is already existing disk and it can be cloud image of any Linux OS.
  • cloud-init - This defines the cloud init that you want to use. Here we are using:
    • root-password-generate - generate a random password and display that
    • disable - Disable cloud-init in the VM for subsequent boots
    • meta-data - file with meta-data
    • user-date - file with cloud-config or user-data
  • network - define network to use
  • os-type - Type of OS.
  • graphics - spice is used for graphics display
  • autoconsole - disable lauching console automatically.
  • virt-type - Virtualization type to use.
  • os-variant - Linux distribution you are installing.

Related Articles:


Authored By Amit Agarwal

Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it