前言

KVM(Kernel-based Virtual Machine,基于内核的虚拟机)是一种内建于 Linux中的开源虚拟化技术。许多主流的云服务提供商(如OpenStack、Red Hat OpenShift等)都使用KVM作为虚拟化技术的基础,可以说,学习KVM,可以帮助我们理解虚拟化基础,快速熟悉云计算基础架构。

虚拟机使用的是考试提供的xnode1

操作过程

  1. 关闭防火墙与Selinux

    #关闭防火墙并禁止其开机自启
    [root@xnode1 ~]# systemctl stop firewalld && systemctl disable firewalld
    #顺便看一下状态是否关闭
    [root@xnode1 ~]# systemctl status firewalld
    ● firewalld.service - firewalld - dynamic firewall daemon
       Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
       Active: inactive (dead)
    
    Jun 08 13:12:35 xnode1 systemd[1]: Stopped firewalld - dynamic firewall daemon.
    
    #通过修改配置文件关闭Selinux
    [root@xnode1 ~]# sed -i "s/SELINUX=permissive/SELINUX=disabled/g" /etc/selinux/config
    #如果此时我们使用getenforce命令查看Selinux的执行模式,会发现并没有更改
    [root@xnode1 ~]# getenforce 
    Enforcing
    #这需要重新加载SELinux策略或者重启后才能生效,我们可以先使用setenforce命令临时关闭它
    [root@xnode1 ~]# setenforce 0
    
    
  2. 配置软件仓库

    #提供的xnode1在主目录下就有一个Centos的系统镜像,我们直接拿它挂载做仓库
    [root@xnode1 ~]# ls C*
    CentOS-7-x86_64-DVD-1511.iso
    #先创建挂载的目录
    [root@xnode1 ~]# mkdir /opt/centos
    

    关于挂载,简单直接的方式就是
    mount -o loop CentOS-7-x86_64-DVD-1511.iso /opt/centos
    但是这样只是临时挂载,重启后就失效了,需要重新打一次命令,比较麻烦。
    所以我提供两种思路,一种是自动挂载,一种是临时挂载。
    自动挂载

    #系统启动时会自动读取该文件里的内容,根据其配置挂载磁盘,所以我们只需要	
    把磁盘的挂载信息写入这个文件即可实现自动挂载。
    #但是如果配置有误,会导致虚拟机无法正常启动,所以并不推荐初学者使用这个方式
    [root@xnode1 ~]# vi /etc/fstab																										
    #在文件末尾写入以下配置
    /root/CentOS-7-x86_64-DVD-1511.iso /opt/centos  iso9660 defaults  0 0	
    #重启虚拟机查看对应目录就会发现已经自动挂载了
    [root@xnode1 ~]# ls /opt/centos/
    CentOS_BuildTag  EFI  EULA  GPL  images  isolinux  LiveOS  Packages  repodata  RPM-GPG-KEY-CentOS-7  RPM-GPG-KEY-CentOS-Testing-7  TRANS.TBL
    

    永久挂载

    #永久挂载实际上就是先挂载到一个无关的空目录下,再把所有文件拷贝到目标目录下,这样虚拟机重启后就不会导致文件丢失。该方法所耗时间比自动挂载久。
    [root@xnode1 ~]# mount -o loop CentOS-7-x86_64-DVD-1511.iso /mnt/	
    mount: /dev/loop2 is write-protected, mounting read-only					
    [root@xnode1 ~]# cp -rvf /mnt/* /opt/centos								
    #cp为拷贝命令,-rvf为选项
    #r为递归复制,及该文件夹及包含的文件全部复制,拷贝文件夹时这个参数必须加上,否则报错
    #v为显示命令执行的操作,可省略
    #F为强制复制,无论目标文件或目录是否存在。
    #*为通配符,代表单个或多个所有字符,这里意义为mnt目录下的所有文件。
    #最后再将最开始无关的目录取消挂载即可											
    [root@xnode1 ~]# umount /mnt/				
    

  3. 配置YUM源 
     

    #删除系统自带的默认源,你也可以移走备份,实操而言无所谓。
    [root@xnode1 ~]# rm -rf /etc/yum.repos.d/*
    #创建一个新的仓库文件,名称无所谓,你自己记得住就行,不过后缀必须是.repo
    [root@xnode1 ~]# vi /etc/yum.repos.d/local.repo 
    [centos]
    name=centos
    baseurl=file:///opt/centos
    enabled=1
    gpgcheck=0
    #检查可用性,统计出来3723个软件包就对了
    [root@xnode1 ~]# yum clean all && yum repolist
    Loaded plugins: fastestmirror
    ...... 
    Determining fastest mirrors
    repo id    repo name    status  
    centos     centos       3,723
    repolist: 3,723
    
  4. 安装KVM

    [root@xnode1 ~]# yum -y install qemu-kvm openssl libvirt
    ...
    Complete!
    #启动libvirtd服务
    [root@xnode1 ~]# systemctl start libvirtd
    #将/usr/libexec/qemu-kvm软链接为/usr/bin/qemu-kvm
    [root@xnode1 ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
    
  5. 创建NAT模式KVM虚拟机
     

    #这里需要用到的cirros镜像和脚本都在主目录下
    [root@xnode1 ~]# ls [cq]*
    cirros-0.3.4-x86_64-disk.img  qemu-ifup-NAT
    #不过这里有个坑,容我细细道来,我们先打开脚本看一下
    #!/bin/bash
    BRIDGE=virbr0
    NETWORK=192.168.122.0
    NETMASK=255.255.255.0
    GATEWAY=192.168.122.1
    DHCPRANGE=192.168.122.2,192.168.122.254
    TFTPROOT=
    BOOTP=
    function check_bridge()
    {
            if brctl show | grep "^$BRIDGE" &> /dev/null; then
                    return 1
            else
                    return 0
            fi
    }
    function create_bridge()
    {
                brctl addbr "$BRIDGE"
                brctl stp "$BRIDGE" on
                brctl setfd "$BRIDGE" 0
                ifconfig "$BRIDGE" "$GATEWAY" netmask "$NETMASK" up
    }
    function enable_ip_forward()
    {
            echo 1 > /proc/sys/net/ipv4/ip_forward
    }
    function add_filter_rules()
    {
            iptables -t nat -A POSTROUTING -s "$NETWORK"/"$NETMASK" \
                    ! -d "$NETWORK"/"$NETMASK" -j MASQUERADE
    }
    function start_dnsmasq()
    {
            # don't run dnsmasq repeatedly
            ps -ef | grep "dnsmasq" | grep -v "grep" &> /dev/null
            if [ $? -eq 0 ]; then
                    echo "Warning:dnsmasq is already running. No need to run it again."
                    return 1
            fi
            dnsmasq \
                    --strict-order \
                    --except-interface=lo \
    #脚本内容包括设置网络环境,创建并配置网桥、启用IP转发、添加NAT过滤规则以及启动dnsmasq服务来提供 DHCP和DNS功能等等。坑在是脚本定义的创建网桥的函数create_bridge()里
    function create_bridge()
    {
                brctl addbr "$BRIDGE"
                brctl stp "$BRIDGE" on
                brctl setfd "$BRIDGE" 0
                ifconfig "$BRIDGE" "$GATEWAY" netmask "$NETMASK" up
    }
    #这个函数用到了ifconfig命令,它需要依赖net-tools工具,但是在我们当前版本中,net-tools是没有默认安装的。所以如果我们执行脚本,是无法正常执行的。
    #所以在开始执行脚本之前,得先安装net-tools工具
    [root@xnode1 ~]# yum -y install net-tools
    ...
    Complete!
    #接着我们继续,先给脚本赋予执行权限
    [root@xnode1 ~]# chmod +x qemu-ifup-NAT
    #然后使用qemu-kvm命令启动KVM虚拟机
    [root@xnode1 ~]# qemu-kvm -m 1024 -drive file=cirros-0.3.4-x86_64-disk.img,if=virtio -net nic,model=virtio -net tap,script=qemu-ifup-NAT -nographic -vnc :1
    #-m 1024 指定了虚拟机的内存大小为1024MB
    #-drive  指定了虚拟机的磁盘驱动器;file指定了磁盘镜像文件的路径;if=virtio指定了使用VirtIO接口来连接磁盘
    #-net nic,model=virtio 添加了一个网络适配器到虚拟机中;model=virtio表示使用VirtIO模型的网卡
    #-net tap,script=qemu-ifup-NAT 指定了使用TAP设备来连接虚拟机,script=qemu-ifup-NAT指定了脚本来配置TAP设备
    #-nographic 以非图形化的方式启动虚拟机
    #-vnc :1 启用了VNC服务器,并指定了VNC服务器的端口号为 1
    #执行完毕后,我们就登入了虚拟机
      ____               ____  ____
     / __/ __ ____ ____ / __ \/ __/
    / /__ / // __// __// /_/ /\ \ 
    \___//_//_/  /_/   \____/___/ 
       http://cirros-cloud.net
    
    #使用默认的用户名cirros和密码cubswin:)登录
    login as 'cirros' user. default password: 'cubswin:)'. use 'sudo' for root.
    cirros login: cirros
    Password:
    $ 
    #最后查看一下ip地址和路由表有没有问题
    $ ip addr list
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
        inet6 ::1/128 scope host 
           valid_lft forever preferred_lft forever
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
        link/ether 52:54:00:12:34:56 brd ff:ff:ff:ff:ff:ff
        inet 192.168.122.89/24 brd 192.168.122.255 scope global eth0
        inet6 fe80::5054:ff:fe12:3456/64 scope link 
           valid_lft forever preferred_lft forever
    $ route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         192.168.122.1   0.0.0.0         UG    0      0        0 eth0
    192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0

结语 

后续还会发布一些云计算1+X中级实战的内容,但像一些基础的操作就不会再赘述了

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐