Showing posts with label partition. Show all posts
Showing posts with label partition. Show all posts

Saturday, December 3, 2022

Extending XFS Partition Based LVM by Increasing the Disk Size

This is the original disk layout before the disk extension











We can see that the LVM is built on /dev/sda2, which is a partition, and not a disk. The disk, /dev/sda is now extended by 100G on the VM manager level. After a reboot, we can now see that the disk size is now 130G. 

 









To include this new space into existing LVM, create a new partition on the new disk space using a tool called cfdisk. Please refer to this post on how to use cfdisk to create a new partition.

The first step is to become root. If you prefer using sudo, please append sudo on each of these commands.

Once we can see that the partition is available, create a new physical volume (PV) using the new partition. 
# pvcreate /dev/sda3 

We should see the message as per below





Then, check what is the name of our volume group (VG)
# vgs 

Extend our VG by including the new PV
# vgextend centos /dev/sda3 





Check what is our logical volume (LV) path
# lvdisplay | grep Path 





Extend our LV to use the whole free space available on the VG
# lvextend -l +100%FREE /dev/centos/root





Check that our LV's size is now expanded 
# lvs 






Determine what is our filesystem type, at the same time we can see that our filesystem is not aware of the new size 
# df -Th / 

Grow xfs filesystem to suit the new partition size
# xfs_growfs /  

We can see that the filesystem is now grown to the full size as per our LV
# df -Th / 

















 


Monday, November 14, 2022

Creating Partition Using cfdisk

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.


First, we need to identify our disk name. This can be achieved by running lsblk
$ lsblk











Above is a sample output of lsblk. We can see that our disk name is sda (append /dev/ to the name for full path), and it has 130GB size. We can also see that only around 30GB has been used. We should have 100GB worth of free space for our new partition.

To start using cfdisk, just run it against the path of our disk, in this case /dev/sda
$ sudo cfdisk /dev/sda

We should be able to see this interface




















Move the cursor using the down arrow key on our keyboard, so that the cursor lies on the 107374.19MB line




















Make sure your cursor is at "New" button, and press Enter.

Choose "Primary" and press Enter.





















We want to use the whole free space, so just leave the size as it is, and press Enter.




















We now have a partition ready. To actually write the partition to the partition table, move your cursor using the right arrow key on the keyboard until it reached the "Write" button, and press Enter.




We will get  a warning message. Type "yes" and press Enter to confirm this action.



We will get this message once the partitioning is complete.



Quit cfdisk by choosing "Quit" and press Enter





















Eventhough we have created the partition, but sometimes the partition table is not automatically updated. Run "partprobe" to inform the OS of the partition table change.
$ sudo partprobe


Use "lsblk" to see that the new partition has been created
$ lsblk







Tuesday, November 1, 2022

Extending LVM By Adding New Disk To a Virtual Machine

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. 


There is another way to increase LVM volume capacity, that is to add new disk into the system. 

1. First, shutdown the virtual machine

2. Add a new virtual disk size into the virtual machine manager

3. Start the virtual machine

4. Check the new disk availability for LVM
$ sudo lvmdiskscan

5. To be able to use the new disk, it needs to be converted into physical volume (PV)
$ sudo pvcreate <path to the new disk>

6. Check list of PVs. Memorize the PV name
$ sudo pvs

7. Extend the volume group using the new PV
$ sudo vgextend <VG name> <PV name>

We can get the name of the VG by using this command. This command also will show how much free space is now available in the VG.
$ sudo vgs

8. Increase the size of logical volume, to use 100% of the free space available inside VG, using this command
$ sudo lvextend -l +100%FREE <LV PATH>

We can get the full  path of LV using this command
$ sudo lvdisplay | grep Path

9. Now, make the filesystem aware that the partition size has been increased. The command to do this differ from filesystem to filesystem, but we include 2 of the most used (based on our experience) filesystem
For xfs:
$ sudo xfs_growfs <mountpoint>

For  ext4:
$ sudo resize2fs <mountpoint>

10. Verify that the mountpoint is now increase in size
$ df -Th

Tuesday, December 1, 2020

Extending / in Default CentOS Partitioning Scheme

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

...


As you can see, we are not going to use 1.9T in /home, so we are going to assign that to / instead.

Second step, make a backup of /home
# cp -apv /home /home2

Next, unmount /home
# umount -fl /home

Remove the logical volume
# lvremove /dev/cl/home

If you get an error saying that the logical volume is in used, you need to kill off the process first.

Get the major, minor number of the logical volume.
# dmsetup -c info cl-home
Name             Maj Min Stat Open Targ Event  UUID                                                                
cl-home          253   2 L--w    1    1      0 LVM-urbQSwHWhdpFDeVDQA3yFkOjVMkWFGloYGbgzAGW8YOuwacqVZLvVx23qNVJTFHc

Search for the process and process id using the major,minor number
# lsof | grep "253,2"
java       179094                           user  cwd       DIR              253,2        95 1073741952 /home
...

kill off the process
# kill 179094

Try to remove the logical volume again
# lvremove /dev/cl/home

Once deleted, extend the logical volume of /dev/cl/root, to use 100% of free space available in the volume group
# lvextend -l +100%FREE cl-root

Check the filesystem type
# df -Th /
Filesystem          Type      Size  Used Avail Use% Mounted on
/dev/mapper/cl-root xfs        50G  4.6G   46G  10% /

Extend the filesystem, xfs_growfs is xfs filesystem grow tool. For ext4, you need to use resize2fs command
# xfs_growfs /

Check that your / partition is showing the new size
# df -Th /
Filesystem          Type  Size  Used Avail Use% Mounted on
/dev/mapper/cl-root xfs   2.0T   19G  2.0T   1% /

Copy back the content of /home2 to /home
# mv /home2/* /home

IMPORTANT: Remove the old /home mount entry in /etc/fstab
# sed -i.ori '/home/d' /etc/fstab

The last step is very important, it can cause your operating system to fail to boot if /etc/fstab is not configured properly.

Enjoy your newly gained disk space in / partition.

Friday, March 22, 2019

Rename and Remount LVM Logical Volume

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.


The original partition scheme 
# df -Th
Filesystem                   Type      Size  Used Avail Use% Mounted on
/dev/mapper/cl-root          xfs        50G  3.0G   48G   6% /
/dev/sda1                    xfs      1014M  179M  836M  18% /boot
/dev/mapper/cl-home xfs       965G   42G  924G   5% /home

Stop the service that might be using the partition that we are going to change
# systemctl stop elasticsearch

Rename the original directory to other name
# mv /var/lib/elasticsearch /var/lib/elasticsearch-old

Create a new directory to replace the one we already renamed above
# mkdir /var/lib/elasticsearch

Unmount /home
# umount /home

Check the name of volume group and logical volume
# lvs

Rename the logical volume to the one we desired. This is totally optional. You can use -t flag to test out the renaming process before proceeding
# lvrename -t cl home elasticsearch
# lvrename cl home elasticsearch

Change /etc/fstab to reflect on the new logical volume name and mount point. Test it out without the  -i option, before permanently make the change using the -i option
# sed 's/cl-home/cl-elasticsearch/g;s/\/home/\/var\/lib\/elasticsearch/g' /etc/fstab
# sed -i 's/cl-home/cl-elasticsearch/g;s/\/home/\/var\/lib\/elasticsearch/g' /etc/fstab

Mount it
# mount -a

Check to see if your new directory and logical volume mounted properly
# df -Th
Filesystem                   Type      Size  Used Avail Use% Mounted on
/dev/mapper/cl-root          xfs        50G  3.0G   48G   6% /
/dev/sda1                    xfs      1014M  179M  836M  18% /boot
/dev/mapper/cl-elasticsearch xfs       965G   42G  924G   5% /var/lib/elasticsearch

Move the content of /var/lib/elasticsearch to /home
# mv /var/lib/elasticsearch/* /home

Move the content of /var/lib/elasticsearch-old to /var/lib/elasticsearch
# mv /var/lib/elasticsearch-old/* /var/lib/elasticsearch

Remove /var/lib/elasticsearch-old
# rmdir /var/lib/elasticsearch-old

Set proper permission for /var/lib/elasticsearch
# chmod 750 -R /var/lib/elasticsearch
# chown -R elasticsearch.elasticsearch /var/lib/elasticsearch

Start your service
# systemctl start elasticsearch

Done.

Wednesday, March 21, 2018

Creating large gpt partition using gdisk

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

Run gdisk on the disk
# 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):

Press 'p' to print current partition, and to see how much free space is available
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

Press 'n' to create new partition, and choose the default partition type, which is 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'

Press 't', and type 'ef02' to change the first partition type, to bios boot partition. This to make sure that that this partition will be used for grub reinstallation later
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'

Press 'p' to check the changes that you have made
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

Press 'w' to write the changes to disk, and answer 'Y' for confirmation
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.

Reinstall grub, so that it will be located in the first partition of the hardisk
# grub2-install /dev/sdaInstalling for i386-pc platform.
Installation finished. No error reported.

Reboot the machine, for the new partition table to be activated
# reboot

Once booted, check whether the new partition has been successfully created, with GPT partition table 
# 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

Friday, July 24, 2009

Labeling linux partition

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