|
|
当我们需要扩展模板镜像的虚拟磁盘大小时,比如原来的虚拟磁盘大小为20G,现在我们想将其扩展到30G,那么我们可以根据如下步骤来操作。
6 ^ k: o/ ~7 B/ ^ {/ p- A$ x5 T v* F' {
整个流程可以分为三个阶段:* g `" a* q V0 u4 `% P' Y" a& V
6 w- Q* K% u4 P" t) z t6 k- w1、扩展KVM镜像磁盘文件大小到30G。
/ z1 |0 T( o$ x; k/ x7 h3 l
' l8 h+ }+ z0 ^9 g! P: k2、扩展磁盘分区大小为30G。
" D$ ^* I$ S% u, G6 x* N) d& S5 J: ]1 E* F2 [' `1 w* r9 I
3、扩展文件系统大小为30G。
7 c0 N1 |# {7 {" n" z- C8 r7 A
. O# e2 @% H- m/ H假若当前有一个名为 test_extend.img 的模板镜像,其格式为 qcow2, virtual_size为 20G1 z* U# o- @7 i
$ r$ _* T5 z- _* z* t1、首先可以使用qemu-img来查看该模板镜像的元信息:; z8 L& T2 M$ W
, G$ R% D- P/ j8 U/ \: N复制代码
+ e) q* L0 ~* C1 @root@# qemu-img info test_extend.img
2 l+ D& T9 Z# e# u: f/ Y0 qimage: test_extend.img' T% m; R4 d0 g: Q2 `. h
file format: qcow2
( o0 ~/ j& M( m# h$ Gvirtual size: 20G (21474836480 bytes)
: ?% X( T* e* h8 c; M. idisk size: 309M
3 Y/ E: f$ X7 C4 O3 B! S1 a' Fcluster_size: 655361 P% h4 B0 w! B# Y7 n5 F
Format specific information:$ b7 W- z h# F/ I; v
compat: 1.1$ u r- n, F' C6 I9 E9 q7 V& ~6 k
lazy refcounts: false
8 |6 {9 k( ~& i! p" `5 u3 n vroot@ # B) f" k1 J" b8 Q) x3 V
复制代码8 g6 L) {, T) G5 M0 \
2、由于需要使用到qemu-nbd,因此我们需要先确认nbd模板已经被load: w# [3 o, A: y# _" q2 n, m
: F/ a i* K+ K7 A: Hroot@ # lsmod | grep nbd
/ W0 I( E$ Q: a. Q, l8 V4 Droot@ #
9 e! O( m' y4 Q' S 执行如上命令发现没有任何输出,则表示当前系统并没有加载nbd模块% x w1 e# d( j6 w4 ~
/ z3 P1 z& k4 G8 S5 `
3、加载nbd模板,并再次确认(若没有nbd模块,则可以参考这里进行编译安装):, h7 A( N" h' V
' ]- P: o+ g/ M2 ]" Sroot@ # modprobe nbd max_part=8* W7 _/ R; }1 }' s. b0 X9 U
root@ # lsmod | grep nbd
: M. q. M4 V2 [" g4 mnbd 17603 0
" ~* A. ^. [. Oroot@ # , ~( p- _0 o, k
此时nbd模板已经正常加载了+ I) |5 L9 R3 Y2 B+ m( n; R
# u! {* d$ n( s- z0 `4、通过qemu-img命令来扩展虚拟磁盘的virtual_size为 30G:, ^) {4 a: U, j) a0 m8 F0 G
2 P6 [2 @) z: ]" D( S复制代码
# n- a9 ?" D9 l' Troot@ # qemu-img resize test_extend.img 32212254720# m) X4 Q! ^/ |( j
Image resized.+ H( |. \$ l* z# _- v
root@ # qemu-img info test_extend.img
. @ v) x- h' C1 W; Bimage: test_extend.img
2 l3 h; G% k, m9 v0 ofile format: qcow24 {4 J* G8 E# ^: B. X# G
virtual size: 30G (32212254720 bytes)0 ]0 {: i" \/ K! W, _+ m0 w
disk size: 309M
1 N1 E7 q: A5 o2 Icluster_size: 65536: i( V/ T& m3 x4 D
Format specific information:
7 \5 z! x( v" m) I) E! e8 W! V5 Z- q compat: 1.1; d3 E! F$ G! k5 g
lazy refcounts: false
( c7 f, {4 r3 m# o6 froot@# ) }7 C3 L# B: u# f" A& d, A
5 m: n' b! e: B% u; O- R
& r# \* f7 T/ ]$ T& A2 n5、下面通过qemu-nbd挂载test_extend.img到/dev/nbd0设备上:, n3 d% p; u2 M# E
- Q3 }7 x% Y, W
root@ # qemu-nbd -c /dev/nbd0 ./test_extend.img 0 T6 G) R3 V7 H( u4 M
root@ # ll /dev/nbd* | grep nbd06 ?$ Y& S* b& d2 ~
brw-rw---- 1 root disk 43, 0 11月 20 18:09 /dev/nbd0
' N D, @: ]8 T7 y: E$ L o2 i1 Ubrw-rw---- 1 root disk 43, 1 11月 20 18:09 /dev/nbd0p1
2 t: `9 r$ i+ X& Eroot@cason:~/image#
! Z0 t# d2 e! n# J ~$ y: _! Q6 L7 L 挂载成功后,在/dev/下会看到如上信息,其中/dev/nbd0p1表示该虚拟磁盘仅有一个分区
# M4 t2 K* {; q; s" Z9 f8 D3 A( {* M6 X, ~3 C: j- y6 i
6、下面开始通过fdisk来扩展虚拟磁盘分区大小:
$ W* E* F! r0 X! k% n# D
8 ?1 l2 y/ c$ v/ e) j/ B复制代码2 Z) c" f+ U- \( Z
root@# fdisk /dev/nbd0
4 `) B3 N9 `1 R. `, H/ G" F2 _- f( P0 d
Command (m for help): p8 f. a$ g0 f% ?) Q$ {
8 Q2 Z2 i5 T5 x; @Disk /dev/nbd0: 32.2 GB, 32212254720 bytes$ M0 g) P" I3 p" q' l/ C, W
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
/ ^; k3 h$ V. p/ ~Units = sectors of 1 * 512 = 512 bytes9 C8 Y" X& c3 A, V
Sector size (logical/physical): 512 bytes / 512 bytes2 Z3 j* N: U2 V5 p
I/O size (minimum/optimal): 512 bytes / 512 bytes
, b1 c, C6 G2 `+ q( h ADisk identifier: 0x000e8e8d( G! J% R! ~; q+ a
* {/ @7 O7 K9 L) A: Z/ I- j Device Boot Start End Blocks Id System6 }0 M' m2 S$ @
/dev/nbd0p1 * 2048 41943039 20970496 83 Linux
2 ^ Y9 c; d: A# o+ ]/ V
. F9 m0 o8 A. Y2 G4 QCommand (m for help): d
) H" q0 z6 z. Q% qSelected partition 1; x* L9 N O6 J8 c7 N {; j w+ h
6 }+ h$ n6 Y& H; `Command (m for help): p8 ^- `% T: A+ h/ ^* O" j4 l+ F) F
; @' C/ ^% j0 \- rDisk /dev/nbd0: 32.2 GB, 32212254720 bytes
2 N3 j0 M: k5 w% o: q- C( B3 T9 s- l255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
" Y* O a% Q/ e; qUnits = sectors of 1 * 512 = 512 bytes
7 J. ^* ?4 |: `4 a" KSector size (logical/physical): 512 bytes / 512 bytes1 b* ~2 O* A3 N! ~- t
I/O size (minimum/optimal): 512 bytes / 512 bytes* u9 c! E0 [( L3 |$ x$ P2 | ?
Disk identifier: 0x000e8e8d
4 e n, O4 ]- h$ d: O
! p5 _/ C4 T' E7 V H: p Device Boot Start End Blocks Id System
( E$ L4 i# n0 |3 u9 l# l; _; n. s) R* v( s7 c
Command (m for help): n' p( A% c5 V2 b0 _
Partition type:
/ H2 Q4 u3 g( F+ U: o2 [8 O5 j. d1 o p primary (0 primary, 0 extended, 4 free)8 d' e* r+ f7 J( Z6 o' N7 K$ K
e extended6 r/ x& R' Y$ {( F# J
Select (default p): p* ?$ u2 x* C8 l9 q; d) K& l8 M) s
Partition number (1-4, default 1):
8 Y9 O2 h; [. w+ C7 N% VUsing default value 1! W# e, j. s; h
First sector (2048-62914559, default 2048): 7 F1 ^- s) ?) X, B* Y# G# e9 W
Using default value 2048) _6 n( z0 N4 t5 B* ?9 z
Last sector, +sectors or +size{K,M,G} (2048-62914559, default 62914559):
7 |# u5 O& q8 f4 Q8 ^/ \Using default value 62914559
( ]: L3 X) J8 J9 {6 \2 ?
" S; n5 q& a' `& _0 iCommand (m for help): p8 w/ k$ t& g1 x
7 `( Q- J0 b/ K5 T; _
Disk /dev/nbd0: 32.2 GB, 32212254720 bytes$ r2 c% ]9 b4 E% D" _) `
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
% U4 v% l3 J' L3 }" R2 TUnits = sectors of 1 * 512 = 512 bytes+ }" o( Y" I% K# N+ P3 @
Sector size (logical/physical): 512 bytes / 512 bytes
: D; `+ I7 y& \ \I/O size (minimum/optimal): 512 bytes / 512 bytes
V/ _+ {. [$ \2 `9 a) _. f6 V& y$ qDisk identifier: 0x000e8e8d
/ f/ b. u! J2 J
, j, T1 K% a6 p; V0 W+ x+ ^ Device Boot Start End Blocks Id System
/ E; a, U' J) |/dev/nbd0p1 2048 62914559 31456256 83 Linux
. q; k# Y! F( O* l. t& J" T5 [6 Y0 |- x) }; Q2 J
Command (m for help): a9 U, q5 g0 a s; V7 r2 [ u7 _2 o6 U
Partition number (1-4): 1( r& ^$ [1 b/ e7 a9 [8 r
0 F) G' v- q9 a7 n; M, r$ |
Command (m for help): p
' i$ u% m% K3 L+ a# O6 |$ l/ ]; w
) t. \9 [9 v' wDisk /dev/nbd0: 32.2 GB, 32212254720 bytes
4 M8 ]% y3 w, D* n) \( G255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors# T7 F$ ]! X1 H$ |
Units = sectors of 1 * 512 = 512 bytes( n2 c! a9 L# O! `+ k9 v, L
Sector size (logical/physical): 512 bytes / 512 bytes+ J+ D1 L2 E1 J; _# G
I/O size (minimum/optimal): 512 bytes / 512 bytes" P, ]* o, B8 k* @# A' F7 M9 V, Q/ \
Disk identifier: 0x000e8e8d1 G7 _9 M+ e2 `7 _% {( J. e: Q: W
; K- J4 Y( f* t5 |9 ^# `4 U- _ Device Boot Start End Blocks Id System5 w9 q. V# j* C4 H" D( e& |
/dev/nbd0p1 * 2048 62914559 31456256 83 Linux5 `9 F$ a+ X4 {. R# G
g" W! E* p! n
Command (m for help): w, p$ S3 J1 }3 X3 ?0 m
The partition table has been altered!2 ^# f+ F: X+ r B: g: e5 f
4 j/ ]( N7 ?" I4 [- B q
Calling ioctl() to re-read partition table.
1 i* z. t! d( Z7 |Syncing disks.
9 E, ^: V6 H+ ]0 s" ^' zroot@# 7 t# ^! f/ G6 ^. K" C% F9 a
复制代码' a$ _0 ?% H8 B
如此以来,我们已经将该虚拟磁盘的分区大小扩展到了30G, u+ @$ \5 s- M- `
a$ ~3 l3 o( z$ D. D5 B: j; h+ F
7、至此,我们还需要将文件系统扩展到30G:! B* w3 T3 w; m, o+ C
$ P. ^0 A/ L: [! b+ E& {( n6 K, K
复制代码& @$ X2 r* I0 O3 U, e# j- Y- _
root@# e2fsck -fp /dev/nbd0p1 8 r. R6 s i0 @
/dev/nbd0p1: Deleted inode 131076 has zero dtime. FIXED.
" Q: ^4 U& n, B& A( ^. W/dev/nbd0p1: 18489/1310720 files (0.2% non-contiguous), 281286/5242624 blocks) x! P" V4 c( v7 y b
- Z3 e. V% W7 X1 o Z+ E
root@cason:~ /image# resize2fs /dev/nbd0p1 ; i: }8 B6 ^/ {' U* A% m* S
resize2fs 1.42.9 (4-Feb-2014)# H( i7 H8 H3 b4 H0 F
Resizing the filesystem on /dev/nbd0p1 to 7864064 (4k) blocks.
$ w8 X" M# P# |9 bThe filesystem on /dev/nbd0p1 is now 7864064 blocks long.0 O4 x: }; ~3 D \; T: i
( ]5 t0 o1 p; `& r; w% Q! Y0 P+ p0 w
root@cason:~ /image# qemu-nbd -d /dev/nbd00 j- p( J3 Q" y& P5 S* Z5 a: [1 A
/dev/nbd0 disconnected |
|