Install 安装

1
2
3
4
5
# yum install qemu-kvm qemu-img
# 使用kvm至少要安装的包,一个提供用户级别kvm模拟器,一个提供磁盘镜像的管理
# 安装虚拟化管理的相关工具
# yum install virt-manager libvirt \
libvirt-python python-virtinst libvirt-client

也可以yum groupinstall虚拟化组件,具体可参考Redhat官方文档

  • KVM 管理工具

    • kvm 内核模块 <- qemu 管理工具 (可用性低)
    • qemu 是开源虚拟化软件, 虚拟不同 CPU 架构, 可以 x86 虚拟 power cpu
    • libvirt, virsh, virt-manager (redhat 的辅助工具)
    • libvirt api 提供管理接口工具
    • virt-manager 调用 libvirt 工具
  • ibvirt接口

    • virsh 命令行工具
    • virt-manager 图形工具
    • RHEV-M (redhat专用收费软件)
  • 支持三种虚拟设备

    • Emulated software devices 仿真设备 -> 南北桥, USB, PS/2 ISA PCI
    • Para-virtualized devices -> 时钟, 网络, 串口
    • Physically shared devices –> 光纤设备

安装完之后就可以启动kvm了

1
# /etc/init.d/libvirtd start

桥接网络

1
# yum install bridge-utils -y

桥接实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NAME="System eth0"
BRIDGE="br0"
# cat /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE="br0"
TYPE="Bridge" # 注意大小写
BOOTPROTO="static"
IPADDR=192.168.80.131
NETMASK=255.255.255.0
GATEWAY=192.168.80.2
ONBOOT="yes"
DELAY=0

具体可参考: CentOS / Redhat: KVM Bridged Network Configuration

构建无人值守,实现KVM PXE安装

安装相关软件

1
# yum install tftp-server syslinux dhcp vsftpd -y
dhcp

dhcp example:

dhcp
1
2
3
4
5
6
7
8
9
# cat /etc/dhcp/dhcpd.conf
subnet 192.168.80.0 netmask 255.255.255.0 {
range 192.168.80.10 192.168.80.100;
default-lease-time 600;
max-lease-time 7200;
next-server 192.168.80.131; # PXE Server地址
filename "pxelinux.0"; # 引导文件名
}
# /etc/init.d/dhcpd restart

tftp

tftp example:

tftp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# cat /etc/xinetd.d/tftp
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /var/lib/tftpboot
disable = no # 默认yes,改为no即可
per_source = 11
cps = 100 2
flags = IPv4
}
# /etc/init.d/xinetd restart

vsftpd

新建/var/ftp/centos目录,把CentOS光盘镜像挂载至/var/ftp/centos下

1
2
3
# mkdir /var/ftp/centos
# mount /dev/cdrom /var/ftp/centos # 挂载镜像使用-o loop
# /etc/init.d/vsftpd restart

无人值守

1
2
3
4
5
6
# mkdir /var/lib/tftpboot/CentOS6
# cp /var/ftp/centos/images/pxeboot/{initrd.img,vmlinuz} /var/lib/tftpboot/CentOS6
# cp /var/ftp/centos/isolinux/{boot.msg,vesamenu.c32} /var/lib/tftpboot/
# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
# mkdir /var/ftp/tftpboot/pxelinux.cfg
# cp /var/ftp/centos/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
tftpboot目录树结构
1
2
3
4
5
6
7
8
9
10
11
12
# tree /var/lib/tftpboot/
/var/lib/tftpboot/
├── boot.msg
├── CentOS6
│   ├── initrd.img
│   └── vmlinuz
├── pxelinux.0
├── pxelinux.cfg
│   └── default
└── vesamenu.c32 2 directories, 6 files

pxelinux.cfg/default example:

pxe default
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# cat /var/lib/tftpboot/pxelinux.cfg/default
# default CentOS6_PXE # 默认启动'default CentOS6_PXE'标记的内核
default vesamenu.c32 # 菜单选项
timeout 100 # 单位是1/10s
# prompt 1 # 为 '0' 时则不提示'boot: ',将会直接启动 'default' 参数中指定的内容 display boot.msg # 启动时显示 # menu background splash.jpg # 菜单背景等
menu title Welcome to CentOS 6.4!
menu color border 0 #ffffffff #00000000
menu color sel 7 #ffffffff #ff000000
menu color title 0 #ffffffff #00000000
menu color tabmsg 0 #ffffffff #00000000
menu color unsel 0 #ffffffff #00000000
menu color hotsel 0 #ff000000 #ffffffff
menu color hotkey 7 #ffffffff #ff000000
menu color scrollbar 0 #ffffffff #00000000 label CentOS6_PXE
menu label ^PXE Install CentOS KVM
kernel /CentOS6/vmlinuz
append ks=ftp://192.168.80.131/ks.cfg initrd=/CentOS6/initrd.img
label rescue
menu label ^Rescue installed system
kernel /CentOS6/vmlinuz
append initrd=/CentOS6/initrd.img rescue

关于PXE的进一步细节可以参考pxelinux官方文档

ks.cfg example:

ks.cfg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# cat /var/ftp/ks.cfg
# System authorization information
auth --useshadow --enablemd5
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Use text mode install
text
# Firewall configuration
firewall --disabled
skipx
# Run the Setup Agent on first boot
firstboot --disable
# System keyboard
keyboard us
# System language
lang en_US
# Installation logging level
logging --level=info
# Use network installation
url --url=ftp://192.168.80.131/centos
# Network information
network --bootproto=dhcp --device=eth0 --onboot=on
# Reboot after installation
reboot
#Root password
rootpw --iscrypted $1$duSkJ1$1P5qGnqUGn3S1MTTFiPJY. # SELinux configuration
selinux --disabled
# System timezone
timezone Asia/Shanghai
# Install OS instead of upgrade
install # Disk partitioning information
part /boot --asprimary --bytes-per-inode=4096 --fstype="ext3" --size=100
part / --bytes-per-inode=4096 --fstype="ext3" --size=5000
part swap --bytes-per-inode=4096 --fstype="swap" --size=512 %packages --nobase
@core
@Development tools
acpid # 如果不安装acpid服务,virsh shutdown virtual_name 命令会失效
vim
wget
lsof
%end

如果最小化安装则软件包选择如下:

Minimal install
1
2
%packages --nobase
@core

关于kickstart的更进一步了解可参考红帽官档Kickstart Options Installing guest virtual machines with PXE

PXE 安装KVM虚拟机

如果要开启–graphics vnc选项,则需要修改vnc监听端口,默认监听的是127.0.0.1,修改为0.0.0.0即可

1
2
3
# grep '^vnc_listen' /etc/libvirt/qemu.conf
vnc_listen = "0.0.0.0"
# /etc/init.d/libvirtd restart

man手册关于vnc端口介绍摘录:

Address to listen on for VNC/Spice connections. Default is typically 127.0.0.1
(localhost only), but some hypervisors allow changing this globally
(for example, the qemu driver default can be changed in /etc/libvirt/qemu.conf).
Use 0.0.0.0 to allow access from other machines. This is use by ’vnc’ and ’spice.

安装实例:

通过location方式结合Kickstart安装
  • –extra-args指定ks相关选项,并且指定console类型使得virsh console可以连接操作,也可指定客户机IP、网关、DNS等,无需DHCP:
1
2
3
4
# virt-install --name centos --ram=1024 --vcpus=1 --os-type=linux --os-variant=rhel6 \
--network bridge:br0 --disk path=/var/lib/libvirt/images/centos6-machine1.img,size=10 \
--location ftp://192.168.80.131/centos/ --extra-args "ks=ftp://192.168.80.131/ks.cfg \
ksdevice=eth0 ip=192.168.80.150 netmask=255.255.255.0 console=ttyS0"
PXE方式安装
1
2
3
4
# virt-install --connect qemu:///system --network=bridge:br0 \
--pxe --name rhel6-machine1 --ram=1024 --vcpus=1 \
--os-type=linux --os-variant=rhel6 --disk \
path=/var/lib/libvirt/images/rhel6-machine1.img,size=10

注意: 如果需要指定console,–pxe是不支持–extra-args额外选项的,所以需要在pxe default 文件添加相关内容[SERIAL和console],如下example

1
2
3
4
5
SERIAL 0 115200
label CentOS6_PXE
menu label ^PXE Install CentOS KVM
kernel /CentOS6/vmlinuz
append ks=ftp://192.168.80.131/ks.cfg initrd=/CentOS6/initrd.img console=tty0 console=ttyS0,115200
本地安装:
1
2
3
# virt-install --name centos --ram=1024 --vcpus=1 --os-type=linux \
--os-variant=rhel6 --location /mnt/ --network bridge:br0 \
--disk path=/var/lib/libvirt/images/rhel6.img,size=10 --extra-args "console=ttyS0"

关于KVM的Guest安装方式,virt-install man手册中也有很多实例,这里不一一介绍。

开启–graphics vnc选项可在Windows下下载vncviewer客户端,输入对应IP和端口即可[ 笔者个人还是习惯通过console连接安装,不开启vnc选项 ],如下

查看对应端口
1
2
3
# netstat -tulnp | grep kvm
tcp 0 0 0.0.0.0:5900 0.0.0.0:* LISTEN 55762/qemu-kvm
tcp 0 0 0.0.0.0:5901 0.0.0.0:* LISTEN 56656/qemu-kvm

连接对应端口

连接之后,就可以正常安装了

virsh 操作命令

这里只介绍一些常用的virsh使用方法,具体的命令可以参看virsh的man手册介绍或者参考红帽官方文档Managing guests with virsh

默认只输入virsh命令会进入virsh的终端:如下,help可以获取命令帮助

1
2
3
4
5
6
7
# virsh
Welcome to virsh, the virtualization interactive terminal. Type: 'help' for help with commands
'quit' to quit virsh #

virsh简单操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
virsh # list    # 显示运行或者暂停的Guest
Id Name State
----------------------------------------------------
28 centos6_1 running virsh # list --all # 显示所有的Guest,包括状态为shut off的
Id Name State
----------------------------------------------------
28 centos6_1 running
- centos shut off virsh # console centos6_1 # console方式连接Guest
Connected to domain centos6_1
Escape character is ^] # 使用Ctrl+]即可退出 CentOS release 6.4 (Final)
Kernel 2.6.32-358.el6.x86_64 on an x86_64 localhost.localdomain login:
virsh # start centos # 开启某个Guest
Domain centos started virsh # list
Id Name State
----------------------------------------------------
28 centos6_1 running
32 centos running
virsh # shutdown centos
# 关闭某个Guest,这里一定要注意,如果Guest没有安装运行acpid服务,
# 则此方式失效,可以kill强制关闭,或者console/ssh连接执行关闭
Domain centos is being shutdown

删除某个Guest,一般需要两步走,对于正在运行的Guest则需要先关闭再继续两步走[也可以直接virsh destroy virtual_name], 这里就演示三步:

1
2
3
virsh destroy guest_name
virsh undefine guest_name
rm -rf guest_img # 删除虚拟存储

挂起主机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
virsh # list
Id Name State
----------------------------------------------------
28 centos6_1 running virsh # suspend centos6_1 # 挂起主机
Domain centos6_1 suspended virsh # list
Id Name State
----------------------------------------------------
28 centos6_1 paused virsh # resume centos6_1 # 把主机从挂起状态切换至运行状态
Domain centos6_1 resumed virsh # list
Id Name State
----------------------------------------------------
28 centos6_1 running virsh #

virt-clone 克隆Guest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# virt-clone --connect=qemu:///system --original=centos6_1 \
--name=centos6_2 -f /var/lib/libvirt/images/centos6_2.img
ERROR Domain with devices to clone must be paused or shutoff.
# virsh suspend centos6_1
Domain centos6_1 suspended # virsh list
Id Name State
----------------------------------------------------
28 centos6_1 paused # virt-clone --connect=qemu:///system --original=centos6_1 \
--name=centos6_2 -f /var/lib/libvirt/images/centos6_2.img
Allocating 'centos6_2.img' 1% [- ] 9.0 MB/s | 112 MB 18:44 ETA

参考和拓展资料

自己之前的两篇挫文: KVM在线迁移(动态迁移) RHEL6 KVM安装备忘

–EOF–

CentOS6.4 X86_64 kvm+PXE备忘的更多相关文章

  1. Centos6.5安装MySQL5.6备忘记录

    Centos6.5安装MySQL5.6 1. 查看系统状态 [root@itzhouq32 tools]# cat /etc/issue CentOS release 6.5 (Final) Kern ...

  2. Centos6.5安装Redis3.0备忘记录

    Centos6.5安装Redis3.0 1. 安装C编译环境 首先需要安装编译Redis的C环境,在命令行执行以下命令: [root@itzhouq32 tools] yum install gcc- ...

  3. php 相关模块备忘

    在安装php的时候,不管是编译安装: ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc -- ...

  4. QT windows msvc下使用boost库(备忘)

    win32-msvc2015: { contains(QMAKE_HOST.arch, x86):{ INCLUDEPATH += D:\3SDK\boost_1_61_0 LIBS += -LD:\ ...

  5. ubuntu下串口编程备忘

    弄了一下串口,一个小问题多折腾了下,备忘.软件环境:zl@zhanglong:~$ cat /etc/lsb-release DISTRIB_ID=UbuntuDISTRIB_RELEASE=12.0 ...

  6. 常用linux命令备忘

    备忘: 关闭防火墙:# systemctl stop firewalld 查看防火墙状态:#  systemctl status firewalld 停止防火墙:#  systemctl disabl ...

  7. Kvm--02 安装centos6系统 ,kvm磁盘管理

    目录 1.安装一个CentOS6的系统的虚拟主机 2.虚拟机的备份 3.企业案例: 4.Kvm磁盘管理 1.安装一个CentOS6的系统的虚拟主机 #上传一个CenOS6系统的镜像到/opt目录下 [ ...

  8. GIS部分理论知识备忘随笔

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.高斯克吕格投影带换算 某坐标的经度为112度,其投影的6度带和3度带 ...

  9. python序列,字典备忘

    初识python备忘: 序列:列表,字符串,元组len(d),d[id],del d[id],data in d函数:cmp(x,y),len(seq),list(seq)根据字符串创建列表,max( ...

随机推荐

  1. SQOOP 添加oracle 驱动

      我的ORACLE 是11G  ,找到ojdbc6.jar,放到sqoop 的lib 下面,我的是cloudera 集群,目录是 /opt/cloudera/parcels/CDH-5.10.0-1 ...

  2. 获取文件夹总大小方法2_获取cmd命令结果,效率最高

    public static long GetDirectorySize(string path) { long res = 0; System.Diagnostics.Process p = new ...

  3. PyQt5系列教程(四)信号和槽

    软硬件环境 OS X EI Capitan Python 3.5.1 PyQt 5.5.1 前言 信号(Signal)和槽(Slot)是Qt编程中对象间通讯的机制.在Qt中,每一个QObject对象, ...

  4. 设置GO环境变量

    linux的设置方法:有4个环境变量需要设置:GOROOT.GOPATH.GOBIN以及PATH.需要设置到某一个profile文件中(~/.bash_profile(单一用户)或/etc/profi ...

  5. windows 下 YII2 配置 memcache

    环境: 操作系统 :Windows 7; php: 5.6.8 apche:2.4.12 1.首先安装PHP  memcache 拓展,安装方法如下: 1.1下载 memcache 拓展DLL: ht ...

  6. gridview发布后,编辑改为edit 原因是未安装 dotNetFx40LP_Full_x86_x64zh-Hans中文语言包

    https://www.microsoft.com/zh-cn/download/details.aspx?id=3324

  7. JVM知识点精华汇总

    本文是学习了<深入理解Java虚拟机>之后的总结,主要内容都来自于书中,也有作者的一些理解.一是为了梳理知识点,归纳总结,二是为了分享交流,如有错误之处还望指出.(本文以jdk1.7的规范 ...

  8. vc通过进程名返回进程id

    std::string WcharToChar(const wchar_t* wp, size_t m_encode = CP_ACP) { std::string str; , wp, wcslen ...

  9. 基础知识 一个工具给win7 win10的同学 或者MAC 可以跳过

  10. 使用twised实现一个EchoServer

    ProtocolsProtocols描述了如何以异步的方式处理网络中断时间,HTTP.DNS已经IMAP是应用应用层协议中的例子,Protocols实现了IProtocol接口,它饱和如下的方法 ma ...