参考: https://cloud-atlas.readthedocs.io/zh_CN/latest/kvm/kvm_vdisk_live.html
CentOS7 安装libvirtd+ vagrant
1 2 3 4
| sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo sudo yum -y install libvirt-daemon-kvm libvirt-client vagrant gcc-c++ make libstdc++-devel libvirt-devel sudo systemctl enable --now libvirtd vagrant plugin install vagrant-libvirt
|
创建存储池(可选)
因为vagrant 的默认镜像路径在/var/lib, 大量的虚拟机可能会导致根目录存储爆炸所以建议单挂盘
ssd1.xml
文件内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <pool type='dir'> <name>ssd1</name> <source> </source> <target> <path>/ssd1/kvm/</path> <permissions> <mode>0755</mode> <owner>0</owner> <group>0</group> </permissions> </target> </pool>
|
1 2 3 4
| mkdir -p /ssd1/kvm/ virsh pool-define --file pool-ssd1.xml virsh pool-start ssd1 virsh pool-autostart ssd1
|
创建虚拟机
Vagrantfile文件内容如下
PS: 已知问题, 如果在provision执行的初始化脚本中重启网络,会导致vagrant up卡住并超时, 建议直接异步执行, 回调通知即可
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
| ENV['VAGRANT_DEFAULT_PROVIDER'] = 'libvirt'
Vagrant.configure("2") do |config| config.vm.define "test001143" do |node| node.vm.box = "anolis79" node.vm.network "public_network", ip: '192.168.1.143', :bridge => 'em1', :dev => 'em1' node.vm.hostname = "test001143" node.vm.provider :libvirt do |v| v.memory = 8192 v.cpus = 4 v.machine_virtual_size = 100 v.storage_pool_name = "ssd1" end end end
|
启动虚拟机
虚拟机附加磁盘
创建一个50G的的Qcow2磁盘
1 2 3 4 5 6
| qemu-img create -f qcow2 data.qcow2 50G
virsh attach-disk test001143 --source /ssd1/kvm/data.qcow2 --target vdb --persistent --driver qemu --subdriver qcow2
|
1 2 3 4 5 6
| mkfs.ext4 /dev/vdb mkdir /data
ll /dev/*/by-uuid/|grep vdd| awk '{print $9}'
|
将以下内容写入/etc/fstab
UUID为上面命令的输出
1
| UUID=$UUID /data ext4 defaults 0 0
|
挂载磁盘