Day_1_云计算_KVM--2

Day_1_云计算_KVM--2_第1张图片
image.png
Day_1_云计算_KVM--2_第2张图片
image.png

4.7 kvm虚拟机克隆

1. 完整克隆
1.1 自动挡克隆
[root@kvm01 /data]# virt-clone --auto-clone -o web-blog -n web-www
Allocating 'web-www.qcow2'                            |  10 GB  00:57     

Clone 'web-www' created successfully.

[root@kvm01 /data]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 2     centos6                        running
 -     web-blog                       shut off
 -     web-www                        shut off

1.2 手动挡克隆(为完善)
#准备磁盘文件
[root@kvm01 /data]# cp web-blog.qcow2 web-bbs.qcow2
[root@kvm01 /data]# ll
total 7200820
-rw------- 1 root root 10737418240 Jul 15 17:34 centos2.raw
-rw-r--r-- 1 qemu qemu  1389953024 Jul 16 09:45 centos6.qcow2
-rw------- 1 root root 10737418240 Jul 15 21:42 centos6.raw
-rw-r--r-- 1 root root  1260126208 Jul 16 09:44 web-bbs.qcow2
-rw-r--r-- 1 root root  1260126208 Jul 16 09:20 web-blog.qcow2
-rw------- 1 qemu qemu  1031471104 Jul 16 09:44 web-www.qcow2

#生成配置文件
virsh dumpxml web01 >web02.xml
###虚拟机名字
###删除uuid
###删除mac地址
###修改磁盘路径


2. 链接克隆
[root@kvm01 /data]# qemu-img create -f qcow2 -b centos2.raw  centos2-test.qcow2
Formatting 'centos2-test.qcow2', fmt=qcow2 size=10737418240 backing_file='centos2.raw' encryption=off cluster_size=65536 lazy_refcounts=off 

[root@kvm01 /data]# qemu-img info centos2-test.qcow2 
image: centos2-test.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 196K
cluster_size: 65536
backing file: centos2.raw
Format specific information:
    compat: 1.1
    lazy refcounts: false

#准备磁盘文件
[root@kvm01 /data]# qemu-img create -f qcow2 -b web-blog.qcow2 web-blog▽-test.qcow2
Formatting 'web-blog-test.qcow2', fmt=qcow2 size=10737418240 backing_file='web-blog.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off 

#导出配置文件
[root@kvm01 /data]# virsh dumpxml web-blog > web-blog-test.xml

#编辑配置文件
[root@kvm01 /data]# vim web-blog-test.xml
#修改虚拟机名字
#删除虚拟机uuid
#修改磁盘路径
#删除mac地址

#导入配置文件
[root@kvm01 /data]# virsh define web-blog-test.xml 
Domain web-blog-test defined from web-blog-test.xml

#启动
[root@kvm01 /data]# virsh start web-blog-test 
Domain web-blog-test started

#查看
[root@kvm01 /data]# virsh list
 Id    Name                           State
----------------------------------------------------
 2     centos6                        running
 3     web-www                        running
 4     web-blog-test                  running


4.8 kvm虚拟机的桥接网络

1. 添加网卡
[root@kvm01 /data]#  virsh iface-bridge eth0 br0
?reated bridge br0 with attached device eth0
Bridge interface br0 started

2. 编辑配置文件
[root@kvm01 /data]# virsh edit web-www 
   
      
      

3. 远程连接
[root@kvm01 /data]# virsh console web-www 
Connected to domain web-www
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-327.el7.x86_64 on an x86_64

localhost login: root
Password: 
Last login: Tue Jul 16 07:52:05 on ttyS0
[root@localhost ~]# 

4. 验证IP
[root@Fvc ~]# ip a 
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:f0:5e:d7 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.128/24 brd 10.0.0.255 scope global dynamic eth0
       valid_lft 1291sec preferred_lft 1291sec
    inet6 fe80::5054:ff:fef0:5ed7/64 scope link 
       valid_lft forever preferred_lft forever

[root@Fvc ~]# lscpu|grep -i kvm
Hypervisor vendor:     KVM

创建桥接网卡失败的原因:
1.宿主机的王可ip地址,不可以dhcp获取

2.开启了networkmanoge


4.9 热添加技术

热添加硬盘、网卡、内存、CPU
4.9.1 热添加硬盘
#添加一块盘
[root@kvm01 /data]# qemu-img create -f qcow2 web-blog.qcow2 50G
Formatting 'web-blog.qcow2', fmt=qcow2 size=53687091200 encryption=off cluster_size=65536 lazy_refcounts=off 

临时生效
[root@kvm01 /data]# virsh attach-disk web-blog /data/web-blog.qcow2 vdb --subdriver qcow2
Disk attached successfully

永久生效
[root@kvm01 /data]# virsh attach-disk web-blog /data/web-blog.qcow2 vdb --subdriver qcow2 --config 
Disk attached successfully

剥离硬盘
扩容
在虚拟机里把扩容盘的挂载目录卸载掉 剥离硬盘 virsh detach-disk web01 vdb 调整容量 qemu-img resize 再次附加硬盘 再次挂载扩容盘 用xfs_growfs(resize2fs)更新扩容盘超级块信息

临时生效

virsh attach-disk web01 /data/web01-add.qcow2 vdb --subdriver qcow2

永久生效

virsh attach-disk web01 /data/web01-add.qcow2 vdb --subdriver qcow2 --config

临时剥离硬盘

virsh detach-disk web01 vdb

永久剥离硬盘

virsh detach-disk web01 vdb --config

扩容:

在虚拟机里把扩容盘的挂载目录,卸载掉

剥离硬盘virsh detach-disk web01 vdb

调整容量qemu-img resize

再次附加硬盘virsh attach-disk web01 /data/web01-add.qcow2 vdb --subdriver qcow2

再次挂载扩容盘

用xfs_growfs更新扩容盘超级块信息

* * *
4.9.2 热添加网卡
[root@kvm01 /data]# virsh attach-interface web04 --type bridge --source br0 --model virtio
Interface attached successfully

4.9.3 热添加内存
[root@kvm01 /data]# qemu-img create -f qcow2 -b web-www.qcow2 web04.qcow2
Formatting 'web04.qcow2', fmt=qcow2 size=10737418240 backing_file='web-www.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off 

[root@kvm01 /data]# virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web04 --memory 512,maxmemory=2048 --vcpus 1 --disk /data/web04.qcow2 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole

Starting install...
Domain creation completed.

[root@kvm01 /data]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 2     centos6                        running
 6     web-www                        running
 7     web-blog-test                  running
 8     web-blog                       running
 9     web04                          running

#临时生效
[root@kvm01 /data]# virsh setmem web04 1024M

#永久生效
[root@kvm01 /data]# virsh setmem web04 1024M --config

#登陆web04查看
[root@kvm01 ~]# virsh console web04 
Connected to domain web04
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-327.el7.x86_64 on an x86_64

localhost login: root
Password: 
Last login: Tue Jul 16 15:51:34 on tty1

[root@localhost ~]# free -l
              total        used        free      shared  buff/cache   available
Mem:        1000504       77768      813644        8488      109092      797128
Low:        1000504      186860      813644
High:             0           0           0
Swap:             0           0           0
[root@localhost ~]# 

4.9.4热添加CPU

4.10 virt-manager和kvm虚拟机热迁移(共享的网络文件系统)

作业1:

扩容kvm虚拟机的根分区

关闭虚拟机
[root@kvm01 /data]# virsh list
 Id    Name                           State
----------------------------------------------------

宿主机磁盘扩容
# 扩容前
[root@kvm01 /data]# qemu-img info web-www.qcow2 
image: web-www.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 984M
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true

# 扩容
[root@kvm01 /data]# qemu-img resize web-www.qcow2 +10G
Image resized.

# 扩容后
[root@kvm01 /data]# qemu-img info web-www.qcow2 
image: web-www.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 984M
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true

进入虚拟机
[root@kvm01 ~]# virsh console web-www 
Connected to domain web-www
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-327.el7.x86_64 on an x86_64

localhost login: root
Password: 
Last login: Tue Jul 16 15:51:34 on tty1

# 检查,没有变化 
[root@localhost ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        10G  912M  9.1G   9% /
devtmpfs        488M     0  488M   0% /dev
tmpfs           497M     0  497M   0% /dev/shm
tmpfs           497M  6.6M  491M   2% /run
tmpfs           497M     0  497M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/0

在虚拟机中调整分区
[root@localhost ~]# fdisk /dev/vda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): p  #p

Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000076b4

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20971519    10484736   83  Linux

Command (m for help): d  #d
Selected partition 1
Partition 1 is deleted

Command (m for help): n  #n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):   #回车
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):   #回车
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set

Command (m for help): w  #w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

重启虚拟机
# 重启
[root@localhost ~]# reboot
[  945.204165] Restarting system.

# 进入虚拟机再次查看
[root@localhost ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        10G  913M  9.1G   9% /
devtmpfs        488M     0  488M   0% /dev
tmpfs           497M     0  497M   0% /dev/shm
tmpfs           497M  6.6M  491M   2% /run
tmpfs           497M     0  497M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/0

# 更新超级块
[root@localhost ~]# xfs_growfs /dev/vda1
meta-data=/dev/vda1              isize=256    agcount=4, agsize=655296 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=2621184, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2621184 to 5242624

# 再次检查,确认ok
[root@localhost ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        20G  913M   20G   5% /
devtmpfs        488M     0  488M   0% /dev
tmpfs           497M     0  497M   0% /dev/shm
tmpfs           497M  6.6M  491M   2% /run
tmpfs           497M     0  497M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user

作业2:

误删centos7虚拟机系统文件rm -fr /lib64/libc.so.6,如何恢复?

LD_PRELOAD=/usr/lib64/libc-2.17.so ln -s /usr/lib64/libc-2.17.so /lib64/libc.so.6

首先创建一个add的虚拟磁盘
qemu-img create -f qcow2 web02-add.qcow2 10G
然后添加给web04
virsh attach-disk web04 /opt/web02-add.qcow2 vdb --subdriver qcow2
mkfs.xfs /dev/vdb
mount /dev/vdb /mnt
然后切换到web05
删除里面的文件
再切换到web04
将web05缺失的文件拷贝到挂载目录下
然后卸载umount 如果busy则-l强制
出来后
virsh detach-disk web04 vdb
然后挂载到web05
然后拷贝出去![image](https://upload-images.jianshu.io/upload_images/16832986-60b35cf7c0342bb6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

你可能感兴趣的:(Day_1_云计算_KVM--2)