Linux下KVM虛擬機器基本管理及常用命令
阿新 • • 發佈:2018-11-24
說明:可能有重複
一、KVM的基本管理
1、檢視KVM虛擬機器配置檔案
#Kvm虛擬機器預設配置檔案位置 [[email protected] qemu]# pwd /etc/libvirt/qemu [[email protected] qemu]# ll total 12 -rw------- 1 root root 3863 Nov 6 17:26 CentOS6.5.xml -rw------- 1 root root 3771 Nov 7 00:57 CentOS6u7.xml -rw------- 1 root root 3628 Nov 7 02:04 centos.xml drwx------ 3 root root 40 Nov 7 00:46 networks
2、啟動與關閉
#顯示正在執行的虛擬機器 [[email protected] ~]# virsh list Id Name State ---------------------------------------------------- #顯示所有虛擬機器 [[email protected] ~]# virsh list --all Id Name State ---------------------------------------------------- - centos shut off - CentOS6.5 shut off - CentOS6u7 shut off #啟動名字為CentOS6.5的虛擬機器 [[email protected] ~]# virsh start CentOS6.5 #關閉名字為CentOS6.5的虛擬機器 [[email protected] ~]# virsh shutdown CentOS6.5 #強制關閉名字為Centos6.5的虛擬機器 [[email protected] ~]# virsh destroy CentOS6.5 #移除名字為CentOS6.5的虛擬機器 [[email protected] ~]# virsh undefine CentOS6.5 #設定CentOS6.5虛擬機器開機啟動 [[email protected]~]# virsh autostart CentOS6.5 #預設情況下virsh工具不能對linux虛擬機器進行關機操作 #linux作業系統需要開啟與啟動acpid服務。在安裝KVM linux虛擬機器必須配置此服務。 # yum -y install acpid # /etc/init.d/acpid start
3、通過配置檔案啟動虛擬機器
[[email protected] ~]# virsh create /etc/libvirt/qemu/CentOS6.5.xml
4、其它(掛起,恢復)virsh命令
#掛起伺服器 [[email protected] qemu]# virsh suspend CentOS6.5 #恢復伺服器 [[email protected] qemu]# virsh resume CentOS6.5
5、重新命名虛擬機器
#停止虛擬機器 [[email protected] qemu]# virsh shutdown CentOS6u5 #匯出虛擬機器的配置檔案 [[email protected] qemu]# pwd /etc/libvirt/qemu [[email protected] qemu-img]# virsh dumpxml CentOS6u5 > test-CentOS6u5.xml #更改配置檔案 [[email protected] qemu]# sed -i 's/CentOS6u5/test-CentOS6u5/g' test-CentOS6u5.xml #注 可以不用更改映象名 #移除原有的虛擬機器 [[email protected] qemu]# virsh undefine CentOS6u5 #載入新建的虛擬機器 [[email protected] qemu]# virsh define test-CentOS6u5.xml #啟動虛擬機器 [[email protected] qemu]# virsh start test-CentOS6u5
6、虛擬機器建立快照
#轉換磁碟映象檔案格式為qcow2 [[email protected] ~]# virsh shutdown test-CentOS6u5 [[email protected] qemu-img]# qemu-img convert -f raw CentOS6u5.raw -O qcow2 CentOS6u5.raw.qcow2 #快照一定需要qcow2格式才行 #我這邊從新建立一個虛擬機器以qcow2 #建立快照 [[email protected] qemu-img]# virsh snapshot-create CentOS6u5 #檢視快照 [[email protected] qemu-img]# virsh snapshot-list CentOS6u5 Name Creation Time State ------------------------------------------------------------ 1479043349 2016-11-13 08:22:29 -0500 shutoff #恢復快照 [[email protected] qemu-img]# virsh snapshot-revert CentOS6u5 1479043349 #刪除快照 [[email protected] qemu-img]# virsh snapshot-delete CentOS6u5 1479043349
二、常用命令
列出所有的虛擬機器
virsh list --all
顯示虛擬機器資訊
virsh dominfo kvm-1
顯示虛擬機器記憶體和cpu的使用情況
yum install virt-top -y virt-top
顯示虛擬機器分割槽資訊
virt-df kvm-1
關閉虛擬機器(shutodwn)
virsh shutdown kvm-1
啟動虛擬機器
virsh start kvm-1
設定虛擬機器(kvm-1)跟隨系統自啟
virsh autostart kvm-1
關閉虛擬及自啟
virsh autostart --disable kvm-1
刪除虛擬機器
virsh undefine kvm-1
通過控制視窗登入虛擬機器
virsh console kvm-1
其它
virsh shutdown centos 關閉centos虛擬機器 virsh destroy centos 強制關閉centos虛擬機器 virsh suspend centos 暫停centos虛擬機器 vrish resume centos 恢復centos虛擬機器 virsh reboot centos 重啟centos虛擬機器 virsh edit centos 編輯centos虛擬機器 virsh vncdisplay centos vnc顯示
給虛擬機器新增硬碟
新增硬碟(lvm卷)或者USB到虛擬機器上
virsh attach-disk kvm-1 /dev/sdb vbd --driver qemu --mode shareable
使用完成之後可以解除安裝usb
virsh detach-disk kvm vdb
新增lvm卷,並掛載
[[email protected] ~]# lvcreate -n kvm-1-data -L 50G vg_shkvm1 [[email protected] ~]# virsh attach-disk kvm-1 /dev/vg_shkvm1/kvm-1-data vdb --driver qemu --mode shareable Disk attached successfully # 登入到kvm-1上檢視lvm是否已經被掛載 [[email protected] ~]# virsh console kvm-1 # 輸入kvm-1的使用者和密碼 [[email protected] ~]# fdisk -l # 檢視硬碟掛載情況 Disk /dev/vda: 21.5 GB, 21474836480 bytes 16 heads, 63 sectors/track, 41610 cylinders Units = cylinders of 1008 * 512 = 516096 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00058197 Device Boot Start End Blocks Id System /dev/vda1 * 3 1018 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/vda2 1018 41611 20458496 8e Linux LVM Partition 2 does not end on cylinder boundary. Disk /dev/mapper/VolGroup-lv_root: 18.8 GB, 18798870528 bytes 255 heads, 63 sectors/track, 2285 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/mapper/VolGroup-lv_swap: 2147 MB, 2147483648 bytes 255 heads, 63 sectors/track, 261 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/vdb: 53.7 GB, 53687091200 bytes # 新新增的硬碟 16 heads, 63 sectors/track, 104025 cylinders Units = cylinders of 1008 * 512 = 516096 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000
格式化新新增的vdb,並新增到lvm組中
# 對新新增的硬碟分割槽 [[email protected] ~]# fdisk /dev/vdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xf04b6807. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): m # 檢視幫助 Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition's system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only) Command (m for help): n #新增一個分割槽 Command action e extended p primary partition (1-4) p #選擇新增一個擴充套件分割槽 Partition number (1-4): Value out of range. Partition number (1-4): 1 First cylinder (1-104025, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-104025, default 104025): Using default value 104025 Command (m for help): t #改變分割槽的格式 Selected partition 1 Hex code (type L to list codes): 8e #改成lvm Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): w # 儲存更改 [email protected] ~]# mkfs.ext4 /dev/vdb1 # 格式化分割槽 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 3276800 inodes, 13107142 blocks 655357 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=4294967296 400 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 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 28 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [[email protected] ~]# pvc reate /dev/vdb1 # 建立pv vdb vdb1 [[email protected] ~]# vgextend VolGroup /dev/vdb # 擴充套件lvm vg vdb vdb1 [[email protected] ~]# vgs VG #PV #LV #SN Attr VSize VFree VolGroup 2 2 0 wz--n- 69.50g 50.00g # 從上面能看出,新新增的 已經加到lvm組中
改變虛擬機器的引數
通過命令列更改建立之後虛擬機器的記憶體,cpu等資訊
更改記憶體
# 1. 檢視虛擬機器當前記憶體 [[email protected] ~]# virsh dominfo kvm-1 | grep memory Max memory: 4194304 KiB Used memory: 4194304 KiB # 2、動態設定記憶體為512MB,記憶體減少 virsh setmem kvm-1 524288 # 注意單位必須是KB # 3、檢視記憶體變化 # virsh dominfo kvm-1 | grep memory Max memory: 14194304 KiB Used memory: 524288 kiB # 4、記憶體增加 virsh shutdown kvm-1 virsh edit kvm-1 # 直接更改memory virsh create /etc/libvirt/demu/kvm-1/xml # 之後操作1,2,3步驟增加記憶體
更改CPU
需要修改配置檔案,因此需要停止虛擬機器
virsh shutdown kvm-1 virsh edit kvm-1 # <vcpu>2</vcpu> # 4 > 2 virsh create /etc/libvirt/demu/kvm-1/xml
硬碟擴容
1. Create a 10-GB non-sparse file: # dd if=/dev/zero of=/vm-images/vm1-add.img bs=1M count=10240 2. Shutdown the VM: # virsh shutdown vm1 3. Add an extra entry for ‘disk’ in the VM's XML file in /etc/libvirt/qemu. You can look copy & paste the entry for your mail storage device and just change the target and address tags. For example: # virsh edit vm1 <disk type='file' device='disk'> <driver name='qemu' type='raw' cache='none' io='threads'/> <source file='/vm-images/vm1.img'/> <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> </disk> Add: <disk type='file' device='disk'> <driver name='qemu' type='raw' cache='none' io='threads'/> <source file='/vm-images/vm1-add.img'/> <target dev='vdb' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> </disk> # 這裡建議使用上面的新增硬碟的方式新增
刪除虛擬機器
第一步,停掉虛擬機器
virsh shutdown kvm-1
第二步
virsh destroy kvm-1
第三步
virsh undefine kvm-1
第四步
rm /dev/vg_shkvm1/kvm-1 # 不建議刪除硬