kvm

2.  安装kvm前的准备工作

a. 清除iptables规则

service iptables stop; service iptables save

b. 关闭selinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

setenforce 0



3. 开始安装kvm

a. 检查你的系统是否支持虚拟化

grep -Ei 'vmx|svm' /proc/cpuinfo

如果有输出内容,则支持,其中intel cpu支持会有vmx,amd cpu支持会有svm


b. 通过yum安装虚拟化的软件包

yum install -y kvm virt-*  libvirt  bridge-utils qemu-img

说明:

kvm:软件包中含有KVM内核模块,它在默认linux内核中提供kvm管理程序

libvirts:安装虚拟机管理工具,使用virsh等命令来管理和控制虚拟机。

bridge-utils:设置网络网卡桥接。

virt-*:创建、克隆虚拟机命令,以及图形化管理工具virt-manager

qemu-img:安装qemu组件,使用qemu命令来创建磁盘等。





c. 检查kvm模块是否加载

lsmod |grep kvm

正常应该是:

kvm_intel              55496  3

kvm                   337772  1 kvm_intel


如果没有,需要执行 

modprobe kvm-intel

还没有就重启一下试试


d. 配置网卡

cd /etc/sysconfig/network-scripts/

cp ifcfg-eth0 ifcfg-br0

分别编辑eth0和br0

ifcfg-eth0改成如下:

DEVICE=eth0

HWADDR=00:0C:29:55:A7:0A

TYPE=Ethernet

UUID=2be47d79-2a68-4b65-a9ce-6a2df93759c6

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=none

BRIDGE=br0



ifcfg-br0改成如下:

DEVICE=br0

#HWADDR=00:0C:29:55:A7:0A

TYPE=Bridge

#UUID=2be47d79-2a68-4b65-a9ce-6a2df93759c6

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=static

IPADDR=192.168.1.124

NETMASK=255.255.255.0

GATEWAY=192.168.11.1

DNS1=202.106.0.20


说明: 我的虚拟机是桥接模式,所以设置br0的ip和我的真机同样的网段,包括网关也是我路由器的


ip,大家可以根据自己的环境去配置,目的是为了让虚拟机可以上网。


/etc/init.d/network restart

查看网卡如下:

br0       Link encap:Ethernet  HWaddr 00:0C:29:55:A7:0A

          inet addr:192.168.1.124  Bcast:192.168.11.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe55:a70a/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:141326 errors:0 dropped:0 overruns:0 frame:0

          TX packets:90931 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0

          RX bytes:456024940 (434.8 MiB)  TX bytes:10933593 (10.4 MiB)


eth0      Link encap:Ethernet  HWaddr 00:0C:29:55:A7:0A

          inet6 addr: fe80::20c:29ff:fe55:a70a/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:341978 errors:0 dropped:0 overruns:0 frame:0

          TX packets:90946 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:468848861 (447.1 MiB)  TX bytes:10934699 (10.4 MiB)


lo        Link encap:Local Loopback

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet6 addr: ::1/128 Scope:Host

          UP LOOPBACK RUNNING  MTU:65536  Metric:1

          RX packets:0 errors:0 dropped:0 overruns:0 frame:0

          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0

          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)


virbr0    Link encap:Ethernet  HWaddr 52:54:00:14:EF:D5

          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:0 errors:0 dropped:0 overruns:0 frame:0

          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0

          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)




e. 启动或重启libvirtd服务和messagebus 服务

/etc/init.d/libvirtd start

/etc/init.d/messagebus restart


此时可以查看网络接口列表

brctl show 结果如下:

bridge name     bridge id               STP enabled     interfaces

br0             8000.000c2955a70a       no              eth0

virbr0          8000.52540014efd5       yes             virbr0-nic

把刚才添加的硬盘分区格式话

fdisk -l

 fdisk /dev/sdb

 mkfs.ext4 /dev/sdb1

 mkfs.ext4 /dev/sdb1

 mount /dev/sdb1 /data/



4. 创建虚拟机

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


virt-install \

--name  ddb1 \

--ram 512 \

--disk path=/data/ddb1.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'



virt-install \

--name  ddb1 \

--ram 512 \

--disk path=/data/ddb11.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/x86_64/' \

--extra-args 'console=ttyS0,115200n8 serial'

安装

 virt-install --name  ddb2 --ram 512 --disk 


path=/data/kvm/ddb2.img,format=qcow2,size=10,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'



192.168.1.125给虚拟机设置的ip


192.168.1.1 网关

────────────────┤ Manual TCP/IP Configuration ├────────────


─────┐

       │                                                                │

       │ Enter the IPv4 and/or the IPv6 address and prefix (address /   │

       │ prefix).  For IPv4, the dotted-quad netmask or the CIDR-style  │

       │ prefix are acceptable. The gateway and name server fields must │

       │ be valid IPv4 or IPv6 addresses.                               │

       │                                                                │

       │ IPv4 address: 192.168.1.125___ / 255.255.255.0___              │

       │ Gateway:      192.168.1.1______________________________        │

       │ Name Server:  8.8.8.8__________________________________        │

       │                                                                │

       │             ┌────┐                        ┌──────┐             │

       │             │ OK │                        │ Back │             │

       │             └────┘                        └──────┘             │

       │                                                                │

       │                                                                │

       └────────────────────────────────────────


─────────────────

进行下载

下载完后选择 re-initialize all





│                                     │

                    │  Asia/Riyadh                     ↑  │

                    │  Asia/Sakhalin                     │

                    │  Asia/Samarkand                    │

                    │  Asia/Seoul                        │

                    │  Asia/Shanghai                   ↓  │

                    │                                     │

                    │      ┌────┐          ┌──────┐       │

                    │      │ OK │          │ Back │       │

                    │      └────┘          └──────┘       │

                    │                              



选择上海

输入要给系统设置的密码





    Pick a root password. You must type it    │

                │    twice to ensure you know it and do not    │

                │    make a typing mistake.                    │

                │                                              │

                │ Password:           ******__________________ │

                │ Password (confirm): ******__________________ │

                │                                              │

                │        ┌────┐               ┌──────┐         │

                │        │ OK │               │ Back │         │

                │        └────┘               └──────┘    


选择Use Anyway




───────────────────┤ Weak Password ├────────────────


────┐

          │                                                         │

          │ You have provided a weak password: it does not contain  │

          │ enough DIFFERENT characters                             │

          │                                                         │

          │        ┌────────┐                ┌────────────┐      


   │

          │        │ Cancel │                │ Use Anyway │         │

          │        └────────┘                └────────────┘      


   │

          │                                             



直接ok



                                                         │

       │                 Use entire drive                              │

       │                 Replace existing Linux system                 │

       │                 Use free space                                │

       │                                                               │

       │   Which drive(s) do you want to use for this installation?    │

       │        [*]    vda    30720 MB (Virtio Block Device) ↑         │

       │                                                              │

       │                                                               │

       │                      ┌────┐   ┌──────┐                        │

       │                      │ OK │   │ Back │                        │

       │                      └────┘   └──────┘     





选择 Write changes to disk


     ┌─────────────┤ Writing storage configuration to disk ├──────


────────┐

     │                                                                    │

     │ The partitioning options you have selected will now be written to  │

     │ disk.  Any data on deleted or reformatted partitions will be lost. │

     │                                                                    │

     │        ┌─────────┐               ┌─────────────────


──────┐         │

     │        │ Go back │               │ Write changes to disk │         │

     │        └─────────┘               └─────────────────


──────┘         │

     │                                                         






用光盘安装


挂在光盘 

mount /dev/cdrom /mnt

[root@localhost ~]# qemu-img create -f qcow2 -o preallocation=metadata  


/data/kvm/ddb2.qcow2 10G


[root@localhost ~]# ls /data/kvm

ddb1.img  ddb2.qcow2


[root@localhost ~]# virt-install --name  ddb2 --ram 512 --disk 


path=/data/kvm/ddb2.qcow2,format=qcow2,size=10,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'




选择English  


┌────────┤ Choose a Language ├────────┐

                    │                                     │

                    │ What language would you like to use │

                    │ during the installation process?    │

                    │                                     │

                    │      Catalan                ↑       │

                    │      Chinese(Simplified)           │

                    │      Chinese(Traditional)          │

                    │      Croatian                      │

                    │      Czech                         │

                    │      Danish                        │

                    │      Dutch                         │

                    │      English                ↓       │

                    │                                     │

                    │               ┌────┐                │

                    │               │ OK │                │

                    │               └────┘                │

                    │                                     │

                    │                                   


选择NFS directory



                ┌───┤ Installation Method ├───┐

                        │                             │

                        │ What type of media contains     │

                        │ the installation p_w_picpath?             │

                        │                                             │

                        │        Local CD/DVD                  │

                        │        Hard drive                      │

                        │        NFS directory                  │

                        │        URL                               │

                        │                                             │

                        │   ┌────┐       ┌──────┐     │

                        │   │ OK    │       │ Back     │     │

                        │   └────┘       └──────┘     │

                        │                                             │

                        │                                             │



选择Manual configuration     



                 ┌────────────┤ Configure TCP/IP ├────────────


                 │                                            │

                 │ [*] Enable IPv4 support                    │

                 │        ( ) Dynamic IP configuration (DHCP) │

                 │        (*) Manual configuration            │

                 │                                            │

                 │ [*] Enable IPv6 support                    │

                 │        (*) Automatic                       │

                 │        ( ) Automatic, DHCP only            │

                 │        ( ) Manual configuration            │

                 │                                            │

                 │        ┌────┐              ┌──────┐        │

                 │        │ OK │              │ Back │        │

                 │        └────┘              └──────┘        │

                 │                                            │

                 │                                            │

                 └───────────────────────────────────


─────────┘



192.168.1.116 是要给创建的虚拟机配置ip


192.168.1.1 (网关) 


                                                               │

       │ Enter the IPv4 and/or the IPv6 address and prefix (address /   │

       │ prefix).  For IPv4, the dotted-quad netmask or the CIDR-style  │

       │ prefix are acceptable. The gateway and name server fields must │

       │ be valid IPv4 or IPv6 addresses.                               │

       │                                                                │

       │ IPv4 address: 192.168.1.116___ / 255.255.255.0___              │

       │ Gateway:      192.168.1.1______________________________        │

       │ Name Server:  8.8.8.8__________________________________        │

       │                                                                │

       │             ┌────┐                        ┌──────┐             │

       │             │ OK │                        │ Back │             │

       │             └────┘                        └──────┘             │

       │                                                     


接下来配置nfs

yum install -y nfs-utile rpcbind


vim /etc/exports


/mnt 192.168.1.0/24


/etc/init.d/rpcbind start

/etc/init.d/nfs start


showmount -e 192.168.1.124


Export list for 192.168.1.124:

/mnt 192.168.1.0/24


192.168.1.124 在那台机器上配置的nfs就写那个ip


 ┌───────────────────────────┤ NFS Setup ├────────


────────────────────┐

     │                                                                    │

     │ Please enter the server and NFSv3 path to your CentOS installation │

     │ p_w_picpath and optionally additional NFS mount options.                 │

     │                                                                    │

     │       NFS server name:              192.168.1.124_____________       │

     │       CentOS directory:             /mnt/p_w_picpaths/install.img_       │

     │       NFS mount options (optional): ro______________________       │

     │                                                                    │

     │             ┌────┐                           ┌──────┐              


     │             │ OK │                           │ Back │              │

     │             └────┘                           └──────┘              


     │                                                                    │

     │                                                                    │

     └─────────────────────────────────────────


──────────────────────

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


vnc啦。




--name  指定虚拟机的名字

--ram 指定内存分配多少

--disk path 指定虚拟磁盘放到哪里,size=30 指定磁盘大小为30G,这样磁盘文件格式为raw,raw格


式不能做快照,后面有说明,需要转换为qcow2格式,如果要使用qcow2格式的虚拟磁盘,需要事先创


建qcow2格式的虚拟磁盘。   示例:qemu-img create -f qcow2 -o preallocation=metadata  


/data/kvm/ddb2.qcow2 10G  --disk path=/data/ddb2.img,format=qcow2,size=7,bus=virtio

--vcpus 指定分配cpu几个

--os-type 指定系统类型为linux

--os-variant 指定系统版本

--network  指定网络类型

--graphics 指定安装通过哪种类型,可以是vnc,也可以没有图形,在这里我们没有使用图形直接使


用文本方式

--console 指定控制台类型

--location 指定安装介质地址,可以是网络地址,也可以是本地的一个绝对路径,如果是绝对路径,


那么后面还需要指定一个安装介质,比如NFS,假如虚拟机设置ip后,不能连外网,那么就会提示让我


们选择安装途径:       


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


vnc啦。

  

最后安装完,reboot就进入刚刚创建的虚拟机了。要想退回到宿主机,ctrl  ] 即可。

virsh list 可以列出当前的子机列表。

virsh console centos6.6_1  可以进入指定的子机


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章