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  可以進入指定的子機


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