虚拟化常用命令

KVM环境安装

虚拟化配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 查看cpu是否支持虚拟化
grep -E 'svm|vmx' /proc/cpuinfo

# 查看虚拟化
cat /sys/module/kvm_intel/parameters/nested

# 开启虚拟化
vi /etc/modprobe.d/kvm-nested.conf

options kvm-intel nested=1
options kvm-intel enable_shadow_vmcs=1
options kvm-intel enable_apicv=1
options kvm-intel ept=1

modprobe -r kvm_intel
modprobe -a kvm_intel

# 工具安装
yum install qemu-kvm libvirt libvirt-python libguestfs-tools virt-instal -y

# 启动服务
systemctl enable libvirtd && systemctl start libvirtd
1
2
3
4
# 创建网桥
vi /etc/sysconfig/network-scripts/ifcfg-INTERFACE_NAME

BRIDGE=br0
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 设置br0网络
vi /etc/sysconfig/network-scripts/ifcfg-br0

DEVICE="br0"
# BOOTPROTO is up to you. If you prefer “static”, you will need to
# specify the IP address, netmask, gateway and DNS information.
BOOTPROTO="dhcp"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
ONBOOT="yes"
TYPE="Bridge"
DELAY="0"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# IP转发设置
vi /etc/sysctl.conf

net.ipv4.ip_forward = 1

# 查看配置结果
sysctl -p /etc/sysctl.conf

# 重启服务
systemctl restart NetworkManager

# 禁用SELinux

参考文档:https://linux.dell.com/files/whitepapers/KVM_Virtualization_in_RHEL_7_Made_Easy.pdf

参考文档:https://blog.csdn.net/Linuxprobe18/article/details/78944974

参考文档:https://www.jianshu.com/p/63368c433bfe

修改镜像(密码、配置文件)

1
2
3
4
5
6
7
8
9
yum install -y libguestfs-tools

export LIBGUESTFS_BACKEND=direct

virt-customize -a filename.qcow2 --root-password password:xxx

virt-customize -a filename.qcow2 \
  --edit '/etc/ssh/sshd_config:s/^#*PermitRootLogin.*/PermitRootLogin yes/' \
  --edit '/etc/ssh/sshd_config:s/^#*PasswordAuthentication.*/PasswordAuthentication yes/'

安装

 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
virt-install \
 --connect qemu:///system \
 --network bridge:br0 \
 --name INSTANCE_NAME \
 --ram=6000 \
 --vcpus=2 \
 --disk path=/home/vm/INSTANCE.img,size=100 \
 --graphics none \
 --location=/home/CentOS-7-x86_64-Minimal-1810.iso \s
 --extra-args="console=tty0 console=ttyS0,115200" \
 --noautoconsole \
 --autostart \
 --import

virt-install --name INSTANCE_NAME --vcpus 2 --disk path=./CentOS-7-x86_64-GenericCloud.qcow2 --import --autostart --network=bridge:br0,model=virtio --ram 8192 --noautoconsole

virt-filesystems --long -h --all -a ctr.qcow2
virt-filesystems --long -h --all -a com.qcow2

# ex:
virt-install --name ds --vcpus 4 --virt-type kvm --disk path=./ds.qcow2 --disk ./devstack.iso,device=cdrom  --import --autostart --os-type linux --network=bridge:br0 --ram 8192 --noautoconsole

virt-install -n dd -r 8192 --disk path=./dd.qcow2,size=100 -c ./ubuntu-22.04.1-live-server-amd64.iso --network=bridge:br0 --graphics vnc,listen=0.0.0.0,port=5910 --noautoconsole -v --cpus=4

virt-install -n base -r 16384 --disk path=./base.qcow2 -c ubuntu-22.04.1-live-server-amd64.iso --network=bridge:br0 --graphics vnc,listen=0.0.0.0,port=5910 --noautoconsole -v --cpus=4

virt-install --name base --vcpus 8 --disk path=./base.qcow2 --import --autostart --network=bridge:br0 --ram 16384 --noautoconsole

克隆

1
2
3
4
5
virt-clone \
 --connect qemu:///system \
 --original centos7-base \
 --name controller_01 \
 --file /home/vm/controller_01.img

网络

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
vi /etc/sysconfig/network-scripts/ifcfg-eth0

IPADDR=192.168.1.233
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=114.114.114.114

systemctl restart network

# hosts
vi /etc/hosts

192.168.1.231 controller
192.168.1.232 compute1

# hostname
vi /etc/hostname

USTC镜像源

1
2
3
4
5
6
sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' \
         -e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://mirrors.ustc.edu.cn/centos|g' \
         -i.bak \
         /etc/yum.repos.d/CentOS-Base.repo

yum makecache

磁盘扩容及分区

1
2
3
4
5
6
7
virsh shutdown xxx

qemu-img create -f qcow2 -o preallocation=metadata eve-ng.qcow2 100G

qemu-img resize ds.qcow2 100G
...分区
virt-resize --resize /dev/sda1=+200M --expand /dev/sda1 olddisk newdisk

虚拟机管理

 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
virsh start ctr
virsh start com

# 终止实例
virsh destroy INSTANCE

# 连接实例
virsh console INSTANCE

# 删除实例
virsh undefine INSTANCE

# 查看快照
virsh snapshot-list ctr
virsh snapshot-list com

# 创建快照
virsh snapshot-create-as ctr init
virsh snapshot-create-as com init

# 删除快照
virsh snapshot-delete INSTANCE --current

# 数据统计查看
openstack hypervisor stats show

# 文件系统查看
virt-filesystems --long -h --all -a


# ubuntu
ssh-keygen -A

network:
  ethernets:
    ens3:
      addresses: [192.168.1.235/24]
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses: [114.114.114.114]
  version: 2
  renderer: networkd

# 添加网卡
virsh attach-interface ds --type bridge --source br0
virsh attach-interface ds --type network --source default --model rtl8139  --live --config
# ubuntu

修改网卡名称

1
2
3
4
5
6
7
8
#
vi /etc/default/grub

GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"

#
update-grub
reboot
comments powered by Disqus