• 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官方文档

    • 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–

  • 相关阅读:
    Php排序
    php导出excel表
    yii的多表查询
    MySQL中自定义排序
    jquery中的obj.attr()和obj.data
    PhpStorm快捷键
    记2019年目标之一没有996的大数据分析BI实战历程
    2018年传统公司技术部门技术变迁和2019展望
    mpvue微信小程序开发随笔
    docker toolbox的redis 配置主从及哨兵模式保证高可用
  • 原文地址:https://www.cnblogs.com/kumulinux/p/3302796.html
Copyright © 2020-2023  润新知