openstack-KVM管理工具(7)

 

一、 virsh

  通过libvirt API管理Hpervisor、node、domain,实现多数功能调用。

  即统一管理多台计算机上的域。

1、管理其他服务器(node)

  (1)修改配置文件:vim /etc/libvirt/libvirt.conf

uri_aliases = [
  "work=qemu+ssh://[email protected]/system",
]

  (2)管理其他服务器上的域:virsh connect work           

  (3)如果不修该配置文件:virsh -c qemu+ssh://[email protected]/system

2、virsh可连接的命令

  (1)格式:virsh 参数

virsh uri                    连接的路径qemu:///system
virsh list                   显示正在运行的虚拟机
virsh list --all             显示所有虚拟机
virsh nodeinfo               显示当前节点信息
virsh hostname               显示当前节点主机名
virsh version                显示当前libvirt版本
virsh iface-list             显示虚拟网络接口信息
virsh iface-mac br0          显示对应网卡物理地址
virsh net-info default       显示默认网络信息
virsh net-uuid default       显示默认网络的UUID

  

  (2)格式:virsh 参数 域名/ID

virsh domname                   域名
virsh vcpucount                 域vcpu数量
virsh vcpuinfo                  域vcpu信息
virsh dommemstat                域内存状态
virsh console                   连接到对应的域
virsh start                     连接到对应的域
virsh destroy                   关闭对应的域
virsh reboot                    重启对应的域

  

  (3)存储块的信息Storage Volume(服务器上操作)

virsh domname                   域名
virsh vcpucount                 域vcpu数量
virsh vcpuinfo                  域vcpu信息
virsh dommemstat                域内存状态
virsh console                   连接到对应的域
virsh start                     连接到对应的域
virsh destroy                   关闭对应的域
virsh reboot                    重启对应的域

      (4)查看域是否支持KVM

virsh qemu-monitor-command chen(ID) --hmp "info kvm"
virsh qemu-monitor-command chen(ID) --hmp info kvm

3、虚拟机快照

snapshot (help keyword "snapshot"):virsh help snapshot
virsh snapshot-create           使用XML生成快照
virsh snapshot-dumpxml          为域快照转储XML
virsh snapshot-parent           获取快照的上级快照名称
virsh snapshot-create-as        使用一组参数生成快照
virsh snapshot-edit             编辑快照XML
virsh snapshot-revert           将域转换为快照
virsh snapshot-current          获取或者设定当前快照
virsh snapshot-info             快照信息
virsh snapshot-delete           删除域快照
virsh snapshot-list             为域列出快照


virsh snapshot-create-as chen --name kuaizhoa1 --description 1
virsh snapshot-list chen
virsh snapshot-info chen kuaizhao1

4、添加设备

  (1)添加网卡

virsh attach-interface 域名 --type bridge --source br0
virsh attach-interface 域名 --type bridge --source br0 --config

  (2)添加磁盘

qemu-img create -f qcow2 /var/lib/libvirt/images/haha.qcow2 2G
virsh attach-disk chen /var/lib/libvirt/images/haha.qcow2 vdb --subdriver qcow2 --cache none --config --live

注:--subdriver:指定磁盘类型为qcow2
  --config:永久添加
  --live:激活

      (3)添加iSCSI磁盘

<disk type='block' device='disk'>
  <driver name='qemu' type='raw' cache='none'/>
  <source dev='/dev/disk/by-path/ip-9.144.44.160:3260-iscsi-iqn.1986-03.com.ibm:2145.flex-v7000-9.144.44.11.node1-lun-0'/>
  <target dev='vdb' bus='virtio'/>
</disk>

virsh attach-device $domain scsi1.xml --live --persistent

5、连接镜像文件

virsh qemu-monitor-command chen --hmp info block
virsh qemu-monitor-command chen --hmp change drive-ide0-0-0 /var/lib/libvirt/images/CentOS-7-x86_64-DVD-1804.iso
virsh qemu-monitor-command chen --hmp eject drive-ide0-0-0

注:change:挂载
   eject:弹出

6、删除虚拟机

virsh undefine chen --storge /var/lib/libvirt/images/chen.qcow2

二、 virt命令管理

1、虚拟机工具

  安装软件:yum install libguestfs-tools -y

virt-top
virt-df /var/lib/libvirt/images/chen.qcow2           域磁盘信息
virt-ls /var/lib/libvirt/images/chen.qcow2 /         显示域的根分区
virt-filesystems -d chen                             显示域为chen的文件系统

 
将域中的passwd拷贝到本机/mnt下(-a指定路径,-d指定域)
virt-copy-out -a /var/lib/libvirt/images/chen.qcow2 /etc/passwd /mnt
virt-copy-out -d chen /etc/passwd /mnt

2、清理镜像

virt-sysprep --list-operations                 显示可以清理的镜像文件
virt-sysprep -d 域 --operations 镜像            在指定域中清除指定镜像

镜像名有以下:
pacct-log,pam-data,crash-data,bash-history,ssh-hostkeys,logfiles,udev-persistent-net,tmp-files,dhcp-client-state,puppet-data-log,net-hostname,net-hwaddr 

3、硬盘启动并安装系统

      创建镜像:qemu-img create -f qcow2 /var/lib/libvirt/images/chen.qcow2 20G

virt-install \
--name chen \
--memory 4096 \
--cpu host-passthrough \
--vcpus 4,maxvcpus=8,sockets=2,cores=2,threads=2 \
--disk path=/var/lib/libvirt/images/chen.qcow2 \
--location /var/lib/libvirt/images/CentOS-7-x86_64-DVD-1804.iso \
--os-type linux \
--os-variant rhel7 \
--network bridge=br0 \
--graphics none \
--console pty,target_type=serial \
--extra-args 'console=ttyS0,115200n8 serial'

4、克隆虚拟机

virt-clone --connect=qemu:///system -o chen -n ke -f /var/lib/libvirt/images/ke.qcow2
ls -lh /etc/libvirt/qemu/chen.xml /etc/libvirt/qemu/ke.xml
grep "mac address" /etc/libvirt/qemu/chen.xml /etc/libvirt/qemu/ke.xml
grep "source file" /etc/libvirt/qemu/chen.xml /etc/libvirt/qemu/ke.xml
ls -lh /var/lib/libvirt/images/chen.qcow2 /var/lib/libvirt/images/ke.qcow2

注:-o:指定域
  -n:指定克隆后的域
  -f:指定克隆后域的磁盘镜像
  mac address:MAC地址信息
  source file:域指定的磁盘来源

三、 qemu-img命令管理

1、镜像快照

qemu-img snapshot -l ke.qcow2
ll /var/lib/libvirt/images/ke.qcow2
qemu-img snapshot -c kuaizhao /var/lib/libvirt/images/ke.qcow2
ll /var/lib/libvirt/iamges/ke.qcow2

qemu-img snapshot -a kuaizhao /var/lib/libvirt/images/ke.qcow2
qemu-img snapshot -d kuaizhao /var/lib/libvirt/images/ke.qcow2

NOTE:snapshot is the name of the snapshot to create,apply or delete
-a:applies a snapshot (revert disk to saved state)        应用快照
-c:creates a snapshot                                     创建快照
-d:deletes a snapshot                                     删除快照
-l:lists all snapshots in the given image                 显示所有快照

四、Console connection

(1)vim /etc/securetty

  文件添加:ttyS0

(2)vim /etc/grub2.cfg

  在第一个linux16行末尾添加:console=ttyS0

(3)echo "S0:1234:respawn:/sbin/agetty/ ttyS0 115200" >> /etc/inittab

      此处信息也可不用添加

(4)宿主机连接虚拟机

  virsh console 域名

猜你喜欢

转载自www.cnblogs.com/chenli90/p/10349891.html