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

参考和拓展资料

  • Automate RHEL Based OS Deployments with PXE Boot and Kickstart
  • Centos& and serial console login
  • kvm virsh console
  • KVM 实时迁移
  • rhel6 kvm备忘

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

–EOF–

转载于:https://www.cnblogs.com/kumulinux/p/3302796.html

CentOS6.4 X86_64 kvm+PXE备忘相关推荐

  1. KVM 虚拟机 virsh 命令备忘单

    这是一个全面的 virsh 命令备忘单: virsh 是 KVM 虚拟机的命令管理用户界面.virsh 可用于创建.暂停.重新启动和关闭域.此外,virsh 可用于列出您的虚拟化管理程序平台中可用的当 ...

  2. Centos6.3安装KVM

    Centos6.3安装KVM 一.安装kvm 1 在安装CentOS6.3时可以选择安装好kvm 2 如果未安装好kvm,请按照下列方式安装 [创建本地yum源] 挂载iso文件 mount -o l ...

  3. 20多个Maven命令和选项(备忘单)

    If you are working on Java, you must know Maven. Maven is the most popular project and dependency ma ...

  4. linux dhcp 安装系统,Linux上基于网络自动化安装系统(CentOS6.5+DHCP+TFTP+PXE)

    Linux上基于网络自动化安装系统(CentOS6.5+DHCP+TFTP+PXE) 一.前言 安装系统常用的方式就是,光驱安装,U盘安装,但是这种手动安装方法效率都很低,而且出错概率大. 现在网卡速 ...

  5. vim的一些快捷键,备忘

    vim的一些快捷键,备忘 快捷键                                            作用 ctrl+g                                ...

  6. 资源 | AI、神经网络、机器学习、深度学习以及大数据学习备忘单

    向AI转型的程序员都关注了这个号☝☝☝ 以下是关于神经网络.机器学习.深度学习以及大数据学习的备忘单,其中部分内容和此前发布的<资源 | 值得收藏的 27 个机器学习的小抄>有所重复,大家 ...

  7. 机器学习项目的备忘清单!

    ↑↑↑关注后"星标"Datawhale 每日干货 & 每月组队学习,不错过 Datawhale干货 作者:Harshit Tyagi,编译:机器之心 机器学习项目中含有众多 ...

  8. 备忘:C语言void *

    由于研究一段代码的时候,看到了 void *.故此进行了学习. 看了 http://www.doc88.com/p-894907672962.html  的说明,感觉写得太好了,已无话可说. 故此备忘 ...

  9. [译] Kotlin 标准方法备忘

    原文地址:Kotlin Standard Functions cheat-sheet 原文作者:Jose Alcérreca 译文出自:掘金翻译计划 本文永久链接:github.com/xitu/go ...

  10. Nancy之结合TinyFox调试备忘

    原文:Nancy之结合TinyFox调试备忘 最近把一个小项目的数据库换成MongoDB,同时用了MongoRepository 这个开源组件来对数据进行操作. 通过NuGet安装之后,它会自动在we ...

最新文章

  1. 给大家几个不花钱看书的办法【人人都是产品经理】
  2. JAVA之ArrayList集合
  3. Document Builder: 如何将structure level的field加入到word document的table中
  4. 操作系统之进程管理:12、生产者消费者问题和多级生产者多级消费者问题
  5. You have to specify ‘-keep‘ options for the shrinking step
  6. android nfc读写demo,android nfc常用标签读取总结
  7. java自画快递单,使用画图功能绘制快递单,并调用打印机打印
  8. 利用python和百度地图API实现数据地图标注
  9. 探索搜索引擎技术的现状和将来(转)
  10. WIN2000服务器安全配置(转)
  11. win7html.exe,win7系统exe程序打开方式还原怎么弄 win7系统还原exe程序打开方式办法介绍...
  12. 笑话 php 程序员,[每天程序员]笑死人不偿命的程序员段子
  13. 独家 | 那些令人细思恐极的AI技术,哪一个戳中你的命门?
  14. adobe illustrator如何裁剪图像
  15. 微信端权限控制java,微信支付:特约子商户商户号未授权服务商的产品权限 的解决方案...
  16. [源码分析] Facebook如何训练超大模型 --- (3)
  17. 智能机房动力环境集中监控管理系统
  18. RocketMQ (六) 主题-Topic
  19. Nibiru、中网智云为“西秦名校”VR 超级教室提供解决方案
  20. 我搜集的各类软件的注册码(免费万岁)

热门文章

  1. Dataframe列赋值值后全部为NAN
  2. ubuntu16.04命令行模式和图形界面互相切换
  3. 如何使用 Python 开发加权平均集成
  4. Linux 命令之——文件行数查询命令温习
  5. python实现arxiv论文数据解析处理
  6. Kubernetes 小白学习笔记(5)--Kubernetes集群的部署service、部署deployment、自动负载均衡、自动伸缩、版本升级、版本回退
  7. FISCO BCOS Solidity 智能合约 返回多个值
  8. linux分配权限o w,O-LinuxShell-W14 Linux权限练习(共50分)
  9. mysql中的where 1 1_SQL语句中where 1=1和where 1=0的作用
  10. linux安装python3和pip3