In this post I’ll explain two different scenarios. In the first, I’ll add a new disk and a new partition to CentOS and in the second, I’ll expand the existing disk/partition.
Add new disk/partition
Once you add the new disk, you have to check if the OS recognized it.
fdisk -l Disk /dev/sda: 17.2 GB, 17179869184 bytes, 33554432 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000c4343 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 33554431 15727616 8e Linux LVM Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-root: 14.4 GB, 14382268416 bytes, 28090368 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-swap: 1719 MB, 1719664640 bytes, 3358720 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
As you can see, our new disk is there and it’s called /dev/sdb. We have to create a new partition there that will allocate 100% of the disk. See the highlighted lines. n means new partition, p is primary, 1 is the first partition, then enter twice for for the defaults of the first and last sector, which means the whole disk. Then I used t to change the partition type, L to get the hex codes, 8e which is Linux LVM, a partition type that I need and finally w to write that information.
fdisk /dev/sdb Welcome to fdisk (util-linux 2.23.2). 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 Building a new DOS disklabel with disk identifier 0x4dd0f270. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-41943039, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): Using default value 41943039 Partition 1 of type Linux and of size 20 GiB is set Command (m for help): t Selected partition 1 Hex code (type L to list all codes): L 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT- 4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx 5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data 6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility 8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt 9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/ 10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b 11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor 12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor 14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary 16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS 17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT 1e Hidden W95 FAT1 80 Old Minix Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM' Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
If you type fdisk -l again, you’ll see that you have a new partition called /dev/sdb1. We have to create a physical volume on this partition.
pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created.
Then, we need a volume group created. I’ll call it nfsvg. This is for my NFS partition.
vgcreate nfsvg /dev/sdb1 Volume group "nfsvg" successfully created
Type vgdisplay to see the volume groups.
Now, we have to create the logical volume that uses the whole disk. The name will be nfsvg.
lvcreate -n nfslv -l 100%FREE nfsvg Logical volume "nfslv" created.
Type lvdisplay to see the logical volumes.
But, we are not ready yet. We have to format the partition. I’ll use ext4 type of filesystem.
mkfs.ext4 /dev/nfsvg/nfslv mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 1310720 inodes, 5241856 blocks 262092 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2153775104 160 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
Now that we have everything ready, we can create a mount point and mount the partition.
mkdir /nfs mount /dev/nfsvg/nfslv /nfs
This will mount the partition, but if we reboot the changes will be lost. So, edit /etc/fstab and add the nfs line so it looks like this. Enter tabs between the parameters, the two zeros are separated by space.
/dev/nfsvg/nfslv /nfs ext4 defaults 0 0
Expand root partition
Let’s say you have a 10GB single disk in a VM running CentOS and you want to expand the root partition. Increase the disk to 20GB from VMWare console. This is how it will look like. You’ve added 10 extra gigs, but there are no changes.
df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 8.0G 1.2G 6.9G 15% / devtmpfs 908M 0 908M 0% /dev tmpfs 920M 0 920M 0% /dev/shm tmpfs 920M 8.8M 911M 1% /run tmpfs 920M 0 920M 0% /sys/fs/cgroup /dev/sda1 1014M 180M 835M 18% /boot tmpfs 184M 0 184M 0% /run/user/1000
Or if you type pvscan.
pvscan PV /dev/sda2 VG centos lvm2 [<9.00 GiB / 0 free] Total: 1 [<9.00 GiB] / in use: 1 [<9.00 GiB] / in no VG: 0 [0 ]
Or if you type fdisk -l.
fdisk -l Disk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000b9ce3 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 20971519 9436160 8e Linux LVM Disk /dev/mapper/centos-root: 8585 MB, 8585740288 bytes, 16769024 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
We have to delete /dev/sda2 first. Type fdisk /dev/sda first. At the prompt type p to see the parititions.
Command (m for help): p Disk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000b9ce3 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 20971519 9436160 8e Linux LVM
Delete the 2nd partition.
Command (m for help): d Partition number (1,2, default 2): 2 Partition 2 is deleted
Now, recreate the 2nd partition again. You’ll see that the end sector is different. Follow the highlighted lines, press enter when asked for the sectors.
Command (m for help): n Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): p Partition number (2-4, default 2): 2 First sector (2099200-41943039, default 2099200): Using default value 2099200 Last sector, +sectors or +size{K,M,G} (2099200-41943039, default 41943039): Using default value 41943039 Partition 2 of type Linux and of size 19 GiB is set Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks.
Because we modified the boot disk, partprobe won’t work, so you have to reboot.
Once the system comes back, we have to expand the physical volume.
pvresize /dev/sda2 Physical volume "/dev/sda2" changed 1 physical volume(s) resized / 0 physical volume(s) not resized
Add the spare 10GB to the logical volume. In my case it’s centos. In your case it might be different. Do df -h and see what does it say. It’s probably something like /dev/mapper/something. Before you type the command below to add exact space, most likely you’ll want to use the whole remaining available space. In that case instead of the command below, use lvextend -l +100%FREE /dev/mapper/something.
lvextend -L +10G /dev/mapper/centos-root Size of logical volume centos/root changed from <8.00 GiB (2047 extents) to <18.00 GiB (4607 extents). Logical volume centos/root successfully resized.
Finally, extend the file system.
xfs_growfs /dev/mapper/centos-root meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=524032 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=2096128, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 2096128 to 4717568
NOTE: Under CentOS 8, when you try to grow the filesystem, you might get an error saying that /dev/mapper/centos-root or /dev/mapper/cl-root is not a mounted XFS filesystem. You’ll have to target the mount point which is /, so do xfs_growfs /
If you do df -h now, you’ll see that you are all set.
df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 18G 1.2G 17G 7% / devtmpfs 908M 0 908M 0% /dev tmpfs 920M 0 920M 0% /dev/shm tmpfs 920M 8.8M 911M 1% /run tmpfs 920M 0 920M 0% /sys/fs/cgroup /dev/sda1 1014M 180M 835M 18% /boot tmpfs 184M 0 184M 0% /run/user/1000
2 comments
Hello Can I ask a question? You said “We have to delete /dev/sda2 first. Type fdisk /dev/sda first. At the prompt type p to see the parititions.” if we delete or format sda2, data will be lost?
That’s a good question. No, you are not deleting or reformatting the partition sda2. You are just removing the pointer for sda2 in the partition tables list. By removing /dev/sda2 from fdisk, you are removing metadata. It’s like detaching the partition.