系统:CentOS7 64位 [root@e3 ~]# cat /etc/redhat-release CentOS Linux release 7.0.1406 (Core) 安装KVM IBM对kvm的介绍https://www.ibm.com/search/csass/search/?q=kvm&sn=dw&lang=zh&cc=CN&en=utf&hpp=20&dws=cndw&lo=zh yum install qemu-kvm libvirt virt-install bridge-utils lsmod |grep kvm kvm_intel 138567 0 kvm 441119 1 kvm_intel [root@e3 ~]#systemctl start libvirtd #启动 libvirtd [root@e3 ~]# systemctl enable libvirtd #开机自启动 libvirtd详解看这位博友的文章http://blog.csdn.net/gaoxingnengjisuan/article/details/9674315 配置KVM虚拟机桥接网络 [root@e3 ~]# nmcli c add type bridge autoconnect yes con-name br0 ifname br0 #添加虚拟桥接网卡br0 [root@e3 ~]# nmcli c modify br0 ipv4.addresses "192.168.0.244/24 192.168.0.1" ipv4.method manual #设置虚拟网卡ip和网关 [root@e3 ~]# nmcli c modify br0 ipv4.dns 192.168.0.1 #设置DNS 192.168.0.1 [root@e3 ~]# nmcli c delete enp3s0 #删除当前网络配置 enp3s0 是我的网路标示,在CentOS 6是eth0 [root@e3 ~]# nmcli c add type bridge-slave autoconnect yes con-name enp3s0 ifname enp3s0 master br0 #添加br0接口 [root@e3 ~]# systemctl stop NetworkManager; systemctl start NetworkManager #停止网络和启动网络 ##nmcli是一个网络管理工具 [root@e3 ~]# which nmcli /usr/bin/nmcli [root@e3 ~]# rpm -qf /usr/bin/nmcli NetworkManager-0.9.9.1-29.git20140326.4dba720.el7_0.x86_64 #ip addr [root@e3 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP qlen 1000 link/ether e0:3f:49:13:0a:7e brd ff:ff:ff:ff:ff:ff 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP link/ether 56:84:7a:fe:97:99 brd ff:ff:ff:ff:ff:ff inet 172.17.42.1/16 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::5484:7aff:fefe:9799/64 scope link valid_lft forever preferred_lft forever 5: vethde86ccb: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master docker0 state UP qlen 1000 link/ether ca:ba:76:4e:c1:92 brd ff:ff:ff:ff:ff:ff inet6 fe80::c8ba:76ff:fe4e:c192/64 scope link valid_lft forever preferred_lft forever 6: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP link/ether e0:3f:49:13:0a:7e brd ff:ff:ff:ff:ff:ff inet 192.168.0.244/24 brd 192.168.0.255 scope global br0 valid_lft forever preferred_lft forever inet6 fe80::e23f:49ff:fe13:a7e/64 scope link valid_lft forever preferred_lft forever OK至此kvm安装完毕下来看看如何安装第一台guset虚拟机 构建安装源, [root@e3 ~]# yum install vsftpd #安装ftp [root@e3 ~]# systemctl start vsftpd #启动ftp [root@e3 ~]# systemctl enable vsftpd #开机自启动 [root@e3 tmp]#wget http://mirrors.sohu.com/centos/6/isos/x86_64/CentOS-6.6-x86_64-minimal.iso #下载Centos6.6 MINI安装盘 [root@e3 tmp]# mkdir /mnt/cdrom/ [root@e3 tmp]# mount CentOS-6.6-x86_64-minimal.iso /mnt/cdrom/ #挂载 [root@e3 tmp]# cp -r /mnt/cdrom /var/ftp/ [root@e3 tmp]# cd /root/centos/cdrom/ [root@e3 cdrom]# vim .treeinfo #修改如下 [general] family = CentOS timestamp = 1414159790.06 variant = totaldiscs = 1 version = 6.6 discnum = 1 packagedir = arch = x86_64 [images-x86_64] kernel = isolinux/vmlinuz ##主要是修改这 initrd = isolinux/initrd.img ##主要修改这 [images-xen] initrd = images/pxeboot/initrd.img [stage2] mainimage = images/install.img #完了保存的时候强制保存 :wq! 然后重启你的vftpd。 测试:看能不能访问
下来创建guset虚拟机 使用virt-install创建虚拟机并安装GuestOS
virt-install是一个命令行工具,它能够为KVM、Xen或其它支持libvrit API的hypervisor创建虚拟机并完成GuestOS安装;此外,它能够基于串行控制台、VNC或SDL支持文本或图形安装界面。安装过程可以使用本地的安装介质如CDROM,也可以通过网络方式如NFS、HTTP或FTP服务实现。对于通过网络安装的方式,virt-install可以自动加载必要的文件以启动安装过程而无须额外提供引导工具。当然,virt-install也支持PXE方式的安装过程,也能够直接使用现有的磁盘映像直接启动安装过程。 [root@e3 cdrom]# virt-install \ > --name centos6.6 \ #指定虚拟机名字 > --ram 512 \ #分配虚拟机的内存大小 > --disk path=/data/kvm/images/centos6.6.img,size=20 \ #虚拟机硬盘安装路径 > --vcpus 2 \ #CPU个数 > --os-type linux \ #操作系统类型 > --os-variant rhel6 \ #虚拟机操作系统的变种,当前CENTOS是redhat的所以。 > --network bridge=br0 \ #网络配置 > --graphics none \ #不使用图形界面 > --console pty,target_type=serial \ #配置接口 > --location 'ftp://192.168.0.244/cdrom/' \ #指定安装源 > --extra-args 'console=ttyS0,115200n8 serial' #额外传的参数 #回车 开始安装...... 搜索文件 .treeinfo...... | 590 B 00:00:00 !!! 搜索文件 vmlinuz...... | 7.9 MB 00:00:00 !!! 搜索文件 initrd.img...... | 66 MB 00:00:00 !!! 创建存储文件 centos6.6.img | 20 GB 00:00:00 创建域...... | 0 B 00:00:00 连接到域 centos6.6 换码符为 ^] Initializing cgroup subsys cpuset Initializing cgroup subsys cpu Linux version 2.6.32-504.el6.x86_64 (mockbuild@c6b9.bsys.dev.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) ) #1 SMP Wed Oct 15 04:27:16 UTC 2014 Command line: method=ftp://192.168.0.244/cdrom/ console=ttyS0,115200n8 serial KERNEL supported cpus: Intel GenuineIntel AMD AuthenticAMD Centaur CentaurHauls BIOS-provided physical RAM map: BIOS-e820: 0000000000000000 - 000000000009fc00 (usable) BIOS-e820: 000000000009fc00 - 00000000000a0000 (reserved) ......省略。 ############### Welcome to CentOS for x86_64 ┌────────┤ 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 │ │ │ └────┘ │ │ │ │ │ └─────────────────────────────────────┘ #用tab键选择语言 Welcome to CentOS for x86_64 ┌────────────┤ Configure TCP/IP ├────────────┐ │ │ │ [*] Enable IPv4 support │ │ (*) Dynamic IP configuration (DHCP) │ │ ( ) Manual configuration │ │ │ │ [*] Enable IPv6 support │ │ (*) Automatic │ │ ( ) Automatic, DHCP only │ │ ( ) Manual configuration │ │ │ │ ┌────┐ ┌──────┐ │ │ │ OK │ │ Back │ │ │ └────┘ └──────┘ │ │ │ │ │ └────────────────────────────────────────────┘ #激活ipv4,局域网有dhcp就下一步取消ipv6,没有手动写个ip跟你ftp server 在同一网关即可 Welcome to CentOS for x86_64 ┌────────────────┤ 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.0.13____ / 24______________ │ │ Gateway: 192.168.0.1______________________________ │ │ Name Server: _________________________________________ │ │ │ │ ┌────┐ ┌──────┐ │ │ │ OK │ │ Back │ │ │ └────┘ └──────┘ │ │ │ │ │ └────────────────────────────────────────────────────────────────┘ #用tab键切换到OK上然后会提示保存网卡信息OK即可下一步 Welcome to CentOS for x86_64 ┌──────────────────┤ CentOS ├───────────────────┐ │ │ │ Welcome to CentOS! │ │ │ │ │ │ ┌────┐ │ │ │ OK │ │ │ └────┘ │ │ │ │ │ └───────────────────────────────────────────────┘ #OK Welcome to CentOS for x86_64 ┌────────────────────────────────┤ Warning ├─────────────────────────────────┐ │ │ │ Error processing drive: ↑ │ │ ▮ │ │ pci-0000:00:04.0-virtio-pci-virtio1 ▒ │ │ 20480MB ▒ │ │ Virtio Block Device ▒ │ │ ▒ │ │ This device may need to be reinitialized. ▒ │ │ ▒ │ │ REINITIALIZING WILL CAUSE ALL DATA TO BE LOST! ▒ │ │ ▒ │ │ This action may also be applied to all other disks ▒ │ │ needing reinitialization. ↓ │ │ │ │ ┌────────┐ ┌────────────┐ ┌───────────────┐ ┌───────────────────┐ │ │ │ Ignore │ │ Ignore all │ │ Re-initialize │ │ Re-initialize all │ │ │ └────────┘ └────────────┘ └───────────────┘ └───────────────────┘ │ │ │ │ │ #选择初始化所有Re-initialize all Welcome to CentOS for x86_64 ┌───────┤ Time Zone Selection ├───────┐ │ │ │ In which time zone are you located? │ │ │ │ [*] System clock uses UTC │ │ │ │ America/Monterrey ↑ │ │ America/Montevideo ▮ │ │ America/Montserrat ▒ │ │ America/Nassau ▒ │ │ America/New York ↓ │ │ │ │ ┌────┐ ┌──────┐ │ │ │ OK │ │ Back │ │ │ └────┘ └──────┘ │ │ │ │ │ └─────────────────────────────────────┘ #选择时区Asia/上海 Welcome to CentOS for x86_64 ┌──────────────┤ Root Password ├───────────────┐ │ │ │ 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 │ │ │ └────┘ └──────┘ │ │ │ │ │ └──────────────────────────────────────────────┘ #设置root密码 Welcome to CentOS for x86_64 ┌─────────────────────┤ Partitioning Type ├─────────────────────┐ │ │ │ Installation requires partitioning of your hard drive. The │ │ default layout is suitable for most users. Select what space │ │ to use and which drives to use as the install target. │ │ │ │ Use entire drive │ │ Replace existing Linux system │ │ Use free space │ │ │ │ Which drive(s) do you want to use for this installation? │ │ [*] vda 20480 MB (Virtio Block Device) ↑ │ │ ▮ │ │ │ │ ┌────┐ ┌──────┐ │ │ │ OK │ │ Back │ │ │ └────┘ └──────┘ │ │ │ │ │ └───────────────────────────────────────────────────────────────┘ <Space>,<+>,<-> selection | <F2> Add drive | <F12> next screen #分区 Welcome to CentOS for x86_64 ┌─────────────┤ 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 │ │ │ └─────────┘ └───────────────────────┘ │ │ │ │ │ └────────────────────────────────────────────────────────────────────┘ #写入保存磁盘。 Welcome to CentOS for x86_64 ┌─────────────────────┤ Package Installation ├──────────────────────┐ │ │ │ │ │ 71% │ │ │ │ Packages completed: 180 of 205 │ │ │ │ Installing kernel-2.6.32-504.el6.x86_64 (123 MB) │ │ The Linux kernel │ │ │ │ │ │ │ └───────────────────────────────────────────────────────────────────┘ <Tab>/<Alt-Tab> between elements | <Space> selects | <F12> next screen #然后初始化,格式化硬盘自动等待安装完毕 Welcome to CentOS for x86_64 ┌───────────────────────────┤ Complete ├────────────────────────────┐ │ │ │ Congratulations, your CentOS installation is complete. │ │ │ │ Please reboot to use the installed system. Note that updates may │ │ be available to ensure the proper functioning of your system and │ │ installation of these updates is recommended after the reboot. │ │ │ │ ┌────────┐ │ │ │ Reboot │ │ │ └────────┘ │ │ │ │ │ └───────────────────────────────────────────────────────────────────┘ #重启即可进入创建的虚拟机 #因为是mimi关盘安装很快的 CentOS release 6.6 (Final) Kernel 2.6.32-504.el6.x86_64 on an x86_64 localhost.localdomain login: #OK,输入用户名和密码即可 [root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 52:54:00:4F:7D:82 inet addr:192.168.0.13 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::5054:ff:fe4f:7d82/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:206 errors:0 dropped:0 overruns:0 frame:0 TX packets:9 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:22155 (21.6 KiB) TX bytes:546 (546.0 b) 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:4 errors:0 dropped:0 overruns:0 frame:0 TX packets:4 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:340 (340.0 b) TX bytes:340 (340.0 b) [root@localhost ~]# # 这里有一篇virt-install 的中文翻译http://blog.csdn.net/starshine/article/details/6998189 #按Ctrl+]从虚拟机里切换到物理机 [root@e3 cdrom]# virsh list #列出已经安装的虚拟机 Id 名称 状态 ---------------------------------------------------- 11 centos6.5 running 13 centos6.6 running [root@e3 cdrom]# virsh console centos6.6 #从物理机连接虚拟机 连接到域 centos6.6 换码符为 ^] [root@localhost ~]# [root@e3 ~]# virsh autostart centos6.5 #自动启动 域 centos6.5标记为自动开始 [root@e3 ~]# virsh autostart centos6.6 #自动启动 域 centos6.6标记为自动开始 # [root@e3 images]# cd /etc/libvirt/ [root@e3 libvirt]# ls libvirt.conf libvirtd.conf lxc.conf nwfilter qemu qemu.conf qemu-lockd.conf storage virtlockd.conf [root@e3 libvirt]# cd qemu/ [root@e3 qemu]# ls autostart centos6.5.xml centos6.6.xml kvm001.xml kvm002.xml networks template.xml [root@e3 qemu]# cd autostart/ [root@e3 autostart]# ll 总用量 0 lrwxrwxrwx. 1 root root 31 1月 20 14:18 centos6.5.xml -> /etc/libvirt/qemu/centos6.5.xml #开机自启动的文件都会在安装目录里创建一个autostart目录,并设置软连接 lrwxrwxrwx. 1 root root 31 1月 20 14:19 centos6.6.xml -> /etc/libvirt/qemu/centos6.6.xml [root@e3 ~]# [root@e3 ~]# virsh autostart --disable centos6.6 #取消自动启动 域 centos6.6取消标记为自动开始 #关闭一台虚拟机 [root@e3 images]# virsh list --all #列出所有启动没启动的虚拟机 Id 名称 状态 ---------------------------------------------------- 23 centos6.5 running 29 centos6.6 running - kvm001 关闭 - kvm002 关闭 - template 关闭 [root@e3 images]# virsh shutdown centos6.6 #关闭一台centos6.6 域 centos6.6 被关闭 好像关不了#默认virsh不能关闭虚拟机需要安装acpid [root@e3 images]# yum install acpid [root@e3 images]# systemctl start acpid.service [root@e3 images]# systemctl enable acpid.service #在关闭试试(责任编辑:IT) |