|
|
当我们需要扩展模板镜像的虚拟磁盘大小时,比如原来的虚拟磁盘大小为20G,现在我们想将其扩展到30G,那么我们可以根据如下步骤来操作。8 }. E4 E4 m: Y# s5 T: ^
+ C; G" x" F0 J& w4 k' o, x9 z; {
整个流程可以分为三个阶段:
, ^( x J, h/ h* Y* r9 k
) S/ }; h* g5 f4 O0 x S, {9 ?1、扩展KVM镜像磁盘文件大小到30G。
( l) y5 j! ?& G' F
1 f% q j# V; a0 t% }7 V2、扩展磁盘分区大小为30G。
9 J% `( r$ O k4 Q3 L+ I/ W/ g8 R1 f2 k2 ]
3、扩展文件系统大小为30G。
/ d* c5 K" }- i, z" J% h! ^9 k8 Z+ x1 M
假若当前有一个名为 test_extend.img 的模板镜像,其格式为 qcow2, virtual_size为 20G7 D3 A0 a' ^2 `( k; k7 h3 D
* ]) u% P# o" Z" B- Q1 A; M2 f
1、首先可以使用qemu-img来查看该模板镜像的元信息:
# W; l# k# c. |, }$ y. X1 L$ o# o* R/ t
复制代码+ t, G: R- m7 W
root@# qemu-img info test_extend.img
' ?: ?" V8 C( F, F7 Y- Ximage: test_extend.img/ \( V* E' s$ s7 I2 L
file format: qcow2
" X2 g3 b8 ~/ _% c# S7 rvirtual size: 20G (21474836480 bytes)( c' h4 U6 q+ w" s, E: ]
disk size: 309M5 n( L3 a. i4 [5 V) v
cluster_size: 65536 A: w5 x; A- T z1 ~
Format specific information:- N" W' K9 o$ s+ q1 W
compat: 1.1
9 h+ i8 X& N- D$ T: ^' K7 U1 L lazy refcounts: false
: N2 [/ T3 x4 j4 S3 e- n1 q* |root@ # * {/ `- F; X# r- b
复制代码! l: B. u, |8 N+ q8 w2 c7 ~
2、由于需要使用到qemu-nbd,因此我们需要先确认nbd模板已经被load:: N3 S9 H. r& c! p5 b9 `. j5 b- X& L+ N
% x3 [1 Q# e, B% eroot@ # lsmod | grep nbd$ `, `4 J# [" M; P/ Y
root@ #) q) H* x$ U2 W, o6 ~
执行如上命令发现没有任何输出,则表示当前系统并没有加载nbd模块
) ?/ h8 [$ B- G3 p! B% d7 e! z+ A. h
3、加载nbd模板,并再次确认(若没有nbd模块,则可以参考这里进行编译安装): [# C2 I2 x: d3 U( U) x [3 m
' \; O5 F6 {" U* t. ]0 E, ?1 ^% Q* lroot@ # modprobe nbd max_part=85 u: p2 ~# \. G% ?1 `
root@ # lsmod | grep nbd4 v. W" g3 r3 G7 E [6 m
nbd 17603 0 : \' M6 ?# X- v* R
root@ # / G1 q! e0 Q/ }+ v0 d& U) I* M
此时nbd模板已经正常加载了: R& M) I- P( m" i' z
$ l$ R+ O5 r! ^3 k4、通过qemu-img命令来扩展虚拟磁盘的virtual_size为 30G:3 A9 J" h% H- l( U" q
% n5 j; J" z* i3 p复制代码
, b8 c7 O; v4 o2 Uroot@ # qemu-img resize test_extend.img 322122547202 l. h; M) y" {6 F( ?
Image resized.8 ^- x: c- A& ^* g
root@ # qemu-img info test_extend.img 2 k/ R! x! {) l; ~6 i+ i
image: test_extend.img0 l0 @) N k( C
file format: qcow2* [3 V9 ?7 }/ \/ j* y
virtual size: 30G (32212254720 bytes)
& J/ U; h# e& p+ l9 R7 p Rdisk size: 309M
2 c1 H( {) A# G' ^& v; J/ fcluster_size: 65536
" e# C- z& M$ O; zFormat specific information:+ y" y+ o$ K* y( }3 k
compat: 1.1
% X; w; F9 h# e: u- ~ lazy refcounts: false
& m5 l$ l5 Q& Q+ ~( S: l( d& troot@#
( B" ]# A V) {* M8 g8 L' Z, Q5 S/ x& u) B+ T* r F
( O/ |7 \# F# F5、下面通过qemu-nbd挂载test_extend.img到/dev/nbd0设备上:& ?- M1 p4 ] |: U4 n8 f
, [3 f7 g) W$ Y& i. w5 F2 \root@ # qemu-nbd -c /dev/nbd0 ./test_extend.img
4 _+ y+ h: h7 s! Zroot@ # ll /dev/nbd* | grep nbd0' n; l4 B: Q O) C
brw-rw---- 1 root disk 43, 0 11月 20 18:09 /dev/nbd0
! g, o: d9 |& D! w; ebrw-rw---- 1 root disk 43, 1 11月 20 18:09 /dev/nbd0p1, f8 @2 G4 A/ ]
root@cason:~/image#
2 C ^7 L6 Z& w t/ M3 b- V 挂载成功后,在/dev/下会看到如上信息,其中/dev/nbd0p1表示该虚拟磁盘仅有一个分区
5 W; }- A1 p: J6 a6 U
/ h& N! {* Q8 d- A `) d6、下面开始通过fdisk来扩展虚拟磁盘分区大小:& I. D1 h, m* H
/ }4 v5 e% \' z: T- H& p( ^
复制代码; u1 ], t. ^) a9 [4 P
root@# fdisk /dev/nbd0
2 L9 o+ {. o+ F) }" s4 `* j+ R" J2 d( F4 m6 ^- ^. E, l
Command (m for help): p$ M' ~/ x0 |4 S% x: d6 Y
# ?1 I! `9 T* e1 J) x( N
Disk /dev/nbd0: 32.2 GB, 32212254720 bytes2 u; n6 O0 _! X, G1 J8 H: i
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors G3 f6 ^0 b6 |/ y }0 _. ^& p/ g
Units = sectors of 1 * 512 = 512 bytes
* }, p4 O( E5 e% H) tSector size (logical/physical): 512 bytes / 512 bytes% Y- K! t6 u0 i2 Y
I/O size (minimum/optimal): 512 bytes / 512 bytes
1 R$ {. C3 s N# k' N _. E: xDisk identifier: 0x000e8e8d
- j5 O9 K7 N2 c; G" @0 Z1 } N) K% M* c4 O
Device Boot Start End Blocks Id System' D M. A C h h1 l7 S
/dev/nbd0p1 * 2048 41943039 20970496 83 Linux
- ]/ g0 b: g* p/ q1 s' I |7 {% {
+ Z d% |8 D' w y1 Z! u! wCommand (m for help): d
( j& I, }# E: e% |( ~# mSelected partition 1
: s. U1 e7 q+ a4 i
: c5 m, K& D- x3 b$ H KCommand (m for help): p
2 h2 d' _7 [: W
& i1 F9 f s2 ?; r2 n, U; nDisk /dev/nbd0: 32.2 GB, 32212254720 bytes
: G) G B' u' o8 G. y# i2 l255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors% f1 w# k6 x/ Q9 y
Units = sectors of 1 * 512 = 512 bytes9 M/ h$ C6 a- s/ t
Sector size (logical/physical): 512 bytes / 512 bytes
/ L6 ?; N' z v% @I/O size (minimum/optimal): 512 bytes / 512 bytes
2 O6 N! I4 S- K Y% i) \Disk identifier: 0x000e8e8d
( j* H* { }5 V: l+ r2 k; X3 s" z, T6 b
Device Boot Start End Blocks Id System$ [! @% j* ?3 s3 {2 x% g- O
4 d. ~+ l+ v; g# z' u6 M! ~' H
Command (m for help): n6 N$ k5 B' }3 n/ p$ N$ U$ ^
Partition type:
1 h9 ^$ {/ a! w; o8 u p primary (0 primary, 0 extended, 4 free)
" U& D: k) j: J d( C; ] e extended
4 @* s4 L% ~2 h* ], }! P( w6 P3 x) }Select (default p): p
- U! {5 z* T$ e7 B4 `8 u2 APartition number (1-4, default 1):
9 Z4 n ]7 ^7 [/ ?. VUsing default value 1( z" L; o( `- f S2 L& b
First sector (2048-62914559, default 2048):
% s2 Z) U7 O9 \( T9 z; J0 LUsing default value 2048
# ]/ q! w& @; M+ |% L7 |- B3 ]Last sector, +sectors or +size{K,M,G} (2048-62914559, default 62914559): ( p7 m4 R" |0 s4 ^4 Z `
Using default value 62914559
/ N% n1 Y2 d3 N% x, y$ m4 `' G D8 p6 P. ?: `# p
Command (m for help): p
9 E7 g; ]+ @' S; S2 u7 G% n: Q
, t+ c4 ?+ r( A+ l" `Disk /dev/nbd0: 32.2 GB, 32212254720 bytes* z6 d9 Y$ b C' f1 W& U+ @9 q
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors P! O- I3 {8 s; ?
Units = sectors of 1 * 512 = 512 bytes+ {6 U" y0 z) a+ A2 z
Sector size (logical/physical): 512 bytes / 512 bytes
- h& J4 } ^; g4 ]- Q5 r$ `I/O size (minimum/optimal): 512 bytes / 512 bytes
) _, G- p! m' ?8 P9 zDisk identifier: 0x000e8e8d
' k7 d. p) L! r+ i% o9 S" Z% V$ k) [! z8 T5 t2 e* k
Device Boot Start End Blocks Id System
1 f4 s. q* ~* E4 e/ Z/dev/nbd0p1 2048 62914559 31456256 83 Linux3 U: D0 |" Q( q; n+ J
3 ]$ K/ f- y8 X) N" L% L5 v! w( a! \7 ^
Command (m for help): a
9 G( ~% K9 ?: Q. U, Z3 [( w9 {" F: U% `' iPartition number (1-4): 1. `* O1 g! L; G
8 j, R) `0 r/ V" G, pCommand (m for help): p5 D$ l4 b3 _% @+ @. Z2 K1 N7 d
$ W5 H; V% s! t* j! P7 N' cDisk /dev/nbd0: 32.2 GB, 32212254720 bytes3 R9 |2 ~# q1 }! R/ q
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
, l; @$ @6 ~1 t; ^/ H- wUnits = sectors of 1 * 512 = 512 bytes
+ z6 w* Z+ y! ^$ [# f( ]% DSector size (logical/physical): 512 bytes / 512 bytes
( f' g/ T3 }4 _4 E; a S& \- eI/O size (minimum/optimal): 512 bytes / 512 bytes
% ` I. `. V5 m" Z) O, m' {Disk identifier: 0x000e8e8d0 u0 W- t* T( w
5 L7 z5 h: S* N6 e3 S) o Device Boot Start End Blocks Id System+ |9 Y1 W/ |3 J, P
/dev/nbd0p1 * 2048 62914559 31456256 83 Linux3 E% N9 @( @0 s* u, y5 ?* I
4 h/ |5 m8 w; R+ W, l5 \
Command (m for help): w2 D# D* Q+ n1 \ O" F
The partition table has been altered!
: ?; ^- v$ V% X, X" o; b
4 y1 i; t* [0 n7 U# N4 YCalling ioctl() to re-read partition table.
$ R" s8 z5 p& y$ l" t! dSyncing disks.
1 r/ F) o+ i+ y u! Y, Sroot@#
2 |1 z$ K2 {& p/ l* W' B& Z9 ]复制代码$ X8 @, j. ?% b8 M
如此以来,我们已经将该虚拟磁盘的分区大小扩展到了30G% f& f1 w- w1 J) I W9 a
. }8 v' `5 M/ m/ ]* `
7、至此,我们还需要将文件系统扩展到30G:
9 y. L, L O# k) E0 {! ^7 V4 K) b8 ^( b# ]
复制代码
1 d* q& K1 m. {6 V- lroot@# e2fsck -fp /dev/nbd0p1 Y* w' p5 h& x4 J2 |# V5 W* u5 @
/dev/nbd0p1: Deleted inode 131076 has zero dtime. FIXED.. W% C$ f: N5 e/ L2 C1 a9 G
/dev/nbd0p1: 18489/1310720 files (0.2% non-contiguous), 281286/5242624 blocks* j' Q, V, o1 L, c
0 I) o" p- e2 P$ D8 C( _- ?! Kroot@cason:~ /image# resize2fs /dev/nbd0p1 2 e" Y, x6 i- X8 J0 w
resize2fs 1.42.9 (4-Feb-2014)
6 F" L+ u, F2 C' C; |Resizing the filesystem on /dev/nbd0p1 to 7864064 (4k) blocks.
. X/ i& Q% j. ~8 m# EThe filesystem on /dev/nbd0p1 is now 7864064 blocks long.
; k# U* x2 i2 X3 k, r' Q1 B/ y Y; X$ m e
root@cason:~ /image# qemu-nbd -d /dev/nbd0
2 [: T: w2 s+ u: t2 _- T/dev/nbd0 disconnected |
|