kvm安装虚拟机

回复 收藏
2016年5月5日
11:57

1--关闭服务器和selinux
[root@localhost-150 ~]# chkconfig  ip6tables off
[root@localhost-150 ~]# chkconfig  iptables off
[root@localhost-150 ~]# chkconfig --list|grep iptables
iptables        0:off   1:off   2:off   3:off   4:off   5:off   6:off
[root@localhost-150 ~]# chkconfig --list|grep ip6tables
ip6tables       0:off   1:off   2:off   3:off   4:off   5:off   6:off

[root@localhost-150 ~]# vi /etc/selinux/config

SELINUX=disabled
重启服务器
2--安装KVM的主要组件及工具
       
[root@localhost-150 ~]# egrep '(vmx|svm)' --color=always /proc/cpuinfo
检查cpu是否支持虚拟化
[root@localhost ~]# yum install -y kvm virt-* libvirt bridge-utils qemu-img
[root@localhost-150 ~]# lsmod |grep --color kvm
kvm_intel              55624  0
kvm                   341551  1 kvm_intel
验证kvm模块已经加载
3--配置KVM虚拟机运行的网络环境
[root@localhost-150 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:1D:A8:CB
TYPE=Ethernet
UUID=c2f778ff-fe31-42f5-a75e-dc45e4aaece3
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
BRIDGE=br0
[root@localhost-150 ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
#HWADDR=00:0C:29:1D:A8:CB
TYPE=Bridge
#UUID=c2f778ff-fe31-42f5-a75e-dc45e4aaece3
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.1.150
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
验证网络配置
[root@localhost-150 ~]# /etc/init.d/network restart
[root@localhost-150 ~]# ifconfig
br0       Link encap:Ethernet  HWaddr 00:0C:29:1D:A8:CB
          inet addr:192.168.1.150  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fd53:9d66:c8d:0:20c:29ff:fe1d:a8cb/64 Scope:Global
          inet6 addr: fe80::20c:29ff:fe1d:a8cb/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:803 errors:0 dropped:0 overruns:0 frame:0
          TX packets:301 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:56953 (55.6 KiB)  TX bytes:39984 (39.0 KiB)

eth0      Link encap:Ethernet  HWaddr 00:0C:29:1D:A8:CB
          inet6 addr: fe80::20c:29ff:fe1d:a8cb/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1067 errors:0 dropped:0 overruns:0 frame:0
          TX packets:309 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:91175 (89.0 KiB)  TX bytes:40950 (39.9 KiB)

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:3E:0C:54
          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)
4-启动或重启libvirtd服务和messagebus 服务
[root@localhost-150 ~]# /etc/init.d/libvirtd start
[root@localhost-150 ~]# /etc/init.d/messagebus restart
[root@localhost-150 ~]# brctl show
bridge name     bridge id               STP enabled     interfaces
br0             8000.000c291da8cb       no              eth0
virbr0          8000.5254003e0c54       yes             virbr0-nic
查看桥接情况


添加一块硬盘
[root@localhost-150 ~]# fdisk -l
Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000c017b

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26         281     2048000   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             281        1306     8231936   83  Linux

Disk /dev/sdb: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
新建分区,格式化,挂载
[root@localhost-150 ~]# fdisk -l
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26         281     2048000   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             281        1306     8231936   83  Linux

Disk /dev/sdb: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xab5b9ed9

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        3916    31455238+  83  Linux

[root@localhost-150 ~]# mke2fs /dev/sdb1
[root@localhost-150 ~]# mkdir /data
[root@localhost-150 ~]# mount /dev/sdb1 /data/
设置开机挂载
[root@localhost-150 ~]# blkid /dev/sdb1
/dev/sdb1: UUID="0985f7cc-f28a-463b-9dbb-67f16c356264" TYPE="ext4"
[root@localhost-150 ~]# vi /etc/fstab
UUID="0985f7cc-f28a-463b-9dbb-67f16c356264"     /data    ext4   defaults  0 0
添加内容如上
[root@localhost-150 ~]# mount
/dev/sda3 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sdb1 on /data type ext4 (rw)
[root@localhost-150 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3       7.7G  1.2G  6.1G  16% /
tmpfs           491M     0  491M   0% /dev/shm
/dev/sda1       190M   27M  154M  15% /boot
/dev/sdb1        30G   44M   28G   1% /data
验证

重启系统查看开机挂载情况
[root@localhost-150 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3       7.7G  1.2G  6.1G  16% /
tmpfs           491M     0  491M   0% /dev/shm
/dev/sda1       190M   27M  154M  15% /boot
/dev/sdb1        30G   44M   28G   1% /data


开始安装虚拟机系统
[root@localhost-150 ~]# mkdir /data/kvm
创建目录
[root@localhost-150 kvm]# virt-install --name xin1 --ram 512 --disk path=/data/kvm/xin1.img,size=10 --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'
说明:
--name  指定虚拟机的名字
--ram 指定内存分配多少
--disk path 指定虚拟磁盘放到哪里,size=10 指定磁盘大小为10G,这样磁盘文件格式为raw,raw格式不能做快照,后面有说明,需要转换为qcow2格式,如果要使用qcow2格式的虚拟磁盘,需要事先创建qcow2格式的虚拟磁盘。 path=/data/test02.img,format=qcow2,size=7,bus=virtio
--vcpus 指定分配cpu几个
--os-type 指定系统类型为linux
--os-variant 指定系统版本
--network  指定网络类型
--graphics 指定安装通过哪种类型,可以是vnc,也可以没有图形,在这里我们没有使用图形直接使用文本方式
--console 指定控制台类型
--location 指定安装介质地址,可以是网络地址,也可以是本地的一个绝对路径,例如--location '/mnt/',   

下面就是安装系统了 ok
2016-05-07 09:22 举报
已邀请:

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: