Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Linux - Storage: Partition

Back


Partition


Types of Partitions


Partitioning Schemes


Thin Provisioning



Geting Disk Device Info

CMD DESC
lsblk List all block devices, including disks and partitions.
fdisk -l List disks and partitions.
parted -l List disks and partitions.
blkid Get UUIDs and filesystem types for all devices.
df -h Display disk space usage of mounted filesystems

Configuration File

/etc/mtab

Field Description
Device Mounted device or remote filesystem.
Mount Point Where the filesystem is mounted.
FS Type The type of filesystem.
Mount Options Options used for the mount.
/dev/sda1 /     ext4  rw,relatime       0   0
/dev/sda2 /home ext4  rw,relatime       0   0
tmpfs     /tmp  tmpfs rw,nosuid,nodev   0   0

/proc/partitions

major minor  #blocks  name

 259        0   20971520 nvme0n1
 259        1     614400 nvme0n1p1
 259        2    1048576 nvme0n1p2
 259        3   19306496 nvme0n1p3
 253        0   17207296 dm-0
 253        1    2097152 dm-1
   8        0    1048576 sda
   8        1      97280 sda1

Common Commands

parted: Manage Partitions

Subcommands Description
parted /dev/sda Start Interactive Mode
parted /dev/sda print Displays the partition table
parted /dev/sda mklabel gpt Create a new disklabel. Common labels are gpt and msdos.
parted /dev/sda mkpart part_name 1MiB 10GiB Create a New Partition.
parted /dev/sda mkpart part_name ext4 1MiB 10GiB Create a New Partition with filesystem.
parted /dev/sda name part_num part_name Assigns a name to a partition
parted /dev/sda resizepart 1 15GiB Resize a Partition
pparted /dev/sdX set part_num lvm on/off Set a flags
parted /dev/sda rm 1 Delete a Partition

Lab: Create new parition

# sda is the new disk
lsblk
# NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
# sda             8:0    0    1G  0 disk
# nvme0n1       259:0    0   20G  0 disk
# ├─nvme0n1p1   259:1    0  600M  0 part /boot/efi
# ├─nvme0n1p2   259:2    0    1G  0 part /boot
# └─nvme0n1p3   259:3    0 18.4G  0 part
#   ├─rhel-root 253:0    0 16.4G  0 lvm  /
#   └─rhel-swap 253:1    0    2G  0 lvm  [SWAP]

# view the current partition, error: unrecognised disk label
parted sda print
# Error: /dev/sda: unrecognised disk label
# Model: ATA VMware Virtual S (scsi)
# Disk /dev/sda: 1074MB
# Sector size (logical/physical): 512B/512B
# Partition Table: unknown
# Disk Flags:

# assign label
parted /dev/sda mklabel msdos
# Information: You may need to update /etc/fstab

# Confirm
parted /dev/sda print
# Model: ATA VMware Virtual S (scsi)
# Disk /dev/sda: 1074MB
# Sector size (logical/physical): 512B/512B
# Partition Table: msdos
# Disk Flags:

# Number  Start  End  Size  Type  File system  Flags
parted /dev/sda mkpart primary 1mb 101mb
# Information: You may need to update /etc/fstab.

# confirm
parted /dev/sda print
# Model: ATA VMware Virtual S (scsi)
# Disk /dev/sda: 1074MB
# Sector size (logical/physical): 512B/512B
# Partition Table: msdos
# Disk Flags:

# Number  Start   End    Size    Type     File system  Flags
#  1      1049kB  101MB  99.6MB  primary

lsblk
# NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
# sda             8:0    0    1G  0 disk
# display table before deleting
parted /dev/sda print
# Model: ATA VMware Virtual S (scsi)
# Disk /dev/sda: 1074MB
# Sector size (logical/physical): 512B/512B
# Partition Table: msdos
# Disk Flags:

# Number  Start   End    Size    Type     File system  Flags
#  1      1049kB  101MB  99.6MB  primary

# delete
parted /dev/sda rm 1
# Information: You may need to update /etc/fstab.

# confirm
parted /dev/sda print
# Model: ATA VMware Virtual S (scsi)
# Disk /dev/sda: 1074MB
# Sector size (logical/physical): 512B/512B
# Partition Table: msdos
# Disk Flags:

# Number  Start  End  Size  Type  File system  Flags


fdisk: Manage Partitions

CMD DESC
lsblk Display block devices and their partitions in a tree view.
lsblk -f Display partitions with file system type.
blkid Display UUIDs and file system types for all partitions.
fdisk -l Display details for all disks
fdisk -l /dev/sda View partition details of a specific disk
fdisk /dev/sda Open a specific disk for editing in Interactive Mode
parted /dev/sdX Open a specific disk for editing in Interactive Mode
parted /dev/sdX resizepart 1 5GiB Resize a Partition
Command Description
m Display a list of available commands.
p Display the current partition table.
n Create a new partition.
d Delete an existing partition.
t Change the type of a partition.
w Write changes to the disk.
q Quit without saving changes.

mkfs: Format a partition

CMD filesystem
mkfs.ext4 ext4
mkfs.xfs xfs
mkfs.vfat FAT32 (for compatibility with Windows).
mkfs.ntfs NTFS (requires the ntfs-3g package).
mkfs.btrfs Btrfs
CMD DESC
mkfs -t xfs /dev/sdb1 Specify the type of filesystem to create
mkfs.ext4 -L mydata /dev/sdb1 Label the Filesystem
mkfs.ext4 -b 4096 /dev/sdb1 Specify Block Size
mkfs.ext4 -v /dev/sdb1 Display detailed information during filesystem creation
mkfs.ext4 -c /dev/sdb1 Check for Bad Blocks

mount: Attach a Filesystem

CMD DESC
mount View All Mounted Filesystems
mount -a Mount all filesystems mentioned in fstab
mount /dev/sdb1 /mnt Mounts the partition /dev/sdb1 to the directory /mnt
mount -t ext4 /dev/sdb1 /mnt Mount a Filesystem with a Specific Type
mount -o ro /dev/sdb1 /mnt Mounts the partition as read-only.
mount -o loop /path/to/file.iso /mnt Temporary mounts an ISO file as a loopback device.
mount /mnt Mount a Filesystem Using /etc/fstab
umount /mnt Unmount Using Mount Point
umount /dev/sdb1 Unmount Using Device
umount -f /mnt Forces the unmount if the filesystem is busy.
umount -l /mnt Detaches the filesystem but waits for any pending processes

Lab: Create and Attach a New Partition

Add the New Disk to the VM


Identify the New Disk in Linux

lsblk
# NAME                   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
# sda                      8:0    0   2G  0 disk
# nvme0n1                259:0    0  30G  0 disk
# ├─nvme0n1p1            259:1    0   1G  0 part /boot
# └─nvme0n1p2            259:2    0  29G  0 part
#   ├─rhel_rhelhost-root 253:0    0  26G  0 lvm  /
#   └─rhel_rhelhost-swap 253:1    0   3G  0 lvm  [SWAP]

fdisk -l
# Disk /dev/nvme0n1: 30 GiB, 32212254720 bytes, 62914560 sectors
# Units: sectors of 1 * 512 = 512 bytes
# Sector size (logical/physical): 512 bytes / 512 bytes
# I/O size (minimum/optimal): 512 bytes / 512 bytes
# Disklabel type: dos
# Disk identifier: 0xed2f4154

# Device         Boot   Start      End  Sectors Size Id Type
# /dev/nvme0n1p1 *       2048  2099199  2097152   1G 83 Linux
# /dev/nvme0n1p2      2099200 62914559 60815360  29G 8e Linux LVM


# Disk /dev/sda: 2 GiB, 2147483648 bytes, 4194304 sectors
# Units: sectors of 1 * 512 = 512 bytes
# Sector size (logical/physical): 512 bytes / 512 bytes
# I/O size (minimum/optimal): 512 bytes / 512 bytes


# Disk /dev/mapper/rhel_rhelhost-root: 26 GiB, 27913093120 bytes, 54517760 sectors
# Units: sectors of 1 * 512 = 512 bytes
# Sector size (logical/physical): 512 bytes / 512 bytes
# I/O size (minimum/optimal): 512 bytes / 512 bytes


# Disk /dev/mapper/rhel_rhelhost-swap: 3 GiB, 3221225472 bytes, 6291456 sectors
# Units: sectors of 1 * 512 = 512 bytes
# Sector size (logical/physical): 512 bytes / 512 bytes
# I/O size (minimum/optimal): 512 bytes / 512 bytes

Note: the /dev/sda is the new device.


Partition the New Disk

# Open fdisk for the New Disk in the interactive mode
fdisk /dev/sda
# Welcome to fdisk (util-linux 2.32.1).
# 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.
# Created a new DOS disklabel with disk identifier 0xca628ef4.

# Command (m for help):

# ---------------------------

# m for help
m
# Help:

#   DOS (MBR)
#    a   toggle a bootable flag
#    b   edit nested BSD disklabel
#    c   toggle the dos compatibility flag

#   Generic
#    d   delete a partition
#    F   list free unpartitioned space
#    l   list known partition types
#    n   add a new partition
#    p   print the partition table
#    t   change a partition type
#    v   verify the partition table
#    i   print information about a partition

#   Misc
#    m   print this menu
#    u   change display/entry units
#    x   extra functionality (experts only)

#   Script
#    I   load disk layout from sfdisk script file
#    O   dump disk layout to sfdisk script file

#   Save & Exit
#    w   write table to disk and exit
#    q   quit without saving changes

#   Create a new label
#    g   create a new empty GPT partition table
#    G   create a new empty SGI (IRIX) partition table
#    o   create a new empty DOS partition table
#    s   create a new empty Sun partition table


# Command (m for help):
# ---------------------------
# n   add a new partition
n
# Partition type
#    p   primary (0 primary, 0 extended, 4 free)
#    e   extended (container for logical partitions)
# ---------------------------
# p to create a primary partition
p
# Partition number (1-4, default 1):enter
# First sector (2048-4194303, default 2048):enter
# Last sector, +sectors or +size{K,M,G,T,P} (2048-4194303, default 4194303):enter
#
# Created a new partition 1 of type 'Linux' and of size 2 GiB.
# ---------------------------
# w   write table to disk and exit
w
# The partition table has been altered.
# Calling ioctl() to re-read partition table.
# Syncing disks.
# ---------------------------


# confirm
lsblk
# NAME                   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
# sda                      8:0    0   2G  0 disk
# └─sda1                   8:1    0   2G  0 part
# nvme0n1                259:0    0  30G  0 disk
# ├─nvme0n1p1            259:1    0   1G  0 part /boot
# └─nvme0n1p2            259:2    0  29G  0 part
#   ├─rhel_rhelhost-root 253:0    0  26G  0 lvm  /
#   └─rhel_rhelhost-swap 253:1    0   3G  0 lvm  [SWAP]

fdisk -l
# Disk /dev/nvme0n1: 30 GiB, 32212254720 bytes, 62914560 sectors
# Units: sectors of 1 * 512 = 512 bytes
# Sector size (logical/physical): 512 bytes / 512 bytes
# I/O size (minimum/optimal): 512 bytes / 512 bytes
# Disklabel type: dos
# Disk identifier: 0xed2f4154

# Device         Boot   Start      End  Sectors Size Id Type
# /dev/nvme0n1p1 *       2048  2099199  2097152   1G 83 Linux
# /dev/nvme0n1p2      2099200 62914559 60815360  29G 8e Linux LVM


# Disk /dev/sda: 2 GiB, 2147483648 bytes, 4194304 sectors
# Units: sectors of 1 * 512 = 512 bytes
# Sector size (logical/physical): 512 bytes / 512 bytes
# I/O size (minimum/optimal): 512 bytes / 512 bytes
# Disklabel type: dos
# Disk identifier: 0xca628ef4

# Device     Boot Start     End Sectors Size Id Type
# /dev/sda1        2048 4194303 4192256   2G 83 Linux


# Disk /dev/mapper/rhel_rhelhost-root: 26 GiB, 27913093120 bytes, 54517760 sectors
# Units: sectors of 1 * 512 = 512 bytes
# Sector size (logical/physical): 512 bytes / 512 bytes
# I/O size (minimum/optimal): 512 bytes / 512 bytes


# Disk /dev/mapper/rhel_rhelhost-swap: 3 GiB, 3221225472 bytes, 6291456 sectors
# Units: sectors of 1 * 512 = 512 bytes
# Sector size (logical/physical): 512 bytes / 512 bytes
# I/O size (minimum/optimal): 512 bytes / 512 bytes

Note: a new partition sda1 has been created within the device sda


Create a Filesystem

# makes an XFS filesystem on the new device
mkfs.ext4 /dev/sda1
# mke2fs 1.45.6 (20-Mar-2020)
# Creating filesystem with 524032 4k blocks and 131072 inodes
# Filesystem UUID: a62ee1c5-9e09-48ed-8671-0723f8899601
# Superblock backups stored on blocks:
#         32768, 98304, 163840, 229376, 294912

# Allocating group tables: done
# Writing inode tables: done
# Creating journal (8192 blocks): done
# Writing superblocks and filesystem accounting information: done

Mount the New Partition

# Create a Mount Point
mkdir /data
ll -d /data
# drwxr-xr-x. 2 root root 6 Dec 19 20:25 /data

# Mount the Partition
mount /dev/sda1 /data

# confirm
df -h
# Filesystem                      Size  Used Avail Use% Mounted on
# devtmpfs                        1.8G     0  1.8G   0% /dev
# tmpfs                           1.8G     0  1.8G   0% /dev/shm
# tmpfs                           1.8G  9.5M  1.8G   1% /run
# tmpfs                           1.8G     0  1.8G   0% /sys/fs/cgroup
# /dev/mapper/rhel_rhelhost-root   26G  7.3G   19G  28% /
# /dev/nvme0n1p1                 1014M  362M  653M  36% /boot
# tmpfs                           364M   12K  364M   1% /run/user/42
# tmpfs                           364M  4.0K  364M   1% /run/user/1001
# /dev/sda1                       2.0G   24K  1.9G   1% /data

Note: partition /dev/sda1 has been mounted on the /data


Persist the Mount in /etc/fstab

# Edit /etc/fstab
# add an entry for the new partition
vi /etc/fstab
# append the entry
# partition         mount point   fs type
# /dev/sda1         /data         ext4         defaults      0       0


# Test the /etc/fstab configuration without rebooting:
mount -a
# reboot and confirm
reboot

df -h
# Filesystem                      Size  Used Avail Use% Mounted on
# devtmpfs                        1.8G     0  1.8G   0% /dev
# tmpfs                           1.8G     0  1.8G   0% /dev/shm
# tmpfs                           1.8G  9.6M  1.8G   1% /run
# tmpfs                           1.8G     0  1.8G   0% /sys/fs/cgroup
# /dev/mapper/rhel_rhelhost-root   26G  7.3G   19G  28% /
# /dev/nvme0n1p1                 1014M  362M  653M  36% /boot
# /dev/sda1                       2.0G   24K  1.9G   1% /data
# tmpfs                           364M   12K  364M   1% /run/user/42
# tmpfs                           364M  4.0K  364M   1% /run/user/1001

Lab: Unmount an Existing Partition

su -

df -h
# Filesystem                      Size  Used Avail Use% Mounted on
# devtmpfs                        1.8G     0  1.8G   0% /dev
# tmpfs                           1.8G     0  1.8G   0% /dev/shm
# tmpfs                           1.8G  9.6M  1.8G   1% /run
# tmpfs                           1.8G     0  1.8G   0% /sys/fs/cgroup
# /dev/mapper/rhel_rhelhost-root   26G  7.3G   19G  28% /
# /dev/nvme0n1p1                 1014M  362M  653M  36% /boot
# /dev/sda1                       2.0G   24K  1.9G   1% /data
# tmpfs                           364M   12K  364M   1% /run/user/42
# tmpfs                           364M  4.0K  364M   1% /run/user/1001

# unmount a partition from a mount point
umount /data

# confirm
df -h
# Filesystem                      Size  Used Avail Use% Mounted on
# devtmpfs                        1.8G     0  1.8G   0% /dev
# tmpfs                           1.8G     0  1.8G   0% /dev/shm
# tmpfs                           1.8G  9.5M  1.8G   1% /run
# tmpfs                           1.8G     0  1.8G   0% /sys/fs/cgroup
# /dev/mapper/rhel_rhelhost-root   26G  7.3G   19G  28% /
# /dev/nvme0n1p1                 1014M  362M  653M  36% /boot
# tmpfs                           364M   12K  364M   1% /run/user/42
# tmpfs                           364M  4.0K  364M   1% /run/user/1001

Lab: Mount all partition mentioned in /etc/fstab

df -h
# Filesystem                      Size  Used Avail Use% Mounted on
# devtmpfs                        1.8G     0  1.8G   0% /dev
# tmpfs                           1.8G     0  1.8G   0% /dev/shm
# tmpfs                           1.8G  9.5M  1.8G   1% /run
# tmpfs                           1.8G     0  1.8G   0% /sys/fs/cgroup
# /dev/mapper/rhel_rhelhost-root   26G  7.3G   19G  28% /
# /dev/nvme0n1p1                 1014M  362M  653M  36% /boot
# tmpfs                           364M   12K  364M   1% /run/user/42
# tmpfs                           364M  4.0K  364M   1% /run/user/1001

#  mount all
mount -a

# confirm
df -h
# Filesystem                      Size  Used Avail Use% Mounted on
# devtmpfs                        1.8G     0  1.8G   0% /dev
# tmpfs                           1.8G     0  1.8G   0% /dev/shm
# tmpfs                           1.8G  9.5M  1.8G   1% /run
# tmpfs                           1.8G     0  1.8G   0% /sys/fs/cgroup
# /dev/mapper/rhel_rhelhost-root   26G  7.3G   19G  28% /
# /dev/nvme0n1p1                 1014M  362M  653M  36% /boot
# tmpfs                           364M   12K  364M   1% /run/user/42
# tmpfs                           364M  4.0K  364M   1% /run/user/1001
# /dev/sda1                       2.0G   24K  1.9G   1% /data