|
|
当我们需要扩展模板镜像的虚拟磁盘大小时,比如原来的虚拟磁盘大小为20G,现在我们想将其扩展到30G,那么我们可以根据如下步骤来操作。) p8 U% P6 o* V) d" x
/ {+ t+ T: S' _9 x. R' @
整个流程可以分为三个阶段:
' U4 q* D; t+ |" c% f
: i1 h7 B- {; ^; k1 N1、扩展KVM镜像磁盘文件大小到30G。2 w9 X' [9 R# |) j4 n9 f
$ j# w: I) H: w4 l2 b0 x
2、扩展磁盘分区大小为30G。
3 [) t' P: Q, z: H2 F3 s* U2 a3 ]& C- X3 s* v4 @; L( W( f. H1 a; I
3、扩展文件系统大小为30G。+ t1 Y- k6 N3 T; H3 m) J* }! o
: G; U; ~9 l( S% H假若当前有一个名为 test_extend.img 的模板镜像,其格式为 qcow2, virtual_size为 20G; E: P( C& Z `. _, ?
; ?; G7 r! B( T# p4 o' i: l+ v1、首先可以使用qemu-img来查看该模板镜像的元信息:; P' u9 F. Z" ?
) |. z4 p' r* s1 B8 H5 ?
复制代码" [" q) O/ v* s# U) i
root@# qemu-img info test_extend.img
4 L4 w: e1 V9 B# l/ ^image: test_extend.img0 h! v% G$ K8 C. p6 n/ b. q
file format: qcow2" C4 n, L- l3 |2 Q
virtual size: 20G (21474836480 bytes)3 c F. @( c, n! n+ M
disk size: 309M
& \* C: M8 G1 r- W* tcluster_size: 65536
. {$ u+ v! L& a/ kFormat specific information:
) T8 {/ r+ H/ D- C6 f compat: 1.1
+ Z! K. O* Z, a lazy refcounts: false
4 u! M5 z) U4 kroot@ #
% p2 O; Z9 `; P9 p* U' o: e复制代码
$ d) J) }8 A* Q: O0 @) E2、由于需要使用到qemu-nbd,因此我们需要先确认nbd模板已经被load:! o. L9 Q; @0 r
2 _5 F/ c4 W. {8 S2 P* droot@ # lsmod | grep nbd
8 j/ v$ t5 F: i, D' P2 droot@ #
5 g; l m" f2 P1 @. y 执行如上命令发现没有任何输出,则表示当前系统并没有加载nbd模块
7 \/ J8 W p1 g$ ]% Z, r' Q
; v, d E8 l! a7 x1 _4 `3、加载nbd模板,并再次确认(若没有nbd模块,则可以参考这里进行编译安装):: |% t2 Q& r) X2 X
$ z# L' w3 Z2 I2 jroot@ # modprobe nbd max_part=83 Z5 N; Z& f8 i
root@ # lsmod | grep nbd
9 J! r U& [# |6 C0 U# }* Hnbd 17603 0 8 f( T% @( Q K* A4 C% ]! y" R3 r
root@ # ( s/ D( I* u( z1 F
此时nbd模板已经正常加载了
% x- R+ b; R" [4 e1 W$ N* N! H S! n Z' f$ _" u1 L! E, d) L, d
4、通过qemu-img命令来扩展虚拟磁盘的virtual_size为 30G: F& z2 |- S6 |7 H" o% `; f7 ]. P8 d
+ d; ]1 h' m$ e2 F7 d; X复制代码 K& d! _: ~3 w7 \( k: J4 I7 t
root@ # qemu-img resize test_extend.img 322122547205 ]6 f( Y- U1 o6 N
Image resized.
& q* U/ X$ k# l5 hroot@ # qemu-img info test_extend.img " g3 o& N/ ~* q$ v( G, s7 P
image: test_extend.img
& w9 r) s( J, G3 F) d' o' x- `file format: qcow2
. r( D3 }6 Q2 b& {- f( ~virtual size: 30G (32212254720 bytes)
% K# [4 v4 f+ N5 D2 }6 Hdisk size: 309M
) P7 K, i: n* {/ D0 zcluster_size: 655362 o0 {% F$ ^- e' c2 _) g' \
Format specific information:
6 b% U" p5 g4 o compat: 1.19 M9 [5 t- v3 c1 C1 j
lazy refcounts: false
2 P/ m2 F' @9 S" R1 Vroot@#
# G* W; U* t7 @: R
: N* {5 V- }( b3 c' o* Y! \3 J! f9 t; [( y, Q/ E; U7 z2 b+ v
5、下面通过qemu-nbd挂载test_extend.img到/dev/nbd0设备上:
) x9 \5 G P9 l* z% D. t) g# E; c j! t, l
root@ # qemu-nbd -c /dev/nbd0 ./test_extend.img % ?* }; h/ k4 F! q3 k. Z7 {
root@ # ll /dev/nbd* | grep nbd0, {6 T3 f }2 S9 {
brw-rw---- 1 root disk 43, 0 11月 20 18:09 /dev/nbd05 V1 t0 ?0 z+ E L
brw-rw---- 1 root disk 43, 1 11月 20 18:09 /dev/nbd0p16 J5 \3 S7 ^+ T+ b+ c
root@cason:~/image#
u5 u2 m3 \' v8 R$ ]7 ^, [ 挂载成功后,在/dev/下会看到如上信息,其中/dev/nbd0p1表示该虚拟磁盘仅有一个分区# {9 K4 P8 w& X" R2 a
% V% |9 g* M, b% ~1 Q- g: P6、下面开始通过fdisk来扩展虚拟磁盘分区大小:
: q" ~0 w9 G# d# ^
& |# X9 D$ S$ @! u复制代码
0 j: U+ {- _3 f. h6 ?root@# fdisk /dev/nbd0
& ~4 d0 s$ [1 V9 c& S
) o+ ^8 R% {- ZCommand (m for help): p
* J, i- C' C. g+ I6 q2 F0 Q0 z/ T: ~1 A# E. m* I
Disk /dev/nbd0: 32.2 GB, 32212254720 bytes9 Z1 S; h/ ^: Z6 ]" e
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
+ j3 q' }& q8 l% cUnits = sectors of 1 * 512 = 512 bytes
' o4 y5 u- y# ~/ N5 MSector size (logical/physical): 512 bytes / 512 bytes% b+ \$ e/ L# z
I/O size (minimum/optimal): 512 bytes / 512 bytes: o6 D6 G) L7 _$ t
Disk identifier: 0x000e8e8d
; z7 w+ n: e/ y. T1 l9 m
/ x8 e) Y% v( M5 F& O9 j/ K Device Boot Start End Blocks Id System
, U z# Q8 z6 C/dev/nbd0p1 * 2048 41943039 20970496 83 Linux- U! Q+ D) a$ h
+ v9 F- [2 N: V
Command (m for help): d
; q7 n! }& Q+ d7 i% O0 lSelected partition 1
7 W( A$ I. G6 h% [ W6 F0 N9 J3 o0 Z0 {3 l; t/ `# K: T
Command (m for help): p
) |+ ~) T& q' G2 x$ e6 n, G0 N2 T& K$ f7 s. D; e' ]" ~
Disk /dev/nbd0: 32.2 GB, 32212254720 bytes4 V9 j* h* j+ L& u- a
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
7 z% q5 a$ O" S% w3 |5 N) vUnits = sectors of 1 * 512 = 512 bytes4 c( u/ K3 v2 j+ h6 o: P
Sector size (logical/physical): 512 bytes / 512 bytes
7 m( ~( b: y& ^I/O size (minimum/optimal): 512 bytes / 512 bytes6 N' C. q& d2 E# i! H9 K3 Y
Disk identifier: 0x000e8e8d
# f& E7 `5 A' }" A; e+ C& N5 \8 e0 X. i" m- z! w
Device Boot Start End Blocks Id System" M1 M# L( c) [! x/ v
+ Z- ^/ c& T8 g7 Y1 BCommand (m for help): n
# q+ l' T% ?/ K$ |Partition type:4 Y% w! P9 m+ r K- w+ K, I
p primary (0 primary, 0 extended, 4 free)6 F" P& Y( I8 I9 `; A/ }+ V: m
e extended! i: [& i, ]# j9 V' a3 }5 p
Select (default p): p
* A4 B; t' H0 o& @Partition number (1-4, default 1):
. b8 ~/ v3 Q0 W. eUsing default value 1! [# T) L) \. i
First sector (2048-62914559, default 2048):
$ e+ k' p. \- [7 T! p2 L' ?Using default value 2048
$ B, P3 s' V% p f, nLast sector, +sectors or +size{K,M,G} (2048-62914559, default 62914559): ! M- R' d: |. m% \
Using default value 629145591 J+ M/ T' H4 W6 Q
# G. X3 \5 J" R4 m% A
Command (m for help): p* k/ o1 r: ^1 ^: R6 i, i( ~1 }' R
9 z$ ~+ ~+ Y& @% v. F
Disk /dev/nbd0: 32.2 GB, 32212254720 bytes* \+ B r: _: i7 P' B7 T" V
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors; n+ p. f: d9 k2 a# N
Units = sectors of 1 * 512 = 512 bytes/ g/ F% F z- S% p7 F% h5 d
Sector size (logical/physical): 512 bytes / 512 bytes& y$ `3 ^) g3 G5 J
I/O size (minimum/optimal): 512 bytes / 512 bytes1 v9 y+ c/ u+ L: ~0 M
Disk identifier: 0x000e8e8d
) F+ F! h% g1 n- e6 b( C$ j7 l5 s6 k% p) c6 a" f n$ S
Device Boot Start End Blocks Id System% T* T" K; Z( Y' J
/dev/nbd0p1 2048 62914559 31456256 83 Linux& M4 N, k0 f& A4 W7 K8 G
+ V. c( E- S8 k7 [5 W
Command (m for help): a
3 ^* `7 I2 L e( u* LPartition number (1-4): 10 o) o3 x# l- F' n2 u" D8 k5 O
$ C) A/ M5 q" u i4 O
Command (m for help): p, k3 Z$ @ G) ]( q7 L& n6 b
# m, n8 q8 Q9 @- x9 p9 xDisk /dev/nbd0: 32.2 GB, 32212254720 bytes
4 ]6 U% Y# Q/ T& P255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors3 P9 O# X2 P7 Q& S" E
Units = sectors of 1 * 512 = 512 bytes4 ?9 W& n- {" U4 @ X: ]( I7 O
Sector size (logical/physical): 512 bytes / 512 bytes# O; j' ]7 {, X9 V
I/O size (minimum/optimal): 512 bytes / 512 bytes
& R7 C5 }1 n8 m% J, L# _Disk identifier: 0x000e8e8d5 E- H7 r. A; O6 g
1 V% r3 J) M' `! j+ q
Device Boot Start End Blocks Id System
; d% r/ b1 ]9 k$ y1 d0 Z! B; i/dev/nbd0p1 * 2048 62914559 31456256 83 Linux0 b$ x! O- o$ W4 }5 F9 B
' I9 u% z" A8 T' _! p: }7 GCommand (m for help): w
( w0 g' u% S# IThe partition table has been altered!2 V# l! m; V8 A; Q$ h% o
0 D/ z" a( w3 ]5 F, R+ b
Calling ioctl() to re-read partition table.% C/ J4 m4 v+ P6 s5 f( M" G
Syncing disks.
) G3 v, V J8 B) Rroot@#
* B% g' W/ s# s' }6 g( \. g5 X5 i复制代码" m( _/ a& a9 b; \) P5 H1 j
如此以来,我们已经将该虚拟磁盘的分区大小扩展到了30G
' s) o0 ^ c; Y6 q2 O# w7 T U
3 `6 W# Z' V9 h# I1 `, h7、至此,我们还需要将文件系统扩展到30G:
8 B% V7 g* O8 e* v* f
! T. T- j+ P/ V; Y O复制代码# [# f/ k4 n c' i/ q( V
root@# e2fsck -fp /dev/nbd0p1
3 Y3 ~/ P, c) k: z7 T N/dev/nbd0p1: Deleted inode 131076 has zero dtime. FIXED." ?6 t: I: G9 h+ f
/dev/nbd0p1: 18489/1310720 files (0.2% non-contiguous), 281286/5242624 blocks
+ A: @1 i3 W& u3 q; V7 V D& I5 ]8 T' {/ v
root@cason:~ /image# resize2fs /dev/nbd0p1
# G9 F( _( w2 E, r7 Vresize2fs 1.42.9 (4-Feb-2014)5 z6 J% x6 u X$ r; t* m+ K
Resizing the filesystem on /dev/nbd0p1 to 7864064 (4k) blocks.
' t! U: G- s. S1 |The filesystem on /dev/nbd0p1 is now 7864064 blocks long.
: H' z% Y% T$ N8 o, F: p7 a
7 r3 ?9 q$ a, g! O( zroot@cason:~ /image# qemu-nbd -d /dev/nbd0
% g, }' M+ r3 |7 r3 D0 u$ \/dev/nbd0 disconnected |
|