zoukankan      html  css  js  c++  java
  • KVM使用小记

    KVM使用小记

    环境:CentOS6.5 x64

    参考文档:
    http://blog.chinaunix.net/uid-30022178-id-5749329.html


    virsh list    #列出目前处于running状态下的虚拟机
    virsh list --all  #列出所有defined的虚拟机

    virsh define /etc/libvirt/qemu/vmode.xml  #定义(新增)一台虚拟机
    virsh undefine vmode   #移除一台叫做vmode的虚拟机

    A.通过virsh来安装虚拟机
    1.安装
    virt-install -n vhost1 -r 768 -vcpus=2 --disk path=/dev/vg0/vhost1 -l http://192.168.0.1/ftp/centos6_1 -x
    ks=http://192.168.0.1/ks/centos6-minimal.cfg -w bridge=br0 --vnc --accelerate

    -n: virtual name
    -r: memory size
    -vcpus=: virtual cpus
    -l: install source
    -x: extra information (ks path)
    --disk path=: install location
    -w: network mode
    --vnc: vnc view
    --accelerate: speed up

    注:命令行安装内存>=768MB没有任何问题,一旦<=512MB,装出来的机器在grub加载后直接kernel panic这个问题不知道大家有没有碰到过,之前还以为是kickstart profile的问题,后来反复利用刚装出来的能正常进系统的anncond-ks.cfg来测试,终于发现在参数都相同的情况下,内存<=512MB会导致kernel panic,后期还有待进一步测试
    原因:selinux可能不允许内存<=512MB的rhel6_x64系统启动,可以在grub菜单的kernel行加上selinux=0关闭selinux,以解决kernel panic问题,顺利进入系统。
    KVM使用小记

    附上centos6-minimal.cfg这个ks
    # Kickstart file automatically generated by anaconda.
    #version=DEVEL
    # Install OS instead of upgrade
    install

    # Use network installation
    url --url=http://192.168.0.1/ftp/centos6_1

    # Use text mode install
    text

    # System language
    lang en_US.UTF-8

    # System keyboard
    keyboard us

    # Network information
    network --onboot yes --device eth0 --bootproto dhcp --noipv6

    # Root password
    #rootpw  --plaintext centos
    rootpw  --iscrypted $6$5xb7Z/30bviKF7xP$UyAYRFqGkeQrlkqxQQzPjzFfQj/3SjomcouuNB2Mk.2CPIsml2K/KC5MKSp9yBiGNhzj0KaY95WqfR/DpNK3K.

    # Firewall configuration
    #firewall --disabled
    firewall --service=ssh

    # System authorization information
    authconfig --enableshadow --passalgo=sha512

    # SELinux configuration
    selinux --enforcing

    # Do not configure the X Window System
    skipx

    # Installation logging level
    logging --level=info

    # Reboot after installation
    reboot

    # System timezone
    timezone --utc Asia/Shanghai

    # System bootloader configuration
    bootloader --location=mbr --driveorder=vda --append="crashkernel=auto text quiet console=tty0 console=ttyS0"

    # The following is the partition information you requested
    # Note that any partitions you deleted are not expressed
    # here so unless you clear all partitions first, this is
    # not guaranteed to work
    # Clear the Master Boot Record
    zerombr

    # Partition clearing information
    #clearpart --none
    clearpart --all --initlabel

    part /boot --fstype=ext4 --size=200
    part pv.253002 --grow --size=200
    volgroup vg0 --pesize=4096 pv.253002
    logvol /home --fstype=ext4 --name=home --vgname=vg0 --size=200
    logvol / --fstype=ext4 --name=root --vgname=vg0 --size=1388
    logvol swap --name=swap --vgname=vg0 --size=256

    repo --name="CentOS"  --baseurl=http://192.168.0.1/ftp/centos6_1 --cost=100

    %packages
    @core
    @server-policy
    %end

    %post
    cd /etc/yum.repos.d/
    rm -rf *
    wget http://192.168.0.1/yum/CentOS-Media.repo
    %end


    2.通过console管理虚拟机
    需要进入到虚拟机中编辑/boot/grub/grub.conf,添加启动内核参数
    在kernel行的末尾加上console=tty0 console=ttyS0,然后重启。
    提示:可以在ks文件中添加内核参数,使安装出来的机器就包含该参数
    重启完成后,可以通过命令在字符界面下管理虚拟机,如:virsh console vhost1


    B.通过lvm snap功能快速创建虚拟机
    ##################################################
    jlive: 物理机
    vmode: 虚拟机,作为模板之用 (vmode 虚拟机的名称)

    1.将源虚拟机的个性化信息去除
    [root@vmode ~]# sys-unconfig

    2.虚拟机将自动关机,关机后在物理机上对源虚拟机所在的LV做快照
    [root@jlive ~]# lvcreate -s -n vhost1 -L 2G /dev/vg0/vmode

    3.复制源虚拟机的配置文件
    [root@jlive ~]# cat /etc/libvirt/qemu/vmode.xml > /etc/libvirt/qemu/vhost1.xml

    4.使用uuidgen生成虚拟机的唯一UUID
    [root@jlive ~]# uuidgen >>/etc/libvirt/qemu/vhost1.xml

    5.修改新虚拟机的配置文件(修改虚拟机的名称、uuid、disk、network部分)
    [root@jlive ~]# vim /etc/libvirt/qemu/vhost1.xml
    KVM使用小记

    6.重启相关服务
    [root@jlive ~]# /etc/init.d/libvirtd restart
    Stopping libvirtd daemon:                                  OK  ]
    Starting libvirtd daemon:                                  OK  ]
    或者
    virsh define /etc/libvirt/qemu/vhost1.xml  #定义(新增)一台虚拟机

    7.启动虚拟机
    [root@jlive ~]#virsh start vhost1


    C.通过kvm模板来创建虚拟机
    1.创建虚拟机模板
    virt-install
    --name ct6-mode
    --ram=768
    --vcpus=2
    --disk path=/var/lib/libvirt/images/ct6-mode,size=10,format=qcow2
    --location="http://192.168.8.254/ftp/centos6_1"
    --extra-args="ks=http://192.168.8.254/ks/centos6-minimal.cfg noipv6"
    --noreboot
    --os-type=linux
    --os-variant=rhel6
    --network bridge=br0,mac=52:54:00:00:00:00 
    --vnc
    --accelerate


    参数和上面的virt-install大致相当,但最大区别在于--disk这个参数,在lvm快照中是将路径指向某个没有任何格式的逻辑卷,这里则是采用qcow2格式的文件size=10,表示10G动态增长

    2.克隆镜像,要做多少个虚拟机就克隆多少个,克隆出来的文件非常小只有几MB
    qemu-img create -f qcow2 -o backing_file=/var/lib/libvirt/images/ct6-mode /var/lib/libvirt/images/vhost1.vol &>/dev/null
    qemu-img create -f qcow2 -o backing_file=/var/lib/libvirt/images/ct6-mode /var/lib/libvirt/images/vhost.vol &>/dev/null

    3.创建虚拟机
    virt-install
    --name vhost1
    --ram=384
    --vcpus=2
    --disk path=/var/lib/libvirt/images/vhost1.vol
    --noreboot
    --os-type=linux
    --os-variant=rhel6
    --network bridge=br0,mac=52:54:00:01:00:01
    --network bridge=br0,mac=52:54:00:02:00:01
    --network bridge=br0,mac=52:54:00:03:00:01
    --network bridge=br0,mac=52:54:00:04:00:01

    --import &>/dev/null

    virt-install
    --name vhost2
    --ram=384
    --vcpus=2
    --disk path=/var/lib/libvirt/images/vhost2.vol
    --noreboot
    --os-type=linux
    --os-variant=rhel6
    --network bridge=br0,mac=52:54:00:01:00:02
    --network bridge=br0,mac=52:54:00:02:00:02
    --network bridge=br0,mac=52:54:00:03:00:02
    --network bridge=br0,mac=52:54:00:04:00:02

    --import &>/dev/null



    要修改虚拟机的名字,存放的位置,当然也可以自定义其它组件如网卡,硬盘等,--import参数很重要

    这样,通过virsh list --all时就会看到有2个虚拟机(vhost1,vhost2)己经生成好,生成速度非常快。

    D.通过virsh命令创建快照和还原
    1.创建快照
    virsh # list --all
     Id    Name                           State
    ----------------------------------------------------
         rhel7-s1                       shut off
         rhel7-s2                       shut off
         ubuntu14.04                    shut off
         vhost1                         shut off
         vhost2                         shut off
         vhost3                         shut off
         win2008r2-snap                 shut off

    virsh # snapshot-create ubuntu14.04
    error: unsupported configuration: internal snapshot for disk vda unsupported for storage type raw
    virsh # snapshot-create win2008r2-snap
    error: unsupported configuration: internal snapshot for disk hda unsupported for storage type raw
    说明:如果磁盘类型qemu不支持,就不能创建快照
    virsh # snapshot-create rhel7-s1
    Domain snapshot 1417060741 created
    virsh # snapshot-list rhel7-s1
     Name                 Creation Time             State
    ------------------------------------------------------------
     1417060741           2014-11-27 11:59:01 +0800 shutoff

    2
    .还原快照

    virsh # snapshot-revert rhel7-s1 --snapshotname 1417060741




    ##################################################
    virsh start vmode #正常开启vmode
    virsh shutdown vmode #正常关闭vmode
    virsh suspend vmode #暂停到内存
    virsh resume vmode  #从内存恢复
    virsh save vmode    #将虚拟机状态保存到本地磁盘
    virsh restore vmode #将虚拟机从本地磁盘恢复到正常状态
    virsh destroy vmode  #强行断电,“拔电源”
    virsh autostart vmode  #随物理机开机自启动
    virsh autostart --disable vmode  #取消随物理机开机自启动
    virsh setvcpus centos7-s1 6 --live #在线增加vcpu数量,前提是在maximum vcpus范围内
    virsh setvcpus centos7-s1 16 --maximum --config #修改vcpu上限,下次重启才生效
    virsh setmem centos7-s1 1024000 --live  #在线增加内存大小,单位kb,前提是在maximum mem范围内
    virsh setmaxmem centos7-s1 2048000 --config  #修改最大内存大小上限,下次重启才生效
    提示:在虚拟机中可以动态enable/disable某个cpu
    echo 1 >/sys/devices/system/cpu/cpu1/online #enable
    echo 0 >/sys/devices/system/cpu/cpu1/online #disable


    E.管理lxc
    1.virsh -c lxc://

    root@router:~#virsh -c lxc://

    Welcome to virsh, the virtualization interactive terminal.


    Type:  'help' for help with commands

           'quit' to quit


    virsh # list

     Id    Name                           State

    ----------------------------------------------------

     26889 cirros                         running


    virsh # console 26889

    2.virsh-manager -c lxc://

    mkdir -p /var/lib/libvirt/lxc/cirros

    curl http://download.cirros-cloud.net/0.3.5/cirros-0.3.5-x86_64-lxc.tar.gz|tar -zxvf - -C /var/lib/libvirt/lxc/cirros


    新建操作系统容器,指定cirros目录即可

    KVM使用小记

    KVM使用小记

    KVM使用小记

    KVM使用小记

    KVM使用小记



  • 相关阅读:
    七牛云的 python sdk 是如何 批量删除资源的
    mysql 主从复制
    django3上线部署踩的坑
    基于linux在线预览
    数据库2
    数据库3
    安装 webstorm--->vue
    Django基础1
    pymysql基础
    前段之jQuery
  • 原文地址:https://www.cnblogs.com/lixuebin/p/10814522.html
Copyright © 2011-2022 走看看