This is the original disk layout before the disk extension
# pvcreate /dev/sda3
# vgs
# vgextend centos /dev/sda3
# lvdisplay | grep Path
# lvextend -l +100%FREE /dev/centos/root
# lvs
# df -Th /
# xfs_growfs /
# df -Th /
Linux is for everybody. Lets enjoy it.
This is the original disk layout before the disk extension
# pvcreate /dev/sda3
# vgs
# vgextend centos /dev/sda3
# lvdisplay | grep Path
# lvextend -l +100%FREE /dev/centos/root
# lvs
# df -Th /
# xfs_growfs /
# df -Th /
Manipulating and partitioning hardisk can be a daunting task, especially for new sysadmin and if the disk has already contain data. Luckily, there is a tool that make this task easier, and that tool is called cfdisk.
$ sudo cfdisk /dev/sda
$ sudo partprobe
$ lsblk
In previous post, we have covered the way to make LVM and filesystem aware of the disk size increase that happen in the virtual machine layer.
$ sudo lvmdiskscan
$ sudo pvcreate <path to the new disk>
$ sudo pvs
$ sudo vgextend <VG name> <PV name>
$ sudo vgs
$ sudo lvextend -l +100%FREE <LV PATH>
$ sudo lvdisplay | grep Path
$ sudo xfs_growfs <mountpoint>
$ sudo resize2fs <mountpoint>
$ df -Th
The default partitioning scheme for CentOS is not ideal, because the installer only give 50Gb to /. and the rest to /home. Luckily, the partitions are being setup in LVM, thus enable us to change it without reinstalling the operating system.
In this example, I will show how to remove logical volume used by /home, and assign it to / instead.
First of all, you need to be root in order to do this.
This is the original partition layout:
# df -Th
Filesystem Type Size Used Avail Use% Mounted on
...
/dev/mapper/cl-root xfs 50G 4.1G 46G 9% /
/dev/mapper/cl-home xfs 1.9T 14G 1.9T 1% /home
/dev/vda1 ext4 976M 194M 716M 22% /boot
...
A usual scenario where rename and remount of LVM logical volume are needed, is when you install a box with a CentOS, and use the default LVM based partitioning scheme. This scheme will take 50G for your / partition, and the rest will be allocated to your /home, which is not practical. In this example, we will be remounting logical volume originally mounted to /home, to /var/lib/elasticsearch, renaming it along the way.
Check the current partition
# parted /dev/sda print
Model: Cisco (scsi)
Disk /dev/sda: 78124994560s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Name Flags
1 2048s 4095s 2048s primary Linux Filesystem
2 4096s 4294967295s 4294963200s primary xfs Linux Filesystem boot
# gdisk /dev/sda
GPT fdisk (gdisk) version 0.8.6
Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: not present
***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format.
THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing 'q' if
you don't want to convert your MBR partitions to GPT format!
***************************************************************
Command (? for help):
Command (? for help): p
Disk /dev/sda: 78124994560 sectors, 36.4 TiB
Logical sector size: 512 bytes
Disk identifier (GUID): F0A6B5A3-3C91-47CE-BD70-FAD8A88DD132
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 78124994526
Partitions will be aligned on 2048-sector boundaries
Total free space is 73830029245 sectors (34.4 TiB)
Number Start (sector) End (sector) Size Code Name
1 2048 4095 1024.0 KiB 8300 Linux filesystem
2 4096 4294967295 2.0 TiB 8300 Linux filesystem
Command (? for help): n
Partition number (3-128, default 3):
First sector (34-78124994526, default = 4294967296) or {+-}size{KMGTP}:
Last sector (4294967296-78124994526, default = 78124994526) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
Command (? for help): t
Partition number (1-3): 1
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): L
0700 Microsoft basic data 0c01 Microsoft reserved 2700 Windows RE
4200 Windows LDM data 4201 Windows LDM metadata 7501 IBM GPFS
7f00 ChromeOS kernel 7f01 ChromeOS root 7f02 ChromeOS reserved
8200 Linux swap 8300 Linux filesystem 8301 Linux reserved
8e00 Linux LVM a500 FreeBSD disklabel a501 FreeBSD boot
a502 FreeBSD swap a503 FreeBSD UFS a504 FreeBSD ZFS
a505 FreeBSD Vinum/RAID a580 Midnight BSD data a581 Midnight BSD boot
a582 Midnight BSD swap a583 Midnight BSD UFS a584 Midnight BSD ZFS a585 Midnight BSD Vinum a800 Apple UFS a901 NetBSD swap
a902 NetBSD FFS a903 NetBSD LFS a904 NetBSD concatenated
a905 NetBSD encrypted a906 NetBSD RAID ab00 Apple boot
af00 Apple HFS/HFS+ af01 Apple RAID af02 Apple RAID offline
af03 Apple label af04 AppleTV recovery af05 Apple Core Storage
be00 Solaris boot bf00 Solaris root bf01 Solaris /usr & Mac Z
bf02 Solaris swap bf03 Solaris backup bf04 Solaris /var
bf05 Solaris /home bf06 Solaris alternate se bf07 Solaris Reserved 1
bf08 Solaris Reserved 2 bf09 Solaris Reserved 3 bf0a Solaris Reserved 4
bf0b Solaris Reserved 5 c001 HP-UX data c002 HP-UX service
ed00 Sony system partitio ef00 EFI System ef01 MBR partition scheme
ef02 BIOS boot partition fb00 VMWare VMFS fb01 VMWare reserved
fc00 VMWare kcore crash p fd00 Linux RAID
Hex code or GUID (L to show codes, Enter = 8300): ef02
Changed type of partition to 'BIOS boot partition'
Command (? for help): p
Disk /dev/sda: 78124994560 sectors, 36.4 TiB
Logical sector size: 512 bytes
Disk identifier (GUID): F0A6B5A3-3C91-47CE-BD70-FAD8A88DD132
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 78124994526
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size Code Name
1 2048 4095 1024.0 KiB EF02 BIOS boot partition
2 4096 4294967295 2.0 TiB 8300 Linux filesystem
3 4294967296 78124994526 34.4 TiB 8300 Linux filesystem
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sda.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot.
The operation has completed successfully.
# grub2-install /dev/sdaInstalling for i386-pc platform.
Installation finished. No error reported.
# reboot
# parted /dev/sda printModel: Cisco (scsi)
Disk /dev/sda: 40.0TB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 2097kB 1049kB BIOS boot partition bios_grub
2 2097kB 2199GB 2199GB xfs Linux filesystem
3 2199GB 40.0TB 37.8TB Linux filesystem
In linux, to label a partition, there are 3 tools that can be used. The tools are e2label, tune2fs and mke2fs.
To use e2label to label the second partition of the first hardisk with label DATA:
# e2label /dev/sda2 DATA
To use tune2fs to do the similar job as above:
# tune2fs -L DATA /dev/sda2
The third tool, mke2fs is actually a tool to build ext2/ext3 filesystem. So, if you want to build the partition's filesystem as ext2/ext3 and at the same time label it, this command can be used. Be careful though, because it will delete all existing data on that particular partition
# mke2fs -L DATA /dev/sda2
To view the label that you have set, there are 3 ways which are using e2label, blkid and viewing /dev/disk/by-label.
To check using e2label:
# e2label /dev/sda2
DATA
blkid tool is even more useful, because it can list out all the partitions that you have in the machine together with their labels,uuid and filesystem type:
# blkid
/dev/sda1: LABEL="/" UUID="1CC08F13C08EF276" TYPE="ext3"
/dev/sda2: LABEL="DATA" UUID="2063f830-fe5d-438e-b727-571b313cb89e" TYPE="ext3"
/dev/sda3: TYPE="swap" LABEL="SWAP" UUID="3e266b53-42e0-4f09-8fe3-d1cf79cb5d37"
To view the /dev/disk/by-label
# ls -l /dev/disk/by-label
total 0
lrwxrwxrwx 1 root root 10 2009-07-24 05:38 / -> ../../sda1
lrwxrwxrwx 1 root root 10 2009-07-24 05:38 DATA -> ../../sda2
lrwxrwxrwx 1 root root 10 2009-07-24 05:38 SWAP -> ../../sda3
Note that the label will stay with the partition although the disk is moved to another computer.
To use it in /etc/fstab:
LABEL=/ / ext3 defaults 1 1
LABEL=DATA /DATA ext3 defaults 1 2
LABEL=SWAP swap swap defaults 0 0