kvm常见用法
1.虚拟机相关
- 管理虚拟机
virsh list —all 查看所有
virsh destroy XX 强制关机
virsh undefine xx 取消定义(删除),硬盘并没有删除
virsh start xx 启动机器
virsh edit xx 修改xml配置
virsh console xx console连接登陆(需要密码)
- 创建虚拟机
#!/bin/bash
startvm(){
undercloud_name=$1
undercloud_suffix=local
root_password=99cloud
stack_password=99cloud
vcpus=4
vram=8000
brmgt=$2
brdata=$3
brstor=$4
vncport=$5
mac=$6
cloud_image=/root/kvm/CentOS-7-x86_64-GenericCloud-1907.qcow2
export LIBGUESTFS_BACKEND=direct
#export LIBGUESTFS_DEBUG=1 LIBGUESTFS_TRACE=1
qemu-img create -f qcow2 /var/lib/libvirt/images/${undercloud_name}.qcow2 80G
qemu-img create -f qcow2 /var/lib/libvirt/images/${undercloud_name}_2.qcow2 60G
virt-resize --expand /dev/sda1 ${cloud_image} /var/lib/libvirt/images/${undercloud_name}.qcow2
virt-customize -a /var/lib/libvirt/images/${undercloud_name}.qcow2 \
--run-command 'xfs_growfs /' \
--root-password password:${root_password} \
--hostname ${undercloud_name}.${undercloud_suffix} \
--run-command 'sed -i "s/PasswordAuthentication no/PasswordAuthentication yes/g" /etc/ssh/sshd_config' \
--run-command 'systemctl enable sshd' \
--run-command 'yum remove -y cloud-init' \
--selinux-relabel
virt-install --name ${undercloud_name} \
--disk /var/lib/libvirt/images/${undercloud_name}.qcow2 \
--disk /var/lib/libvirt/images/${undercloud_name}_2.qcow2 \
--vcpus=${vcpus} \
--ram=${vram} \
--network bridge=${brmgt},model=virtio,mac=${mac} \
--network bridge=${brmgt},model=virtio \
--network bridge=${brdata},model=virtio \
--network bridge=${brdata},model=virtio \
--network bridge=${brstor},model=virtio \
--network bridge=${brstor},model=virtio \
--virt-type kvm \
--import \
--os-variant rhel7 \
--graphics vnc,listen=0.0.0.0,port=${vncport} \
--serial pty \
--noautoconsole \
--console pty,target_type=virtio
}
#for i in {1..2}
#do
i=02
virsh destroy ictnode${i}
virsh undefine ictnode${i}
startvm ictnode${i} brnet bdatal0 bstorl0 59${i} 52:54:22:2b:32:${i}
sleep 5
#done
- 添加网卡
virsh domiflist ictnode10 查看网卡
virsh attach-interface ictnode10 --type bridge --source brnet --model virtio --config 挂载网卡(需要重启)
virsh detach-interface ictnode10 --type bridge --mac 52:54:00:1c:cb:58 —config 卸载网卡
- 克隆虚拟机
virt-clone --connect qemu:///system -o overcloud-compute13 -n overcloud-compute13-clone -f /var/lib/libvirt/images/overcloud-compute13-clone.qcow2
- 修改虚拟机密码
virsh set-user-password overcloud-compute13 root 99cloud
- virsh define XX 报错nvram,添加参数--nvram
virsh undefine overcloud-compute13-clone --nvram
- detach硬盘qcow2格式时大小显示不对
#创建了一个disk文件, 用于给7号VM添加100G的新硬盘
sudo qemu-img create -f qcow2 ubuntu_vm7_disk_100G 100G
#挂载这个文件到sdb,sdb需要是虚机上未使用的名字
virsh attach-disk ubuntu_7 /var/lib/libvirt/images/ubuntu_vm7_disk_100G vdb --cache none
#提示挂载成功,但是进入虚机查看硬盘设备,看到设备,但是容量明显不对,194k只是文件的大小,虚机里面应该看到100G才对
me@ubuntu7:~$ lsblk
vdb 252:16 0 194K 0 disk
#原因是当创建qcow2格式的硬盘时,挂载需要使用--subdriver 制定驱动类型#先卸载硬盘
virsh detach-disk ubuntu_7 /var/lib/libvirt/images/ubuntu_vm7_disk_100G
#再重新挂载
virsh attach-disk ubuntu_7 /var/lib/libvirt/images/ubuntu_vm7_disk_100G vdb --subdriver=qcow2
#进入虚机查看
me@ubuntu7:~$ lsblk
vdb 252:16 0 100G 0 disk
2.网桥相关
- 网桥管理
brctl show
brctl addbr XX
brctl delbr xx
brctl addif br0 xx
brctl delif br0 xx
ip link delete xx
ip link set xx up/down
- 添加网桥vlan,物理网卡必须能与外界
1.#在宿主机上添加网桥
ip link add link ens1f0 name ens1f0.144 type vlan id 144
ip link set ens2f0.144 up
brctl addbr br_jun
brctl addif br_jun ens2f0.144
ifconfig br_jun up
brctl show 查看网桥
2.#在kvm虚拟机内部添加地址与网关(想要外网访问需要网关)
ip addr add 192.168.0.11/24 dev eth0
route add default gw 192.168.0.1
3.vbmc相关
vbmc list
vbmc start ictnode1
vbmc delete ictnode1
vbmc add ictnode1 --port 6889 --address 172.20.128.213 --username admin --password password --libvirt-uri qemu+ssh://root@127.0.0.1/system
ipmitool -I lanplus -H 172.20.128.213 -p 6889 -U admin -P password power status
4.qemu-img相关
qemu-img create -f qcow2 /var/lib/libvirt/images/${undercloud_name}.qcow2 80G
qemu-img info xx
5.iscsi存储相关
# vim /etc/iscsi/initiatorname.iscsi (修改后需要重启服务)
InitiatorName=iqn.2020-01.sjtu.com:ict01
# iscsiadm -m session 查看已挂载的lun
# iscsiadm --mode discovery --type sendtargets --portal 172.20.128.234 发现可用的lun
# iscsiadm --mode node --targetname iqn.2020-01.sjtu.com:ict01 --portal 172.20.128.234:3260 --logout 卸载lun
# iscsiadm --mode node --targetname iqn.2020-01.sjtu.com:ict01 --portal 172.20.128.234:3260 --login 挂载lun
# iscsiadm --mode node --targetname iqn.2020-01.sjtu.com:ict01 --portal 172.20.128.234:3260 --op update -n node.startup
-v automatic 设置开机自动挂载lun