4.7 kvm虛擬機(jī)克隆
1. 完整克隆
1.1 自動(dòng)擋克隆
[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 手動(dòng)擋克隆(為完善)
#準(zhǔn)備磁盤文件
[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
###虛擬機(jī)名字
###刪除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
#準(zhǔn)備磁盤文件
[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
#導(dǎo)出配置文件
[root@kvm01 /data]# virsh dumpxml web-blog > web-blog-test.xml
#編輯配置文件
[root@kvm01 /data]# vim web-blog-test.xml
#修改虛擬機(jī)名字
#刪除虛擬機(jī)uuid
#修改磁盤路徑
#刪除mac地址
#導(dǎo)入配置文件
[root@kvm01 /data]# virsh define web-blog-test.xml
Domain web-blog-test defined from web-blog-test.xml
#啟動(dòng)
[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虛擬機(jī)的橋接網(wǎng)絡(luò)
1. 添加網(wǎng)卡
[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
<interface type='bridge'>
<mac address='52:54:00:f0:5e:d7'/>
<source bridge='br0'/>
3. 遠(yuǎn)程連接
[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. 驗(yàn)證IP
[root@Fvc ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> 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
創(chuàng)建橋接網(wǎng)卡失敗的原因:
1.宿主機(jī)的王可ip地址据途,不可以dhcp獲取
2.開啟了networkmanoge
4.9 熱添加技術(shù)
熱添加硬盤干发、網(wǎng)卡朱巨、內(nèi)存、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
臨時(shí)生效
[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
剝離硬盤
擴(kuò)容
在虛擬機(jī)里把擴(kuò)容盤的掛載目錄卸載掉 剝離硬盤 virsh detach-disk web01 vdb 調(diào)整容量 qemu-img resize 再次附加硬盤 再次掛載擴(kuò)容盤 用xfs_growfs(resize2fs)更新擴(kuò)容盤超級(jí)塊信息
臨時(shí)生效
virsh attach-disk web01 /data/web01-add.qcow2 vdb --subdriver qcow2
永久生效
virsh attach-disk web01 /data/web01-add.qcow2 vdb --subdriver qcow2 --config
臨時(shí)剝離硬盤
virsh detach-disk web01 vdb
永久剝離硬盤
virsh detach-disk web01 vdb --config
擴(kuò)容:
在虛擬機(jī)里把擴(kuò)容盤的掛載目錄枉长,卸載掉
剝離硬盤virsh detach-disk web01 vdb
調(diào)整容量qemu-img resize
再次附加硬盤virsh attach-disk web01 /data/web01-add.qcow2 vdb --subdriver qcow2
再次掛載擴(kuò)容盤
用xfs_growfs更新擴(kuò)容盤超級(jí)塊信息
* * *
4.9.2 熱添加網(wǎng)卡
[root@kvm01 /data]# virsh attach-interface web04 --type bridge --source br0 --model virtio
Interface attached successfully
4.9.3 熱添加內(nèi)存
[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
#臨時(shí)生效
[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虛擬機(jī)熱遷移(共享的網(wǎng)絡(luò)文件系統(tǒng))
作業(yè)1:
擴(kuò)容kvm虛擬機(jī)的根分區(qū)
關(guān)閉虛擬機(jī)
[root@kvm01 /data]# virsh list
Id Name State
----------------------------------------------------
宿主機(jī)磁盤擴(kuò)容
# 擴(kuò)容前
[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
# 擴(kuò)容
[root@kvm01 /data]# qemu-img resize web-www.qcow2 +10G
Image resized.
# 擴(kuò)容后
[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
進(jìn)入虛擬機(jī)
[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
在虛擬機(jī)中調(diào)整分區(qū)
[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.
重啟虛擬機(jī)
# 重啟
[root@localhost ~]# reboot
[ 945.204165] Restarting system.
# 進(jìn)入虛擬機(jī)再次查看
[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
# 更新超級(jí)塊
[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
# 再次檢查,確認(rèn)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
作業(yè)2:
誤刪centos7虛擬機(jī)系統(tǒng)文件rm -fr /lib64/libc.so.6搀暑,如何恢復(fù)沥阳?
LD_PRELOAD=/usr/lib64/libc-2.17.so ln -s /usr/lib64/libc-2.17.so /lib64/libc.so.6
首先創(chuàng)建一個(gè)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強(qiáng)制
出來(lái)后
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)