14_虚拟机配置

发布时间 2023-10-11 16:23:18作者: 鸟叔书
1. 常用命令
# 1. 查看有几个启动的虚拟机
[root@stream9 ~]# virsh list
 Id   Name   State
--------------------


# 2. 查看所有虚拟机
[root@stream9 ~]# virsh list --all
 Id   Name   State
--------------------

# 3. 查看模板虚拟机的类型格式
cd /var/lib/libvirt/images # 记住次目录
[root@stream9 images]# ls
stream9.qcow2
[root@stream9 images]# qemu-img info stream9.qcow2
image: stream9.qcow2
file format: qcow2
virtual size: 20 GiB (21474836480 bytes)
disk size: 3.13 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
    extended l2: false
Child node '/file':
    filename: stream9.qcow2
    protocol type: file
    file length: 3.13 GiB (3360358400 bytes)
    disk size: 3.13 GiB


# 4. 根据镜像模板,克隆一个新的镜像
[root@stream9 images]# ls
stream9.qcow2
[root@stream9 images]# qemu-img create -f qcow2 -F qcow2 -b stream9.qcow2 vm1.img 20G
Formatting 'vm1.img', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=21474836480 backing_file=stream9.qcow2 backing_fmt=qcow2 lazy_refcounts=off refcount_bits=16
[root@stream9 images]#
[root@stream9 images]# ls
stream9.qcow2  vm1.img

# 5. 创建一个描述文件
cd /etc/libvirt/qemu

<domain type='kvm'>
  <name>vm1</name>
  <memory unit='KiB'>2097152</memory>
  <currentMemory unit='KiB'>2097152</currentMemory>
  <vcpu placement='static'>2</vcpu>
  <os>
    <type arch='x86_64' machine='pc'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='custom' match='exact' check='partial'>
    <model fallback='allow'>SandyBridge-IBRS</model>
    <feature policy='require' name='md-clear'/>
    <feature policy='require' name='spec-ctrl'/>
    <feature policy='require' name='ssbd'/>
  </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='qcow2'/>
      <source file='/var/lib/libvirt/images/vm1.img'/>
      <target dev='vda' bus='virtio'/>
    </disk>
    <interface type='bridge'>
       <source bridge='vbr1'/>
       <model type='virtio'/>
    </interface>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='vnc' port='5800' autoport='yes' listen='0.0.0.0' keymap='en-us'>
       <listen type='address' address='0.0.0.0'/>
    </graphics>
  </devices>
</domain>


# 6. 创建虚拟机
cd /etc/libvirt/qemu
[root@stream9 qemu]# ls
autostart  networks  stream9.xml  vm1.xml
[root@stream9 qemu]#
[root@stream9 qemu]# pwd
/etc/libvirt/qemu
[root@stream9 qemu]#
[root@stream9 qemu]# ls /var/lib/libvirt/images/
stream9.qcow2  vm1.img
[root@stream9 qemu]#
[root@stream9 qemu]#
[root@stream9 qemu]# virsh define vm1.xml
Domain 'vm1' defined from vm1.xml

[root@stream9 qemu]#
[root@stream9 qemu]# virsh start vm1
Domain 'vm1' started

[root@stream9 qemu]# virsh list
 Id   Name   State
----------------------
 1    vm1    running

# 7. 登录虚拟机
[root@stream9 qemu]# arp -a
? (10.1.1.254) at 00:50:56:ed:13:70 [ether] on eth0
? (10.10.11.197) at 52:54:00:e9:4f:7f [ether] on vbr1
? (10.1.1.1) at 00:50:56:c0:00:08 [ether] on eth0
? (10.1.1.2) at 00:50:56:ef:3e:18 [ether] on eth0
[root@stream9 qemu]#
[root@stream9 qemu]#
[root@stream9 qemu]# ssh root@10.10.11.197
Warning: Permanently added '10.10.11.197' (ED25519) to the list of known hosts.
root@10.10.11.197s password:
Last login: Wed Oct 11 00:31:01 2023 from 10.10.11.1
[root@stream9 ~]#
[root@stream9 ~]# lsblk
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
vda    253:0    0  20G  0 disk
└─vda1 253:1    0  20G  0 part /
[root@stream9 ~]#
[root@stream9 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        4.0M     0  4.0M   0% /dev
tmpfs           889M     0  889M   0% /dev/shm
tmpfs           356M  432K  356M   1% /run
/dev/vda1        20G  2.8G   18G  14% /
tmpfs           178M  4.0K  178M   1% /run/user/0
[root@stream9 ~]#
[root@stream9 ~]# exit
logout
Connection to 10.10.11.197 closed.

# 8. 删除虚拟机/释放虚拟机
[root@stream9 ~]# virsh list --all
 Id   Name   State
----------------------
 1    vm1    running
 2    vm2    running

[root@stream9 ~]#
[root@stream9 ~]# virsh destroy vm2
Domain 'vm2' destroyed

[root@stream9 ~]# virsh list --all
 Id   Name   State
-----------------------
 1    vm1    running
 -    vm2    shut off

[root@stream9 ~]#
[root@stream9 ~]# virsh undefine vm2
Domain 'vm2' has been undefined

[root@stream9 ~]#
[root@stream9 ~]# virsh list --all
 Id   Name   State
----------------------
 1    vm1    running


 # 9. 查看已经运行的虚拟机相信信息
 virsh dumpxml vm1

 # 10. 设置虚拟机名字 需要退出 exit 重新登录才能生效
 hostnamectl set-hostname vm1

 # 11. 删除虚拟网桥
 [root@kvm ~]# virsh net-list
 Name   State    Autostart   Persistent
-----------------------------------------
 vbr1   active   yes         yes
 vbr2   active   yes         yes
 vbr3   active   yes         yes

[root@kvm ~]#
[root@kvm ~]# virsh net-destroy vbr2
Network vbr2 destroyed

[root@kvm ~]# virsh net-destroy vbr3
Network vbr3 destroyed

[root@kvm ~]# virsh net-undefine vbr2
Network vbr2 has been undefined

[root@kvm ~]# virsh net-undefine vbr3
Network vbr3 has been undefined

[root@kvm ~]#
[root@kvm ~]# virsh net-list
 Name   State    Autostart   Persistent
-----------------------------------------
 vbr1   active   yes         yes

# 12. vbr1 如果没有启动,需要手动启动
2.简易俄罗斯方块
# 简易俄罗斯方块
for i in {10..50}; do
    sleep 1
    clear
    echo -e "\e[46m\e[${i};5H    \e[0m"
done