Create and Extend XFS filesystem based on LVM

From roonics
Jump to navigation Jump to search

Create XFS filesystem on LVM

Create a partition using fdisk

Run this command and set the options as below obviously changing the bits that are unique to your setup (i.e size):

fdisk /dev/sdb
[root@lab01 ~]# 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 0x26d678c7.

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-104857599, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599): +40G
Partition 1 of type Linux and of size 40 GiB is set

Command (m for help): t
Selected partition 1
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.

Create LVM components : pvcreate, vgcreate and lvcreate

pvcreate /dev/sdb1
[root@lab01 ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
vgcreate vg_xfs /dev/sdb1
[root@lab01 ~]# vgcreate vg_xfs /dev/sdb1
  Volume group "vg_xfs" successfully created
lvcreate -L +6G -n xfs_db vg_xfs
[root@lab01 ~]# lvcreate -L +6G -n xfs_db vg_xfs
  Logical volume "xfs_db" created.

Or to use all the free disk space use

lvcreate -l+100%FREE -n xfs_db vg_xfs

Create XFS file system on lvm parition “/dev/vg_xfs/xfs_db”

mkfs.xfs /dev/vg_xfs/xfs_db
[root@lab01 ~]# mkfs.xfs /dev/vg_xfs/xfs_db
meta-data=/dev/vg_xfs/xfs_db     isize=512    agcount=4, agsize=393216 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1572864, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

Mount the xfs file system

Create a directory named as xfs_test under /root and mount it using mount command.

mkdir /root/xfs_test
mount /dev/vg_xfs/xfs_db/ root/xfs_test/

Check by running:

df -h
[root@lab01 /]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/centos-root     50G  1.5G   49G   3% /
devtmpfs                   3.9G     0  3.9G   0% /dev
tmpfs                      3.9G     0  3.9G   0% /dev/shm
tmpfs                      3.9G  8.8M  3.9G   1% /run
tmpfs                      3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/sda1                 1014M  189M  826M  19% /boot
/dev/mapper/centos-home     42G   33M   42G   1% /home
tmpfs                      783M     0  783M   0% /run/user/0
/dev/mapper/vg_xfs-xfs_db  6.0G   33M  6.0G   1% /root/xfs_test

This mount will disappear on reboot, to make it permanent use /etc/fstab file.

Permanent mount using /etc/fstab

Edit /etc/fstab and insert the below ling:

/dev/mapper/vg_xfs-xfs_db /xfs-test xfs defaults 0 2


Extend the size of xfs file system

Check there is space to grow

Check if you have space to grow it, if not and its a vm then add some space and rescan the bus in the os.

vgs vg_xfs
[root@lab01 ~]# vgs vg_xfs
  VG     #PV #LV #SN Attr   VSize   VFree
  vg_xfs   1   1   0 wz--n- <40.00g <34.00g

Extending the file system

We will extend the file system by 20GB.

lvextend -L +20G /dev/vg_xfs/xfs_db -r
[root@lab01 ~]# lvextend -L +20G /dev/vg_xfs/xfs_db -r
Phase 1 - find and verify superblock...
Phase 2 - using internal log
        - zero log...
        - scan filesystem freespace and inode maps...
        - found root inode chunk
Phase 3 - for each AG...
        - scan (but don't clear) agi unlinked lists...
        - process known inodes and perform inode discovery...
        - agno = 0
        - agno = 1
        - agno = 2
        - agno = 3
        - process newly discovered inodes...
Phase 4 - check for duplicate blocks...
        - setting up duplicate extent list...
        - check for inodes claiming duplicate blocks...
        - agno = 0
        - agno = 1
        - agno = 3
        - agno = 2
No modify flag set, skipping phase 5
Phase 6 - check inode connectivity...
        - traversing filesystem ...
        - traversal finished ...
        - moving disconnected inodes to lost+found ...
Phase 7 - verify link counts...
No modify flag set, skipping filesystem flush and exiting.
  Size of logical volume vg_xfs/xfs_db changed from 6.00 GiB (1536 extents) to 26.00 GiB (6656 extents).
  Logical volume vg_xfs/xfs_db successfully resized.
meta-data=/dev/mapper/vg_xfs-xfs_db isize=512    agcount=4, agsize=393216 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=1572864, 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 1572864 to 6815744

As you can see this confirms the grow:

Size of logical volume vg_xfs/xfs_db changed from 6.00 GiB (1536 extents) to 26.00 GiB (6656 extents).

You can now confirm by doing:

df -h
[root@lab01 /]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/centos-root     50G  1.5G   49G   3% /
devtmpfs                   3.9G     0  3.9G   0% /dev
tmpfs                      3.9G     0  3.9G   0% /dev/shm
tmpfs                      3.9G  8.8M  3.9G   1% /run
tmpfs                      3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/mapper/centos-home     42G   33M   42G   1% /home
/dev/sda1                 1014M  189M  826M  19% /boot
tmpfs                      783M     0  783M   0% /run/user/0
/dev/mapper/vg_xfs-xfs_db   26G   33M   26G   1% /root/xfs_test

NOTE: If the xfs is not based on a LVM you need to use the command below to grow:

xfs_growfs <Mount_Point> -D <Size>

‎<comments />