ubuntu centos 安装kvm

来源:互联网 发布:电气工程算量软件 编辑:程序博客网 时间:2024/06/05 23:52

**

一、安装

**
sudo apt-get install kvm qemu
sudo apt-get install virtinst python-libvirt virt-viewer virt-manager

启动LVM
sudo virt-manager

二、创建虚拟机

创建空硬盘,相当于物理机的硬盘(公司常用的qcow2格式)

[root@localhost kvm]# qemu-img create -f qcow2 /home/kvm/centos72.qcow2 50G   Formatting '/home/kvm/centos72.qcow2', fmt=qcow2 size=53687091200 encryption=off cluster_size=65536 

virt-install方式安装

virt-install --virt-type kvm --name kvm-demo --boot network,cdrom,menu=on --ram 1024 --cdrom=/home/kvm/CentOS-7.iso --graphics vnc,listen=0.0.0.0 --noautoconsole --os-type=linux --os-variant=rhel7 --accelerate --disk path=/home/kvm/kvm-demo2.qcow2,size=5,format=qcow2,bus=virtio --bridge=br0,model=virtio --autostart

–virt-type
–name kvm-demo
新客户虚拟机实例名字,在连接的hypervisor已知的所有虚拟机中必须唯一,包括那些当前未活动的虚拟机。( Name of the new guest virtual machine instance. This must be unique amongst all guests known to the hypervisor on the connection, including those not currently active. )
–boot
–ram
–os-type
–cdrom
–graphics vnc,listen=0.0.0.0
–noautoconsole
–os-variant
–accelerate
–disk path=/home/kvm/kvm-demo2.qcow2,size=5,format=qcow2,bus=virtio
–bridge=br0,model=virtio
–autostart

三、常用管理命令

查看虚拟运行状态

[root@localhost jingpan]# virsh list --all Id    名称                         状态---------------------------------------------------- 9     centos7                        running

开启虚拟机
virsh start centos7
关闭虚拟机
virsh shudown centos7
强制关闭虚拟机
virsh destroy centos7
配置系统在开机时启动

[root@localhost jingpan]# virsh autostart  centos72         域 centos72标记为自动开始
[root@localhost jingpan]# virsh autostart --disable centos72域 centos72取消标记为自动开始

挂起:

[root@kvm01 ~]# virsh suspend vm1
恢复:

[root@kvm01 ~]# virsh resume vm1

四、将虚拟机重命名

比如我们希望将服务器上的虚拟机pc_vm94 改名称为 vm94
1)使用 virsh list –all命令查看当前都哪些虚拟机。

 # virsh list --all     

2) 导出原有虚拟机的配置文件
在导出配置之前使用virsh shutdown centos7命令将虚拟机关闭。

#virsh shutdown centos7

转到/etc/libvirt/qemu 目录下对原来的虚拟机配置进行导出
virsh dumpxml centos7 > centos74.xml
(注意>前面并没有.xml ,这是因为我们需要导出虚拟机的配置,将配置保存到一个xml文件中 )

3)编辑centos74.xml
导出之后我们需要将虚拟机的文件修改一下,centos7改为centos74

<domain type='kvm'>  <name>centos74</name>  <uuid>d1f6ab4b-ee50-e7e2-57b3-bfa1b619f327</uuid>  <memory unit='KiB'>1048576</memory>

至于虚拟机所对应的磁盘文件。可以沿用之前的,不需要任何修改;也可以改成与名称一直的。如果虚拟机对应的磁盘文件修改的话,必须也要在xml文件中做对应的修改。

4)虚拟机的重新定义
使用virsh undefine 命令将之前的虚拟机删除

[root@localhost jingpan]# virsh undefine centos7域 centos7 已经被取消定义

使用virsh define 命令将重新定义新的虚拟机

[root@localhost jingpan]# virsh define centos74.xml 定义域 centos74(从 centos74.xml)

5)这时候使用命令就可以看到已经将原来的虚拟机重新更改为另一个名称

[root@localhost jingpan]# virsh list --all Id    名称                         状态---------------------------------------------------- -     centos74                       关闭

经过上述几步的处理,仅仅是将虚拟机的名称改变了一下,其余都没有发生变化。也就是对虚拟机进行了重命名

五、快照操作

这里用之前克隆的虚拟机vm1-clone进行快照操作
注:

  • 快照实际上做的是虚拟机的XML配置文件,默认快照XML文件在/var/lib/libvirt/qemu/snapshot/虚拟机名/下
  • .快照只支持qcow2磁盘格式的系统

1、虚拟机创建快照

[root@localhost ch4]# virsh snapshot-create centos72Domain snapshot 1511947835 created

或创建快照的同时,再创建一个快照别名

[root@localhost ch4]# virsh snapshot-create-as centos74 install_finishedDomain snapshot install_finished created

2、查看虚拟机历史快照信息

[root@localhost ch4]# virsh snapshot-list centos72 名称               Creation Time             状态------------------------------------------------------------ 1511920366           2017-11-29 09:52:46 +0800 running 1511947835           2017-11-29 17:30:35 +0800 shutoff[root@localhost ch4]# virsh snapshot-list centos74 名称               Creation Time             状态------------------------------------------------------------ install_finished     2017-11-29 17:29:11 +0800 shutoff

3、查看虚拟机磁盘信息

[root@localhost kvm]# qemu-img info kvm-demo2.qcow2 image: kvm-demo2.qcow2file format: qcow2virtual size: 50G (53687091200 bytes)disk size: 5.5Gcluster_size: 65536Snapshot list:ID        TAG                 VM SIZE                DATE       VM CLOCK1         install_finished          0 2017-11-29 17:29:11   00:00:00.000

可以看到里面已经含有虚拟机快照信息install_finished
四、查看当前快照信息

[root@localhost kvm]# virsh snapshot-current centos74  <domainsnapshot>  <name>install_finished</name>  <state>shutoff</state>  <creationTime>1511947751</creationTime>  <memory snapshot='no'/>  <disks>    <disk name='vda' snapshot='internal'/>    <disk name='hdc' snapshot='no'/>  </disks>  <domain type='kvm'>    <name>centos74</name>    <uuid>d1f6ab4b-ee50-e7e2-57b3-bfa1b619f327</uuid>    <memory unit='KiB'>1048576</memory>    <currentMemory unit='KiB'>1048576</currentMemory>    <vcpu placement='static'>1</vcpu>    <os>      <type arch='x86_64' machine='rhel6.6.0'>hvm</type>      <boot dev='network'/>      <boot dev='cdrom'/>      <bootmenu enable='yes'/>    </os>    <features>      <acpi/>      <apic/>      <pae/>    </features>    <clock offset='utc'/>    <on_poweroff>destroy</on_poweroff>    <on_reboot>restart</on_reboot>    <on_crash>restart</on_crash>    <devices>      <emulator>/usr/libexec/qemu-kvm</emulator>      <disk type='file' device='disk'>        <driver name='qemu' type='qcow2' cache='none'/>        <source file='/home/kvm/kvm-demo2.qcow2'/>        <target dev='vda' bus='virtio'/>        <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>      </disk>      <disk type='block' device='cdrom'>        <driver name='qemu' type='raw'/>        <target dev='hdc' bus='ide'/>        <readonly/>        <address type='drive' controller='0' bus='1' target='0' unit='0'/>      </disk>      <controller type='usb' index='0' model='ich9-ehci1'>        <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>      </controller>      <controller type='usb' index='0' model='ich9-uhci1'>        <master startport='0'/>        <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>      </controller>      <controller type='usb' index='0' model='ich9-uhci2'>        <master startport='2'/>        <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>      </controller>      <controller type='usb' index='0' model='ich9-uhci3'>        <master startport='4'/>        <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>      </controller>      <controller type='ide' index='0'>        <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>      </controller>      <interface type='bridge'>        <mac address='52:54:00:57:2a:5c'/>        <source bridge='br0'/>        <model type='virtio'/>        <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>      </interface>      <serial type='pty'>        <target port='0'/>      </serial>      <console type='pty'>        <target type='serial' port='0'/>      </console>      <input type='tablet' bus='usb'/>      <input type='mouse' bus='ps2'/>      <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>        <listen type='address' address='0.0.0.0'/>      </graphics>      <video>        <model type='cirrus' vram='9216' heads='1'/>        <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>      </video>      <memballoon model='virtio'>        <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>      </memballoon>    </devices>  </domain></domainsnapshot>

5、恢复到之前快照

[root@localhost kvm]# virsh snapshot-revert  centos74 install_finished

六、删除指定快照

[root@localhost kvm]# virsh snapshot-delete  centos74 install_finished

六 增加虚拟硬盘

1:创建一个新的qcow2镜像文件,使用qemu-img命令

qemu-img create -f qcow2 centos74.qcow2 20G

2:修改虚拟机配置信息
使用virsh命令进入管理系统
使用edit [虚拟机]编辑虚拟机配置信息

[root@localhost kvm]# virsh欢迎使用 virsh,虚拟化的交互式终端。输入:'help' 来获得命令的帮助信息       'quit' 退出virsh # edit centos74

在后增加一块磁盘信息,
centos74.qcow2增加的硬盘文件
dev=’vdb’,不能上之前的相同。slot改为没使用的。

<disk type='file' device='disk'>      <driver name='qemu' type='qcow2' cache='none'/>      <source file='/home/kvm/centos74.qcow2'/>      <target dev='vdb' bus='virtio'/>      <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>    </disk>

重启虚拟机,出现设备vdb

[root@localhost ~]# ll /dev/vd*brw-rw----. 1 root disk 252,  0 11月 30 16:02 /dev/vdabrw-rw----. 1 root disk 252,  1 11月 30 16:02 /dev/vda1brw-rw----. 1 root disk 252,  2 11月 30 16:02 /dev/vda2brw-rw----. 1 root disk 252, 16 11月 30 16:02 /dev/vdb

3、分区

[root@localhost ~]# fdisk /dev/vdb欢迎使用 fdisk (util-linux 2.23.2)。更改将停留在内存中,直到您决定将更改写入磁盘。使用写入命令前请三思。Device does not contain a recognized partition table使用磁盘标识符 0xa360f26f 创建新的 DOS 磁盘标签。命令(输入 m 获取帮助):nPartition type:   p   primary (0 primary, 0 extended, 4 free)   e   extendedSelect (default p): p分区号 (1-4,默认 1):起始 扇区 (2048-41943039,默认为 2048):将使用默认值 2048Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):+1G分区 1 已设置为 Linux 类型,大小设为 1 GiB命令(输入 m 获取帮助):wThe partition table has been altered!Calling ioctl() to re-read partition table.正在同步磁盘。 

4:格式化磁盘

[root@localhost ~]# mkfs.ext4 /dev/vdb1

5:挂载磁盘到/mnt/vdb1下

[root@localhost ~]# mkdir /mnt/vdb1  [root@localhost ~]# mount /dev/vdb1 /mnt/vdb1/

参考连接:http://www.cnblogs.com/liang-wei/p/6732055.html
http://duxiansheng.blog.51cto.com/11062970/1737585
http://blog.csdn.net/hnhuangyiyang/article/details/50886844

原创粉丝点击