Note: Tutorial นี้สมมติว่าคุณไม่มี virt-sysprep ดูด้านล่าง!
ไม่ นี่ไม่ใช่ another generic howto เกี่ยวกับการใช้ virt-clone แม้ virt-clone จะเป็น rather useful tool สำหรับสร้าง duplicates ของ VMs แต่ใน current configuration มันจะ only assign unique ID และ MAC address ให้ cloned system บังคับให้คุณทำ additional tweaks และ customizations เมื่อ clone boots นี่หมายความว่าคุณอาจต้อง meddle (แทรกแซง/ปรับแต่ง) ค่อนข้างมาก ซึ่งไม่ใช่สิ่งที่คุณต้องการ
Okay ผมจะแสดง different way ของการสร้าง identical copies ของ VMs ใน KVM technology พร้อม unique per-host information เพื่อให้ any number of duplicates กลายเป็น individual hosts บน network ของคุณ กล่าวคือ install once แล้ว create as many instances ของ guest ตามที่ต้องการ แต่ละตัวมี hostname, MAC address, IP address และอื่นๆ ของตัวเอง Fully scriptable และทั้งหมด ติดตามมา
Note: รูปภาพนี้มาจาก Wikimedia licensed under CC BY 2.0
วิธีที่ less efficient (น้อยประสิทธิภาพ)
มาพูดถึง virt-clone สั้นๆ มันไม่ใช่ bad utility แตการใช้งานไม่ standard across distributions บาง distros ship ทั้ง GUI component (ส่วนหนึ่งของ Virtual Machine Manager) อื่นๆก็ offer command line tool ในทั้งสองกรณี clones จะ only be assigned new unique ID และ MAC address แต่ no other information Configuration files จะ stored ภายใต้ /etc/libvirt/qemu
ตัวอย่าง:
ตอนนี้เราไม่ต้องการสิ่งนี้ Not perfect (ไม่สมบูรณ์)
The cloning + tools of the trade (การโคลน + เครื่องมือของอาชีพ)
ตอนนี้ right way (วิธีที่ถูกต้อง) คุณต้องการ very few tools เพื่อ achieve desired result จริงๆแค่ one tool ชื่อ kpartx utility ที่สามารถ create device maps จาก partition tables โดยเฉพาะที่นี่ คุณสามารถใช้มันสร้าง partition mappings สำหรับ guest machines โดย query virtual disks เราจะ demonstrate ใช้ Linux แต่ most UNIX-based systems จะ work
สิ่งที่เราต้องทำคือ read partition table จาก virtual disk ก่อนอื่น เราต้อง create copy ของ virtual disk เนื่องจากแต่ละ machine จะมีของตัวเอง นี่คือ one slow step ของ clone procedure และจะ depend (ขึ้นอยู่กับ) ขนาดไฟล์และความช้าของ disks โดยทั่วไป คุณต้องการ few minutes เพื่อ copy typical 20GB disk file
cp <old disk file> <new disk file>
ตอนนี้ partition table:
kpartx -a <virtual disk file>
นี่จะ generate output จริงๆคุณจะเห็น separate mapper สำหรับแต่ละ individual partition ภายใน VM ตัวอย่าง หากคุณมี / และ /home คุณจะมี two mappers
kpartx -av /kvm/disk.raw
add map loop0p1 (253:0): 0 4206592 linear /dev/loop0 2048
add map loop0p2 (253:1): 0 79677440 linear /dev/loop0 4208640
ตอนนี้ mount relevant device และทำ per-host changes:
mount /dev/mapper/<relevant partition loopback> /mountpoint
ตัวอย่าง:
mount /dev/mapper/loop0p1 /mnt
Unique host changes (การเปลี่ยนแปลงเฉพาะโฮสต์)
ตอนนี้ VM ของคุณ most likely ต้องการ changes ต่อไปนี้:
/etc/hosts
/etc/HOSTNAME
และขึ้นอยู่กับว่าคุณใช้ Debian หรือ RedHat/SUSE network configurations แม้ว่าอาจไม่ necessary หากคุณใช้ DHCP อย่างไรก็ตาม หากใช้ static IP addresses นี่คือ must:
/etc/sysconfig/network/ifcfg-<device>
OR
/etc/network/interfaces
Other changes อาจจำเป็น แต่นี่คือ basic ones หลัง save changes umount ขั้นตอนต่อไปคือ edit KVM configuration file สำหรับ clone ใหม่ XML files ปกติ kept ใน /etc/kvm/vm หรือ /etc/libvirt/qemu directories หาก machines autocreated โดย Virtual Machine Manager
XML file changes (การเปลี่ยนแปลงไฟล์ XML)
ก่อนอื่น สร้าง copy ของ template configuration
ตอนนี้คุณต้อง edit unique ID, name และ MAC address ของ domain เหมือนก่อน คุณจะต้อง apply uniqueness logic ใดๆ น่าจะใช้ grep, awk และ inline search & replace ด้วย sed บวก healthy dose ของ regular expressions เมื่อจำเป็น เพื่อ fully script procedure ของคุณ Naturally (โดยธรรมชาติ) คุณยังต้อง point hard disk directive ไปที่ correct file
ตัวอย่าง ชุดคำสั่งที่คุณอาจต้องการ:
sed -i -e 's/\<uuid\>.*/\<\/uuid\>/uuid\>dedoimedo\<\/uuid/'
file.xml
และกับ grep เพื่อ ensure มัน work:
sed -i -e 's/\<uuid\>.*/\<\/uuid\>/uuid\>dedoimedo\<\/uuid/'
file.xml. | grep dedoimedo
<uuid>dedoimedo</uuid>
รวมทั้งหมดเข้าด้วยกัน (Putting it all together)
ดังนั้น complete clone script จะเป็น - ผมให้ typical would be RedHat example คุณสามารถ make your own adjustments ได้ Likewise (เช่นกัน) บางส่วนของ script จะต้องการ your own logic เช่น deciding how to parse partitions output อีกทั้ง คุณอาจต้องการ extra work ใน parsing output ของ XML files และ stripping opening/closing brackets, quotation marks และ characters อื่นๆ
#!/bin/bash
# suspend template OS while cloning
virsh suspend <domain>
cp <old disk> <new disk>
virsh resume <domain>
kpartx -a <new disk>
# some logic to read relevant partitions
mount <desired partition> <mount point>
# some logic to parse contents of host-unique files
sed -i -e 's/old hostname line/new hostname line/' /etc/HOSTNAME
sed -i -e 's/old hostname line/new hostname line/' /etc/hosts
sed -i -e 's/old IP config/new IP config/' /etc/sysconfig/network/ifcfg-eth0
umount <mount point>
cp /etc/kvm/vm/old.xml /etc/kvm/vm/new.xml
# some logic to create new uuid, name, mac, etc
# some logic to parse XML files correctly
sed -i -e 's/old uuid/new uuid/' /etc/kvm/vm/new.xml
sed -i -e 's/old name/new name/' /etc/kvm/vm/new.xml
sed -i -e 's/old mac/new mac/' /etc/kvm/vm/new.xml
sed -i -e 's/old disk/new disk/' /etc/kvm/vm/new.xml
virsh create /etc/kvm/vm/new.xml
exit 0
และเรา done (เสร็จ) Boot และ enjoy!
The future most likely: virt-sysprep
Tutorial ทั้งหมดนี้จะ useless (ไร้ประโยชน์) หากคุณมี virt-sysprep available บน box ของคุณ ซึ่ง probably means (น่าจะหมายถึง) คุณใช้ later editions ของ favorite OS ของคุณ อย่างไรก็ตาม ใน business environment นี่ most likely not the case (น่าจะไม่ใช่กรณีนี้) ดังนั้น tool จะไม่ available ให้ most system administrators หากคุณได้ chance ใช้มัน มันเสนอ really handy improvements (การปรับปรุงที่สะดวกมาก) ให้ basic clone tool อ้างอิง original documentation:
Virt-sysprep resets or unconfigures a virtual machine so that clones can be made from it. Steps in this process include removing SSH host keys, removing persistent network MAC configuration, and removing user accounts. Each step can be enabled or disabled as required. Virt-sysprep modifies the guest or disk image in place. The guest must be shut down. If you want to preserve the existing contents of the guest, you must copy or clone the disk first.
เรา WILL discuss (จะอภิปราย) handy tool นี้แยกใน tutorial อื่น
อ่านเพิ่มเติม (More reading)
สำหรับ KVM fun เพิ่มเติม:
KVM + VirtualBox living in harmony
KVM bridged network tutorial
KVM storage and network administration
And here's how to clone ESXi machines - similar in concept
Another dandy resource of virtual machine disk image tools - libguestfs
สรุป
นี่คือ tutorial very simple และ elegant (สง่างาม/เรียบร้อย) ผมคิดว่าคุณจะพบว่ามัน extremely useful (มีประโยชน์มาก) เพราะมัน complement (เสริม/ทำให้สมบูรณ์) mostly manual cloning procedures ด้วย other tools คุณได้ชุดคำสั่งที่สามารถวางลงใน single BASH script ได้ง่ายๆ และ Bob's your uncle (สำนวน: ทุกอย่างจะเรียบร้อย/เสร็จสิ้น)
On a more serious note (ในโน้ตที่จริงจังกว่า) คุณเรียนรู้เพิ่มเติมนิดหน่อยเกี่ยวกับ KVM configurations และ file locations, very rudimentary use (การใช้งานขั้นพื้นฐานมาก) ของ virt-clone, วิธี mount VM disks และ edit data ใน relevant partitions ใช้ partx (kpartx) รวมถึง network และ host configurations และวิธี assign unique fields ให้ virtual domain ของคุณ และมี libguestfs ด้วย บวก guides ที่ highly useful อื่นๆ All in all (โดยรวม) not bad สำหรับ five minutes of work (งาน 5 นาที)
สุดท้าย virt-sysprep ทำให้ cloning easy มาก อย่างไรก็ตาม manual method แม้จะ complicated (ซับซ้อน) นิดหน่อย แต่ foolproof (ป้องกันข้อผิดพลาดได้/มั่นคง) คุณทำอะไรก็ได้ และไม่ limited to (จำกัดอยู่ที่) existing subset ของ command-line switches และ options มันจะ always work (ทำงานได้ตลอด) และ always be fully scripted (สามารถเขียนสคริปต์ได้เต็มที่) Splendid (ยอดเยี่ยม)
Well I guess that would be all (ผมเดาว่านั่นคือทั้งหมดแล้ว)
ขอแสดงความนับถือ