1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > KVM--virsh虚拟机的日常操作(开关 挂起虚拟机 导配置文件 磁盘增容 转化磁盘格式等)

KVM--virsh虚拟机的日常操作(开关 挂起虚拟机 导配置文件 磁盘增容 转化磁盘格式等)

时间:2019-10-20 22:31:16

相关推荐

KVM--virsh虚拟机的日常操作(开关 挂起虚拟机 导配置文件 磁盘增容 转化磁盘格式等)

一、 查看命令帮助

[root@localhost ~]# virsh -h

virsh [options]... [<command_string>]virsh [options]... <command> [args...]options:-c | --connect=URIhypervisor connection URI-d | --debug=NUM debug level [0-4]-e | --escape <char> set escape sequence for console-h | --help this help-k | --keepalive-interval=NUMkeepalive interval in seconds, 0 for disable-K | --keepalive-count=NUMnumber of possible missed keepalive messages-l | --log=FILE output logging to file-q | --quiet quiet mode-r | --readonly connect readonly-t | --timing print timing information-v short version-V long version--version[=TYPE] version, TYPE is short or long (default short)commands (non interactive mode):Domain Management (help keyword 'domain')attach-device 从一个XML文件附加装置attach-disk附加磁盘设备attach-interface获得网络界面autostart 自动开始一个域blkdeviotune 设定或者查询块设备 I/O 调节参数。blkiotune 获取或者数值 blkio 参数blockcommit启动块提交操作。blockcopy 启动块复制操作。blockjob 管理活跃块操作blockpull 使用其后端映像填充磁盘。blockresize创新定义域块设备大小change-media 更改 CD 介质或者软盘驱动器console 连接到客户会话cpu-stats 显示域 cpu 统计数据create从一个 XML 文件创建一个域define从一个 XML 文件定义(但不开始)一个域desc 显示或者设定域描述或者标题destroy 销毁(停止)域detach-device 从一个 XML 文件分离设备detach-device-alias detach device from an aliasdetach-disk分离磁盘设备detach-interface分离网络界面domdisplay 域显示连接 URIdomfsfreezeFreeze domain's mounted filesystems.domfsthaw Thaw domain's mounted filesystems.domfsinfo Get information of domain's mounted filesystems.domfstrim 在域挂载的文件系统中调用 fstrim。domhostname输出域主机名domid把一个域名或 UUID 转换为域 iddomif-setlink 设定虚拟接口的链接状态domiftune 获取/设定虚拟接口参数domjobabort忽略活跃域任务domjobinfo 域任务信息domname 将域 id 或 UUID 转换为域名domrename rename a domaindompmsuspend 使用电源管理功能挂起域dompmwakeup从 pmsuspended 状态唤醒域domuuid 把一个域名或 id 转换为域 UUIDdomxml-from-native 将原始配置转换为域 XMLdomxml-to-native将域 XML 转换为原始配置dump 把一个域的内核 dump 到一个文件中以方便分析dumpxml XML 中的域信息edit 编辑某个域的 XML 配置eventDomain Eventsinject-nmi 在虚拟机中输入 NMIiothreadinfo view domain IOThreadsiothreadpincontrol domain IOThread affinityiothreadaddadd an IOThread to the guest domainiothreaddeldelete an IOThread from the guest domainsend-key 向虚拟机发送序列号send-process-signal 向进程发送信号lxc-enter-namespace LXC 虚拟机进入名称空间managedsave管理域状态的保存managedsave-remove 删除域的管理保存managedsave-editedit XML for a domain's managed save state filemanagedsave-dumpxml Domain information of managed save state file in XMLmanagedsave-define redefine the XML for a domain's managed save state filememtune 获取或者数值内存参数perf Get or set perf eventmetadata show or set domain's custom XML metadatamigrate 将域迁移到另一个主机中migrate-setmaxdowntime 设定最大可耐受故障时间migrate-getmaxdowntime get maximum tolerable downtimemigrate-compcache 获取/设定压缩缓存大小migrate-setspeed设定迁移带宽的最大值migrate-getspeed获取最长迁移带宽migrate-postcopySwitch running migration from pre-copy to post-copynumatune 获取或者数值 numa 参数qemu-attachQEMU 附加qemu-monitor-command QEMU 监控程序命令qemu-monitor-event QEMU Monitor Eventsqemu-agent-command QEMU 虚拟机代理命令reboot重新启动一个域reset重新设定域restore 从一个存在一个文件中的状态恢复一个域resume重新恢复一个域save 把一个域的状态保存到一个文件save-image-define 为域的保存状态文件重新定义 XMLsave-image-dumpxml 在 XML 中保存状态域信息save-image-edit为域保存状态文件编辑 XMLschedinfo 显示/设置日程安排变量screenshot 提取当前域控制台快照并保存到文件中set-lifecycle-action change lifecycle actionsset-user-password set the user password inside the domainsetmaxmem 改变最大内存限制值setmem改变内存的分配setvcpus 改变虚拟 CPU 的号shutdown 关闭一个域start开始一个(以前定义的)非活跃的域suspend 挂起一个域ttyconsole tty 控制台undefine 取消定义一个域update-device 从 XML 文件中关系设备vcpucount 域 vcpu 计数vcpuinfo 详细的域 vcpu 信息vcpupin 控制或者查询域 vcpu 亲和性emulatorpin控制火车查询域模拟器亲和性vncdisplay vnc 显示guestvcpus query or modify state of vcpu in the guest (via agent)setvcpu attach/detach vcpu or groups of threadsdomblkthresholdset the threshold for block-threshold event for a given block device or it's backing chain elementDomain Monitoring (help keyword 'monitor')domblkerror在块设备中显示错误domblkinfo 域块设备大小信息domblklist 列出所有域块domblkstat 获得域设备块状态domcontrol 域控制接口状态domif-getlink 获取虚拟接口链接状态domifaddr Get network interfaces' addresses for a running domaindomiflist 列出所有域虚拟接口domifstat 获得域网络接口状态dominfo 域信息dommemstat 获取域的内存统计domstate 域状态domstats get statistics about one or multiple domainsdomtime domain timelist 列出域Host and Hypervisor (help keyword 'host')allocpages Manipulate pages pool sizecapabilities 性能cpu-baseline 计算基线 CPUcpu-compare使用 XML 文件中描述的 CPU 与主机 CPU 进行对比cpu-models CPU modelsdomcapabilitiesdomain capabilitiesfreecell NUMA可用内存freepages NUMA free pageshostname 打印管理程序主机名hypervisor-cpu-baseline compute baseline CPU usable by a specific hypervisorhypervisor-cpu-compare compare a CPU with the CPU created by a hypervisor on the hostmaxvcpus 连接 vcpu 最大值node-memory-tune获取或者设定节点内存参数nodecpumap 节点 cpu 映射nodecpustats 输出节点的 cpu 状统计数据。nodeinfo 节点信息nodememstats 输出节点的内存状统计数据。nodesuspend在给定时间段挂起主机节点sysinfo 输出 hypervisor sysinfouri 打印管理程序典型的URIversion 显示版本Interface (help keyword 'interface')iface-begin生成当前接口设置快照,可在今后用于提交 (iface-commit) 或者恢复 (iface-rollback)iface-bridge 生成桥接设备并为其附加一个现有网络设备iface-commit 提交 iface-begin 后的更改并释放恢复点iface-define define an inactive persistent physical host interface or modify an existing persistent one from an XML fileiface-destroy 删除物理主机接口(启用它请执行 "if-down")iface-dumpxml XML 中的接口信息iface-edit 为物理主机界面编辑 XML 配置iface-list 物理主机接口列表iface-mac 将接口名称转换为接口 MAC 地址iface-name 将接口 MAC 地址转换为接口名称iface-rollback 恢复到之前保存的使用 iface-begin 生成的更改iface-start启动物理主机接口(启用它请执行 "if-up")iface-unbridge 分离其辅助设备后取消定义桥接设备iface-undefine 取消定义物理主机接口(从配置中删除)Network Filter (help keyword 'filter')nwfilter-define使用 XML 文件定义或者更新网络过滤器nwfilter-dumpxmlXML 中的网络过滤器信息nwfilter-edit 为网络过滤器编辑 XML 配置nwfilter-list 列出网络过滤器nwfilter-undefine 取消定义网络过滤器nwfilter-binding-create create a network filter binding from an XML filenwfilter-binding-delete delete a network filter bindingnwfilter-binding-dumpxml XML 中的网络过滤器信息nwfilter-binding-listlist network filter bindingsNetworking (help keyword 'network')net-autostart 自动开始网络net-create 从一个 XML 文件创建一个网络net-define define an inactive persistent virtual network or modify an existing persistent one from an XML filenet-destroy销毁(停止)网络net-dhcp-leasesprint lease info for a given networknet-dumpxmlXML 中的网络信息net-edit 为网络编辑 XML 配置net-event Network Eventsnet-info 网络信息net-list 列出网络net-name 把一个网络UUID 转换为网络名net-start 开始一个(以前定义的)不活跃的网络net-undefine undefine a persistent networknet-update 更新现有网络配置的部分net-uuid 把一个网络名转换为网络UUIDNode Device (help keyword 'nodedev')nodedev-create 根据节点中的 XML 文件定义生成设备nodedev-destroy销毁(停止)节点中的设备nodedev-detach 将节点设备与其设备驱动程序分离nodedev-dumpxmlXML 中的节点设备详情nodedev-list 这台主机中中的枚举设备nodedev-reattach重新将节点设备附加到他的设备驱动程序中nodedev-reset 重置节点设备nodedev-event Node Device EventsSecret (help keyword 'secret')secret-define 定义或者修改 XML 中的 secretsecret-dumpxml XML 中的 secret 属性secret-event Secret Eventssecret-get-valuesecret 值输出secret-list列出 secretsecret-set-value设定 secret 值secret-undefine取消定义 secretSnapshot (help keyword 'snapshot')snapshot-create使用 XML 生成快照snapshot-create-as 使用一组参数生成快照snapshot-current获取或者设定当前快照snapshot-delete删除域快照snapshot-dumpxml为域快照转储 XMLsnapshot-edit 编辑快照 XMLsnapshot-info 快照信息snapshot-list 为域列出快照snapshot-parent获取快照的上级快照名称snapshot-revert将域转换为快照Storage Pool (help keyword 'pool')find-storage-pool-sources-as 找到潜在存储池源find-storage-pool-sources发现潜在存储池源pool-autostart 自动启动某个池pool-build 建立池pool-create-as 从一组变量中创建一个池pool-create从一个 XML 文件中创建一个池pool-define-as 在一组变量中定义池pool-definedefine an inactive persistent storage pool or modify an existing persistent one from an XML filepool-delete删除池pool-destroy 销毁(删除)池pool-dumpxml XML 中的池信息pool-edit 为存储池编辑 XML 配置pool-info 存储池信息pool-list 列出池pool-name 将池 UUID 转换为池名称pool-refresh 刷新池pool-start 启动一个(以前定义的)非活跃的池pool-undefine 取消定义一个不活跃的池pool-uuid 把一个池名称转换为池 UUIDpool-event Storage Pool EventsStorage Volume (help keyword 'volume')vol-clone 克隆卷。vol-create-as 从一组变量中创建卷vol-create 从一个 XML 文件创建一个卷vol-create-from生成卷,使用另一个卷作为输入。vol-delete 删除卷vol-download 将卷内容下载到文件中vol-dumpxmlXML 中的卷信息vol-info 存储卷信息vol-key 为给定密钥或者路径返回卷密钥vol-list 列出卷vol-name 为给定密钥或者路径返回卷名vol-path 为给定密钥或者路径返回卷路径vol-pool 为给定密钥或者路径返回存储池vol-resize 创新定义卷大小vol-upload 将文件内容上传到卷中vol-wipe 擦除卷Virsh itself (help keyword 'virsh')cd 更改当前目录echo echo 参数exit 退出这个非交互式终端help 打印帮助pwd 输出当前目录quit 退出这个非交互式终端connect 连接(重新连接)到 hypervisor(指定 help <group> 获取组中命令的详情)(使用 --help <command> 来获得这个命令的详细信息)

二、虚拟机管理

2.1. 查看 KVM 的配置文件存放目录

[root@localhost ~]# ll /etc/libvirt/qemu/总用量 8-rw-------. 1 root root 4631 6月 30 16:11 centos7.0.xmldrwx------. 3 root root 42 4月 28 networks#用 ps -ef |grep qemu可以看到正在运行的虚拟机的磁盘内容等。

2.2.列出全部虚拟机

[root@localhost ~]# virsh list --allId 名称状态----------------------------------------------------4centos7.0 running[root@localhost ~]#

列出运行的虚拟机

[root@kvm images]# virsh listId Name State----------------------------------------------------

2.3. 开启虚拟机

[root@localhost ~]# virsh start centos7.0 域 centos7.0 已开始

2.4. 关闭虚拟机

[root@localhost ~]# virsh shutdown centos7.0 域 centos7.0 被关闭

2.5. 强制关闭

如果执行了shutdown但是vnc的虚拟机仍在运行,此时要强制关机(拔电源关机,数据会丢失)

[root@localhost ~]# virsh destroy centos7.0 # 结果[root@localhost ~]# virsh list --allId 名称状态-----------------------------------------------------centos7.0 关闭

2.6.重启虚拟机

[root@kvm images]# virsh reboot nebula1

2.7.查看配置文件

[root@kvm images]# virsh dumpxml nebula1

2.8. 导出虚拟机配置到其它虚拟机中

把centos7.0 的配置放到centos7.1 里:

[root@localhost ~]# virsh dumpxml centos7.0 > /etc/libvirt/qemu/centos7.1# 查看配置文件是否生效[root@localhost ~]# ls /etc/libvirt/qemu/autostart centos7.0.xml centos7.1 networks

2.9 导出虚拟机配置到XML文件

# 将这个配置文件写入到一个文件里,就可以看到内存,cpu,磁盘等情况。[root@kvm images]# virsh dumpxml nebula1>>nebula1.xml

2.10 基于配置文件,创建虚拟机

只要有磁盘文件+配置文件,虚拟机就可以迁移到任何电脑上

# 只要有磁盘文件+配置文件,虚拟机就可以迁移到任何电脑上,# 将配置文件导入, 就可以迁移到其他主机上了# 基于配置文件,创建虚拟机[root@kvm images]# virsh define nebula1.xml

2.11 通过配置文件启动虚拟机系统

create时需要相应的qcow2磁盘文件

[root@localhost ~]# virsh create /etc/libvirt/qemu/centos7.0.xml 域 centos7.0 被创建(从 /etc/libvirt/qemu/centos7.0.xml)# 重启后的状态[root@localhost ~]# virsh list --allId 名称状态----------------------------------------------------6centos7.0 running

2.12. 配置虚拟机开机自启动

[root@localhost ~]# virsh autostart centos7.0 域 centos7.0标记为自动开始

# 查看开机启动虚拟机列表

[root@kvm ~]# cd /etc/libvirt/qemu[root@kvm qemu]# ls ###看到有一个autostart, 就是开机自启动的目录autostart nebula1.xml networks[root@kvm qemu]# cd autostart/ ###进入到此目录,看有哪些是开机自启动[root@kvm autostart]# ls ###看到nebula1是设置了开机自启动nebula1.xml[root@kvm autostart]# ll -h nebula1.xml ###这是一个软链接文件,如果把源文件删除掉了,那么即使给它设置了开机自启动,它也启动不起来lrwxrwxrwx 1 root root 29 May 1 15:15 nebula1.xml -> /etc/libvirt/qemu/nebula1.xml[root@kvm autostart]# ln -s /etc/libvirt/qemu/nebula1.xml /etc/libvirt/qemu/autostart #如果没有设置开机自启动,也可以直接给它做软链接

2.13 取消开机自启动

[root@kvm autostart]# virsh autostart --disable nebula1Domain nebula1 unmarked as autostarted[root@kvm ~]# cd /etc/libvirt/qemu/autostart[root@kvm autostart]# ls ###这个目录下没内容了,实质上就是删除了软链接[root@kvm autostart]#

2.14 挂起虚拟机

[root@localhost ~]# virsh suspend centos7.0 域 centos7.0 被挂起# 结果[root@localhost ~]# virsh list --allId 名称状态----------------------------------------------------6centos7.0 暂停

2.15 恢复虚拟机

[root@localhost ~]# virsh resume centos7.0

2.16删除虚拟机

[root@localhost ~]# virsh undefine centos7.0 域 centos7.0 已经被取消定义# 结果: centos7.0 的配置文件已经没有了[root@localhost ~]# ls /etc/libvirt/qemu/autostart centos7.1 networks# 现在没有运行的虚拟机[root@localhost ~]# virsh list --allId 名称状态----------------------------------------------------# 恢复删除了虚拟机:[root@kvm images]# virsh define nebula1.xml

2.17 虚拟机重命名

# 虚拟机关闭后才能重命名[root@localhost ~]# virsh shutdown centos7.0[root@kvm images]# virsh domrename centos7.0 web-blog

2.18 查看vnc端口号

[root@kvm ~]# virsh vncdisplay nebula1:10050

2.19 进入到命令行版的控制台页面:(可以代替vnc,console登录)

[root@kvm autostart]# virsh console nebula1Connected to domain nebula1Escape character is ^] ###用^和 ]才可以退出来,长按Ctrl+][root@kvm autostart]#

2.20 配置文件重命名

进入到配置文件目录,进行配置文件的重命名

[root@localhost ~]# cd /etc/libvirt/qemu/[root@localhost qemu]# mv centos7.1 centos7.0# 重新定义[root@localhost qemu]# virsh define centos7.0 定义域 centos7.0(从 centos7.0)# 结果[root@localhost qemu]# virsh list --allId 名称状态-----------------------------------------------------centos7.0 关闭

2.21 修改虚拟机配置信息(用来修改系统内存大小、磁盘文件等信息)

# 1.直接通过 vim 命令修改:[root@localhost qemu]# vim centos7.0# 或者# (相当于编辑上面的配置文件):[root@localhost qemu]# virsh edit centos7.0 # 2. 修改完成关闭虚拟机[root@localhost qemu]# virsh shutdown centos7.0 # 3. 启动虚拟机,生效[root@localhost qemu]# virsh start centos7.0

2.22 修kvm的虚拟机的内核参数:

[root@kvm ~]# grubby --update-kernel=ALL --args="console=ttyS0,115200n8"reboot,必须重启才有效

2.23 查看虚拟机状态

[root@localhost ~]# virsh domstate tidb-vm1

2.24kvm查看虚拟机ip

1、查看win7虚拟机mac[root@localhost home]# virsh dumpxml <vm-name> | grep mac 2、显示mac与ip对应关系[root@localhost home]# arp -a3、示例:步骤一;[root@localhost home]# virsh dumpxml openshift-vm1 | grep mac[root@localhost home]# virsh dumpxml tidb-vm1 | grep mac<partition>/machine</partition><type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type><mac address='52:54:04:8d:56:bt'/>步骤二:[root@localhost home]# arp -a | grep 52:54:00:8d:56:ba? (192.168.122.70) at 52:54:00:8d:56:ba [ether] on virbr0步骤三:[root@localhost home]# ssh 192.168.122.70说明:1、openshift-vm1是虚拟机名称;2、arp -a | grep 52:54:00:11:c5:4e 是找mac对应的ip。

三、虚拟机磁盘管理

3.1. 修改虚拟机磁盘路径

也可以利用该方法给磁盘重命名:先关闭虚拟机,然后重命名qcow2磁盘文件,最后通过下面的virsh edit方式更新到新的qcow2文件名称。

[root@kvm images]# mkdir /data[root@kvm images]# mv nebula1.qcow2 /data/[root@kvm images]# cd /etc/libvirt/qemu[root@kvm images]# ls /etc/libvirt/qemuautostart nebula1.xml networks[root@kvm images]# virsh edit nebula1(其实就是vim nebula1.xml,不过为了安全起见,配置文件里面让我们选择这种修改方式)修改内容如下: <driver name='qemu' type='qcow2'/><source file='/data/nebula1.qcow2'/>

3.2. 查看当前磁盘格式(qemu -img info)

# 安装libguestfs-tools[root@localhost ~]# yum install libguestfs-tools -y# 镜像目录: /var/lib/libvirt/images/[root@localhost ~]# qemu-img info /var/lib/libvirt/images/centos7.0.qcow2 image: /var/lib/libvirt/images/centos7.0.qcow2file format: qcow2virtual size: 5.0G (5368709120 bytes)disk size: 5.0Gcluster_size: 65536Format specific information:compat: 1.1lazy refcounts: true[root@localhost ~]# # 查看qcow2快照:qemu-img snapshot -l /home/kvm/vmfs/openshift-vm1.qcow2# 删除qcow2快照:qemu-img snapshot /home/kvm/vmfs/openshift-vm1.qcow2 -d openshift-vm1-1227

3.3. kvm虚拟机虚拟磁盘格式转化:

raw:裸格式,占用空间大,有多少空间直接占满,不支持快照,性能较好,不方便传输;qcow2:cow(copy on write),要用多少内存给多少内存,不会一下子占完,占用空间小,支持快照,性能比raw能差一点,方便传输。# 虚拟机磁盘工具的常用命令:qemu -img info,create,resize,covert

3.4.创建磁盘

创建一块大小为1G的qcow2格式的虚拟磁盘:

[root@kvm data]# qemu-img create -f qcow2 /data/ll.qcow2 1Gimage: ll.qcow2file format: qcow2virtual size: 1.0G (1073741824 bytes)

3.5.调整磁盘容量(给磁盘加100G容量)

qcow2格式的磁盘只能加不能减,raw格式的盘可以减。

# 1.查看需要扩容的磁盘信息[root@localhost ~]# virsh domblklist tidb-vm1 (tidb-vm1: 虚拟机名字)TargetSource------------------------------------------------hda /data/vmfs/tidb-vm1.qcow2hdb -# 可以看到,目前大小200G[root@localhost ~]# qemu-img info /data/vmfs/tidb-vm1.qcow2image: /data/vmfs/tidb-vm1.qcow2file format: qcow2virtual size: 200G (214748364800 bytes)disk size: 2.3Gcluster_size: 65536Snapshot list:ID TAG VM SIZEDATE VM CLOCK1 1664466694 459M -09-29 23:51:34 00:01:52.471Format specific information:compat: 1.1lazy refcounts: true# 2.虚拟机停机[root@kvm data]# virsh shutdown vm-name# 3. 扩容​​​​​​​# qcow2是一个写时复制的磁盘格式,给这个磁盘写入2G的内容,ll -h看就增加了2G,# 如果此时删掉了2G的内容,再df -h查看,原来增加的2G内存还在,# 因为qcow2格式文件只能增,不能减。[root@kvm data]# qemu-img resize /data/vmfs/tidb-vm1.qcow2 +100GImage resized.# 4.重新定义虚拟机(不必执行)# [root@kvm data]# virsh define /etc/libvirt/qemu/tidb-vm1.xml# local.xml[root@kvm data]# ll -h /data/vmfs/tidb-vm1.qcow2-rw------- 1 qemu qemu 2.4G Sep 30 14:40 /data/vmfs/tidb-vm1.qcow2# 5.查看虚拟机磁盘容量[root@kvm data]# qemu-img info /data/vmfs/tidb-vm1.qcow2image: /data/vmfs/tidb-vm1.qcow2file format: qcow2virtual size: 200G (214748364800 bytes) ###可以看到内存由1G加到了2Gdisk size: 2.4Gcluster_size: 65536Snapshot list:ID TAG VM SIZEDATE VM CLOCK1 1664466694 459M -09-29 23:51:34 00:01:52.471Format specific information:compat: 1.1lazy refcounts: true

6. 启动并进入虚拟机,以下在虚拟机中操作

df -h 查看更新前容量

开始分区:查看设备:fdisk -l

fdisk /dev/sda (因为我们是扩容的sda,所以在sda新增一个分区)

p查询分区

n创建分区,其他默认

p查询分区

t设置新创建的分区格式8e

p查询分区格式是否设置成功

w保存退出

partprobe重读分区表

创建物理卷扩容:

创建物理卷: pvcreate /dev/sda3

查询pv:pvs查询group:vgs扩容: vgextend centos /dev/sda3查看逻辑卷:lvdisplay扩容逻辑卷大小: lvextend -l +100%FREE /dev/centos/root更新修改到文件系统: xfs_growfs /dev/centos/root

查询更新后容量:df -h

前后对比,从6.5G已经扩容到17G,扩容容量10G左右,扩容成功。

3.6. 给raw盘缩容(生产上不要缩容,会丢失数据!!!)

[root@kvm data]# qemu-img info liyu.rawimage: liyu.rawfile format: rawvirtual size: 2.0G (2147483648 bytes)###可以看到原来是2G内存# 减掉1G内存:[root@kvm data]# qemu-img resize liyu.raw -1GImage resized.[root@kvm data]# qemu-img info liyu.rawimage: liyu.rawfile format: rawvirtual size: 1.0G (1073741824 bytes) ###缩容成功

3.7. 磁盘格式转化:

# -f指定将raw格式,转化为-O,qcow2格式, 后面跟着两个要转化的文件[root@kvm data]# qemu-img convert -f raw -O qcow2 liyu.raw liyu.qcow2 [root@kvm data]# ll -htotal 1.3G-rw-r--r-- 1 root root 193K May 1 17:03 liyu.qcow2-rw-r--r-- 1 root root 1.0G May 1 16:50 liyu.raw-rw------- 1 qemu qemu 11G May 1 17:03 nebula1.qcow2#格式转化完之后,要修改配置文件才起效:(以liyu举例)[root@kvm data]# virsh edit liyu#修改disk里的raw为qcow2,修改type的raw为qcow2。修改两处# 再开启它:[root@kvm data]# virsh start liyu# qcow2 convertqemu-img convert -O qcow2 old.qcow2 new.qcow2

3.8. 查看系统配置信息:

[root@localhost ~]# yum -y install libguestfs-tools-c[root@localhost ~]# virt-cat -a /var/lib/libvirt/images/centos7.0.qcow2 /etc/sysconfig/grub GRUB_TIMEOUT=5GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"GRUB_DEFAULT=savedGRUB_DISABLE_SUBMENU=trueGRUB_TERMINAL_OUTPUT="console"GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"GRUB_DISABLE_RECOVERY="true"

3.9. 在宿主机上查看kvm虚拟机centos7.0 的dns地址

root@localhost ~]# virt-edit -a /var/lib/libvirt/images/centos7.0.qcow2 /etc/resolv.conf # Generated by NetworkManager

3.10. 查看虚拟机存储状态

[root@localhost ~]# virt-df -h centos7.0文件系统 大小 已用空间 可用空间 使用百分比%centos7.0:/dev/sda1 1014M 105M 909M 11%centos7.0:/dev/centos/root3.5G 1.3G 2.2G 39%[root@localhost ~]#

四、虚拟机克隆

4.1 克隆虚拟机

# 1. 克隆# -o指定旧的虚拟机,-n指定新的虚拟机[root@localhost ~]# virt-clone \-o centos7.0 \-n centos7.1 \-f /var/lib/libvirt/images/centos7.1.qcow2正在分配 'centos7.1.qcow2' | 5.0 GB 00:00:24成功克隆 'centos7.1'。## usevirt-clone \-o openshift-vm1 \-n openshift-vm2 \-f /data/vmfs/openshift-vm2.qcow2# 2. 再看一下状态,发现有centos7.1[root@localhost ~]# virsh list --allId 名称状态-----------------------------------------------------centos7.0 关闭-centos7.1 关闭# 3. 开启centos7.1[root@localhost ~]# virsh start centos7.1域 centos7.1 已开始

4.2 修改新克隆虚拟机配置

进入新建虚拟机的命令行,执行如下命令:

hostnamectl set-hostname centos-7.1

vi /etc/sysconfig/network-scripts/ifcfg-ens3

修改完成后,重启虚拟机:virsh reboot centos-7.1

TYPE="Ethernet"PROXY_METHOD="none"BROWSER_ONLY="no"#BOOTPROTO="dhcp"DEFROUTE="yes"IPV4_FAILURE_FATAL="no"IPV6INIT="yes"IPV6_AUTOCONF="yes"IPV6_DEFROUTE="yes"IPV6_FAILURE_FATAL="no"IPV6_ADDR_GEN_MODE="stable-privacy"NAME="ens3"#UUID="a6fd0deb-887c-4752-8343-048f1b6f4236"DEVICE="ens3"ONBOOT="yes"IPADDR=192.168.0.120PREFIX=24GATEWAY=192.168.0.1DNS=114.114.114.114DNS1=114.114.114.114BOOTPROTO=static

虚拟机克隆命令参数详解:

[root@ren12 ~]# virt-clone --helpusage: virt-clone --original [NAME] ...复制一个虚拟机,需修改如 MAC 地址,名称等所有主机端唯一的配置。虚拟机的内容并没有改变:virt-clone 不修改任何客户机系统内部的配置,它只复制磁盘和主机端的修改。所以像修改密码,修改静态 IP 地址等操作都在本工具复制范围内。如何修改此类型的配置,请参考 virt-sysprep(1)。optional arguments:-h, --help show this help message and exit--version show program's version number and exit--connect URI 通过 libvirt URI 连接到虚拟机管理程序通用选项:-o ORIGINAL_GUEST, --original ORIGINAL_GUEST原始客户机名称;必须为关闭或者暂停状态。--original-xml ORIGINAL_XML将 XML 文件用于原始客户机。--auto-clone从原始客户机配置中自动生成克隆名称和存储路径。-n NEW_NAME, --name NEW_NAME新客户机的名称--reflink 使用 btrfs COW 轻量副本存储配置:-f NEW_DISKFILE, --file NEW_DISKFILE为新客户机使用新的磁盘镜像文件--force-copy TARGET 强制复制设备(例如:如果 'hdc'是只读光驱设备,则使用 --force-copy=hdc)--nonsparse 不使用稀疏文件作为克隆的磁盘镜像--preserve-data 不克隆存储,通过 --file参数指定的新磁盘镜像将保留不变--nvram NEW_NVRAMNew file to use as storage for nvram VARS联网配置:-m NEW_MAC, --mac NEW_MAC为克隆客户机生成新的固定 MAC地址。默认为随机生成 MAC。其它选项:--replace 不检查命名冲突,覆盖任何使用相同名称的客户机。--print-xml 打印生成的 XML 域,而不是创建客户机。--check CHECK 启用或禁用验证检查。例如:--check path_in_use=off--check all=off-q, --quiet 抑制非错误输出-d, --debug 输入故障排除信息请参考 man 手册,以便了解示例和完整的选项语法。

五、快照管理

定时创建和删除虚拟机快照,请参考:KVM虚拟机定时创建和删除快照

5.1. 创建快照

快照会保存在磁盘文件里,所以每创建一个快照,磁盘文件的内存就会越变越大,

快照保存目录:/var/lib/libvirt/qemu/snapshot/{虚拟机名称}/

# 创建快照(理解成VMware的快照)[root@localhost ~]# virsh snapshot-create centos7.0已生成域快照 1656584049# 根据默认生成的快照名字(默认以时间戳为名字命名快照), 查看快照创建日期[root@localhost ~]# date -d @1664466694Thu Sep 29 23:51:34 CST

自定义快照名称:

[root@localhost ~]# virsh snapshot-create-as gitlab-vm --name gitlab-vm-`date +%Y%m%d%H%M%S`Domain snapshot gitlab-vm-1021120551 created[root@localhost ~]# virsh snapshot-list gitlab-vmName Creation Time State------------------------------------------------------------gitlab-vm-1021120551 -10-21 12:05:51 +0800 running# 快照默认存储路径: /var/lib/libvirt/qemu/snapshot/gitlab-vm/gitlab-vm-1009-0933.xml

5.2. 删除快照

[root@localhost ~]# virsh snapshot-delete centos7.0 1656584049 已删除域快照 1656584049

5.3. 查看快照时间

[root@localhost ~]# virsh snapshot-list centos7.0名称生成时间 状态------------------------------------------------------------1656584049 -06-30 18:14:09 +0800 shutoff1656584521 -06-30 18:22:01 +0800 shutoff

5.4. 应用快照

[root@localhost ~]# virsh snapshot-revert centos7.0 1656584049

恢复到指定的版本1475214943,没有任何输出则代表恢复成功:

[root@kvm ~]# virsh snapshot-revert snale 1475214943

确定是否恢复到了指定版本:

[root@kvm ~]#virsh snapshot-current snale|grep creationTime<creationTime>1475214943</creationTime>

5.5. 查看快照信息

[root@localhost ~]# virsh snapshot-current centos7.0

[root@localhost ~]# virsh snapshot-current centos7.0<domainsnapshot><name>1656584049</name><state>shutoff</state><creationTime>1656584049</creationTime><memory snapshot='no'/><disks><disk name='vda' snapshot='internal'/><disk name='hda' snapshot='no'/></disks><domain type='kvm'><name>centos7.0</name><uuid>8f9cd4d6-a1d1-4296-bf6e-379682af6b05</uuid><memory unit='KiB'>1048576</memory><currentMemory unit='KiB'>1048576</currentMemory><vcpu placement='static'>1</vcpu><resource><partition>/machine</partition></resource><os><type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type></os><features><acpi/><apic/></features><cpu mode='custom' match='exact' check='full'><model fallback='forbid'>Broadwell-IBRS</model><feature policy='disable' name='md-clear'/><feature policy='disable' name='ssbd'/><feature policy='disable' name='hle'/><feature policy='disable' name='rtm'/><feature policy='require' name='spec-ctrl'/><feature policy='require' name='hypervisor'/><feature policy='require' name='xsaveopt'/></cpu><clock offset='utc'><timer name='rtc' tickpolicy='catchup'/><timer name='pit' tickpolicy='delay'/><timer name='hpet' present='no'/></clock><on_poweroff>destroy</on_poweroff><on_reboot>restart</on_reboot><on_crash>destroy</on_crash><pm><suspend-to-mem enabled='no'/><suspend-to-disk enabled='no'/></pm><devices><emulator>/usr/libexec/qemu-kvm</emulator><disk type='file' device='disk'><driver name='qemu' type='qcow2'/><source file='/var/lib/libvirt/images/centos7.0.qcow2'/><target dev='vda' bus='virtio'/><boot order='1'/><address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/></disk><disk type='file' device='cdrom'><driver name='qemu' type='raw'/><target dev='hda' bus='ide'/><readonly/><boot order='2'/><address type='drive' controller='0' bus='0' target='0' unit='0'/></disk><controller type='usb' index='0' model='ich9-ehci1'><address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x7'/></controller><controller type='usb' index='0' model='ich9-uhci1'><master startport='0'/><address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/></controller><controller type='usb' index='0' model='ich9-uhci2'><master startport='2'/><address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/></controller><controller type='usb' index='0' model='ich9-uhci3'><master startport='4'/><address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/></controller><controller type='ide' index='0'><address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/></controller><controller type='virtio-serial' index='0'><address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/></controller><controller type='pci' index='0' model='pci-root'/><interface type='network'><mac address='52:54:00:8d:e0:b0'/><source network='default'/><model type='virtio'/><boot order='3'/><address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/></interface><serial type='pty'><target type='isa-serial' port='0'><model name='isa-serial'/></target></serial><console type='pty'><target type='serial' port='0'/></console><channel type='unix'><target type='virtio' name='org.qemu.guest_agent.0'/><address type='virtio-serial' controller='0' bus='0' port='1'/></channel><channel type='spicevmc'><target type='virtio' name='com.redhat.spice.0'/><address type='virtio-serial' controller='0' bus='0' port='2'/></channel><input type='tablet' bus='usb'><address type='usb' bus='0' port='1'/></input><input type='mouse' bus='ps2'/><input type='keyboard' bus='ps2'/><graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1' keymap='en-us'><listen type='address' address='127.0.0.1'/></graphics><sound model='ich6'><address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/></sound><video><model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/><address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/></video><redirdev bus='usb' type='spicevmc'><address type='usb' bus='0' port='2'/></redirdev><redirdev bus='usb' type='spicevmc'><address type='usb' bus='0' port='3'/></redirdev><memballoon model='virtio'><address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/></memballoon></devices><seclabel type='dynamic' model='selinux' relabel='yes'/><seclabel type='dynamic' model='dac' relabel='yes'/></domain></domainsnapshot>

六、 虚拟机热添加及热修改

kvm虚拟化管理工具

所谓热添加就是在开机状态下添加

6.1、在线热添加硬盘(含临时,永久添加及剥离硬盘)

先给nebula1的磁盘加5G容量

[root@kvm data]# qemu-img create -f qcow2 nebula1-add.qcow2 5G

然后再来创建磁盘(源虚拟机,源虚拟机磁盘路径,目标磁盘)

[root@kvm data]# virsh attach-disk nebula1 /data/nebula1-add.qcow2 vdbDisk attached successfully

此时vdb磁盘已经添加成功,在kvm虚拟机里执行fdisk -l就可以看到添加的磁盘

可是为什么刚才明明加了5G内容,vdb上面却显示0MB呢?因为添加硬盘的时候要指定磁盘类型,不指定默认为raw类型,qcow2的磁盘肯定显示不了raw磁盘的大小,所以我们现在要把添加的硬盘剥离出来,使用以下命令:(临时剥离硬盘),要想永久剥离硬盘和下面永久添加硬盘一样,加上- -config参数

[root@kvm data]# virsh detach-disk nebula1 vdbDisk detached successfully

然后要指定添加硬盘的类型为qcow2,添加最后一个参数

[root@kvm images]# virsh attach-disk nebula1 /data/nebula1-add.qcow2 vdb --subdriver qcow2Disk attached successfully

这下再进到kvm虚拟机里fdisk -l就可以看到刚才添加的5G硬盘容量了

但是!在我们强制关机之后,再次开机进入到kvm虚拟机以后,添加的硬盘又不见了,因为这只是临时添加,所以我们想要以后开机添加的硬盘永久存在就得永久添加,加上下列参数即可。

[root@kvm images]# virsh attach-disk nebula1 /data/nebula1-add.qcow2 vdb --subdriver qcow2 --config

- -config这个参数是在下次重启的时候才生效。

要想这块添加的盘能用还得格式化(mkfs.xfs /dev/vdb),再进行挂载,查看

6.2、在线扩容

在进行扩容时,必须要将刚才挂载的硬盘卸载掉

umount /mnt

还要把刚才添加的硬盘剥离出来

[root@kvm images]# virsh detach-disk nebula1 vdbDisk detached successfully

再给它进行扩容然后进行查看,磁盘容量已经由我们添加的5G变为8G了

[root@kvm images]# qemu-img resize /data/nebula1-add.qcow2 +3GImage resized.[root@kvm images]# qemu-img info /data/nebula1-add.qcow2image: /data/nebula1-add.qcow2file format: qcow2virtual size: 8.0G (8589934592 bytes)disk size: 16Mcluster_size: 65536Format specific information:compat: 1.1lazy refcounts: false

现在再将磁盘添加上去

[root@kvm images]# virsh attach-disk nebula1 /data/nebula1-add.qcow2 vdb --subd river qcow2Disk attached successfully

再在kvm虚拟机的磁盘挂载上去,我们看到之前拷贝的文件也没有丢失,但是df -h时看到磁盘还是5G,并没有更新到8G

为什么呢?因为我们在格式化时已经规定了inode和block大小(超级块),但是我们扩容的盘在后面,所以得更新一下超级块信息

用以下命令更新超级块信息,可以看到block信息已经更新了

再次df -h查看,由5G变成8G了

如果是ext3/ext4文件系统,则用resize2fs /dev/vdb来更新

扩容的思路:

6.3、在线热添加网卡(含移除网卡)

添加- -model virtio参数,附加的网卡就是按顺序来的,eth0,eth1

[root@kvm ~]# virsh attach-interface nebula1 --type bridge --source virbr0 --model virtioInterface attached successfully

在kvm虚拟机里可以查看IP,看到新增网卡eth1

移除网卡:

[root@kvm images]# virsh detach-interface nebula1 --type bridgeerror: Domain has 2 interfaces. Please specify which one to detach using --macerror: Failed to detach interface

上述方式移除网卡会提示有两块接口都是bridge类型,需要指定mac地址进行移除,看到上图eth1的mac地址为52:54:00:2f:f2:5f,所以加上mac参数

[root@kvm images]# virsh detach-interface nebula1 --type bridge --mac 52:54:00:2f:f2:5fInterface detached successfully

可以看到kvm虚拟机里的eth1没了,移除网卡成功

这只是临时修改,要想永久生效还得加上- -config参数

[root@kvm images]# virsh detach-interface nebula1 --config --type bridge --mac 52:54:00:2f:f2:5fInterface detached successfully

6.4、在线热添加内存

注意这里刚开始安装的Kvm虚拟机要指定现有内存以及最大内存,才方便以后添加内存

virt-install --name=nebula --virt-type=kvm --memory 800,maxmemory=1024 --vcpus 1 --cdrom=/home/iso/CentOS-7-x86_64-Minimal-1708.iso --disk path=/home/images/nebula.qcow2,size=10 --network bridge=br0 --graphicsvnc --vncport=15950 --vnclisten=0.0.0.0 --noautoconsole --autostart

添加内存的命令如下

[root@kvm images]# virsh setmem nebula1 1024M

在kvm虚拟机里查看内存容量,就看到增加了

但是如果我们安装kvm虚拟机的时候没有指定最大内存,那么就要进入到配置文件里面修改了,你可以将单位修改为MIB,保存文件再次进入时系统就会自动转换单位为KB

[root@kvm images]# vim nebula1.xml<memory unit='KiB'>1048576</memory><currentMemory unit='KiB'>819200</currentMemory>

如果修改的虚拟机处于运行状态,那么修改的配置文件是不会生效的,需要重启才会生效。

6.5、在线热添加cpu

用下列命令安装kvm虚拟机的时候,就有添加cpu信息,最大cpu数量

virt-install \--name=nebula \--virt-type=kvm \--memory 800,maxmemory=1024 \--vcpus 1,maxvcpus=10 \--cdrom=/home/iso/CentOS-7-x86_64-Minimal-1708.iso \--disk path=/home/images/nebula.qcow2,size=10 \--network bridge=br0 \--graphicsvnc \--vncport=15950 \--vnclisten=0.0.0.0 \--noautoconsole \--autostart

然后再设置CPU数量,将nebula1的CPU调整为2

[root@kvm images]# virsh setvcpus nebula1 2

再lscpu查看就可以看到两个cpu了,原来只有0,现在是0和1

那么cpu可以加,可以减吗?我们试一下,可以看到之前的cpu数量是6,现在设置为2,提示不支持CPU回收

6.6、kvm虚拟机的热迁移

热迁移的原理如下:

想将一台正在运行的虚拟机迁移过去必须借助共享存储,将磁盘挂载到共享存储上,谁想用就挂载

三台机器的环境配置如下:

在nfs主机上的文件配置如下命令

[root@nfs01 data]# vim /etc/exports#shared dir/data 192.168.119.0/24(rw,async,no_root_squash,no_all_squash)

在两边kvm虚拟机上进行测试

[root@kvm ~]# showmount -e 192.168.119.129Export list for 192.168.119.129:[root@backup ~]# showmount -e 192.168.119.146Export list for 192.168.119.146:

两台宿主机上将磁盘文件所在路径挂载到nfs的/data目录

[root@kvm images]# cd /data/[root@kvm data]# lsnebula1.qcow2 nebula1.xml[root@kvm data]# mount -t nfs 192.168.119.7:/data /home/images[root@kvm data]# mv /data/nebula1.qcow2 /home/images/[root@backup ~]# mount -t nfs 192.168.119.7:/data /home/images/

再在nfs主机上进行查看

[root@nfs01 data]# ll -htotal 1.7G-rw-------. 1 107 107 11G May 6 nebula1.qcow2qemu放入UID就是107

再在第一台主机上安装kvm虚拟机

[root@kvm data]# virt-install \--virt-type kvm \--os-type=linux \--name web01 \--memory 1024 \--vcpus 1,maxvcpus=10 \--cdrom=/home/iso/CentOS-7-x86_64-Minimal-1708.iso \--disk /home/images/web01.qcow2,size=10 \--network bridge=virbr0 \--graphics vnc,listen=0.0.0.0 \--noautoconsole \--check disk_size=offStarting install...Allocating 'web01.qcow2' | 10 GB 00:00:00Domain installation still in progress. You can reconnect to the console to complete the installation process.

在第二台主机上查看有磁盘文件,证明nfs共享存储成功

[root@backup ~]# ls /home/images/nebula1.qcow2 web01.qcow2

在两台主机上做好host解析,添加如下内容

192.168.119.146 kvm192.168.119.129 backup

然后在kvm上面做迁移,再次进行查看virsh list,kvm虚拟机都不在了,被迁移到backup上去了

[root@kvm data]# virsh migrate \--live \--verbose web01 qemu+ssh://192.168.119.129/system \--unsafe

参数解释:

migrate迁移的意思

- -live运行的状态迁移

- -verbose迁移哪一台Kvm虚拟机

qemu+ssh://192.168.119.129/systemqemu+ssh认证,IP为backup的IP

- -unsafe这个参数一定要加,不然会报错,不让迁移

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。