|
|
楼主 |
发表于 2019-12-30 19:48:01
|
显示全部楼层
当我们需要扩展模板镜像的虚拟磁盘大小时,比如原来的虚拟磁盘大小为20G,现在我们想将其扩展到30G,那么我们可以根据如下步骤来操作。
$ i2 M/ H U4 @+ L- O, ~
v6 [' x. w7 U! ^) }1 U整个流程可以分为三个阶段:
$ i. K* K5 I/ v5 N& s8 T6 r | G+ A7 M. w
1、扩展KVM镜像磁盘文件大小到30G。
6 A3 K/ v3 a1 m6 _* z: ]6 n$ d, s9 |+ I* }' B
2、扩展磁盘分区大小为30G。
7 r5 K4 _* ]/ ~4 W# {8 B% L* u5 {
3、扩展文件系统大小为30G。
" o$ P; s; m; w# m$ w3 A3 h6 L/ Y$ H$ I0 M9 l5 h
假若当前有一个名为 test_extend.img 的模板镜像,其格式为 qcow2, virtual_size为 20G
8 ~" C5 |- B5 l2 W; H6 c& E0 V- x/ n3 I6 j }$ o$ F H
1、首先可以使用qemu-img来查看该模板镜像的元信息:& |' t+ v6 `6 _0 x
) t1 [ a% c9 }0 N# u复制代码
! F. E4 k" B5 [9 P+ Q9 u" qroot@# qemu-img info test_extend.img
/ [9 c( V# D* v* p( d1 ~image: test_extend.img! B# q9 l! c% r, A2 c1 v, B' Q
file format: qcow2% o, s0 Y& r1 J: q& d7 ^
virtual size: 20G (21474836480 bytes)
% V7 H9 p/ t) s. Kdisk size: 309M# Z5 u& S. V2 b, [# O
cluster_size: 65536. H9 E9 \0 L3 b; g! b6 y
Format specific information:
9 z5 b$ ~6 n" `" z _6 H9 V, U compat: 1.1
' K. P0 h& Z% J3 E; B" t" G lazy refcounts: false
3 X* G# x2 d, [- v2 R! g$ droot@ #
. f$ m4 Y3 O" a- V3 A复制代码; `' |1 ]( t! Q6 \, O/ m3 Y
2、由于需要使用到qemu-nbd,因此我们需要先确认nbd模板已经被load:
5 p4 V% i6 Y- ~; G; u3 w, x/ E& w$ D' b& O q( ~* w. ^
root@ # lsmod | grep nbd; K5 X7 V6 b2 x$ T5 W% r: m, l
root@ #
1 W! V z. D2 O4 k \+ R 执行如上命令发现没有任何输出,则表示当前系统并没有加载nbd模块2 r4 \( s/ W* i7 I w
# W( k, y) `: _$ \6 Y- Q
3、加载nbd模板,并再次确认(若没有nbd模块,则可以参考这里进行编译安装):
) M8 [) I; B; {4 c, h
3 ` ~- E2 f0 f+ }" |' Lroot@ # modprobe nbd max_part=8
2 z: X) o5 k) L$ o$ b+ wroot@ # lsmod | grep nbd P0 e, G# z' y
nbd 17603 0
- T* h, ?( F& K: S0 _root@ # % m9 i. ?3 b; K. [. D$ X1 F; T
此时nbd模板已经正常加载了
X. c( Y1 u8 I& i5 j: Z# ^" p* d9 }8 A6 B
4、通过qemu-img命令来扩展虚拟磁盘的virtual_size为 30G:
/ ^! S' N* m. d2 \
# I1 S% E2 A0 G复制代码
/ c Y4 ]5 o2 n+ O9 C2 i; f7 Jroot@ # qemu-img resize test_extend.img 32212254720( ^. ?- I E$ ?' T" s- c9 w
Image resized., k0 y; P/ i/ Y0 x# @2 W
root@ # qemu-img info test_extend.img % ~& a6 U' U/ Q, {) d2 ^8 g L
image: test_extend.img$ t& ~7 N9 a: n! r" D
file format: qcow2
4 }$ P$ \' z7 \9 n j$ yvirtual size: 30G (32212254720 bytes)" q/ r% W, q+ }* V
disk size: 309M
- `8 R- t7 X- i1 [! c j kcluster_size: 65536- g& ]: \8 F6 ~, P
Format specific information:
8 [; N# W* l, S* O. u* l compat: 1.13 [# r3 {4 f- D4 X* F
lazy refcounts: false, o7 f9 z/ `7 O6 w3 G! A; L
root@# + F2 S5 O/ A- D7 o: k/ P
' g- k( y6 G" w- L( T! o$ @( d
. `, M3 `3 q2 C$ _& U8 @5、下面通过qemu-nbd挂载test_extend.img到/dev/nbd0设备上:
4 D7 J' S; H4 i2 c: n o
/ J5 `( j/ r2 U5 U$ Nroot@ # qemu-nbd -c /dev/nbd0 ./test_extend.img - S7 T1 M* J* E$ {7 n' j
root@ # ll /dev/nbd* | grep nbd0
9 m" E, O# V$ B! H: Qbrw-rw---- 1 root disk 43, 0 11月 20 18:09 /dev/nbd0
+ R9 G9 s. [; [- h. L& xbrw-rw---- 1 root disk 43, 1 11月 20 18:09 /dev/nbd0p1
- p1 I; F7 O6 G! M* H; Xroot@cason:~/image#
% _, b1 ^$ K a% x8 A) g2 k7 W' K 挂载成功后,在/dev/下会看到如上信息,其中/dev/nbd0p1表示该虚拟磁盘仅有一个分区$ C) Q' Z% s' Z* N
2 }, I4 `* {; V/ A) n) {) H
6、下面开始通过fdisk来扩展虚拟磁盘分区大小:
1 G& E1 }1 Y8 m$ x' I
3 p# x( }& G, j复制代码
/ d5 j: j% p; }) Z' [root@# fdisk /dev/nbd0# S5 k, H7 _, ?
- ` O S9 d9 O' y6 z/ gCommand (m for help): p
o) J2 v/ _1 u, |2 V6 [ E8 a9 Y
5 |+ w; V5 l3 U7 v) s( \% eDisk /dev/nbd0: 32.2 GB, 32212254720 bytes- l6 G6 ~# p Y/ F# ] l+ N& t
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
3 p( u! ]0 m3 L! WUnits = sectors of 1 * 512 = 512 bytes- @( A2 T( _; a' x7 Y2 f
Sector size (logical/physical): 512 bytes / 512 bytes
D( ^; e) w0 r3 @/ HI/O size (minimum/optimal): 512 bytes / 512 bytes1 I- s, ]) x2 Z! p5 V+ v+ C
Disk identifier: 0x000e8e8d4 }8 D, G/ Y1 Q" n; P) f+ y
( e' R1 S7 g% R, n! q
Device Boot Start End Blocks Id System
* h) T/ |! S9 w/dev/nbd0p1 * 2048 41943039 20970496 83 Linux$ s7 C2 A& h3 L5 E" |
$ r$ J! V9 ~. A
Command (m for help): d! Z9 b8 Y H9 T
Selected partition 1
, X( P" ^" [9 g$ r% O" @# {
& _% o$ ]7 P, ~) RCommand (m for help): p
+ ~3 O# j; q' p0 \, }7 P) L6 }
, ~3 C4 _' W$ d- S+ n1 [! ADisk /dev/nbd0: 32.2 GB, 32212254720 bytes
, I6 R3 y3 b2 K6 d+ ?255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
6 A5 g4 d& N tUnits = sectors of 1 * 512 = 512 bytes
6 P4 b+ e! M: L- u5 `5 r" KSector size (logical/physical): 512 bytes / 512 bytes, |7 a- g# U: D( L
I/O size (minimum/optimal): 512 bytes / 512 bytes
+ d! X/ I# v) M7 a: T7 c. uDisk identifier: 0x000e8e8d
. y( ^( [$ p) ]# f7 T# V, s+ c% K& O. R; e! w# f5 D4 l
Device Boot Start End Blocks Id System; l5 t) H" N9 _/ [! n. v
/ w- b) z- F) t0 U8 e5 k" K# v4 c2 cCommand (m for help): n
) G' u( Y) Y! V, WPartition type:% k0 }/ V) ]( W6 R0 t, n [
p primary (0 primary, 0 extended, 4 free)
0 m8 ]$ M- U i7 N% B( Q1 \ e extended
3 h3 S. h( V: [* J3 A/ wSelect (default p): p* N1 h9 L: L/ o4 w+ S, A* U
Partition number (1-4, default 1):
0 r5 h6 a0 v2 k Q# @ ~) lUsing default value 1
M" Y% J' |! V) L3 U1 B, gFirst sector (2048-62914559, default 2048): ' V$ K* @7 i! ~1 L" w
Using default value 2048" X8 f7 n7 s3 x$ W6 i: R; ?& B
Last sector, +sectors or +size{K,M,G} (2048-62914559, default 62914559): # W2 k$ _* V4 S `- E, S
Using default value 62914559
$ M) ] b7 U4 _1 [/ N8 @
% j0 C' Z8 J1 e* wCommand (m for help): p
9 ~/ O' v4 s6 ?" }: n1 I& C
e+ d$ `2 M) p% D' f3 j/ h3 CDisk /dev/nbd0: 32.2 GB, 32212254720 bytes8 G7 j* [" k- A4 C: z4 E9 g3 j" a9 ~9 |
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
2 G5 W9 o& K, oUnits = sectors of 1 * 512 = 512 bytes! `: l o9 \1 D
Sector size (logical/physical): 512 bytes / 512 bytes
4 b! W0 Z4 `( h* v2 k) p. z: SI/O size (minimum/optimal): 512 bytes / 512 bytes
" O$ R, c2 Q5 f+ d/ pDisk identifier: 0x000e8e8d7 _8 p# }+ h& U4 i5 z# i q7 T |. \
) @- E8 A+ ]$ u2 @4 @" V0 n
Device Boot Start End Blocks Id System! F1 y0 k. Y+ r# d
/dev/nbd0p1 2048 62914559 31456256 83 Linux1 w: F* ^/ v8 m- o- U# K
9 C2 W ?2 p( ~: G
Command (m for help): a
% S+ ^9 j- X1 X% YPartition number (1-4): 1
0 x! S* K7 l+ p$ I
& a" q$ r x+ j$ r ~2 g& }( ~Command (m for help): p
6 d3 X" w3 K' }8 ?/ I. W
" ^' z* R4 N) k/ m6 D3 d8 }Disk /dev/nbd0: 32.2 GB, 32212254720 bytes& ]' p) p: W) M3 |& A$ X
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
5 x3 T; C7 m% j$ W" ~Units = sectors of 1 * 512 = 512 bytes% g3 E# u% c$ V6 a; e- D
Sector size (logical/physical): 512 bytes / 512 bytes* l3 c* W% Q) N1 l) y
I/O size (minimum/optimal): 512 bytes / 512 bytes
( j! g8 t: W8 l& K* e. S. qDisk identifier: 0x000e8e8d! E* j6 i( a* g6 G+ t S
) X5 c7 D w7 ~. y( U) Y& b Device Boot Start End Blocks Id System4 |" [4 b$ H6 H
/dev/nbd0p1 * 2048 62914559 31456256 83 Linux$ [9 s9 j7 S0 B; t* ^0 z
# i% z" s4 E7 |/ _. s8 F6 n% F& OCommand (m for help): w
( h( R, _ u4 B& s; O0 P4 TThe partition table has been altered!+ c& \! w5 s( ]( O" R
- ?% T' S* v, H: G. e+ qCalling ioctl() to re-read partition table.
" R# G+ P; O3 ~! q/ D4 C* }Syncing disks.
% m( U2 D( ~5 j2 wroot@# ; S) n m/ S0 D
复制代码. ~9 h. W: d* Z8 ]/ ` G
如此以来,我们已经将该虚拟磁盘的分区大小扩展到了30G6 I r: ]. Y) H) u
+ S) q; T( T. k0 W( J2 z7、至此,我们还需要将文件系统扩展到30G:
7 {3 m- N9 m' p5 } M! t% x% B, \ q
复制代码" i z3 h1 ^( i+ x$ e
root@# e2fsck -fp /dev/nbd0p1
. Z" v2 {" Q; `/dev/nbd0p1: Deleted inode 131076 has zero dtime. FIXED. u5 F0 n0 {4 p. s
/dev/nbd0p1: 18489/1310720 files (0.2% non-contiguous), 281286/5242624 blocks* L+ u$ {7 \1 H# j* z4 ^* S! Y
/ y* A" ?- P f: droot@# resize2fs /dev/nbd0p1 : ~6 P" M3 K# z' v; Z O" ?
resize2fs 1.42.9 (4-Feb-2014)/ ~* _4 O$ n% H2 Q3 S( G% ]3 ]* ^+ l
Resizing the filesystem on /dev/nbd0p1 to 7864064 (4k) blocks.! n, A8 `6 O" Y0 h
The filesystem on /dev/nbd0p1 is now 7864064 blocks long.
% ~; g; x; n, A8 f4 n4 O2 f( @! r! h7 m& g; ~1 r* [' r0 V
root@# qemu-nbd -d /dev/nbd0
* ^7 A9 u0 }, z# H+ Y& E/dev/nbd0 disconnected |
|