CentOS6.4 X86_64 kvm+PXE備忘

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官方文檔php

  • 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了html

1
# /etc/init.d/libvirtd start 

橋接網絡

1
# yum install bridge-utils -y 

橋接實例:node

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 Configurationpython

構建無人值守,實現KVM PXE安裝

安裝相關軟件

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

dhcp example:linux

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:vim

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下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:api

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官方文檔bash

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–

相關文章
相關標籤/搜索