Linux Disk Commands - Cheatsheet

Short note on useful commands and lexicon concerning linux and disks

MBR : Partition format (BIOS Legacy) 2 T Max per partition / 4 primary partitions MAX

GPT : New partition format (UEFI BIOS) Partition size limited by OS / 128 partitions MAX

Superblock : Contains FS information

Journaled FS : Ensures data integrity in the event of a failure / outage (thanks to the journal referencing write operations) NTFS, ext3, ext4, xfs are journaled FS

List / discover discs

List connected disks

udisksctl status

MODEL                     REVISION  SERIAL               DEVICE
--------------------------------------------------------------------------
Samsung SSD 850 EVO 1TB   EMT02B6Q  S2RFNX0J104659B      sda     
KINGSTON SUV400S37120G    0C3FD6SD  50026B776507FB40     sdb     
ST4000DM004-2CV104        0001      WFN0T5BY             sdc     
ATAPI   iHAS124   F       CL9M      3524728_2H8703502973 sr0     

List connected and mounted disks

lsblk 

NAME                  MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sdb                     8:16   0 111,8G  0 disk  
├─sdb4                  8:20   0 111,2G  0 part  
├─sdb2                  8:18   0   100M  0 part  
├─sdb3                  8:19   0    16M  0 part  
└─sdb1                  8:17   0   450M  0 part  
sr0                    11:0    1  1024M  0 rom   
sdc                     8:32   0   3,7T  0 disk  
└─sdc1                  8:33   0   3,7T  0 part  
sda                     8:0    0 931,5G  0 disk  
├─sda2                  8:2    0     1K  0 part  
├─sda5                  8:5    0   931G  0 part  
│ └─sda5_crypt        253:0    0   931G  0 crypt 
│   ├─mint--vg-root   253:1    0 899,1G  0 lvm   /
│   └─mint--vg-swap_1 253:2    0  31,9G  0 lvm   
│     └─cryptswap1    253:3    0  31,9G  0 crypt [SWAP]
└─sda1                  8:1    0   487M  0 part  /boot

List disk FS UUID and format

blkid

/dev/sda1: UUID="c8e2c7e4-2451-4c32-a49f-687d9ae7e489" TYPE="ext4" PARTUUID="9341c34b-01"
/dev/sda5: UUID="0c485bf5-130d-47e9-84c4-03d8ff2c8bb5" TYPE="swap" PARTUUID="9341c34b-05"
/dev/sdc1: UUID="b69e632d-7c1e-4f3a-87d5-d46b673d9456" TYPE="xfs" PARTUUID="35106799-01"
/dev/sdb1: UUID="65f42c2c-2348-4abd-8899-675cb1e3d93b" TYPE="ext4" PARTUUID="88c20315-01"

List the FS and their sizes

df -Th

Sys. de fichiers Type     Taille Utilisé Dispo Uti% Monté sur
udev             devtmpfs   983M       0  983M   0% /dev
tmpfs            tmpfs      199M    3,1M  196M   2% /run
/dev/sda1        ext4        18G    4,1G   13G  25% /
tmpfs            tmpfs      994M       0  994M   0% /dev/shm
tmpfs            tmpfs      5,0M       0  5,0M   0% /run/lock
tmpfs            tmpfs      994M       0  994M   0% /sys/fs/cgroup
tmpfs            tmpfs      199M       0  199M   0% /run/user/1000
/dev/sdc1        xfs         25G     63M   25G   1% /XFS
/dev/sdb1        ext4        30G     44M   28G   1% /mnt

Disk commands for VMWare hypervisor

Detect disks increased in size (VMware)

echo 1 > /sys/class/scsi_device/0\:0\:0\:0/device/rescan
echo 1 > /sys/class/scsi_device/0\:0\:1\:0/device/rescan
echo 1 > /sys/class/scsi_device/0\:0\:2\:0/device/rescan
....

Detect added disks (VMware)

echo "- - -" > /sys/class/scsi_host/host0/scan
echo "- - -" > /sys/class/scsi_host/host1/scan
....

Partitioning Commands

Under Linux it is preferable to use LVM to partition a disk, the LVM allows a hot use much more flexible.

Without LVM the contiguous partitions cannot be modified (example: if there are 2 partitions, the first cannot be increased without overwriting part of the 2nd)

Partition a disk

fdisk /dev/sdb

n       (Créer nouvelle partition)
p       (Partition primaire)
1       (Numéro de partition)
2048 par defaut    (par défaut Début de la partition)
Dernier secteur    (par defaut Fin de la partition)
w       (Confirmer configuration)

Create an MSDS

mkfs.exfat #Créer FS exfat
mkfs.ext2  #Créer FS ext2
mkfs.ext3  #Créer FS ext3
mkfs.ext4  #Créer FS ext4
mkfs.minix #Créer FS minix
mkfs.ntfs  #Créer FS ntfs
mkfs.xfs   #Créer FS xfs

mkfs.ext4 /dev/sdc1

#Pour installer d'autres FS
apt-get install ntfs-3g  #Installer ntfs
apt-get install exfat-fuse exfat-utils  #Installer exfat
....

Enlarge Linux Partition (Old Method)

fdisk /dev/sdb

d   (Supprimer la partition)
n   (Créer nouvelle partition)
p   (primaire)
1       (Numéro de partition)
2048 par defaut    (par défaut Début de la partition)
Dernier secteur    (par defaut Fin de la partition)

Voulez-vous supprimer la signature ? [O]ui/[N]on : N
w       (Confirmer configuration)

partprobe   #Faire relire la table de partition au kernel sans reboot (Installer parted si outil absent)

Enlarge a Linux Partition (Clean Method)

parted /dev/sdb unit MB print free # Pour recuperer la partition
 
 
Model: VMware Virtual disk (scsi)
Disk /dev/sdc: 32208MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
 
Number  Start   End      Size     File system  Flags
 1      0,00MB  32208MB  32208MB  ext4


parted /dec/sdc1 resizepart 1 # Pour l'extension de la partition
 
Warning: Partition /dev/sdg1 is being used. Are you sure you want to continue?
Yes/No? Yes              # Utilisez Yes pour confirmer
End? [60052MB]? 100%     # Utilisez 100% ou -1s pour utiliser tout l'espace disponible

Resize FS (Format EXT2/EXT3/EXT4)

resize2fs /dev/sdb1

Le système de fichiers sur /dev/sdb1 a maintenant une taille de xxx blocs

Resize FS (XFS Format)

xfs_growfs /dev/sdc1

data blocks changed from xxxx to xxxx

LVM management

LVM allows the creation and management of logical volumes under Linux. The use of logical volumes in a way replaces the partitioning of disks.

It is a much more flexible system, which makes it possible, for example, to reduce the size of a file system in order to be able to enlarge another, without worrying about their location on the disk.

Install lvm

apt-get install lvm2

Create an LVM assembly

  • Create the “physical volume” ( pvcreate )
  • Create the “group volume” ( vgcreate )
  • Create the “logical volume” ( lvcreate )
  • Formatting FS on Logical Volume ( mkfs. )
pvcreate /dev/sdd  #Créer le PV

vgcreate VG-DATA /dev/sdd   #Créer le VG (Adaptez le nom du VG)

lvcreate -l +100%FREE -n LV-Data VG-DATA  #Créer le LV (Adaptez le nom du LV)

mkfs.ext4 /dev/VG-DATA/LV-Data #Créer le FS sur le LVM

Extend an LVM logical volume

pvresize /dev/sdd   #Agrandir le PV

lvextend -l +100%FREE /dev/VG-DATA/LV-Data  #Agrandir le LV

resize2fs /dev/VG-DATA/LV-Data #Resizer le FS

Some useful commands

####LVM####

pvdisplay #Afficher les PV
vgdisplay #Afficher les VG
lvdisplay #Afficher les LV

####Global####

e2fsck #Verifier un FS ext
tune2fs #Convertir un FS
findmnt #Afficher les points de montage complet (Disque, ram, ...)
fsck  #Vérifier un FS