How to add a new disk to Linux?¶
A brief summary of the commands¶
Command | Description |
---|---|
lsblk |
list block devices |
fdisk /dev/sdb |
create a new partition in the unallocated area /dev/sdb , n - create partition, p - create main partition, w - write changes to disk |
pvs |
list physical volumes |
vgs |
list volume groups |
lvs |
list logical volumes |
df -hT |
list partitions and file systems |
pvcreate /dev/sdb1 |
create a new physical volume /dev/sdb1 |
vgextend centos /dev/sdb1 |
add volume /dev/sdb1 to the centos group |
lvextend -l +100%FREE /dev/centos/root |
expand logical volume /dev/centos/root |
xfs_growfs /dev/mapper/centos-root |
increase the size of the XFS file system on a logical volume /dev/mapper/centos-root (for Fedora, CentOS, RHEL, Oracle Linux, Alma Linux, Rocky Linux) |
resize2fs /dev/mapper/ubuntu-root |
increase the size of an EXT4 file system on a logical volume (for Ubuntu, Debian) |
Info
For the btrfs
file system, you should use different instructions.
Disk Adding¶
This example will describe how to expand the disk space of the / (root) partition by 10Gb by adding a new device (disk) using the capabilities of LVM. The extension will be performed on the example of the CentOS
distribution. This manual is suitable for most Linux distributions, gives detailed information on how to correctly expand disk space even with a non-standard partition layout, and takes into account the features of commands of various distributions.
Note
All commands must be run as root or superuser (sudo).
When following this instruction, there is a risk of data loss, so it is recommended to make a backup of the virtual machine first.
Step 1. Analysis of disk space configuration and search for a new device (disk)¶
First you need to determine the name of the newly added device (disk) in the system using the command lsblk
.
[root@centos~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 30G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 29G 0 part
├─centos-root 253:0 0 28G 0 lvm /
└─centos-swap 253:1 0 1G 0 lvm [SWAP]
sdb 8:16 0 10G 0 disk
sr0 11:0 1 1024M 0 rom
vda 252:0 0 20G 0 disk
The structure of disk space can be displayed as a diagram:
In this example, we see that there are 3 devices in the system:
- /dev/sda
- /dev/sdb
- /dev/vda
On device /dev/sda (30Gb) 2 partitions are created:
- The /dev/sda1 (1Gb) partition is the primary partition labeled /boot, on which the Linux kernel loader is installed.
- The /dev/sda2 (29Gb) partition is an extended LVM partition on which the /dev/sda2 physical volume is created.
Based on the physical volume, a centos volume group was created, which includes 2 logical volumes root and swap.
The devices /dev/sdb (10Gb) and /dev/vda (20Gb) are added to the system but are not partitioned.
Let's select a device /dev/sdb with a size of 10Gb for expansion. Devices (disks) in Linux can have the following names: sda, sdb, sdc, etc.
Note
The vda device was created during the initial order of the virtual machine with the additional disk option. In this example, we are using the new device /dev/sdb. (Please note your device name may be different)
Step 2. Creating a new partition on a new device (disk)¶
You need to create a new partition with the command:
fdisk /dev/sdb
- where /dev/sdb
is the name of the new partition.
Next, enter the following keys in sequence:
n
- create partition
p
- create main partition
select the partition number, its first and last sectors (Enter
by default)
w
- write changes to disk
The created partition can be seen by typing the command again lsblk
.
-
Using the fdisk utility, create a new partition on a new device, in our case
/dev/sdb
.Note
Your device name may be different.
Enter
n
to create a new partition:[root@centos ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.32.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table. Created a new DOS disklabel with disk identifier 0xba91dc06. Command (m for help): n
-
Then choose
p
to create a new primary partition:
Note
Your system can only have 4 primary partitions on a single drive. If you have already reached this limit, create an extended partition.
-
Select the partition number and its first and last sectors, if you press
Enter
, then, by default, the new partition will use all available disk space: -
Finally, you need to write the partition to disk using the
w
command. -
Check if the partition has been created using the
lsblk
command.[root@centos~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 30G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 29G 0 part ├─centos-root 253:0 0 28G 0 lvm / └─centos-swap 253:1 0 1G 0 lvm [SWAP] sdb 8:16 0 10G 0 disk ├─sdb1 8:17 0 10G 0 disk sr0 11:0 1 1024M 0 rom vda 252:0 0 20G 0 disk
The /dev/sdb (10Gb) device has a /dev/sdb1 (10Gb) partition.
In this example, we see that the system has a physical volume /dev/sda2, which is part of the created centos volume group. This volume group has 2 logical volumes: root and swap.
Step 3. Analysis of the existing LVM configuration and file system¶
To check an existing LVM configuration, you can enter the commands:
pvs
- display the name and number of available physical volumes (PV)
vgs
- list volume groups (VG)
lvs
- list logical volumes (LV)
[root@centos~]# pvs
PV VG Fm Attr PSize PFree
dev/sda centos lvm2 a-- <29.00g 0
[root@centos~]# vgs
VG #PV #LV #SN Attr VSize PFree
dev/sda centos lvm2 0 wz--n <29.00g 0
[root@centos~]# lvs
LG VG Attr LSize
root centos -wi-ao---- <28.00g
swap centos -wi-ao---- 1.00g
You need to determine the system partition and the type of file system on it, which you want to expand using the command df -hT
.
[root@centos~]# df-hT
Filesystem Type Size Used Avail Use Mounted on
devtmpfs devtmpfs 856M 0 856M 0% /dev
tmpfs tmpfs 875M 0 875M 0% /dev/shm
tmpfs tmpfs 875M 8.5M 867M 1% /run
tmpfs tmpfs 875M 0 875M 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 28G 2.1G 26G 8% /
/dev/sda1 xfs 1014M 339M 676M 34% /boot
tmpfs tmpfs 175M 0 175M 0% /run/user/0
In this example, we want to expand a 28Gb partition called /dev/mapper/centos-root with an xfs file system that has a mount point /.
The structure of disk space before the LVM expansion is shown in the diagram:
Step 4. LVM extension¶
-
On the new disk, you need to create a new physical volume using the command:
pvcreate /dev/sdb1
- where/dev/sdb1
is the name of the new partition. -
Extend a volume group by adding a new physical volume to it using the command:
vgextend centos /dev/sdb1
- wherecentos
is the name of the volume group (can be found using thevgs
command). -
Extend the logical volume by running the command:
lvextend -l +100%FREE /dev/centos/root
- where
/dev/centos/root
is the path of the expandable file system,
centos
is the name of the group (can be found with thevgs
command),
root
is the name of the logical volume (can be found with thelvs
command).
In the command above, instead of /dev/centos/root
, you can specify another path /dev/mapper/centos-root
(can be found with the
df -hT
command), and the result will be the same.
- Create a physical volume on the newly created partition using the
pvcreate
command.
Info
In our example, the created partition is /dev/sdb1
. Please note that your partition name may differ, and also check the created physical volume with the pvs
command.
[root@centos~]# pvcreate/dev/sdb1
Physical volume "dev/sdb1" successfully created.
[root@centos~]# pvs
PV VG Fmt Attr PSize PFree
dev/sda2 centos lvm2 a-- <29.00g 0
dev/sdb1 lvm2 --- <10.00g <10.00g
In this example, the physical volume has 10Gb free space and no volume group.
- In step 3, we learned the name of the volume group. Extend this volume group by adding a new physical volume to it using the
vgextend
command.
Check the changes with thevgs
command.
Info
In our example, the volume group was called centos
, the new physical volume is /dev/sdb1
. Note that you will have your own group name and physical volume. The VFree column shows the amount of free space.
[root@centos~]# vgextend centos /dev/sdb1
Volume group "centos" successfully extended.
[root@centos~]# vgs
VG #PV #LV #SN Attr PSize PFree
centos 2 2 0 wz--n- 38.99g <10.00g
In this example, the centos volume group has 2 physical volumes and 10Gb of free space.
- Also, at step 3, in addition to the volume group name, we learned the name of the logical volume to be expanded. Expand this logical volume using the
lvextend
command.
Note
Note that you will have your own name for the group and logical volume.
Make sure to expand the logical volume with the lvs
command.
[root@centos~]# lvextend -l +100%FREE /dev/centos/root
Size of logical volume centos/root changed from <28.00 GiB (7167 extents) to 37.99 GiB (9726 extents).
Logical volume centos/root successfully resized.
[root@centos~]# lvs
LV VG Attr LSize
root centos -wi-ao---- 37.99g
swap centos -wi-ao---- 1.00g
In this example, the total size of the logical volume from the centos volume group is expanded to 37.99Gb.
Step 5. Expanding the file system¶
Note
This operation carries the risk of data loss. It is recommended that you make a backup of the virtual machine beforehand.
-
To increase the file system on a logical volume, you must enter the command, depending on the distribution:
-
Fedora, CentOS, RHEL, Oracle Linux, Alma Linux, Rocky Linux (XFS file system):
xfs_growfs /dev/mapper/centos-root
- where
/dev/mapper/centos-root
- the name of the file system being expanded (the name and type can be found with thedf -hT
command) -
Ubuntu, Debian (ext4 file system):
resize2fs /dev/mapper/ubuntu-root
- where
/dev/mapper/ubuntu-root
is the name of the expandable file system (the name and type can be found with thedf -hT
command)
-
-
Let's determine the mount path of the file system of the root partition, the mount point, and the type of the file system. In our example, the path is
/dev/mapper/centos-root
(or/dev/centos/root
),
the mount point is/
,
the file system type isxfs
.
Note
You will have your own path and file system type.
-
Next, you need to increase the size of the file system on the logical volume. Depending on the type of file system and distribution, select the type that suits you below.
-
For the XFS file system (Fedora, CentOS, RHEL, Oracle Linux, Alma Linux, Rocky Linux distributions), the
xfs_growfs
utility is used and the path specified in the output of thedf -hT
command for the expandable partition (in the example below for Centos it is/dev/mapper /centos-root
).[root@centos~]# df -hT /dev/centos/root Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/centos-root xfs 28G 2.1G 26G 8% / [root@centos~]]# xfs_growfs /dev/mapper/centos-root meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1834752 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=1, sparse=1, rmapbt=0 = reflink=1 data = bsize=4096 blocks=7339008, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0, ftype=1 log =internal log bsize=4096 blocks=3583, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 7339008 to 9959424
-
For the
ext4
file system (Ubuntu, Debian distributions), use theresize2fs
utility and the path specified in the output of thedf -hT
command for the expandable partition (in the example below, for Ubuntu this is/dev/mapper/ubuntu-root
).[root@ubuntu:~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/ubuntu-root ext4 38G 7.1G 29G 20% / root@ubuntu:~# resize2fs /dev/mapper/ubuntu-root resize2fs 1.45.5 (07 Jan 2020) Filesystem at /dev/mapper/ubuntu-root is mounted on/; on-line resizing required old_desc_blocks=4, new_desc_blocks=5 The filesystem on /dev/mapper/ubuntu-root is now 10101760 (4k) blocks long.
-
Step 6. Verification¶
To check the new disk size and file system, repeat the commands df -hT
and lsblk
.
[root@centos~]# df -hT /
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 38G 2.2G 36G 6% /
[root@centos~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 30G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 29G 0 part
├─centos-root 253:0 0 38G 0 lvm /
└─centos-swap 253:1 0 1G 0 lvm [SWAP]
sdb 8:16 0 10G 0 disk
├─sdb1 8:17 0 10G 0 disk
└─centos-swap 253:1 0 38G 0 lvm /
sr0 11:0 1 1024M 0 rom
vda 252:0 0 20G 0 disk
The final configuration of disk space and the file system is shown in the diagram: