ใช้งาน CentOS qcow2 image ใน KVM
2 min readJul 15, 2018
หา download ได้จากไหน
https://cloud.centos.org/centos/ ตาม link นี้เลยครับ มีให้เลือกทั้ง CentOS 6 , CentOS 7 และมีให้เลือกทั้ง รูปแบบ qcow2 และ vagrant
ขั้นตอนเตรียมนำไปใช้งาน
- เตรียมเครื่องมือในการ customize บนเครื่องเราให้พร้อมก่อน
# yum install -y qemu-img libguestfs-tools
2. เปลี่ยน root password
# virt-customize --root-password password:mynewpassword \
-a myimage.qcow2
3. เปลี่ยนขนาด root partition
# virt-filesystems --long -h --all -a myimage.qcow2
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem xfs - - 8.0G -
/dev/sda1 partition - - 83 8.0G /dev/sda
/dev/sda device - - - 8.0G -# qemu-img resize myimage.qcow2 40G
# cp myimage.qcow2 myimage.qcow2.original
# virt-resize --expand /dev/sda1 myimage.qcow2.original \ myimage.qcow2
# virt-customize --run-command 'xfs_growfs /' -a myimage.qcow2# virt-filesystems --long -h --all -a myimage.qcow2
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem xfs - - 40G -
/dev/sda1 partition - - 83 40G /dev/sda
/dev/sda device - - - 40G -# qemu-img info myimage.qcow2
image: master.local.img
file format: qcow2
virtual size: 40G (42949672960 bytes)
disk size: 11G
cluster_size: 65536
Format specific information:
compat: 0.10
refcount bits: 16อ
4. กำหนดค่า network configuration
# export IP=192.168.6.54
# export DNS=192.168.6.1
# export GW=192.168.6.1
# virt-customize --run-command "echo -e 'DEVICE=eth0\nBOOTPROTO=none\nONBOOT=yes\nNETMASK=255.255.255.0\nIPADDR=${IP}\nUSERCTL=no\nDNS1=${DNS}\nGATEWAY=${GW}\n' > /etc/sysconfig/network-scripts/ifcfg-eth0" -a myimage.qcow2
[ 0.0] Examining the guest ...
[ 17.5] Setting a random seed
[ 17.6] Running: echo -e 'DEVICE=eth0\nBOOTPROTO=none\nONBOOT=yes\nNETMASK=255.255.255.0\nIPADDR=192.168.6.54\nUSERCTL=no\nDNS1=192.168.6.1\nGATEWAY=192.168.6.1\n' > /etc/sysconfig/network-scripts/ifcfg-eth0
[ 17.9] Finishing off
5. กำหนดค่า hostname
# export HOSTNAME=server1.example.com
# virt-customize --hostname ${HOSTNAME} -a myimage.qcow2
[ 0.0] Examining the guest ...
[ 20.3] Setting a random seed
[ 20.4] Setting the hostname: server1.example.com
[ 20.5] Finishing off
6. เขียน context ของ SELinux ใหม่
# virt-customize --selinux-relabel -a /tmp/myimage.qcow2
[ 0.0] Examining the guest ...
[ 19.2] Setting a random seed
[ 19.3] SELinux relabelling
[ 41.6] Finishing off
7. ตัวอย่างการทำ image ที่ปรับแต่งแล้วไปใช้งาน
# virt-install -n testx --memory 1024 --vcpu 1 --import --disk myimage.qcow2 --noautoconsole --os-variant=rhel7Starting install...
Domain creation completed.
เพิ่มเติม
- update image :: ในกรณีที่ต้องการ update package ใน image ให้เห็น latest version
# virt-customize --update -a myimage.qcow2
[ 0.0] Examining the guest ...
[ 10.6] Setting a random seed
[ 10.7] Updating packages
[ 19.1] Finishing off
2. ติดตั้ง software เพิ่มเติมจากต้นฉบับที่ download มา
จากตัวอย่างป็นการติดตั้ง vsftp server เพิ่มเติม
# virt-customize --install vsftpd -a myimage.qcow2
[ 0.0] Examining the guest ...
[ 14.8] Setting a random seed
[ 14.8] Installing packages: vsftpd
[ 17.1] Finishing off
3. ถอนการติดตั้ง software ในกรณีที่ software ที่ติดตั้งมาไม่จำเป็นต่อการใช้งาน
จากตัวอย่างเป็นการถอนการติดตั้ง cloud-init
# virt-customize --uninstall cloud-init -a myimage.qcow2
[ 0.0] Examining the guest ...
[ 12.4] Setting a random seed
[ 12.4] Uninstalling packages: cloud-init
[ 15.2] Finishing off