Learning KVM - part6 掛載并擴(kuò)容磁盤(pán)

這一章節(jié)我們將來(lái)探討下如何熱添加虛擬磁盤(pán)檐蚜,熱擴(kuò)容虛擬磁盤(pán)宁仔。熱添加、熱擴(kuò)容意味著您可以這些操作可以在虛擬機(jī)保持運(yùn)行的狀態(tài)下執(zhí)行只盹,而不必關(guān)閉或者暫停虛擬機(jī)。KVM支持掛載物理磁盤(pán)或者虛擬磁盤(pán)兔院。為了創(chuàng)建虛擬磁盤(pán)殖卑,我們可以使用qemu-img命令工具創(chuàng)建“qcow2”或者“img”格式的虛擬磁盤(pán)。當(dāng)然使用強(qiáng)大的dd命令去創(chuàng)建一個(gè)非稀疏格式的的磁盤(pán)也是一個(gè)不錯(cuò)的選擇坊萝。


為虛擬機(jī)掛載SAN/SCSI物理磁盤(pán)

  1. 為宿主機(jī)掛載物理磁盤(pán)“sdb”孵稽;
[root@test-kvm images]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0     11:0    1  436K  0 rom  
vda    253:0    0   20G  0 disk 
└─vda1 253:1    0   20G  0 part /
sdb    253:16   0    1G  0 disk 
  1. 查看vm當(dāng)前磁盤(pán)映射關(guān)系;
[root@test-kvm images]# virsh list
Id    Name                           State
----------------------------------------------------
 1     generic                        running
 4     centos7                        running

[root@test-kvm images]# virsh domblklist centos7 --details
Type       Device     Target     Source
------------------------------------------------
file       disk       vda        /var/lib/libvirt/images/centos7.img
file       cdrom      hda        -
  1. 將“sdb”掛載到虛擬機(jī)的“vdb”磁盤(pán)上屹堰;
[root@test-kvm images]# virsh attach-disk centos7 --source /dev/sdb --target vdb --persistent
Disk attached successfully

[root@test-kvm images]# virsh domblklist centos7 --details
Type       Device     Target     Source
------------------------------------------------
file       disk       vda        /var/lib/libvirt/images/centos7.img
block      disk       vdb        /dev/sdb
file       cdrom      hda        -
  1. 登錄到虛擬機(jī)驗(yàn)證掛載情況;
[root@test-kvm images]# virsh console centos7
Connected to domain centos7
Escape character is ^]

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

localhost login: root
Password: 
Last login: Tue Jun 13 11:22:34 on ttyS0
[root@localhost ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0          11:0    1 1024M  0 rom  
vda         252:0    0   10G  0 disk 
├─vda1      252:1    0    1G  0 part /boot
└─vda2      252:2    0    9G  0 part 
  ├─cl-root 253:0    0    8G  0 lvm  /
  └─cl-swap 253:1    0    1G  0 lvm  [SWAP]
vdb         252:16   0    1G  0 disk 

創(chuàng)建并掛載虛擬磁盤(pán):

  1. 創(chuàng)建虛擬磁盤(pán)肛冶,我們可以使用qemu-imgdd命令創(chuàng)建虛擬磁盤(pán)街氢。
_請(qǐng)留意我們?cè)趧?chuàng)建“qcow2”格式磁盤(pán)的時(shí)候使用了“preallocation=metadata”選項(xiàng)扯键,如果不適用預(yù)分配格式,掛載的虛擬磁盤(pán)將讀取不到設(shè)置的大小_
[root@test-kvm images]# qemu-img create -f qcow2 -o preallocation=metadata centos7.disk1.qcow2 1G
Formatting 'centos7.disk1.qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 preallocation='metadata'  lazy_refcounts=off 
[root@test-kvm images]# qemu-img create -f raw centos7.disk2.img 256M
Formatting 'centos7.disk2.img', fmt=raw size=268435456 
[root@test-kvm images]# dd if=/dev/zero of=centos7.disk3.img bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 0.792503 s, 1.3 GB/s
[root@test-kvm images]# ll centos7.disk*
-rw-r--r-- 1 root root 1074135040 Jun 13 13:57 centos7.disk1.qcow2
-rw-r--r-- 1 root root  268435456 Jun 13 13:57 centos7.disk2.img
-rw-r--r-- 1 root root 1048576000 Jun 13 13:58 centos7.disk3.img
  1. 掛載創(chuàng)建的虛擬磁盤(pán)珊肃;
[root@test-kvm images]# virsh attach-disk centos7 --source /var/lib/libvirt/images/centos7.disk1.qcow2 --target vdb --persistent
Disk attached successfully

[root@test-kvm images]# virsh attach-disk centos7 --source /var/lib/libvirt/images/centos7.disk2.img --target vdc --persistent
Disk attached successfully

[root@test-kvm images]# virsh attach-disk centos7 --source /var/lib/libvirt/images/centos7.disk3.img --target vdd --persistent
Disk attached successfully

熱擴(kuò)容虛擬磁盤(pán)

  1. 格式化荣刑、mount虛擬磁盤(pán)馅笙;
[root@localhost ~]# mkdir /storage
[root@localhost /]# mkfs.xfs /dev/vdb
meta-data=/dev/vdb               isize=512    agcount=4, agsize=65560 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=262240, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost /]# mount /dev/vdb /storage
[root@localhost /]# mount -v | grep storage
/dev/vdb on /storage type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
  1. 查看已掛載的虛擬磁盤(pán)信息,找到“vdb”的block id厉亏;
[root@test-kvm images]# virsh domblklist centos7 --details
Type       Device     Target     Source
------------------------------------------------
file       disk       vda        /var/lib/libvirt/images/centos7.img
file       disk       vdb        /var/lib/libvirt/images/centos7.disk1.qcow2
file       disk       vdc        /var/lib/libvirt/images/centos7.disk2.img
file       disk       vdd        /var/lib/libvirt/images/centos7.disk3.img

[root@test-kvm images]# virsh pool-list
 Name                 State      Autostart 
-------------------------------------------
 default              active     yes       

[root@test-kvm images]# virsh vol-list default
 Name                 Path                                    
------------------------------------------------------------------------------
 CentOS-7-x86_64-Minimal-1611.iso /var/lib/libvirt/images/CentOS-7-x86_64-Minimal-1611.iso
 centos7.disk1.qcow2  /var/lib/libvirt/images/centos7.disk1.qcow2
 centos7.disk2.img    /var/lib/libvirt/images/centos7.disk2.img
 centos7.disk3.img    /var/lib/libvirt/images/centos7.disk3.img
 centos7.img          /var/lib/libvirt/images/centos7.img     
 cirros-0.3.4-x86_64-disk.img /var/lib/libvirt/images/cirros-0.3.4-x86_64-disk.img

[root@test-kvm images]# virsh qemu-monitor-command centos7 --hmp "info block"
drive-virtio-disk0: removable=0 io-status=ok file=/var/lib/libvirt/images/centos7.img ro=0 drv=qcow2 encrypted=0 bps=0 bps_rd=0   bps_wr=0 iops=0 iops_rd=0 iops_wr=0
drive-virtio-disk1: removable=0 io-status=ok file=/var/lib/libvirt/images/centos7.disk1.qcow2 ro=0 drv=raw encrypted=0 bps=0 bps_rd=0 bps_wr=0 iops=0 iops_rd=0 iops_wr=0
drive-virtio-disk2: removable=0 io-status=ok file=/var/lib/libvirt/images/centos7.disk2.img ro=0 drv=raw encrypted=0 bps=0 bps_rd=0 bps_wr=0 iops=0 iops_rd=0 iops_wr=0
drive-virtio-disk3: removable=0 io-status=ok file=/var/lib/libvirt/images/centos7.disk3.img ro=0 drv=raw encrypted=0 bps=0 bps_rd=0 bps_wr=0 iops=0 iops_rd=0 iops_wr=0
  1. 下面我們來(lái)為“vdb”擴(kuò)容董习;
__注意不要減少虛擬磁盤(pán)的大小爱只,這是不允許的,有可能會(huì)造成數(shù)據(jù)的丟失__
[root@test-kvm images]# virsh qemu-monitor-command centos7 --hmp "block_resize drive-virtio-disk1 2G"
  1. 現(xiàn)在虛擬磁盤(pán)的大小已經(jīng)擴(kuò)展到2G,但是要想使用這些擴(kuò)容的容量恬试,我們需要登錄到虛擬機(jī)使用命令xfs_growfs調(diào)整一下掛載文件的大小窝趣。
[root@test-kvm images]# virsh console centos7
Connected to domain centos7
Escape character is ^]

[root@localhost /]# lsblk | grep vdb
vdb         252:16   0    2G  0 disk /storage
[root@localhost /]# df -h /storage/
Filesystem      Size  Used Avail Use% Mounted on
/dev/vdb       1015M   33M  983M   4% /storage
[root@localhost /]# mount -v | grep storage
/dev/vdb on /storage type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
[root@localhost /]# xfs_growfs /storage/
meta-data=/dev/vdb               isize=512    agcount=4, agsize=65560 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=262240, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
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 262240 to 524288
[root@localhost /]# df -h /storage/
Filesystem      Size  Used Avail Use% Mounted on
/dev/vdb        2.0G   33M  2.0G   2% /storage
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市训柴,隨后出現(xiàn)的幾起案子哑舒,更是在濱河造成了極大的恐慌,老刑警劉巖幻馁,帶你破解...
    沈念sama閱讀 217,509評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件洗鸵,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡仗嗦,警方通過(guò)查閱死者的電腦和手機(jī)膘滨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)稀拐,“玉大人吏祸,你說(shuō)我怎么就攤上這事」澄茫” “怎么了贡翘?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,875評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)砰逻。 經(jīng)常有香客問(wèn)我鸣驱,道長(zhǎng),這世上最難降的妖魔是什么蝠咆? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,441評(píng)論 1 293
  • 正文 為了忘掉前任踊东,我火速辦了婚禮,結(jié)果婚禮上刚操,老公的妹妹穿的比我還像新娘闸翅。我一直安慰自己,他們只是感情好菊霜,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布坚冀。 她就那樣靜靜地躺著,像睡著了一般鉴逞。 火紅的嫁衣襯著肌膚如雪记某。 梳的紋絲不亂的頭發(fā)上司训,一...
    開(kāi)封第一講書(shū)人閱讀 51,365評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音液南,去河邊找鬼壳猜。 笑死,一個(gè)胖子當(dāng)著我的面吹牛滑凉,可吹牛的內(nèi)容都是我干的统扳。 我是一名探鬼主播,決...
    沈念sama閱讀 40,190評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼畅姊,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼闪幽!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起涡匀,我...
    開(kāi)封第一講書(shū)人閱讀 39,062評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤盯腌,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后陨瘩,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體腕够,經(jīng)...
    沈念sama閱讀 45,500評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評(píng)論 3 335
  • 正文 我和宋清朗相戀三年舌劳,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了帚湘。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,834評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡甚淡,死狀恐怖大诸,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情贯卦,我是刑警寧澤资柔,帶...
    沈念sama閱讀 35,559評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站撵割,受9級(jí)特大地震影響贿堰,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜啡彬,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評(píng)論 3 328
  • 文/蒙蒙 一羹与、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧庶灿,春花似錦纵搁、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,779評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春妄辩,著一層夾襖步出監(jiān)牢的瞬間惑灵,已是汗流浹背山上。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,912評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工眼耀, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人佩憾。 一個(gè)月前我還...
    沈念sama閱讀 47,958評(píng)論 2 370
  • 正文 我出身青樓哮伟,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親妄帘。 傳聞我的和親對(duì)象是個(gè)殘疾皇子楞黄,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容