231321 发表于 2016-3-21 08:37:45

kvm创建虚拟机



1.启动或重启libvirtd服务和messagebus 服务
/etc/init.d/libvirtd start
/etc/init.d/messagebus restart

此时可以查看网络接口列表,表示正常
brctl show 结果如下:
bridge name   bridgeid               STPenabled   interfaces
br0            8000.000c2955a70a       no             eth0
virbr0         8000.52540014efd5       yes            virbr0-nic


2. 创建虚拟机
mkdir /data/   #创建一个存储虚拟机虚拟磁盘的目录,该目录所在分区必须足够大

virt-install \
--nameaming1 \
--ram 512 \
--disk path=/data/aming1.img,size=30 \
--vcpus 1 \
--os-type linux \
--os-variant rhel6 \
--network bridge=br0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://mirrors.163.com/centos/6.7/os/i386/' \
--extra-args 'console=ttyS0,115200n8 serial'


说明:
--name指定虚拟机的名字
--ram 指定内存分配多少
--disk path 指定虚拟磁盘放到哪里,size=30 指定磁盘大小为30G,这样磁盘文件格式为raw,raw格式不能做快照,后面有说明,需要转换为qcow2格式,如果要使用qcow2格式的虚拟磁盘,需要事先创建qcow2格式的虚拟磁盘。参考http://www.361way.com/kvm-qcow2-preallocation-metadata/3354.html   示例:qemu-img create -f qcow2 -opreallocation=metadata/data/test02.img 7G;--diskpath=/data/test02.img,format=qcow2,size=7,bus=virtio
--vcpus 指定分配cpu几个
--os-type 指定系统类型为linux
--os-variant 指定系统版本
--network指定网络类型
--graphics 指定安装通过哪种类型,可以是vnc,也可以没有图形,在这里我们没有使用图形直接使用文本方式
--console 指定控制台类型
--location 指定安装介质地址,可以是网络地址,也可以是本地的一个绝对路径,(--location '/mnt/', 其中/mnt/下就是我们挂载的光盘镜像mount /dev/cdrom /mnt)如果是绝对路径,那么后面还需要指定一个安装介质,比如NFS,假如虚拟机设置ip后,不能连外网,那么就会提示让我们选择安装途径:                     
                     ┌───┤ Installation Method ├───┐
                     │                                                       │
                     │ What type of mediacontains               │
                     │ the installationimage?                     │
                     │                                                       │
                     │       Local CD/DVD                            │
                     │       Hard drive                                 
                     │       NFS directory                           │
                     │       URL                                          │
                     │                                                       │
                     │   ┌────┐       ┌──────┐│
                     │   │ OK      │       │ Back            ││
                     │   └────┘       └──────┘│
                     │                                                       │
                     │                                                       │
                     └─────────────────┘

我在这里选择NFS,设置参数如下:
    ┌───────────────────────────┤ NFS Setup ├─────────
   │                                                                                          │
   │ Please enter theserver and NFSv3 path to your CentOS installation   │
   │ image and optionallyadditional NFS mount options.                     │
   │                                                                                           │
   │      NFS server name:             172.7.15.3______________             │
   │      CentOS directory:            /mnt/images/install.img_                │
   │      NFS mount options (optional): ro______________________      │


--extra-args 设定内核参数

当按下回车后,稍等几秒钟就可以看到安装提示了。
开始安装......
搜索文件 .treeinfo......                           |720 B    00:00 ...
搜索文件 vmlinuz......                           | 7.7 MB    00:02 ...
搜索文件 initrd.img......                        |63 MB    00:23 ...
创建存储文件 centos6.6_1.img                     |30 GB   00:00
创建域......                                       |    0 B   00:00
连接到域 centos6.6_1
Escape character is ^]


然后就是我们非常熟悉的OK orNext 了,只不过这个过程是文本模式,如果想使用图形,只能开启vnc


最后安装完,reboot就进入刚刚创建的虚拟机了。要想退回到宿主机,ctrl] 即可。
virsh list 可以列出当前的子机列表。
virsh console aming1可以进入指定的子机





NFS安装步骤基本类似,这里我们用磁盘文件格式为qcow2。因为它支持虚拟机快照功能。

而且把系统镜像文件mount /mnt下

    1.    qemu-img create -f qcow2 -opreallocation=metadata/data/wyp2.qcow2 10G

创建磁盘格式为qcow2    /data/wyp2.qcow2是虚拟机文件名,可以自定义。

2.    virt-install \
--namewyp2 \
--ram 512 \
--disk path=/data/wyp2.qcow2,format=qcow2,size=7,bus=virtio\
--vcpus 1 \
--os-type linux \
--os-variant rhel6 \
--network bridge=br0 \
--graphics none \
--console pty,target_type=serial \
--location'/mnt/' \
--extra-args 'console=ttyS0,115200n8 serial'

3. 因为虚拟机没有CD,硬盘也是空的,所以用NFS是最好选择

宿主机启用NFS服务

Yum install –y nfs-utils rpcbind             #安装NFS

Vim /etc/exports                         #配置服务

/mnt 192.168.11.0/24

/etc/init.d/nfs start;/etc/init.d/rpcbind start   #启动服务

showmount -e 192.168.11.2 #检测是否共享成功

虚拟机选择NFS后填写NFS服务器地址,镜像地址/mnt/images/install.img固定路径

挂载选项ro即可,以后步骤同上。



页: [1]
查看完整版本: kvm创建虚拟机