Disk Management - jrwhetse/jrwhetse.github.io GitHub Wiki
-
[Examples]
A disk can be a single entity or broken up into partitions. Disks are located in /dev and are named sd[a-z]. Disk partitions are logical divisions of disks located in the /dev directory and are named /dev/sd[a-z][1-9].
A partition table is a special structure containing partition orgnizations. Two types of partition tables exist: Master Boot Record (MBR) and GUID Partition Table (GPT).
MBR partition table uses 512-byte sectors and allows for 4 main primary partitions only. If you want more, you need to create an extended partition using one of the 4 primary slots then create a logical partition inside.
GPT uses 4096-byte sectors and was created to work around the limitations imposed by MBR, specifically the 2TB limitation.
Scan for new disk on machine to which disk is attached.
ls /sys/class/scsi_host/
ls /dev/sd*
#use the following command on all hosts listed by the first command
echo "- - -" > /sys/class/scsi_host/host0/scan
echo "- - -" > /sys/class/scsi_host/host1/scan
echo "- - -" > /sys/class/scsi_host/host2/scan
ls /dev/sd*
#you should see the new disk
Three primary tools are used for management of disks and partitions: parted, gdisk and fdisk.
Below are example commands for interacting with the parted utility.
Command | Purpose |
---|---|
# parted | Starts parted utility |
# (parted) ? | Prints help commands |
# (parted) print all | Prints all devices |
# (parted) select | Makes the active partition |
# (parted) mktable | Creates MBR or GPT partition table |
# (parted) mkpart primary ext4 0% 100% | Creates Ext4 partition |
# (parted) align-check optimal 1 | Checks alignment of partition |
# (parted) mkpart primary linux-swap 400GB 402GB | Creates 2GB swap partition |
# (parted) set 1 boot on | Sets first partition as bootable |
# (parted) set 1 lvm on | Sets firs tparition as LVM |
# (parted) rm 2 | Removes partition #2 (swap) |
# partprobe | Forces kernel to see disk udpates |
GPT partition table manipulator for Linux
Command | Purpose |
---|---|
# gdisk [disk] or [partition] | Runs gdisk for given disk or partition |
# (gdisk) p | Print partition information |
# (gdisk) n, 1, [enter], [enter], 8e00 | Create new partition - 8e00 (lvm) or 8300 (swap) |
# (gdisk) p | Print new parition information |
# (gdisk) w | Write partition |
# (gdisk) y | Confirm |
# partprobe | Forces kernel to see disk udpates |
Command | Purpose |
---|---|
# gdisk [disk] or [partition] | Runs gdisk for configuration of [disk] |
# (gdisk) d | Delete partition |
# (gdisk) p | Verify partition has been deleted |
# (gdisk) w | Write partition |
# (gdisk) y | Confirmation |
# partprobe | Forces kernel to see disk udpates |
Partition table manipulator for Linux
Command | Purpose |
---|---|
# fdisk -l | Lists all disks |
# fdisk [disk] or [partition] | Runs fdisk for given disk or partition |
# (fdisk) p | View partition information |
# (fdisk) n, p, 1, [start], [end] | Create primary partition 1 |
# (fdisk) t, 8e | Sets partition type 8e (lvm) 82 (swap) |
# (fdisk) p | View partition changes |
# (fdisk) w | Commits changes |
# partprobe | Forces kernel to see disk udpates |
Command | Purpose |
---|---|
# fdisk [disk] or [partition] | Runs fdisk for given disk or partition |
# (fdisk) d | Select partition to delete |
# (fdisk) p | Verify partition has been deleted |
# (fdisk) w | Write partition |
# partprobe | Forces kernel to see disk udpates |
If the newly created partition will not be added to LVM, it will need to be formatted. The same format commands used at the end of the LVM section can be used on a partition.
After creating a partition from a disk, several options exist. The newly created partition could be formatted to one of several file system types or the partition could be registered with LVM. If the partition is not going to be added to LVM, skip to the File System Types for an explanation on how to format, label and mount the partition.
Partitions and Logical volumes require formatting. The commands below show how to format, label, mount and manage different partitions and logical volumes depending on the file system type.
Format
Label
Mount
Format
Label
Mount
Format
Label
Mount
Lists disks
fdisk -l
Format disk
fdisk /dev/<disk_name>
# fdisk /dev/sdc
# Options for fdisk
p
n, p, 1, <enter>, <enter>
t, 8e
w
Create Physical Volume
pvcreate /dev/<physical_volume>
# pvcreate /dev/sdc1
Create Volume Group
vgcreate <volume_group> /dev/<physical_volume>
# vgcreate vg_opt /dev/sdc1
Create Logical Volume
lvcreate -l +100%FREE -n <logical_volume> <volume_group_name>
# lvcreate -l +100%FREE -n lv_opt vg_opt
Create filesystem
mkfs.ext4 -L /opt /dev/<volume_group>/<logical_volume_name>
# mkfs.ext4 -L /opt /dev/vg_opt/lv_opt
Enter mount options in /etc/fstab
/dev/mapper/<volume_group>-<logical_volume> /<mount> ext4 defaults 1 2
# /dev/mapper/vg_opt-lv_opt /opt ext4 defaults 1 2
Make Directory
mkdir <mount>
# mkdir /opt
Mount volume
mount -a
Identify Physical volume name attached to volume group
pvdisplay | more
# pvdisplay | more
Umount the volume to delete
umount <mount>
# umount /opt
Remove mount options from /etc/fstab
/dev/mapper/<volume_group>-<logical_volume> /<mount> ext4 defaults 1 2
# /dev/mapper/vg_opt-lv_opt /opt ext4 defaults 1 2
Remove Logical Volume
lvremove <volume_group>/<logical_volume>
# lvremove vg_opt/lv_opt
# Do you really want to remove active logical volume lv_opt? [y/n]: y
# Logical volume "lv_opt" successfully removed
Remove Volume Group
vgremove <volume_group>
# vgremove vg_opt
# Volume group "vg_opt" successfully removed
Remove Physical Volume
pvremove /dev/<physical_volume>
# pvremove /dev/sdb1
# Labels on physical volume "/dev/sdb1" successfully wiped
Resizing a partition involves resizing the physical disk, physical volume, volume group and logical volume.
List disks to identify disk to change
fdisk -l
# fdisk -l
# Disk /dev/sdd: 42.9 GB, 42949672960 bytes, 83886080 sectors
Identify the SCSI ID attached to the device
lsscsi -v
# lsscsi -v
# [0:0:3:0] disk VMware Virtual disk 1.0 /dev/sdd
# dir: /sys/bus/scsi/devices/0:0:3:0
# [/sys/devices/pci0000:00/0000:00:10.0/host0/target0:0:3/0:0:3:0]
Rescan the SCSI bus using the SCSI ID
echo 1 > /sys/class/scsi_device/0\:0\:3\:0/device/rescan
Verify changes
fdisk -l
# fdisk -l
# Disk /dev/sdd: 42.9 GB, 42949672960 bytes, 83886080 sectors
Reformat Disk
fdisk /dev/<physical_volume> or fdisk -u -l /dev/<physical_volume>
# fdisk /dev/sdd or fdisk -u -l /dev/sdd
# Options:
d
n
p
1, <enter>, <enter>
t, 8e
w
Run partprobe to rescan the partition table
partprobe
Resize Physical Volume
pvresize /dev/<physical_volume>
# pvresize /dev/sdd1
Resize Volume Group
vgchange -a y <volume_group>
# vgchange -a y vg_opt
Resize Logical Volume
lvextend -l +100%FREE /dev/<volume_group>/<logical_volume>
# lvextend -l +100%FREE /dev/vg_opt/lv_opt
# Please run 'e2fsck -f /dev/vg_opt/lv_opt' first.
Resize Filesystem:
resize2fs /dev/<volume_group>/<logical_volume>
# resize2fs /dev/vg_opt/lv_opt
For RHEL7 using XFS, resize2fs does not work. Instead, use xfs_growfs:
# xfs_growfs /dev/vg_opt/lv_opt
Extending a partition involves adding an additional physical disk, adding the new disk to the volume group, extending the volume group and logical volume.
Scan for new disk
ls /sys/class/scsi_host/
ls /dev/sd*
#use the following command on all hosts listed by the first command
echo "- - -" > /sys/class/scsi_host/host0/scan
echo "- - -" > /sys/class/scsi_host/host1/scan
echo "- - -" > /sys/class/scsi_host/host2/scan
ls /dev/sd*
#you should see the new disk
Lists all disks
fdisk -l
Format Disk
fdisk /dev/<disk_name>
# fdisk /dev/sdb
# Options for fdisk
p
n, p, 1, <enter>, <enter>
t, 8e
w
Create Physical Volume
pvcreate /dev/<physical_volume>
# pvcreate /dev/sdb1
Scan for new Physical Volume
pvscan
Extend existing volume group
vgextend <volume_group> /dev/<physical_volume>
# vgextend vg_opt /dev/sdb1
Resize volume group
lvextend -l +100%FREE /dev/<volume_group>/<logical_volume>
# lvextend -l +100%FREE /dev/vg_opt/lv_opt
Resize file system
resize2fs /dev/<volume_group>/<logical_volume>
# resize2fs /dev/vg_opt/lv_opt
For RHEL7 using XFS, resize2fs does not work. Instead, use xfs_growfs:
# xfs_growfs /dev/vg_opt/lv_opt
Mount volume (Not Required) mount -a
Display Volume Groups
vgdisplay | more
Rename Volume Group
vgrename <old_volume_group> <new_volume_group>
# vgrename vg_u01 vg_opt
# Volume group "vg_u01" successfully renamed to "vg_opt"
Display Logical Volumes
lvdisplay | more
Rename Logical Volume
lvrename /dev/<volume_group>/<old_logical_volume> /dev/<volume_group>/<new_logical_volume>
# lvrename /dev/vg_opt/lv_u01 /dev/vg_opt/lv_opt
Fix /etc/fstab
/dev/mapper/<new_volume_group>-<new_logical_volume> /<mount> ext4 defaults 1 2
# /dev/mapper/vg_opt-lv_opt /opt ext4 defaults 1 2
Remount
mount -a
You can chose to:
- Turn off all processes using the mount. Use lsof to check
- Restart in run level 1
- Use a rescue cd to run commands without the root file system mounted
Unmount
umount <mount>
# umount /var/log/audit
Shrink Logical Volume
lvreduce -r -L <size>G /dev/<volume_group>/<logical_volume>
# lvreduce -r -L 750G /dev/vg_root/lv_root
# lvreduce checks and resizes the file system when the -r option is used
Mount
mount <mount>
mount /var/log/audit
Login to the instance containing the volume to be increased. There are 2 different actions that can be taken depending on if the volume is partitioned or the entire device block is managed by LVM. To determine if the disk contains a partition, run the lsblk command. If the disk contains a partition, you will see an entry under the disk with the TYPE "part". If a partition isn't used, you will see TYPE "lvm"
# example of a disk with partition
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme1n1 259:0 0 1000G 0 disk
├─nvme1n1p1 259:1 0 60G 0 part
│ └─vg_opt-lv_opt 253:3 0 2.5T 0 lvm /opt
└─nvme1n1p2 259:2 0 940G 0 part
# example of a disk without partition
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvdf 202:80 0 100G 0 disk
└─vg_docker-lv_var_lib_docker 253:7 0 100G 0 lvm /var/lib/docker
For TYPE part, run the following commands
# install utility in order to grow partition size without using fdisk
yum install cloud-utils-growpart
# expand the partition
growpart /dev/<device_name> <partition> 1
# list physical volumes
pvs
# resize partition
pvresize /dev/<parition>
# list volume groups and logical volumes
vgs;lvs;
# extend the logical volume
lvextend -l +100%FREE /dev/<volume_group>/<logical_volume>
xfs_growfs /dev/<volume_group>/<logical_volume>
# verify
lsblk
For TYPE lvm, run the following commands
# list physical volumes
pvs
# resize device
pvresize /dev/<device_name>
# list volume groups and logical volumes
vgs;lvs;
# extend the logical volume
lvextend -l +100%FREE /dev/<volume_group>/<logical_volume>
xfs_growfs /dev/<volume_group>/<logical_volume>
# verify
lsblk
Example
pvresize /dev/xvda2
lvextend -l +100%FREE /dev/rootvg/var
xfs_growfs /dev/rootvg/var