|
|
楼主 |
发表于 2019-12-30 19:43:50
|
显示全部楼层
当我们需要扩展模板镜像的虚拟磁盘大小时,比如原来的虚拟磁盘大小为20G,现在我们想将其扩展到30G,那么我们可以根据如下步骤来操作。' p1 _! |. ]+ p- g) E3 m
- B8 ?% Z1 }+ e9 O2 k% s
整个流程可以分为三个阶段:, C" y, t/ ]2 g' z9 o8 }
z( @- F; P0 M: g0 u- g
1、扩展KVM镜像磁盘文件大小到30G。6 R+ Y6 U+ g& J4 a5 A0 }# |
! u! w1 _6 R. ^" n2、扩展磁盘分区大小为30G。( I9 a' ~/ { F1 V' D$ G) @4 p
4 |, @2 A' ^) n, |1 u5 f: w3、扩展文件系统大小为30G。: F1 G7 {: u( ^' k& Q' x# e* I8 o
3 V, z6 R, T- S7 `6 z. t H
假若当前有一个名为 test_extend.img 的模板镜像,其格式为 qcow2, virtual_size为 20G! Y! u% z$ Q2 N1 d h
5 c9 B) E* a4 N) j' d
1、首先可以使用qemu-img来查看该模板镜像的元信息:$ e1 L4 V7 M& g- B' A
/ F6 J6 P% G( F% C* x5 v复制代码% b8 ], a$ _% F7 v% A4 W
qemu-img info test_extend.img ! A2 T. @3 ? Q6 l! E- [$ u9 E
image: test_extend.img3 x9 T# J% f. _' O4 X j
file format: qcow29 Z% E, h4 o. }. |# \+ i. l! A$ Y
virtual size: 20G (21474836480 bytes)9 L2 J; P7 F( [' |. N) `
disk size: 309M+ |6 ^# M4 f8 I" n* F
cluster_size: 65536
( X: H5 W: i% MFormat specific information:
; s$ @" q1 n0 N+ y compat: 1.1
. x. n+ ^. Y( w' R. J lazy refcounts: false2 g* ?5 o( L8 A
+ i9 L+ \, T# a" a7 P$ R复制代码5 f( m+ C, I& ~( L% r
2、由于需要使用到qemu-nbd,因此我们需要先确认nbd模板已经被load:
+ D0 X- Q) I: D; L, ~# H a; Y3 m3 u8 Y+ b% i. Y
lsmod | grep nbd/ Z" B: I1 b) ]* i: G
9 l. G @* x0 _7 P 执行如上命令发现没有任何输出,则表示当前系统并没有加载nbd模块
9 {- b f" N# e. t3 G8 ?$ j+ e( U
3、加载nbd模板,并再次确认(若没有nbd模块,则可以参考这里进行编译安装):5 P, l6 }8 O' l$ }
/ b7 |, V4 s; m3 | modprobe nbd max_part=8
0 n" V, q. i/ e! a" G. {. }0 k, o lsmod | grep nbd
( k. h3 X# s2 L/ U" Anbd 17603 0 / W# E) v; v' g" i- v# W1 [
- e# Y# Q' I$ l3 k
此时nbd模板已经正常加载了6 x) I+ q) i* M+ b
3 M; X5 u/ q! ?; {1 D- u4、通过qemu-img命令来扩展虚拟磁盘的virtual_size为 30G:
! }0 d! @; \7 W4 u6 }! x- y; U4 D/ b: O
, d7 C& p, d" z2 D% v$ K1 `3 q6 D2 R# k
qemu-img resize test_extend.img 32212254720. T2 y( h: J/ e& T9 p. E2 }
Image resized.
# g' l" Z6 \% H1 v' w qemu-img info test_extend.img 9 G$ b. x, h( F s+ {( h' e
image: test_extend.img) c0 l d& |3 j+ X
file format: qcow2) c1 U( y* c( u8 {" i: _
virtual size: 30G (32212254720 bytes)! d" B* K% f& v+ |! C T. ~
disk size: 309M) a+ R4 L, \; l; E
cluster_size: 65536
2 p; C5 P$ M* H3 ? V/ UFormat specific information:
& i" z4 K3 Z: ]1 x& `" d6 H compat: 1.1
# r" a* F7 ~ I0 V: N! a' I; s% ?' e lazy refcounts: false/ l4 a4 h" q7 N/ l, N0 P: Y5 B
' P# H) M f! v I2 `& Y3 B
) `/ `* `4 L7 T" S$ q( W' i H4 f9 A- S% V. z: _
5、下面通过qemu-nbd挂载test_extend.img到/dev/nbd0设备上:
. }. l4 z1 |) B# _
0 Z, E2 t. h6 v. X- v9 A w qemu-nbd -c /dev/nbd0 ./test_extend.img
* L6 s6 D( ~; K ll /dev/nbd* | grep nbd0
6 J( q& P; G6 j) Q5 w$ I/ `2 Hbrw-rw---- 1 root disk 43, 0 11月 20 18:09 /dev/nbd0* L- P6 n5 O% s2 [8 R& R
brw-rw---- 1 root disk 43, 1 11月 20 18:09 /dev/nbd0p19 F# K( i- E3 E
# I5 H: T1 G% X. X# y* q: B& {3 ~ |9 O 挂载成功后,在/dev/下会看到如上信息,其中/dev/nbd0p1表示该虚拟磁盘仅有一个分区
. h7 r% X( c0 l: |- ~# U+ I8 y: f* ]
6、下面开始通过fdisk来扩展虚拟磁盘分区大小:
" _1 U5 V9 _4 a3 f) L& R/ C& |, s3 |
fdisk /dev/nbd08 F5 O* W- ^! J8 A
) p5 }$ `$ R5 f) s, R$ `
Command (m for help): p" H4 n- C. R5 ~, B1 V
3 ~. u6 ~3 E K1 h& p& UDisk /dev/nbd0: 32.2 GB, 32212254720 bytes9 s, M, z+ s, V4 P& X! G; _
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
6 L% ^) r1 n8 K: g' AUnits = sectors of 1 * 512 = 512 bytes: x, K: x4 R+ x9 q# I V4 p& S% x
Sector size (logical/physical): 512 bytes / 512 bytes% N: c9 n( ~+ [+ H) c& r+ ~
I/O size (minimum/optimal): 512 bytes / 512 bytes
9 x4 D* [" h- A% K% s3 l: f1 a5 D, `+ iDisk identifier: 0x000e8e8d4 B, Y3 W4 B1 _5 h2 T. U6 I5 V2 c" |
5 z, l6 `% N; q* C" x2 O/ q Q Device Boot Start End Blocks Id System
: l% u7 L! f( q0 x( D% O. j* x- y2 S/dev/nbd0p1 * 2048 41943039 20970496 83 Linux1 l9 h; {8 @# H# y- y; c; A
/ D0 ^, D- H1 UCommand (m for help): d- n' A, b; D# J. N1 d
Selected partition 1# T6 \& x9 B4 x: e
E5 e( j8 k* h7 ?2 `
Command (m for help): p
0 t3 B! K8 X! Y- _( t) c+ F9 U" k3 L# G2 c" M3 d" ]! N
Disk /dev/nbd0: 32.2 GB, 32212254720 bytes
6 e9 q* @4 Y& N( q9 }255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
; j% _7 ~7 k" H8 R" f0 vUnits = sectors of 1 * 512 = 512 bytes( i$ G/ w: C5 \' {- k2 B
Sector size (logical/physical): 512 bytes / 512 bytes
$ @- h1 N; _9 l% v, ~! QI/O size (minimum/optimal): 512 bytes / 512 bytes0 @2 T# A: K; h7 _ D
Disk identifier: 0x000e8e8d
" I, g' k) I6 }- I% j& G0 u6 w: z" Z6 Y) ?0 h
Device Boot Start End Blocks Id System
& M1 `5 D5 G9 p% Y$ ]% ~% i% x/ P0 f4 u/ V# B/ _- r2 i/ E
Command (m for help): n
* R) W0 i p( fPartition type:' j! u3 b" B$ c N) b0 `& i
p primary (0 primary, 0 extended, 4 free)( @6 P% Z# ^7 w2 {7 d4 }
e extended
0 T( k- O; Z XSelect (default p): p
" l6 v; }' P$ aPartition number (1-4, default 1): 3 z3 |+ H5 ?9 a% {5 y8 }
Using default value 1
" |2 l' m6 d. x* H: Q! KFirst sector (2048-62914559, default 2048): - B/ x- L! M1 t! f3 x8 N6 F& m
Using default value 2048
: j* w) v( x: D: M2 J- p7 iLast sector, +sectors or +size{K,M,G} (2048-62914559, default 62914559): . `2 P, t% z# A5 c6 Q/ M" T
Using default value 62914559/ J5 \# p G3 r
) F6 b2 C V( K$ Z2 j% n
Command (m for help): p
z8 i; E4 ?8 w% Y) R* M, U% X1 r6 P9 u# q6 d+ {
Disk /dev/nbd0: 32.2 GB, 32212254720 bytes
# f$ G% T7 \% v1 S255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
@$ u7 D6 E1 g. e5 u& VUnits = sectors of 1 * 512 = 512 bytes
( q+ D1 ?9 p2 H/ o" XSector size (logical/physical): 512 bytes / 512 bytes
9 B/ |8 t' a5 H. YI/O size (minimum/optimal): 512 bytes / 512 bytes( B9 E& c% u! p& I
Disk identifier: 0x000e8e8d2 v4 |2 {/ l9 W; ~. G
+ M6 Y. J8 p1 V( v' V- _) r! z+ [6 D Device Boot Start End Blocks Id System) L) h5 V2 [. C% e0 F* Y8 y% s
/dev/nbd0p1 2048 62914559 31456256 83 Linux
* s9 G' R; ~7 T; T. a8 H+ z1 F5 a3 A% A3 b
Command (m for help): a
* y( y6 ]. c1 n |( ^& pPartition number (1-4): 12 Y- P. b) L3 b1 |# q
, k2 E! O& e' N4 s1 ~' _
Command (m for help): p
2 {. y6 D* f4 g
/ y/ S6 F( T6 bDisk /dev/nbd0: 32.2 GB, 32212254720 bytes7 S+ _! s, q+ n) @8 E
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
( J4 t8 d$ |$ ~) C& k" KUnits = sectors of 1 * 512 = 512 bytes
% Z- E1 l( }. e6 o4 F" cSector size (logical/physical): 512 bytes / 512 bytes6 R6 m) K$ H* c
I/O size (minimum/optimal): 512 bytes / 512 bytes
: r3 r! Z) Z# A3 K2 x GDisk identifier: 0x000e8e8d
+ V5 _ o3 O2 A/ O5 s9 h `" I3 A5 z6 ]: N$ F
Device Boot Start End Blocks Id System
/ g1 G* Q# o3 X+ ^+ [/dev/nbd0p1 * 2048 62914559 31456256 83 Linux% H( ~7 H+ W. Z7 X# y r
! f6 \' T& C, h1 z1 H3 pCommand (m for help): w, ?3 R! `) d6 W& A- O8 @- O0 C4 |9 Z5 |5 x
The partition table has been altered!
. M* O) f% Z7 r; D
: S# E- w% u2 r E6 HCalling ioctl() to re-read partition table.
5 A2 l: j4 ?2 c7 P: |- n/ U2 ~' |Syncing disks.
4 H" E- z3 v* l4 U3 x4 F9 N/ h; A# O0 A# D$ f- J. `' @/ i- e
复制代码
( L! H' I3 E$ ] 如此以来,我们已经将该虚拟磁盘的分区大小扩展到了30G
2 f. ]/ i; J$ b, Z3 }5 ?
) _4 A9 v3 Q0 w5 z# u7、至此,我们还需要将文件系统扩展到30G:: z0 h* F C8 n3 ]0 H
- s6 @ V- Y: k! j: W- X3 o
复制代码; V: U7 {; I6 H& p) W) y {
e2fsck -fp /dev/nbd0p1 , l3 H$ N% n* ?( t( f
/dev/nbd0p1: Deleted inode 131076 has zero dtime. FIXED.$ W( \" }0 o9 s. G, \
/dev/nbd0p1: 18489/1310720 files (0.2% non-contiguous), 281286/5242624 blocks0 Y6 d4 C- p% Q$ J6 Y9 W
/ ]5 y" I$ i* f n" T resize2fs /dev/nbd0p1 / G5 f, C4 z2 G
resize2fs 1.42.9 (4-Feb-2014)' {) G7 i Z! x9 p
Resizing the filesystem on /dev/nbd0p1 to 7864064 (4k) blocks.6 |8 i$ o" r4 r) _7 N: ?
The filesystem on /dev/nbd0p1 is now 7864064 blocks long.: V% G. {& H& ~0 E
qemu-nbd -d /dev/nbd0. {& [- O& R" r* M
/dev/nbd0 disconnected |
|