Friday, 23 September 2011

HOWTO--> Disk and File system Modification


HOWTO: Disk and File system Modification

This document shows how to add disk to a partition, extend and reduce file systems and finally how to remove disk devices.


Adding Disk devices


To dynamically add disk devices to a running Linux system requires the following steps

1.      List the current disk devices (so you will know which ones have been added)

            fdisk -l

2.      Get the list of host adapters

            ls /sys/class/scsi_host

            This will return a list of host adapters e.g. host0

3.      For each host adapter perform a scan

            echo “- - -“ > /sys/class/scsi_host/<host adapter>/scan

            e.g.  echo “- - -“ > /sys/class/scsi_host/host0/scan

4.      Check for added devices

            fdisk -l



Extending a File System


The following steps are required to extend a file system.

The following assumes that there is enough space available in the disk group/volumes.

1.      Get the logical volume device for the file system to be extended using the df command e.g.

            df -h /opt

            Returns

       Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/rootvg-optlv
                             1.3G   34M  1.2G   3% /opt

            The logical volume device name is highlighted in blue

2.      Increase the size of the logical volume

            lvextend -L+<size><Units> <device name> [<disk>]

Where Units is expressed as K, M, G or T. The command will automatically round up to the nearest multiple of  PE Size (can be found using vgdisplay).
            One or more <disk> devices can be specified, but this is optional. Full path to
the devices is required e.g. /dev/sdb

            More advanced options are also available e.g. stripe size, specifying specific          physical extents can also be used.

3.      Increase the size of the file system

            resize2fs <logical volume device>

            This will resize the file system to the size of the logical volume.




Reducing a File System


Reducing a file system cannot be carried out while the file system is active. The resize is a 2 stage process. First to reduce the size of the file system and second to the logical volume. There are no safeguards for the second phase of this operation i.e. the system will let you reduce the logical volume to less than the size of the file system.

1.      Get the logical volume device for the file system to be reduced, and the size in Mbytes using the following df command

df --block-size=1M <file system>

            e.g.
           
            df--block-size=1M  /opt

            Returns

Filesystem           1M-blocks      Used Available Use% Mounted on
/dev/mapper/rootvg-optlv
                          1240        34      1142   3% /opt

            The logical volume device name is highlighted in blue and the size in Mbytes
            is highlighted in red

2.      Unmount the file system

umount <file system>

3.      Check the file system (required before reducing)

            e2fsck -f <device>

4.      Reduce the size of the file system

You need to know the final size of the file system to resize it i.e. you cannot specify a size to reduce by.

resize2fs <file system> <size>M

Different units can be used, but to ensure we have exact sizings (for reducing the logical volumes), then Mbytes is a good Unit to use.

To ensure the correct reduction the following command could be used

resize2fs <file system> $((<current size>-<amount>))M

Where <current size> is the size in Mbytes from the previous df command
            <amount> is the reduction require in Mbytes

e.g.

resize2fs /opt $((1240-200))M

5.      Reduce the size of the logical volume

lvreduce -L-<size><Unit> <device>

Where <size> is the amount to reduce the logical volume by
<Unit> is K, M, G , T, P or E
<device> is the path to the logical volume device
           
            e.g.

                        lvreduce -L-200M /dev/mapper/rootvg-optlv

6.      Re mount the file system

mount <file system>



Increasing a current LUN


It’s possible to increase a LUN, or VMware backing device, and then use the additional space within the Linux partition.

1.      Check the current LUN size

fdisk -l <device>

e.g.  fdisk -l /dev/sdb

2.      Resize the LUN (performed by storage)

3.      Force Linux to re read the partition table of the device

blockdev --rereadpt <device>

e.g.  blockdev --rereadpt /dev/sdb

4.      Check the device has increased in size

fdisk –l <device>



Removing a disk device


The following process can be followed to remove a disk device from a Linux system.

NB. This process assumes that the device is has no active partitions

1.      Remove the device files from /dev

rm /dev/<disk>
rm /dev/disk/by-path/<device>

            Where <device> is the disk device e.g. sdb

2.      Delete the disk

echo 1 > /sys/block/<device>/device/delete

            Where <device> is the disk device e.g. sdb