虛機安裝成功後,,學習虛機的管理linux
虛機的管理主要使用如下命令ruby
1. 虛機的管理服務器
a. 建立網絡
virt-install --name=wintest01 --ram 512 dom
--vcpus=2 --disk path=/data/wintest01.img,size=8 socket
--accelerate ide
--cdrom /data/iso/Windows2003.iso 工具
--vnc --vncport=5911 --vnclisten=0.0.0.0 學習
--network bridge=br0 --force --autostart ui
----------------------------------------------------------------------
- -n --name= 客戶端虛擬機名稱
- -r --ram= 客戶端虛擬機分配的內存
- -u --uuid= 客戶端UUID 默認不寫時,系統會自動生成
- --vcpus= 客戶端的vcpu個數
- -v --hvm 全虛擬化
- -p --paravirt 半虛擬化
- -l --location=localdir 安裝源,有本地、nfs、http、ftp幾種,多用於ks網絡安裝
- --vnc 使用vnc ,另有--vnclient=監聽的IP --vncport =VNC監聽的端口
- -c --cdrom= 光驅 安裝途徑
- --disk= 使用不一樣選項做爲磁盤使用安裝介質
- -w NETWORK, --network=NETWORK 鏈接客戶機到主機網絡
- -s --file-size= 使用磁盤映像的大小 單位爲GB
- -f --file= 做爲磁盤映像使用的文件
b. 克隆虛機
virt-clone -o oeltest01 -n oeltest02 -f /data/test02.img
Options(一些基本的選項): --version:查看版本 -h,--help:查看幫助信息 --connect=URI:鏈接到虛擬機管理程序 libvirt 的URI General Option(通常選項): -o ORIGINAL_GUEST, --original=ORIGINAL_GUEST:原來的虛擬機名稱 -n NEW_NAME, --name=NEW_NAME:新的虛擬機名稱 --auto-clone:從原來的虛擬機配置自動生成克隆名稱和存儲路徑。 -u NEW_UUID, --uuid=NEW_UUID:克隆虛擬機的新的UUID,默認值是一個隨機生成的UUID Storage Configuration(存儲配置): -f NEW_DISKFILE, --file=NEW_DISKFILE:指定新的虛擬機磁盤文件 --force-copy=TARGET:強制複製設備 --nonsparse:不使用稀疏文件複製磁盤映像 Networking Configuration:(網絡配置) -m NEW_MAC, --mac=NEW_MAC:設置一個新的mac地址,默認是一個隨機的mac
c. 虛機的管理
(1) KVM虛擬機默認配置文件位置: /etc/libvirt/qemu/
(2) virsh命令幫助# virsh -help
(3) 查看kvm虛擬機狀態# virsh list --all
(4) KVM虛擬機開機# virsh start oeltest01
(5) KVM虛擬機關機或斷電
默認狀況下virsh工具不能對linux虛擬機進行關機操做,linux操做系統須要開啓與啓動acpid服務。在安裝KVM linux虛擬機必須配置此服務。
# chkconfig acpid on
# service acpid restart
virsh關機
# virsh shutdown oeltest01
強制關閉電源
# virsh destroy wintest01
(6) 經過配置文件啓動虛擬機# virsh create /etc/libvirt/qemu/wintest01.xml
(7)配置開機自啓動虛擬機# virsh autostart oeltest01
(8) 導出KVM虛擬機配置文件# virsh dumpxml wintest01 > /etc/libvirt/qemu/wintest02.xml
(9) 添加與刪除KVM虛擬機
a. 刪除kvm虛擬機# virsh undefine wintest01
b. 從新定義虛擬機配置文件經過導出備份的配置文件恢復原KVM虛擬機的定義,並從新定義虛擬機。
# mv /etc/libvirt/qemu/wintest02.xml /etc/libvirt/qemu/wintest01.xml
# virsh define /etc/libvirt/qemu/wintest01.xml
(10) 編輯KVM虛擬機配置文件# virsh edit wintest01
(11) 掛起服務器# virsh suspend oeltest01
(12) 恢復服務器# virsh resume oeltest01
2. 虛機的調整
CPU添加
cpu添加有兩種方式:
1 建立虛擬機的時候能夠添加
# virt-install --help | grep cpu
--vcpus=VCPUS Number of vcpus to configure for your guest. Ex:
--vcpus 5
--vcpus 5,maxcpus=10
--vcpus sockets=2,cores=4,threads=2
安裝的時候手動能夠添加
--vcpus 5:添加5個虛擬vcpu 當前
--vcpus 5,maxcpus=10:當前CPU爲5 最大CPU設置爲10
2 建立好的 能夠編輯文件更改
更改前內容
<domain type='kvm'>
<name>CentOS-6.5-x86_64</name>
<uuid>e9258e70-6372-9820-2f2f-cb713c74e2fc</uuid>
<memory unit='KiB'>1048576</memory>
<currentMemory unit='KiB'>1048576</currentMemory>
<vcpu placement='static'>1</vcpu>
更改後的內容
# virsh list --all
Id Name State
----------------------------------------------------
2 CentOS-6.5-x86_64 running
# virsh edit CentOS-6.5-x86_64
Domain CentOS-6.5-x86_64 XML configuration edited.
<domain type='kvm'>
<name>CentOS-6.5-x86_64</name>
<uuid>e9258e70-6372-9820-2f2f-cb713c74e2fc</uuid>
<memory unit='KiB'>1048576</memory>
<currentMemory unit='KiB'>1048576</currentMemory>
<vcpu placement='auto' current="1">4</vcpu>
更改成當前1vcpu 最大可得到4vcpu
更改完以後須要重啓虛擬機
CentOS7 支持熱添加
#virsh setvcpus name 3 --live
熱添加總數不能超過最大CPU
內存添加
# virsh edit CentOS-6.5-x86_64
更改前
<domain type='kvm'>
<name>CentOS-6.5-x86_64</name>
<uuid>e9258e70-6372-9820-2f2f-cb713c74e2fc</uuid>
<memory unit='KiB'>1048576</memory>
<currentMemory unit='KiB'>1048576</currentMemory>
<vcpu placement='auto' current='1'>4</vcpu>
更改後
<domain type='kvm'>
<name>CentOS-6.5-x86_64</name>
<uuid>e9258e70-6372-9820-2f2f-cb713c74e2fc</uuid>
<memory unit='KiB'>4048576</memory>
<currentMemory unit='KiB'>1048576</currentMemory>
<vcpu placement='auto' current='1'>4</vcpu>
更改成最大4G 重啓虛擬機
查看內存
# virsh qemu-monitor-command CentOS-6.5-x86_64 --hmp --cmd info balloon
balloon: actual=1024
內存熱添加
# virsh qemu-monitor-command CentOS-6.5-x86_64 --hmp --cmd balloon 2048
經過命令修改KVM磁盤大小
創建KVM虛擬機的時候,硬盤大小隻給了10G。後來使用過程當中發現根目錄很快就100%,增長磁盤大小迫在眉睫。kvm 虛擬機的磁盤大小可經過命令:qemu-img resize filename size 來改,要注意的是resize只支持raw格式的磁盤文件,修改以後須要重啓。
1.查看虛擬機磁盤信息
[root@agent]# qemu-img info vm1.img
image: vm1.img
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 4.9G
2.修改磁盤大小
[root@agent]# qemu-img resize vm1.img +10G
Image resized.
3.查看結果
[root@agent]# qemu-img info vm1.img
image: vm1.img
file format: raw
virtual size: 20G (21474836480 bytes)
disk size: 4.9G