using-libvirt-create-vm-combine-rbd
前情提要 在一台没有桌面环境的远程服务器上需要新建虚拟机测试ceph,中间涉及桥接网络的配置、使用ISO镜像安装系统的xml文件、
操作流程 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 # 确保qemu,libvirt相关的包已经安装, # qemu-img create -f raw debian9.4Template.raw 2G # 第二步,配置桥接网络 # # # root@test-vm:~# brctl addbr br0 root@test-vm:~# brctl show bridge name bridge id STP enabled interfaces br0 8000.000000000000 no # root@test-vm:~# brctl addif bond1 # root@test-vm:/home/zhoulin/vmxml# brctl show bridge name bridge id STP enabled interfaces br0 8000.fe5400b23c01 no vnet0 # ifconfig br0 up ip link set br0 up # ip addr del 192.168.1.198/24 dev bond1 ip addr add 192.168.1.198/24 dev br0 ip route add default via 192.168.1.254 #route add default gw 192.168.1.254 # # 第三步,使用最后附件的最精简xml新建虚拟机 virsh create debian9.4Template.xml # 第四步,查询vnc端口,使用vnc连接,安装系统,然后测试网络,登入后做好配置,删除网卡udev信息,删除网络配置信息,删除虚拟机,作为镜像 netstat -tlp|grep qemu virsh destroy debian9.4Template # 第五步,根据上一步制作的模板生成新虚拟机磁盘文件 # # root@test-vm:~# fdisk -lu test-vm.img Disk test-vm.img: 2.5 GiB, 2684354560 bytes, 5242880 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x0009a368 Device Boot Start End Sectors Size Id Type debian9.4Template1 * 2048 5240831 5238784 2.5G 83 Linux # root@test-vm:~# mount -o loop,offset=1048576 debian9.4Template.raw /mnt # root@test-vm:~# echo ${hostname} > /mnt/etc/hostname root@test-vm:~# echo "auto eth0" >> /mnt/etc/network/interfaces root@test-vm:~# echo "iface eth0 inet static" >> /mnt/etc/network/interfaces root@test-vm:~# echo "address "${ip} >> /mnt/etc/network/interfaces root@test-vm:~# echo "netmask 255.255.255.0" >> /mnt/etc/network/interfaces root@test-vm:~# echo "gateway 10.200.53.254" >> /mnt/etc/network/interfaces # 第六步,libvirt对接ceph ceph auth get-or-create client.admin mon 'allow *' osd 'allow *' -o /etc/ceph/ceph.client.admin.keyring keyring=`cat /etc/ceph/ceph.client.admin.keyring | grep key | awk '{print $3}'` ceph_cluster_uuid=`ceph -s | grep cluster | awk '{print $2}'` echo "<secret ephemeral='no' private='no'>" > secret.xml echo " <uuid>$ceph_cluster_uuid</uuid>" >> secret.xml echo " <usage type='ceph'>" >> secret.xml echo " <name>client.admin secret</name>" >> secret.xml echo " </usage>" >> secret.xml echo "</secret>" >> secret.xml virsh secret-define --file secret.xml virsh secret-set-value $ceph_cluster_uuid $keyring # 第七步,生成新虚拟机的xml文件 # # # # # #
附件 附件1:libvirt新建开机自动iso安装系统最精简的xml文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 <domain type ="kvm" > <name > test-vm</name > <memory > 524288</memory > <vcpu > 1</vcpu > <cputune > <vcpupin vcpu ="0" cpuset ="1" /> </cputune > <os > <type > hvm</type > <boot dev ="cdrom" /> <boot dev ="hd" /> </os > <features > <acpi /> </features > <clock offset ="utc" > <timer name ="pit" tickpolicy ="delay" /> <timer name ="rtc" tickpolicy ="catchup" /> </clock > <cpu mode ="host-model" match ="exact" /> <devices > <disk type ="file" device ="disk" > <driver name ="qemu" type ="qcow2" cache ="none" /> <source file ="/home/zhoulin/vmxml/vm1.i.nease.net.qcow2" /> <target bus ="virtio" dev ="vda" /> </disk > <disk type ='file' device ='cdrom' > <driver name ='qemu' type ='raw' /> <source file ='/var/lib/libvirt/images/debian-9.4.0-amd64-xfce-CD-1.iso' /> <target dev ='hdb' bus ='ide' /> <readonly /> </disk > <interface type ="bridge" > <source bridge ="br0" /> <mac address ="52:54:00:b2:3c:01" /> </interface > <serial type ="pty" /> <input type ="tablet" bus ="usb" /> <graphics type ="vnc" autoport ="yes" keymap ="en-us" listen ="0.0.0.0" /> </devices > </domain >
附件2:vm添加rbd作为硬盘的xml 1 2 3 4 5 6 7 8 <disk type ='network' device ='disk' > <driver name ='qemu' type ='raw' cache ='none' /> <auth username ='admin' > <secret type ='ceph' uuid ='${ceph_uuid}' /> </auth > <source protocol ='rbd' name ='${pool_name}/${rbd_name}' /> <target dev ='vdb' bus ='virtio' /> </disk >
其他问题 1.莫名操作libvirt挂了 错误如下,试了一些办法,无效,重启才恢复
1 2 3 4 5 6 root@test-vm:/home/zhoulin/vdbs# /etc/init.d/libvirtd restart [ ok ] Restarting libvirt management daemon: /usr/sbin/libvirtd. root@test-vm:/home/zhoulin/vdbs# virsh list error: failed to connect to the hypervisor error: no valid connection error: Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory
2.单纯virsh create在destroy后vm消失 1 2 virsh define XXX.xml virsh list --all
上述方法注册之后,即使destroy也只是强关vm,还是依然在的,重新start可以拉起vm