kvm安装记录及常用命令
又开始新的项目,又开始新的搭环境,工作记录,记录kvm命令及安装/问题
virsh list --all # 列出所有虚拟机
virsh start kvm001 # 启动kvm001虚拟机
virsh autostart kvm001 # 设置自启动
virsh autostart --disable kvm001 # 关闭自启动
virsh shutdown kvm001 # 关闭虚拟机
virsh dominfo kvm001 # 显示虚拟机的基本信息
virsh dumpxml kvm001 # 显示虚拟机的配置文件
macvtap-net 设置
# 查看系统是否支持macvtap-net If you get an error or the lsmod command returns no results, then you may have a problem using the macvlan driver
modprobe macvlan
lsmod | grep macvlan
# 编写xml文件
vim macvtap-def.xml
--------------写入以下内容
<network>
<name>macvtap-net</name>
<forward mode="bridge">
<interface dev="eth1"/> # 注意网卡,一般是宿主机对外网卡名
</forward>
</network>
--------------写入以上内容
# macvtap网络创建及开机自启
virsh net-define macvtap-def.xml
virsh net-autostart macvtap-net
virsh net-start macvtap-net
# 在install时,网络配置 --network network:macvtap-net,model=virtio
安装虚拟机 命令行设置console安装centos7
virt-install --name=mimicpool1 --ram=2048 --vcpus=2 \
--os-type=linux --os-variant=rhel7 \
--location=/home/kvm_images/CentOS-7-x86_64-Minimal-2009.iso \
--disk path=/home/kvm_images/mimicpool1.qcow2,size=100,format=qcow2 \
--network network:macvtap-net,model=virtio --graphics none \
--console=pty,target_type=serial --extra-args="console=tty0 console=ttyS0"
# 在宿主机中进入kvm虚拟机,按照上面使用console安装,之后进入直接virsh console mimicpool1即可以console方式进入,console界面同时只能打开一个
# 多网卡配置,安装时使用多个network参数即可,安装后的直接到配置文件virsh edit image_name,增加interface参数重启即可,需要注意新加一个mac,且address参数的slot不能和已有的slot重复,一般就从9朝上了
# 虚拟机qcow2拷贝 (一个父镜像,多个子镜像)
#终端输入“sudo qemu-img create -b ubuntu18.04.qcow2 -f qcow2 apple.qcow2”
#原型:qemu-img create -b 基本镜像名字 -f 镜像的类型(raw,qcow2,qcow,这里选qcow2) 增量镜像名字 ;将基本镜像copy出子镜像文件cat /
cd /etc/libvirt/qemu
cp ubuntu18.04.xml mimicpool001.xml
#更改几行:
name
uuid
file # 镜像路径
mac
#之后用下面方式导入就行了
参考连接:https://www.pianshen.com/article/1457282846/
# 导入qcow2镜像 这个命令会报warn,具体什么太快了看不清,不影响使用。。
virt-install --name=mimicpool001 --ram=2048 --vcpus=2 \
--disk path=/home/kvm_images/mimicpool001.qcow2 --import \
--network network:macvtap-net,model=virtio --graphics none \
--console=pty,target_type=serial
如果安装时没记住IP,可以通过以下方法找到,然后再配置静态IP
virsh dumpxml kvm002 | grep mac # 查看对应虚拟机mac
# 结果
<partition>/machine</partition>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<mac address='52:54:00:89:67:66'/> #记录mac地址
arp -a | grep 52:54:00:89:67:66 #查询IP
# 结果
? (192.168.0.100) at 52:54:00:89:67:66 [ether] on br0 #找到IP
修改虚拟机
# 1、关机
virsh shutdown kvm001
# 2、把名称改为kvm100
virsh domrename kvm001 kvm100
# 3、修改磁盘文件名称
mv /home/raw/kvm001.raw /home/raw/kvm100.raw
# 4、修改配置文件
virsh edit kvm100
# 修改内容
<source file='/home/raw/kvm100.raw'/>
# 5、开机
virsh start kvm100
删除虚拟机
# 强制关闭虚拟机
virsh destroy kvm001
# 删除定义虚拟机
virsh undefine kvm001
# 查找虚拟机文件
find / -name kvm002*
/var/log/libvirt/qemu/kvm002.log
/home/raw/kvm002.raw
# 删除
find / -name kvm002* | xargs rm -rf
修改镜像大小 warn: 不建议使用以下办法对镜像进行修改,这里仅记录,操作可能导致qcow2镜像损坏,具体恢复方法没有去找
#查看kvm镜像的格式信息
1、查看kvm镜像的格式信息
[root@base-12-199 bakup]# qemu-img info 10.1.12.201_base-12-201
image: 10.1.12.201_base-12-201
file format: qcow2
virtual size: 200G (214748364800 bytes)
disk size: 88G
cluster_size: 65536
2、给镜像增加100G空间
[root@base-12-199 bakup]# qemu-img resize 10.1.12.201_base-12-201 +100G
Image resized.
[root@base-12-199 bakup]# qemu-img info 10.1.12.201_base-12-201
image: 10.1.12.201_base-12-201
file format: qcow2
virtual size: 300G (322122547200 bytes)
disk size: 88G
cluster_size: 65536
3、给镜像减少100G空间
qcow2格式降低空间
[root@base-12-199 bakup]# qemu-img resize 10.1.12.201_base-12-201-- -100G
This image format does not support resize
由于qcow2格式不支持空间减少,所以要使用raw格式。
[root@base-12-199 bakup]# qemu-img convert -O raw 10.1.12.201_base-12-201 10.1.12.201_base-12-201.raw
查看镜像信息
[root@base-12-199 bakup]# qemu-img info 10.1.12.201_base-12-201.raw
image: 10.1.12.201_base-12-201.raw
file format: raw
virtual size: 300G (429496729600 bytes)
disk size: 88G
[root@base-12-199 bakup]# qemu-img resize 10.1.12.201_base-12-201.raw -- 100G
Image resized.
[root@base-12-199 bakup]# qemu-img info 10.1.12.201_base-12-201.raw
image: 10.1.12.201_base-12-201.raw
file format: raw
virtual size: 200G (214748364800 bytes)
disk size: 88G
转换为qcow2
[root@base-12-199 bakup]# qemu-img convert -O qcow2 10.1.12.201_base-12-201.raw 10.1.12.201_base-12-201.qcow2
[root@base-12-199 bakup]# qemu-img info 10.1.12.201_base-12-201.qcow2
image: 10.1.12.201_base-12-201.qcow2
file format: qcow2
virtual size: 200G (214748364800 bytes)
disk size: 88G
导入qcow2文件
创建一个name为VM01,4G内存,2个CPU,qcow2所在磁盘目录及文件名,网络为桥接口br0,强制导入,并设置虚拟机为随宿主机自动启动
virt-install --name VM01 --ram 4096 --vcpus=2 --disk path=VM01.qcow2 --network=bridge:br0 --force --import --autostart
问题记录
安装过程还是遇到不少问题的,这里记录一些比较难解决的问题
- kvm使用virt-install安装ubuntu的时候(或大部分ubuntu的时候),会报错Couldn't find hvm kernel for Ubuntu tree
这个问题国内大部分解决方案并不适用,引用国外的一句话
顺便挂一个可用的ubuntu20 iso http://cdimage.ubuntu.com/ubuntu-legacy-server/releases/20.04/release/
If you use --cdrom in replace of --location you will loose some options like --extra-args ! This useful parameter could not be used in cooperation with --cdrom ! If you want to use --extra-args you must use --location !
If you like this parameter you must overcome to this problem such a different way!
If you want to overcome to this problem, you must know that .iso files are a type of compressed files, first of all! Yes! They are some special type of compressed files like .zip or .rar, .tar.gz and so etc.
You could learn how to extract them by following this link. # 这里连接不复制过来了,想看的话去查下iso文件结构
The reason that you got ERROR: Couldn't find hvm kernel for Ubuntu tree. is that your image(.iso file) doesn't include install/vmlinux file(vmlinuz file is not available directly in your root install folder)! Check the existence of this file by extracting .iso file( It must have vmlinuz file exactly within your root install folder).
If you couldn't find that, you must find other related .iso from ubuntu website which includes this desired file at this specific path!
It's remarkable that Ubuntu provides different .iso files like live iso, minimal iso and so etc for each distro and version and platform! You need just to look for much appropriate one.
Do your best and have fun.
- ubuntu安装完后,console口不能进入问题
先配置IP,通过ssh登入,之后使用下面命令开启console和自启
sudo systemctl start serial-getty@ttyS0
sudo systemctl enable serial-getty@ttyS0
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
有任何问题请随时交流~
Email: araise1@163.com