|
|
楼主 |
发表于 2019-12-30 19:43:50
|
显示全部楼层
当我们需要扩展模板镜像的虚拟磁盘大小时,比如原来的虚拟磁盘大小为20G,现在我们想将其扩展到30G,那么我们可以根据如下步骤来操作。
s5 T5 X9 n! K' Q. \$ H
8 M4 l6 d5 U2 A$ q N整个流程可以分为三个阶段:4 i' y5 p/ A# `, Q$ m% S5 S
% j% Q) j6 e( ^4 l7 ~2 D* j' |0 V+ m1、扩展KVM镜像磁盘文件大小到30G。
& W! p2 M, M) T9 c% [8 r7 Q7 X. n" K6 Q. E3 E8 b3 c
2、扩展磁盘分区大小为30G。2 {' i* q7 Z% J0 C" L& F
+ N! \% S2 G3 X4 \
3、扩展文件系统大小为30G。3 \7 ]8 j0 D; l5 c3 M. [, Y3 g
% F6 Z' z$ Y) e" _* o/ f/ Q
假若当前有一个名为 test_extend.img 的模板镜像,其格式为 qcow2, virtual_size为 20G& s3 n1 x, A2 m Y
' I" H" H) n: T+ X1、首先可以使用qemu-img来查看该模板镜像的元信息:
; v$ y/ `" X4 \& W, T
* r5 R+ u3 p, Y- O3 D$ d复制代码
4 a* h$ M( j, M8 d+ jqemu-img info test_extend.img 6 f. j4 ~+ @" J4 {7 U- Z& m% B$ ^
image: test_extend.img
. Q/ j( }$ f+ Z0 b& v7 k" z# Z5 sfile format: qcow2
- [/ r, j7 O; e# M6 hvirtual size: 20G (21474836480 bytes)4 I/ K' q; }# l& `1 x8 M, r3 k
disk size: 309M
9 G: Q* `2 V4 f* x( Gcluster_size: 65536* D8 [0 {) A( b0 y' r4 c
Format specific information:
3 }% T0 l8 O/ x1 z: l* K compat: 1.1
. i0 L8 o# Q; e) z* [+ B( f lazy refcounts: false( w" h" _$ n/ {. X$ [$ H
( e, X! P' }$ @, X [) {7 \7 f& q0 C复制代码 S" W& _' N f) i+ f9 M4 R3 S: `
2、由于需要使用到qemu-nbd,因此我们需要先确认nbd模板已经被load:; ?1 R6 a: Y1 J
7 m F1 ]) P$ k$ y lsmod | grep nbd# q! }+ x0 r2 a+ M g
, A5 b# T/ K3 u$ U/ I3 n) t; N: |- l 执行如上命令发现没有任何输出,则表示当前系统并没有加载nbd模块: J' a/ p+ W. A
8 p8 i5 G7 J5 c% P" G3、加载nbd模板,并再次确认(若没有nbd模块,则可以参考这里进行编译安装):4 g7 [* ]7 J- z9 j, ` N; Y
" s/ U# D3 M! G! p e; J) r, | modprobe nbd max_part=8+ u5 L) V% N0 L9 Z$ q
lsmod | grep nbd
3 j8 `0 D4 k9 ?- ^' F( snbd 17603 0
& b- U0 Z6 x# Y# S
3 d! \; y! x h4 O$ ] 此时nbd模板已经正常加载了
5 q' ^3 d Y9 H' e) n9 [7 B5 c2 i0 Q. x5 U/ W+ g
4、通过qemu-img命令来扩展虚拟磁盘的virtual_size为 30G:& b6 E5 E2 o3 v( L' t1 m( [! e
1 U0 n# H. q0 F# ]- C
) w- A5 C- t9 i5 Y0 w5 x qemu-img resize test_extend.img 32212254720 }" N, u p7 G% D# h8 v
Image resized.# p- {/ r2 X6 L" v4 t
qemu-img info test_extend.img / \* P1 D8 P' a/ ]/ C. ~
image: test_extend.img1 x5 _5 s0 t! d8 ` b* W6 v# C& A
file format: qcow22 @ X9 d) B, s% |. e! C
virtual size: 30G (32212254720 bytes)' c O9 }: B/ S4 e1 s- u
disk size: 309M, m9 o% x' y, a8 Z+ i
cluster_size: 65536% L5 v6 r# P M
Format specific information:
}3 y) Z; L Y, b compat: 1.1
* H9 j" a) f* _3 x lazy refcounts: false
* \9 r7 s3 M2 ?2 \( _# r6 @ ^6 ]# R) \3 i) J8 V
1 o7 u# Q3 u7 m: k2 O
) O l/ s# |, F# v5、下面通过qemu-nbd挂载test_extend.img到/dev/nbd0设备上:" {9 u3 m( o3 _1 V# O8 P( ^
6 L3 U U" }: G1 Y
qemu-nbd -c /dev/nbd0 ./test_extend.img
1 x5 h9 ~6 W* F, }" Q: d' u ll /dev/nbd* | grep nbd01 ~5 R: a! y' |+ {+ Y! X
brw-rw---- 1 root disk 43, 0 11月 20 18:09 /dev/nbd0! ?) ^1 K" v0 m5 Q/ o* O. w, \
brw-rw---- 1 root disk 43, 1 11月 20 18:09 /dev/nbd0p1
, m' l" t0 k) F8 t. p; z) G- d5 ?* A% A% a, s0 ]
挂载成功后,在/dev/下会看到如上信息,其中/dev/nbd0p1表示该虚拟磁盘仅有一个分区
7 p6 i/ n7 n- d2 S. D7 ^% j* |" d
% A4 F0 N- p; D/ A$ Z6、下面开始通过fdisk来扩展虚拟磁盘分区大小:
% R( B; y4 ^* f% p& _" \5 H1 m2 D( g0 G1 Z; r( m5 G
fdisk /dev/nbd0
" r Z. f" Y0 J6 S$ x' J9 @( L
Command (m for help): p& l* V$ _' B+ A4 R. v( ^: k' r; g
$ W) U" _! g$ ^Disk /dev/nbd0: 32.2 GB, 32212254720 bytes
9 O$ N E2 ~' l+ F2 ~255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors" A ^ C& a. f# t, \! ~) U: i; R+ {
Units = sectors of 1 * 512 = 512 bytes
. j: q6 L& p6 d! e, z7 w! g, USector size (logical/physical): 512 bytes / 512 bytes) s5 L2 a) F- ^' d% p4 \
I/O size (minimum/optimal): 512 bytes / 512 bytes2 N1 P6 I8 h8 d3 n$ r. n$ J
Disk identifier: 0x000e8e8d& h6 J1 l- o+ k/ r
1 L1 c; L- Z3 J9 b( f; q3 M3 n Device Boot Start End Blocks Id System
0 s. m' M* ^. Q$ P! a) [ l/dev/nbd0p1 * 2048 41943039 20970496 83 Linux
- P0 N, I8 _/ {- N0 e
4 E+ k. G" W u* T9 kCommand (m for help): d. C8 @* F- N7 f" h E
Selected partition 1) e. `% g, H3 x; [1 P# \
' c/ P- }0 ?% b) |! n; G0 lCommand (m for help): p" w6 `2 S. _0 ?
8 m: y2 w6 _8 R. M, o$ F6 G4 [
Disk /dev/nbd0: 32.2 GB, 32212254720 bytes2 _% g0 S/ g; g" E0 W
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
% G2 K0 O% {0 k4 U$ ~Units = sectors of 1 * 512 = 512 bytes
/ J; r! o- b" j& H) RSector size (logical/physical): 512 bytes / 512 bytes
$ N. T4 l5 J6 h) V, L L* @' OI/O size (minimum/optimal): 512 bytes / 512 bytes+ Y: D6 O8 M5 v/ K L6 {
Disk identifier: 0x000e8e8d' c; w2 J2 z) ^. t) v
G4 s+ K- ~1 }. o2 h6 B Device Boot Start End Blocks Id System
" s7 `; U4 Z1 j4 x8 W2 x$ @0 ^3 I# ]- `5 U2 U3 t, L
Command (m for help): n7 ~ ^: c0 Q- O% e" r
Partition type:
" ~; S; l$ E- W p primary (0 primary, 0 extended, 4 free)# t7 t. S3 G6 |1 R- U E6 a8 e
e extended% L$ L8 s4 X2 p# c5 M! h, B$ h
Select (default p): p
: D9 Z9 x' k x6 c1 SPartition number (1-4, default 1): 2 l/ g4 D1 H- c. I: _
Using default value 1
9 n: s$ L# k4 x8 Z3 s! YFirst sector (2048-62914559, default 2048): 8 H: `8 z) Y2 l0 o2 I# u! j. K. L
Using default value 2048 h2 P% a9 ~/ y- p; ]$ V
Last sector, +sectors or +size{K,M,G} (2048-62914559, default 62914559):
W, N$ K4 H9 U% kUsing default value 62914559
, j" B; c9 Q- [* C8 z7 [9 _9 a% Q) Q" E+ g3 Y2 L M' C- D1 _
Command (m for help): p
3 x$ v; C7 |6 [6 |* i1 b2 s+ H C$ g, w
Disk /dev/nbd0: 32.2 GB, 32212254720 bytes
( b# x' o7 d4 c; N255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
! y5 O# ?) j* s# o4 ]Units = sectors of 1 * 512 = 512 bytes( F8 j" M3 |9 D5 i% {$ f
Sector size (logical/physical): 512 bytes / 512 bytes8 ^, z8 ^% Y$ o. @7 D
I/O size (minimum/optimal): 512 bytes / 512 bytes
1 `$ P1 K, }* f$ S8 }( `. DDisk identifier: 0x000e8e8d. O5 N1 T' ~7 ~8 o5 }& N+ J! B
) l- x' k- P0 l* N# Q Device Boot Start End Blocks Id System# ^* N2 }/ ]1 H
/dev/nbd0p1 2048 62914559 31456256 83 Linux
8 _1 N& J& I7 G) o9 h
3 @- y% C, m7 I8 b: D% ~$ DCommand (m for help): a% [; m: y4 x9 J4 F' ^7 C8 _5 Q+ y
Partition number (1-4): 1, E- D5 |0 L' v
! b% E \/ c5 D8 kCommand (m for help): p
' m. ?+ L4 K( E; W; l
( K( j' x) n7 o, t& }3 S7 hDisk /dev/nbd0: 32.2 GB, 32212254720 bytes7 c$ v# r$ [% q
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
+ t" h: G- s! D, ~- G LUnits = sectors of 1 * 512 = 512 bytes
K- e$ W. r: |6 D% D g" jSector size (logical/physical): 512 bytes / 512 bytes6 G) |* C0 q8 H4 @; ~+ U
I/O size (minimum/optimal): 512 bytes / 512 bytes R, v+ M2 J0 A/ i4 r' v
Disk identifier: 0x000e8e8d
. s5 b/ A, G& q. [/ [ |& G: k9 `0 B
Device Boot Start End Blocks Id System
) b6 k( |3 z9 M( x6 C/dev/nbd0p1 * 2048 62914559 31456256 83 Linux j+ j- | c6 G$ Z# q b
4 U2 Y" H. Q! e" j5 f
Command (m for help): w$ n" V' V5 }$ f
The partition table has been altered!
1 i+ |, O, V8 H# t
2 b/ v+ u N4 N* b1 u4 H3 xCalling ioctl() to re-read partition table.6 `/ R' J# k* [
Syncing disks.
/ ?1 d. s! A" ^ \& S; w F1 j$ F9 V/ G% u- T/ z6 d
复制代码/ f8 ^2 t/ T" C Q
如此以来,我们已经将该虚拟磁盘的分区大小扩展到了30G! d) M* N4 Y- G; t, V% Q
, k1 J- D, G- k9 U7 a7、至此,我们还需要将文件系统扩展到30G:
9 y3 d D- L3 s* b, I1 M4 S! P4 {" m* W$ n: |
复制代码
) N! |# T- B, ve2fsck -fp /dev/nbd0p1
4 d- O2 j" {& ~) D3 R/ r/dev/nbd0p1: Deleted inode 131076 has zero dtime. FIXED.
# J- s$ ^: F' t7 A& A9 H) O# R3 x/dev/nbd0p1: 18489/1310720 files (0.2% non-contiguous), 281286/5242624 blocks, q" v% l- q+ V' ^% Q; E" l
% u5 o- E6 N7 m5 P( `
resize2fs /dev/nbd0p1 1 @ a8 _8 s! a& A
resize2fs 1.42.9 (4-Feb-2014)4 C3 |% g1 x) A4 {/ P/ q8 y M
Resizing the filesystem on /dev/nbd0p1 to 7864064 (4k) blocks. I) {) {5 Y* S) k- H) z
The filesystem on /dev/nbd0p1 is now 7864064 blocks long.
! r ^0 c, j8 ?. n! X/ b) {! ? qemu-nbd -d /dev/nbd0% G9 r1 ~' k8 C5 G, }( F* Q$ h# c/ `
/dev/nbd0 disconnected |
|