• Centos使用KVM创建虚拟机


    服务器做raid阵列

    分区格式化挂载

    [root@localhost home]# lsblk
    NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda               8:0    0  1.8T  0 disk
    ├─sda1            8:1    0  500M  0 part /boot
    └─sda2            8:2    0  1.8T  0 part
      ├─centos-root 253:0    0   50G  0 lvm  /
      ├─centos-swap 253:1    0 31.5G  0 lvm  [SWAP]
      └─centos-home 253:2    0  1.8T  0 lvm  /home
    sdb               8:16   0  3.7T  0 disk
    [root@localhost home]# fdisk -l
    
    磁盘 /dev/sda:1999.8 GB, 1999844147200 字节,3905945600 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    磁盘标签类型:dos
    磁盘标识符:0x000aa786
    
       设备 Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048     1026047      512000   83  Linux
    /dev/sda2         1026048  3905945599  1952459776   8e  Linux LVM
    
    磁盘 /dev/sdb:3999.7 GB, 3999688294400 字节,7811891200 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    
    
    磁盘 /dev/mapper/centos-root:53.7 GB, 53687091200 字节,104857600 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    
    
    磁盘 /dev/mapper/centos-swap:33.8 GB, 33822867456 字节,66060288 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    
    
    磁盘 /dev/mapper/centos-home:1911.7 GB, 1911738597376 字节,3733864448 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    
    
    [root@localhost home]# parted /dev/sdb
    GNU Parted 3.1
    使用 /dev/sdb
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    (parted) mklabel gpt
    (parted) mkpart primary 0 3999.7GB
    警告: The resulting partition is not properly aligned for best performance.
    忽略/Ignore/放弃/Cancel? cancel
    (parted) p
    Model: DELL PERC H700 (scsi)
    Disk /dev/sdb: 4000GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags:
    
    Number  Start  End  Size  File system  Name  标志
    
    (parted) q
    信息: You may need to update /etc/fstab.
    
    [root@localhost home]# fdisk -l
    
    磁盘 /dev/sda:1999.8 GB, 1999844147200 字节,3905945600 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    磁盘标签类型:dos
    磁盘标识符:0x000aa786
    
       设备 Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048     1026047      512000   83  Linux
    /dev/sda2         1026048  3905945599  1952459776   8e  Linux LVM
    
    磁盘 /dev/sdb:3999.7 GB, 3999688294400 字节,7811891200 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    磁盘标签类型:dos
    磁盘标识符:0x00000000
    
       设备 Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1  4294967295  2147483647+  ee  GPT
    
    磁盘 /dev/mapper/centos-root:53.7 GB, 53687091200 字节,104857600 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    
    
    磁盘 /dev/mapper/centos-swap:33.8 GB, 33822867456 字节,66060288 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    
    
    磁盘 /dev/mapper/centos-home:1911.7 GB, 1911738597376 字节,3733864448 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    
    [root@localhost home]# mkfs.ext4 -F /dev/sdb
    mke2fs 1.42.9 (28-Dec-2013)
    文件系统标签=
    OS type: Linux
    块大小=4096 (log=2)
    分块大小=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    244121600 inodes, 976486400 blocks
    48824320 blocks (5.00%) reserved for the super user
    第一个数据块=0
    Maximum filesystem blocks=3124756480
    29800 block groups
    32768 blocks per group, 32768 fragments per group
    8192 inodes per group
    Superblock backups stored on blocks:
            32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
            4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
            102400000, 214990848, 512000000, 550731776, 644972544
    
    Allocating group tables: 完成
    正在写入inode表: 完成
    Creating journal (32768 blocks): 完成
    Writing superblocks and filesystem accounting information: 完成
    
    [root@localhost home]# df -h
    文件系统                 容量  已用  可用 已用% 挂载点
    /dev/mapper/centos-root   50G  3.1G   47G    7% /
    devtmpfs                  24G     0   24G    0% /dev
    tmpfs                     24G     0   24G    0% /dev/shm
    tmpfs                     24G  8.7M   24G    1% /run
    tmpfs                     24G     0   24G    0% /sys/fs/cgroup
    /dev/sda1                497M  124M  373M   25% /boot
    /dev/mapper/centos-home  1.8T   33M  1.8T    1% /home
    [root@localhost home]# mount /dev/sdb /bigdata/
    [root@localhost home]# vi /etc/fstab
    [root@localhost home]# df -h
    文件系统                 容量  已用  可用 已用% 挂载点
    /dev/mapper/centos-root   50G  3.1G   47G    7% /
    devtmpfs                  24G     0   24G    0% /dev
    tmpfs                     24G     0   24G    0% /dev/shm
    tmpfs                     24G  8.7M   24G    1% /run
    tmpfs                     24G     0   24G    0% /sys/fs/cgroup
    /dev/sda1                497M  124M  373M   25% /boot
    /dev/mapper/centos-home  1.8T   33M  1.8T    1% /home
    /dev/sdb                 3.6T   89M  3.4T    1% /bigdata

    内核级虚拟化技术KVM

    Kernel-based Virtual Machine

    [root@localhost bigdata]# yum install qemu-kvm qemu-kvm-tools virt-manager libvirt virt-install -y
    • qemu-kvm 虚拟化软件
    • virsh virt-manager virt-viewer virt-install other-tools是libvirt用户态的工具
    [root@localhost bigdata]# systemctl start libvirtd
    [root@localhost bigdata]# systemctl enable libvirtd
    Created symlink from /etc/systemd/system/sockets.target.wants/virtlockd.socket to /usr/lib/systemd/system/virtlockd.socket.
    [root@localhost bigdata]# ifconfig
    em1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.10.88  netmask 255.255.255.0  broadcast 192.168.10.255
            inet6 fe80::862b:2bff:fe18:a93f  prefixlen 64  scopeid 0x20<link>
            ether 84:2b:2b:18:a9:3f  txqueuelen 1000  (Ethernet)
            RX packets 3379310  bytes 5047701070 (4.7 GiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 1772728  bytes 119035768 (113.5 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    em2: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
            ether 84:2b:2b:18:a9:41  txqueuelen 1000  (Ethernet)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    em3: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
            ether 84:2b:2b:18:a9:43  txqueuelen 1000  (Ethernet)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    em4: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
            ether 84:2b:2b:18:a9:45  txqueuelen 1000  (Ethernet)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::1  prefixlen 128  scopeid 0x10<host>
            loop  txqueuelen 0  (Local Loopback)
            RX packets 14  bytes 1184 (1.1 KiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 14  bytes 1184 (1.1 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
            inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
            ether 52:54:00:c1:bc:67  txqueuelen 0  (Ethernet)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    virbr0 桥接网卡

    创建虚拟机

    安装VNC客户端 https://www.tightvnc.com/download.php

    [root@localhost bigdata]# qemu-img create -f raw /bigdata/Centos-7-x86_64.raw 120G
    Formatting '/bigdata/Centos-7-x86_64.raw', fmt=raw size=128849018880

    安装虚拟机

    [root@localhost bigdata]# virt-install --virt-type kvm --name Centos-7-x86_64 --ram 20480 --cdrom=/home/CentOS-7-x86_64-DVD-2003.iso --disk path=/bigdata/Centos-7-x86_64.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
    ERROR    主机不支持 任意虚拟化选项

    BIOS已经开启了虚拟化

    [root@localhost ~]# yum -y install qemu-kvm qemu-kvm-tools qemu-img virt-manager libvirt libvirt-python libvirt-client virt-install virt-viewer bridge-utils libguestfs-tools
    
    [root@localhost ~]# yum install -y openssl-1.0.2k*

    再安装

    打开VNCViewer,输入宿主机IP即可连接虚拟机

    [root@localhost ~]# virsh list --all
     Id    名称                         状态
    ----------------------------------------------------
     -     Centos-7-x86_64                关闭
    
    [root@localhost ~]# virsh start Centos-7-x86_64
    域 Centos-7-x86_64 已开始

    Libvirt 管理KVM虚拟机

    virsh 对虚拟机操作

    [root@localhost ~]# cd /etc/libvirt/
    [root@localhost libvirt]# ll
    总用量 80
    -rw-r--r--. 1 root root   450 5月  13 00:30 libvirt-admin.conf
    -rw-r--r--. 1 root root   547 5月  13 00:30 libvirt.conf
    -rw-r--r--. 1 root root 16529 5月  13 00:30 libvirtd.conf
    -rw-r--r--. 1 root root  1175 5月  13 00:30 lxc.conf
    drwx------. 2 root root  4096 6月  12 22:13 nwfilter
    drwx------. 3 root root    47 6月  12 23:21 qemu
    -rw-r--r--. 1 root root 30306 5月  13 00:30 qemu.conf
    -rw-r--r--. 1 root root  2169 5月  13 00:30 qemu-lockd.conf
    drwx------. 2 root root     6 6月  12 22:13 secrets
    drwxr-xr-x  3 root root    55 6月  12 23:21 storage
    -rw-r--r--. 1 root root  3202 5月  13 00:30 virtlockd.conf
    -rw-r--r--. 1 root root  3247 5月  13 00:30 virtlogd.conf
    [root@localhost libvirt]# cd qemu/
    [root@localhost qemu]# ll
    总用量 8
    -rw-------  1 root root 4099 6月  12 23:21 Centos-7-x86_64.xml
    drwx------. 3 root root   40 5月  13 00:30 networks

    里面的xml文件是虚拟机的配置

    [root@localhost qemu]# cat Centos-7-x86_64.xml
    <!--
    WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
    OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
      virsh edit Centos-7-x86_64
    or other application using the libvirt API.
    -->
    
    <domain type='kvm'>
      <name>Centos-7-x86_64</name>
      <uuid>e031c6cf-9b74-407d-b80c-4cf548657d15</uuid>
      <memory unit='KiB'>20971520</memory>
      <currentMemory unit='KiB'>20971520</currentMemory>
      <vcpu placement='static'>1</vcpu>
      <os>
        <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
        <boot dev='hd'/>
      </os>
      <features>
        <acpi/>
        <apic/>
      </features>
      <cpu mode='custom' match='exact' check='partial'>
        <model fallback='allow'>Nehalem</model>
      </cpu>
      <clock offset='utc'>
        <timer name='rtc' tickpolicy='catchup'/>
        <timer name='pit' tickpolicy='delay'/>
        <timer name='hpet' present='no'/>
      </clock>
      <on_poweroff>destroy</on_poweroff>
      <on_reboot>restart</on_reboot>
      <on_crash>destroy</on_crash>
      <pm>
        <suspend-to-mem enabled='no'/>
        <suspend-to-disk enabled='no'/>
      </pm>
      <devices>
        <emulator>/usr/libexec/qemu-kvm</emulator>
        <disk type='file' device='disk'>
          <driver name='qemu' type='raw'/>
          <source file='/bigdata/Centos-7-x86_64.raw'/>
          <target dev='vda' bus='virtio'/>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
        </disk>
        <disk type='file' device='cdrom'>
          <driver name='qemu' type='raw'/>
          <target dev='hda' bus='ide'/>
          <readonly/>
          <address type='drive' controller='0' bus='0' target='0' unit='0'/>
        </disk>
        <controller type='usb' index='0' model='ich9-ehci1'>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
        </controller>
        <controller type='usb' index='0' model='ich9-uhci1'>
          <master startport='0'/>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
        </controller>
        <controller type='usb' index='0' model='ich9-uhci2'>
          <master startport='2'/>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
        </controller>
        <controller type='usb' index='0' model='ich9-uhci3'>
          <master startport='4'/>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
        </controller>
        <controller type='pci' index='0' model='pci-root'/>
        <controller type='ide' index='0'>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
        </controller>
        <controller type='virtio-serial' index='0'>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
        </controller>
        <interface type='network'>
          <mac address='52:54:00:81:54:74'/>
          <source network='default'/>
          <model type='virtio'/>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
        </interface>
        <serial type='pty'>
          <target type='isa-serial' port='0'>
            <model name='isa-serial'/>
          </target>
        </serial>
        <console type='pty'>
          <target type='serial' port='0'/>
        </console>
        <channel type='unix'>
          <target type='virtio' name='org.qemu.guest_agent.0'/>
          <address type='virtio-serial' controller='0' bus='0' port='1'/>
        </channel>
        <input type='tablet' bus='usb'>
          <address type='usb' bus='0' port='1'/>
        </input>
        <input type='mouse' bus='ps2'/>
        <input type='keyboard' bus='ps2'/>
        <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
          <listen type='address' address='0.0.0.0'/>
        </graphics>
        <video>
          <model type='cirrus' vram='16384' heads='1' primary='yes'/>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
        </video>
        <memballoon model='virtio'>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
        </memballoon>
        <rng model='virtio'>
          <backend model='random'>/dev/urandom</backend>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
        </rng>
      </devices>
    </domain>

    修改CPU

    [root@localhost qemu]# virsh edit Centos-7-x86_64
    <domain type='kvm'>
      <name>Centos-7-x86_64</name>
      <uuid>e031c6cf-9b74-407d-b80c-4cf548657d15</uuid>
      <memory unit='KiB'>16777216</memory>
      <currentMemory unit='KiB'>16777216</currentMemory>
      <vcpu placement='auto' current="1">6</vcpu>
      <os>

    重启虚拟机

    [root@localhost qemu]# virsh shutdown Centos-7-x86_64
    域 Centos-7-x86_64 被关闭
    
    [root@localhost qemu]# virsh start Centos-7-x86_64

    会发现启动不了,xml文件中生成了

    <domain type='kvm'>
      <name>Centos-7-x86_64</name>
      <uuid>e031c6cf-9b74-407d-b80c-4cf548657d15</uuid>
      <memory unit='KiB'>16777216</memory>
      <currentMemory unit='KiB'>16777216</currentMemory>
      <vcpu placement='auto' current='1'>6</vcpu>
      <numatune>
        <memory mode='strict' placement='auto'/>
      </numatune>

    虚拟内存相关,strict改为preferred 

    成功开启

    https://blog.csdn.net/weixin_34260991/article/details/91380527

    [root@localhost qemu]# virsh setvcpus Centos-7-x86_64 6 --live

    内存

    virsh qemu-monitor-command Centos-7-x86_64 --hmp --cmd ballon 16777216

    网络配置

    [root@localhost qemu]# brctl show
    bridge name     bridge id               STP enabled     interfaces
    virbr0          8000.525400c1bc67       yes             virbr0-nic
                                                            vnet0

    默认情况下网桥走的是NAT

    现在网络是NAT模式,做成桥接模式

    创建桥接网卡,把虚拟机改成桥接网卡

    下面是本机的ip和网关

    编写脚本,运行

    #!/bin/bash
    
    brctl addbr br0
    brctl addif br0 em1
    ip addr del dev em1 192.168.10.88/24
    ifconfig br0 192.168.10.88/24 up
    route add default gw 192.168.10.1

    配置虚拟机

    [root@localhost qemu]# virsh edit Centos-7-x86_64
        <interface type='bridge'>
          <mac address='52:54:00:81:54:74'/>
          <source bridge='br0'/>
          <model type='virtio'/>

    进入虚拟机修改网卡文件

    cd /etc/sysconfig/network-scripts/

    修改ifcfg-etho

    TYPE=Ethernet
    PROXY_METHOD=none
    BROWSER_ONLY=no
    BOOTPROTO=static
    DEFROUTE=yes
    IPV4_FAILURE_FATAL=no
    #IPV6INIT=yes
    #IPV6_AUTOCONF=yes
    #IPV6_DEFROUTE=yes
    #IPV6_FAILURE_FATAL=no
    #IPV6_ADDR_GEN_MODE=stable-privacy
    NAME=eth0
    #UUID=c69d1864-5103-45af-b4b0-ae72313a35aa
    DEVICE=eth0
    ONBOOT=yes
    IPADDR=192.168.10.100
    NETMASK=255.255.255.0
    GATEWAY=192.168.10.1
    DNS1=223.6.6.6

     重启网络

    service network restart

    再克隆两台虚拟机

    第一台

    [root@localhost qemu]# virt-clone --connect qemu:///system --original Centos-7-x86_64 --name Centos-7-x86_64_2 --file /bigdata/Centos-7-x86_64_2.raw
    正在分配 'Centos-7-x86_64_2.raw'                        | 120 GB  01:25
    
    成功克隆 'Centos-7-x86_64_2'。
    [root@localhost qemu]# ll
    总用量 16
    -rw-------  1 root root 4184 6月  13 13:18 Centos-7-x86_64_2.xml
    -rw-------  1 root root 4178 6月  13 12:07 Centos-7-x86_64.xml
    drwx------. 3 root root   40 5月  13 00:30 networks
    
    [root@localhost qemu]# virsh list --all
     Id    名称                         状态
    ----------------------------------------------------
     -     Centos-7-x86_64                关闭
     -     Centos-7-x86_64_2              关闭

     第二台同上

    安装windows虚拟机

    如果客户机被占用

    ERROR    客户机名称 'window10' 已经在使用。
    
    [root@localhost ~]# virsh shutdown window10
    域 window10 被关闭
    
    [root@localhost ~]# virsh undefine window10
    域 window10 已经被取消定义

    如果关闭不了,可以直接删除 https://jingyan.baidu.com/article/219f4bf7b13e24de452d386f.html

    我的密码:000000

  • 相关阅读:
    好用的在线作图软件
    xml简介
    P1832 A+B Problem(再升级)
    P1831 杠杆数
    P1802 5倍经验日
    P1679 神奇的四次方数
    Android学习四、Android中的Adapter
    Android Activity Fragment 生命周期
    《C语言编写 学生成绩管理系统》
    线程池QueueUserWorkItem
  • 原文地址:https://www.cnblogs.com/aidata/p/13111163.html
Copyright © 2020-2023  润新知