giovtorres / kvm-install-vm

Bash script to build local virtual machines using KVM/libvirt and cloud-init.
MIT License
333 stars 161 forks source link

Network config alternative #30

Open mariusrugan opened 5 years ago

mariusrugan commented 5 years ago

Hi,

First of all, thanks for your work, really nice assembled, (however i'm trying master on a debian host, as a non root user, with all permissions given to libvirt, some vars fail to get assigned properly; i'll open a separate issue for that). So in the light of the above, i had to cut down to a working solution and also solve static networking.

Wrt networking:

i'm using centos-atomic which at this moment has cloud-init version 18.2 https://cloudinit.readthedocs.io/en/18.2/

I'm adding to the ISO cloud-init image the following yaml file named network-config which gets picked up. Be aware that in cloud-init NoCloud this should NOT be under YAML node networking: just the: version:1 config: .... works perfectly fine. Again this is Cloud-Init dependent and works fine in my ATM latest centos atomic.

export NETWORK_CONFIG=/opt/kvm/ci/${VMNAME}/network-config
export CI_ISO=/opt/kvm/images/${VMNAME}-cidata.iso

sudo rm -rf ${NETWORK_CONFIG}
sync
sudo cat > ${NETWORK_CONFIG} << _EOF_
version: 1
config:
    - type: physical
      name: eth0
      mac_address: "${MAC}"
      subnets:
         - type: static
           address: 192.168.0.101
           netmask: 255.255.255.0
           gateway: 192.168.0.1
           network: 192.168.0.0
    - type: nameserver
      address: 
         - 192.168.0.254
      search:
         - mylocaldnsname.dev
_EOF_

sudo genisoimage -output ${CI_ISO} -volid cidata -joliet -rock ${USER_DATA} ${META_DATA} ${NETWORK_CONFIG}
eayin2 commented 5 years ago

You generated an image with your custom cloud-init configuration. Can you use the default image and instead run kvm-install-vm with -s (Custom shell script) to set the static IP? Creating a new ISO for each static IP seems time consuming.

mariusrugan commented 5 years ago

Not really that time consuming @eayin2.

resizing my qcow2 images from 8 (default) to 32GB takes more time than generating this iso via genisoimage.

cloud-init uses user-data, network-config & metadata files which are needed in a readable format at boot time, in the vm. that's why the ISO. cloud-init is a widely adopted standard for configuring the virtual machines (from aws to my home-lab) and this is how it works.

from kvm perspective you're attaching a "virtual" drive from which it boots and there is no such abstraction of running a script when you start the vm.

alternatively you can edit the qcow2 images as explained here https://medium.com/@farazfazli/libguestfs-cloud-init-kvm-provisioning-made-easy-17931d9ea8cf

[ Edit ] i understand your remark now better because i've looked at the -s option.

https://github.com/giovtorres/kvm-install-vm/blob/master/kvm-install-vm#L454

and if you're referring to the runcmd part of cloud-init. let's say that is ran amongst the last things cloud-init runs. The vm's networking setup, you need it it way more earlier than runcmd.

but still, this script is still packaged in the iso, look into the deep-link above.

eayin2 commented 5 years ago

Yep I just saw it :-)