Disk & Filesystems - alex-aleyan/linux_wiki GitHub Wiki
-
Quick Cheat Sheet:
parted -l parted -> mklabel parted -> mkpart fdisk -l fdisk /dev/sdb sfdisk -d /dev/sda sfdisk -d /dev/sda | sfdisk --force /dev/sdb mkfs.ext4 /dev/sdb1 mkfs.xfs /dev/mapper/vg_opt-lv_opt
-
Locate the drive using
ls -la /dev | grep sd.
(look for devices named as sda or sdb or sdc which are partitioned as sda1, sda2...) -
See if the drive is mounted by typing:
mount
-
See where it's mounted by typing:
df -TH /dev/sda
ormount
if/dev
contains sdc1 and sdc2 for example, it means the partitions of the drive are mounted. Unmount them usingumount
orumount -l
. -
optionally see the size of the filesystem
df -TH
-
du -sh /a_dir
ordu -ah /a_dir
ordu -s ./* | sort -n
-
Partition the drive by launching the
parted
program:- Type
parted
to launch the program (typing End: -0 will partition it to the end of drive). - Type
print
to list all drives and their partitions (you can also usefdisk -l
to get a more detailed list). - Type
mklabel gpt
(> 2GB) ormklable msdos
to make a label. - Type
mkpart
to make a partition. Use enter/return key to skip all the settings except the Partition Type, Start and End (-0 will partition it to the end of drive):mkpart primary ext4 0% 100%
set 1 lvm <on|off>
parted -s -a optimal mkpart primary 0% 100%
- Type
-
Create a File System by typing:
mkfs.ext4 /dev/sdc1
mkfs -t ext4 /dev/sdc1
-
Mount it by first creating a desired mount directory and then by typing
mount /dev/sdc1 /media/stick_1
-
Add a label to file system:
e2label /dev/sda1 "my_lable" blkid /dev/sda1 LABEL="my_label"
- Terminology
-
LVM Physical Volume - a partition (sda2 and sda3) created on a physical drive to be used to compose a Volume Group.
-
pvdisplay /dev/sda3
displays which Physical Volumes (partitions) were used to compose a Volume Group. pvscan
-
-
Volume Group - a collection of Physical Volume partitions used as a single virtual disk for Logic Volumes.
-
vgdisplay vg1
displays Volume Group's information.
-
-
Logical Volume - a filesystem residing on a Volume Group.
-
lvdisplay vg1
For the Volume Group vg1, displays info on each Logic Volumes (file systems).
-
-
LVM Physical Volume - a partition (sda2 and sda3) created on a physical drive to be used to compose a Volume Group.
-
Creating LVM Logical Volumes:
- Create one or more Physical Volumes (pv; Linux LVM partitions) using fdisk command:
fdisk /dev/sda
- identify this partition as an LVM Physical Volume using pvcreate command:
pvcreate /dev/sda3
- Using the Physical Volumes created in step 1, either:
- create a Volume Group:
vgcreate vg_1 /dev/sda3
- or add the Physical Volume to the existing Volume Group:
vgextened vg_1 /dev/sda3
- create a Volume Group:
- Create a Logical Volume (lv; a file system that will be mounted) using the Volume Group created in step 2:
lvcreate -n my_lv -L 100m vg_1
lvcreate -n my_lv -l 100%FREE vg_1
- Check that the device for the just created Logic Volume:
ls /dev/mapper/vg_1
- Make a filesystem on the just created Logic Volume:
mkfs -t ext4 /dev/mapper/vg_1-my_lv
mkfs.xfs /dev/mapper/vg_1-my_lv
- Mount the Logic Volume:
mkdir -m 775 /mnt/my_lv; mount /dev/mapper/vg_1-my_lv /opt/my_lv
- See the space occupied by the Mounted Logic Volume:
df -TH /opt/my_lv
- (Optional) Add the Logic Volume to fstab:
/dev/mapper/vg_1-my_lv /opt/my_lv ext4 defaults 1 2 /dev/mapper/vg_1-my_lv /opt/my_lv xfs defaults 0 0
- Create one or more Physical Volumes (pv; Linux LVM partitions) using fdisk command:
-
Growing LVM Logical Volumes:
-
Short and Fast:
- Check the current size of a Volume Group:
vgdisplay vg_1
lvdisplay
df -TH /mnt/my_lv/
- Turn the added partition into a phys vol:
pvcreate /dev/sdb
- Expand the Volume Group size:
vgextend vg_1 /dev/sdb
- Expand the logical volume:
lvextend -L +100<b|B|s|S|k|K|m|M|g|G|t|T|p|P|e|E> /dev/mapper/vg_1-my_lv
lvextend -l +100%<FREE|ORIGIN|VG> /dev/mapper/vg_1-my_lv
lvextend 0l +100%FREE /dev/mapper/vg1-my_lv
- Resize the filesystem on the Logic Volume to fit the new logical volume size:
- CenOS6:
resize2fs -p /dev/mapper/vg_1-my_lv
- CentOS7:
xfs_growfs /dev/mapper/vg_1-my_lv
- CenOS6:
- Check the current size of a Volume Group:
-
Long and Detailed:
-
Use these commands to see what goes on in terms of partitions and file systems:
[root@test1 ~]# df –TH [root@test1 ~]# lvs; pvscan; pvdisplay; vgdisplay [root@test1 ~]# fdisk –l [root@test1 ~]# parted /dev/sda print [root@test1 ~]# mount
-
Add new partition to sda drive:
[root@test1 ~]# fdisk Command (m for help): m Command (m for help): p Command (m for help): n Command action e extended p primary partition (1-4) p Partition Number (1-4): 3 First Cylinder(1-2310, default 1): Using default value 1 Last Cylinder (1-2610), default 2610): Using default value 2610 Command (m for help): t Hex code (type L to list the codes): 8e #which corresponds to Linux LVM Command (m for help): w
-
Type this command to avoid problems with booting the machine after creating the partition above (Use CloneZilla image to remove the added partition parted rm /dev/sda YOUR_PARTITION_NUM ):
partx -a /dev/sda
-
Check the partitions again:
[root@test1 ~]# parted /dev/sda print
-
Make the ext4 file system on the partition we just created:
[root@test1 ~]# mkfs.ext4 /dev/sda3
-
Initialize the /dev/sda3 partition as LVM physical volume so it can later be used as a part of LVM logical volumes:
[root@test1 ~]# pvcreate /dev/sda3
-
Use
pvscan
to view physical volumes created bypvcreate
; usepvdisplay
to view more detailed information. -
Determine the group name (here it's the group's name vg1):
[root@test1 ~]# vgs VG #PV #LV #SN Attr VSize VFree vg1 2 7 0 wz--n- 1.86t 0
-
Add the physical volume to LVM:
[root@test1 ~]# vgextend vg1 /dev/sda3
-
Determine the name of your LVM:
[root@test1 ~]# df -TH
-
Add space to LVM:
[root@test1 ~]# lvextend –l +100%FREE /dev/mapper/vg1-root
-
Resize your filesystem:
[root@test1 ~]# resize2fs /dev/mapper/vg1-root (or xfs_growfs /dev/mapper/vg_1-my_lv on Centos7)
-
Display your logic volumes’ information:
[root@test1 ~]# lvs
-
Display your volume groups’ information:
[root@test1 ~]# vgs
-
-
-
Shrinking LVM Logical Volumes:
-
Note:
- we are shrinkg /dev/vg1/root Logical Volume.
- we need to make sure that the filesystem is first reduced to a size A and then Logic Volume is reduced to a size B where A < B.
- once Logic Volume is reduced to size B we can extend filesystem's size from size A to occupy as much of Logic Volume as possible.
-
Using CloneZilla Disk, launch Command Line:
CloneZilla -> Don't touch keymap -> Enter_Shell -> Command Line
-
In Clonezilla, switch to the SuperUser mode:
sudo su -
-
Activate all logic volumes to make them avaiable to the shell:
vgchange -a y
-
Force the filesystem check on the Logic Volume of the interest:
e2fsck -f /dev/vg1/root
-
Resize the filesystem to 1.6T first before resizing the Logic Volume:
resize2fs /dev/vg1/root 1600G #set to 1600G resize2fs /dev/vg1/root -200G #reduce by 200G
-
Reduce the size of the Logical Volume from 1.8T to 1.65T:
lvreduce -L 1650G /dev/vg1/root
-
Grow filesystem so that it occupies all of the Logical Volume:
resize2fs /dev/vg1/root
(orxfs_growfs /dev/mapper/vg_1-my_lv
on Centos7)
-
pvdisplay /dev/sda3 # displays which Physical Volumes (partitions) were used to compose a Volume Group.
pvscan
vgdisplay vg1 # displays Volume Group's information
lvdisplay vg1 # For the Volume Group vg1, displays info on each Logic Volumes (file systems).
pvcreate, pvscan, pvdisplay, vg create, vgdisplay, lvcreate, , lvextend
lvscan
vgscan
vgchange -a y <vg_name>
ll /dev/mapper/
#!/bin/bash
function showmem()
{
#create an array containing the list of the sd* devices on the system
sdList=($(ls /dev/sd* | egrep "sd.$"))
for sdDevice in ${sdList[@]}
doA
#for each sd* device, perform "parted sd* print"
parted $sdDevice print
done
#list the sd* and VolumeGroup partitions
fdisk -l
df -TH
#Physical Volumes the physical partitons on a given harddisk like sda1, sda2...
pvs
pvscan
pvdisplay
#Volume Group is a collection of Physical Volume; grouped hard drive resources used to create Logic Volumes
vgs
vgscan
vgdisplay
#Logic Volume is a partition created using disk space from a Volume Group
lvs
lvscan
lvdisplay
return
}