AKAI TSUKI

System development or Technical something

KVM with cloud-init

CentOS 8 VM Image

I get CentOS 8 VM Image.

[root@pm01 images]# wget https://cloud.centos.org/centos/8/x86_64/images/CentOS-8-GenericCloud-8.2.2004-20200611.2.x86_64.qcow2
[root@pm01 ~]# qemu-img info images/CentOS-8-GenericCloud-8.2.2004-20200611.2.x86_64.qcow2
image: images/CentOS-8-GenericCloud-8.2.2004-20200611.2.x86_64.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.1G
cluster_size: 65536
Format specific information:
    compat: 0.10
[root@pm01 ~]#
[root@pm01 ~]# cp -vp images/CentOS-8-GenericCloud-8.2.2004-20200611.2.x86_64.qcow2 /var/lib/libvirt/images/vm05.qcow2
‘images/CentOS-8-GenericCloud-8.2.2004-20200611.2.x86_64.qcow2’ -> ‘/var/lib/libvirt/images/vm05.qcow2’
[root@pm01 ~]#

I resize VM Image from 10G to 20G.

[root@pm01 ~]# cd /var/lib/libvirt/images/
[root@pm01 images]# qemu-img resize vm05.qcow2 20G
Image resized.
[root@pm01 images]#

prepare for cloud-init

I get hashed pass by python command like below.

[root@pm01 images]# python -c 'import crypt,getpass; print(crypt.crypt(getpass.getpass(), crypt.mksalt(crypt.METHOD_SHA512)))'
Password: <input password>
<hashed pass>
[root@pm01 images]#

I create cloud-confg file.
This file makes hostname "testnode" and creates "centos" user in a VM.

[root@pm01 images]# vi config.yaml
[root@pm01 images]# cat config.yaml
#cloud-config
fqdn: testnode
users:
  - name: centos
    groups: wheel
    lock_passwd: false
    passwd: '<hashed pass>'
    shell: /bin/bash
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
[root@pm01 images]#
[root@pm01 images]# yum install -y cloud-utils

 *snip*

Installed:
  cloud-utils.x86_64 0:0.27-20.el7.centos

Dependency Installed:
  cloud-utils-growpart.noarch 0:0.29-5.el7 euca2ools.noarch 0:3.4.1-1.el7 python-lxml.x86_64 0:3.2.1-4.el7 python-progressbar.noarch 0:2.3-4.el7 python-requestbuilder.noarch 0:0.7.1-1.el7
  rsync.x86_64 0:3.1.2-10.el7

Complete!
[root@pm01 images]#

I generate config.iso file by cloud-localds command.

[root@pm01 images]# cloud-localds config.iso config.yaml
wrote config.iso with filesystem=iso9660 and diskformat=raw
[root@pm01 images]#

Unfortunantly, I could not find "CentOS 8".

[root@pm01 images]# osinfo-query  os | grep -i centos
 centos5.0            | CentOS 5.0                                         | 5.0      | http://centos.org/centos/5.0
 centos5.1            | CentOS 5.1                                         | 5.1      | http://centos.org/centos/5.1
 centos5.10           | CentOS 5.10                                        | 5.10     | http://centos.org/centos/5.10
 centos5.11           | CentOS 5.11                                        | 5.11     | http://centos.org/centos/5.11
 centos5.2            | CentOS 5.2                                         | 5.2      | http://centos.org/centos/5.2
 centos5.3            | CentOS 5.3                                         | 5.3      | http://centos.org/centos/5.3
 centos5.4            | CentOS 5.4                                         | 5.4      | http://centos.org/centos/5.4
 centos5.5            | CentOS 5.5                                         | 5.5      | http://centos.org/centos/5.5
 centos5.6            | CentOS 5.6                                         | 5.6      | http://centos.org/centos/5.6
 centos5.7            | CentOS 5.7                                         | 5.7      | http://centos.org/centos/5.7
 centos5.8            | CentOS 5.8                                         | 5.8      | http://centos.org/centos/5.8
 centos5.9            | CentOS 5.9                                         | 5.9      | http://centos.org/centos/5.9
 centos6.0            | CentOS 6.0                                         | 6.0      | http://centos.org/centos/6.0
 centos6.1            | CentOS 6.1                                         | 6.1      | http://centos.org/centos/6.1
 centos6.10           | CentOS 6.10                                        | 6.10     | http://centos.org/centos/6.10
 centos6.2            | CentOS 6.2                                         | 6.2      | http://centos.org/centos/6.2
 centos6.3            | CentOS 6.3                                         | 6.3      | http://centos.org/centos/6.3
 centos6.4            | CentOS 6.4                                         | 6.4      | http://centos.org/centos/6.4
 centos6.5            | CentOS 6.5                                         | 6.5      | http://centos.org/centos/6.5
 centos6.6            | CentOS 6.6                                         | 6.6      | http://centos.org/centos/6.6
 centos6.7            | CentOS 6.7                                         | 6.7      | http://centos.org/centos/6.7
 centos6.8            | CentOS 6.8                                         | 6.8      | http://centos.org/centos/6.8
 centos6.9            | CentOS 6.9                                         | 6.9      | http://centos.org/centos/6.9
 centos7.0            | CentOS 7                                           | 7        | http://centos.org/centos/7.0
[root@pm01 images]#

Excute command(virt-install) to create CentOS 8 with cloud-init on KVM.

I use above all information.
and then I prepare script file to create VM.

[root@pm01 kvm_work]# vi create_vm05.sh
[root@pm01 kvm_work]#
[root@pm01 kvm_work]# cat create_vm05.sh
#!/bin/bash

name=vm05
cpu=1
memory=4096
# disk=30

# cd /var/lib/libvirt/images/
# cp -vp CentOS-8-GenericCloud-8.2.2004-20200611.2.x86_64.qcow2 ${name}.qcow2
# qemu-img resize ${name}.qcow2 ${disk}G

virt-install \
 --name=${name} \
 --disk /var/lib/libvirt/images/${name}.qcow2,device=disk \
 --disk /var/lib/libvirt/images/config.iso,device=cdrom \
 --network network=default,model=virtio \
 --vcpus=${cpu} \
 --ram=${memory} \
 --accelerate \
 --hvm \
 --virt-type kvm \
 --graphics none \
 --os-type Linux \
 --arch=x86_64 \
 --import

[root@pm01 kvm_work]#

Finally, CentOS 8 VM was created by cloud-init with cloud config on KVM.
and I can log in to this VM.

[root@pm01 kvm_work]# bash create_vm05.sh
 *snip*

CentOS Linux 8 (Core)
Kernel 4.18.0-193.6.3.el8_2.x86_64 on an x86_64

Activate the web console with: systemctl enable --now cockpit.socket

testnode login: centos
Password:
[centos@testnode ~]$ id
uid=1000(centos) gid=1000(centos) groups=1000(centos),10(wheel) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[centos@testnode ~]$