Project

General

Profile

Resizing an existing disk with LVM » History » Version 1

offbyone, 2023-07-13 09:44
Initial version

1 1 offbyone
h1. Resizing an existing disk with LVM
2
3
{{>toc}}
4
5
6
h2. Synopsis
7
8
If an existing disk is resized and you want to add the extra space to an existing LVM logical volume, follow this guide.
9
10
Source: https://www.ibm.com/support/pages/how-increase-partition-size-using-existing-disk-rhel-lvm
11
12
13
h2. Procedure
14
15
The output of the commands below are from a real situation:
16
* The disk was resized from 100GB to 300GB
17
* The extra space was to be added to the root filesystem
18
* The disk is @/dev/sda@; adapt where necessary for your situation
19
20
h3. Rescan the disk
21
22
When a disk is resized, the Linux kernel doesn't automatically pick up this change.
23
24
To trigger it to rescan a disk, execute this command:
25
<pre>
26
root@server:~# </notextile>*echo 1 > /sys/class/block/sda/device/rescan*<notextile>
27
</pre>
28
29
30
Make sure the disk space is refreshed:
31
<pre>
32
root@server:~# </notextile>*fdisk -l*<notextile>
33
Disk /dev/sda: </notextile>%{background-color: yellow}300 GiB%, 322122547200 bytes, 629145600 sectors<notextile>
34
Disk model: Virtual disk
35
Units: sectors of 1 * 512 = 512 bytes
36
Sector size (logical/physical): 512 bytes / 512 bytes
37
I/O size (minimum/optimal): 512 bytes / 512 bytes
38
Disklabel type: dos
39
Disk identifier: 0xc4a390f9
40
41
Device     Boot   Start       End   Sectors  Size Id Type
42
/dev/sda1  *       2048    999423    997376  487M 83 Linux
43
/dev/sda2       1001470 104855551 103854082 49.5G  5 Extended
44
/dev/sda5       1001472 104855551 103854080 49.5G 8e Linux LVM
45
46
47
Disk /dev/mapper/volume-vg-root: 48.56 GiB, 52143587328 bytes, 101842944 sectors
48
Units: sectors of 1 * 512 = 512 bytes
49
Sector size (logical/physical): 512 bytes / 512 bytes
50
I/O size (minimum/optimal): 512 bytes / 512 bytes
51
52
53
Disk /dev/mapper/volume-vg-swap_1: 980 MiB, 1027604480 bytes, 2007040 sectors
54
Units: sectors of 1 * 512 = 512 bytes
55
Sector size (logical/physical): 512 bytes / 512 bytes
56
I/O size (minimum/optimal): 512 bytes / 512 bytes
57
</pre>
58
Looks good.
59
60
61
h3. Create a new partition containing the extra disk space
62
63
Before disk space can be added to a LVM volume group, it needs to be inside a disk partition.
64
65
We create a primary partition (which will become @/dev/sda3@) for the disk space:
66
<pre>
67
root@server:~# </notextile>*fdisk /dev/sda*<notextile>
68
69
Welcome to fdisk (util-linux 2.36.1).
70
Changes will remain in memory only, until you decide to write them.
71
Be careful before using the write command.
72
73
74
Command (m for help): </notextile>*F*<notextile>
75
Unpartitioned space /dev/sda: 250 GiB, 268436504576 bytes, 524290048 sectors
76
Units: sectors of 1 * 512 = 512 bytes
77
Sector size (logical/physical): 512 bytes / 512 bytes
78
79
    Start       End   Sectors  Size
80
104855552 629145599 524290048  250G
81
82
Command (m for help): </notextile>*n*</notextile>
83
Partition type
84
   p   primary (1 primary, 1 extended, 2 free)
85
   l   logical (numbered from 5)
86
Select (default p): </notextile>*p*<notextile>
87
Partition number (3,4, default 3): </notextile>*&lt;ENTER&gt;*<notextile>
88
First sector (104855552-629145599, default 104855552): </notextile>*&lt;ENTER&gt;*<notextile>
89
Last sector, +/-sectors or +/-size{K,M,G,T,P} (104855552-629145599, default 629145599): </notextile>*&lt;ENTER&gt;*<notextile>
90
91
Created a new partition 3 of type 'Linux' and of size 250 GiB.
92
93
Command (m for help): </notextile>*p*<notextile>
94
Disk /dev/sda: 300 GiB, 322122547200 bytes, 629145600 sectors
95
Disk model: Virtual disk
96
Units: sectors of 1 * 512 = 512 bytes
97
Sector size (logical/physical): 512 bytes / 512 bytes
98
I/O size (minimum/optimal): 512 bytes / 512 bytes
99
Disklabel type: dos
100
Disk identifier: 0xc4a390f9
101
102
Device     Boot     Start       End   Sectors  Size Id Type
103
/dev/sda1  *         2048    999423    997376  487M 83 Linux
104
/dev/sda2         1001470 104855551 103854082 49.5G  5 Extended
105
/dev/sda3       104855552 629145599 524290048  250G 83 Linux
106
/dev/sda5         1001472 104855551 103854080 49.5G 8e Linux LVM
107
108
Partition table entries are not in disk order.
109
110
Command (m for help): </notextile>*w*<notextile>
111
The partition table has been altered.
112
Syncing disks.
113
</pre>
114
115
116
h3. Refresh the partition table
117
118
Like we had to trigger the kernel to rescan the disk to pick up on the newly created diskspace, we need to trigger the kernel to reread the partition table to pick up the changes we just made.
119
120
<pre>
121
root@server:~# </notextile>*partprobe -s*<notextile>
122
/dev/sda: msdos partitions 1 2 <5> 3
123
/dev/mapper/volume-vg-swap_1: loop partitions 1
124
/dev/mapper/volume-vg-root: loop partitions 1
125
</pre>
126
127
128
h3. Create a physical volume (PV) for LVM
129
130
In order to use a raw disk partition with LVM, it must be wrapped in a LVM physical volume.
131
132
To create such a physical volume with our new partition, execute:
133
<pre>
134
root@server:~# </notextile>*pvcreate /dev/sda3*<notextile>
135
  Physical volume "/dev/sda3" successfully created.
136
</pre>
137
138
139
h3. Add the PV to the volume group (VG)
140
141
First we need to get the name of the VG where we want to add the disk space:
142
<pre>
143
root@server:~# </notextile>*vgdisplay*<notextile>
144
  --- Volume group ---
145
  VG Name               </notextile>%{background-color: yellow}volume-vg%<notextile>
146
  System ID
147
  Format                lvm2
148
  Metadata Areas        1
149
  Metadata Sequence No  3
150
  VG Access             read/write
151
  VG Status             resizable
152
  MAX LV                0
153
  Cur LV                2
154
  Open LV               2
155
  Max PV                0
156
  Cur PV                1
157
  Act PV                1
158
  VG Size               <49.52 GiB
159
  PE Size               4.00 MiB
160
  Total PE              12677
161
  Alloc PE / Size       12677 / <49.52 GiB
162
  Free  PE / Size       0 / 0
163
  VG UUID               qE7Xrc-ePAK-B6vq-eGHt-qRCw-M6K9-TR9YlL
164
</pre>
165
166
167
Now we'll extend the volume group by adding the physical volume @/dev/sda3@:
168
<pre>
169
root@server:~# </notextile>*vgextend volume-vg /dev/sda3*<notextile>
170
  Volume group "volume-vg" successfully extended
171
</pre>
172
173
List the PVs on the system to verify:
174
<pre>
175
root@server:~# </notextile>*pvscan*<notextile>
176
  PV /dev/sda5   VG volume-vg   lvm2 [<49.52 GiB / 0    free]
177
</notextile>%{background-color: yellow}  PV /dev/sda3   VG volume-vg   lvm2 [250.00 GiB / 250.00 GiB free]%<notextile>
178
  Total: 2 [<299.52 GiB] / in use: 2 [<299.52 GiB] / in no VG: 0 [0   ]
179
</pre>
180
181
Check the VG to verify that the free space is added:
182
<pre>
183
root@server:~# </notextile>*vgdisplay*<notextile>
184
  --- Volume group ---
185
  VG Name               volume-vg
186
  System ID
187
  Format                lvm2
188
  Metadata Areas        2
189
  Metadata Sequence No  4
190
  VG Access             read/write
191
  VG Status             resizable
192
  MAX LV                0
193
  Cur LV                2
194
  Open LV               2
195
  Max PV                0
196
  Cur PV                2
197
  Act PV                2
198
  VG Size               <299.52 GiB
199
  PE Size               4.00 MiB
200
  Total PE              76677
201
  Alloc PE / Size       12677 / <49.52 GiB
202
</notextile>%{background-color: yellow}  Free  PE / Size       64000 / 250.00 GiB%<notextile>
203
  VG UUID               qE7Xrc-ePAK-B6vq-eGHt-qRCw-M6K9-TR9YlL
204
</pre>
205
206
207
h3. Extend the logical volume (LV) with the new disk space
208
209
First, display the LV so we can get the path:
210
<pre>
211
root@server:~# </notextile>*lvdisplay*<notextile>
212
  --- Logical volume ---
213
</notextile>%{background-color: yellow}  LV Path                /dev/volume-vg/root%<notextile>
214
  LV Name                root
215
  VG Name                volume-vg
216
  LV UUID                cH0jGQ-qF4y-aoMg-2mXp-qM6g-FOIu-2vH7sr
217
  LV Write Access        read/write
218
  LV Creation host, time volume, 2023-05-24 09:13:41 +0200
219
  LV Status              available
220
  # open                 1
221
  LV Size                48.56 GiB
222
  Current LE             12432
223
  Segments               1
224
  Allocation             inherit
225
  Read ahead sectors     auto
226
  - currently set to     256
227
  Block device           254:0
228
229
  --- Logical volume ---
230
  LV Path                /dev/volume-vg/swap_1
231
  LV Name                swap_1
232
  VG Name                volume-vg
233
  LV UUID                nt3bJn-w6lN-RSnY-iEFQ-uWd7-f3ve-jkj2jw
234
  LV Write Access        read/write
235
  LV Creation host, time volume, 2023-05-24 09:13:41 +0200
236
  LV Status              available
237
  # open                 2
238
  LV Size                980.00 MiB
239
  Current LE             245
240
  Segments               1
241
  Allocation             inherit
242
  Read ahead sectors     auto
243
  - currently set to     256
244
  Block device           254:1
245
</pre>
246
247
248
Extend the LV, giving it all the free space.
249
By using the @--resizefs@ parameter, @lvresize@ will automatically resize the underlying filesystem to accomodate for the change in available diskspace. Without this parameter, resizing the filesystem must be done manually (using @resize2fs@, @xfs_growfs@, &hellip;).
250
251
252
Execute:
253
<pre>
254
root@server:~# </notextile>*lvresize --resizefs --extents +100%FREE /dev/volume-vg/root*<notextile>
255
  Size of logical volume volume-vg/root changed from 48.56 GiB (12432 extents) to 298.56 GiB (76432 extents).
256
  Logical volume volume-vg/root successfully resized.
257
resize2fs 1.46.2 (28-Feb-2021)
258
Filesystem at /dev/mapper/volume-vg-root is mounted on /; on-line resizing required
259
old_desc_blocks = 7, new_desc_blocks = 38
260
The filesystem on /dev/mapper/volume-vg-root is now 78266368 (4k) blocks long.
261
</pre>
262
263
264
Finally, use @df@ to verify the disk space is actually available on the filesystem:
265
<pre>
266
root@server:~# </notextile>*df -h*<notextile>
267
Filesystem                            Size  Used Avail Use% Mounted on
268
udev                                   16G     0   16G   0% /dev
269
tmpfs                                 3.2G  604K  3.2G   1% /run
270
</notextile>%{background-color: yellow}/dev/mapper/volume-vg-root            294G  1.4G  280G   1&percnt; /%<notextile>
271
tmpfs                                  16G     0   16G   0% /dev/shm
272
tmpfs                                 5.0M     0  5.0M   0% /run/lock
273
/dev/sda1                             470M   87M  359M  20% /boot
274
tmpfs                                 3.2G     0  3.2G   0% /run/user/1000
275
</pre>