Managing Partitions and File Systems
1. Creating Partitions with fdisk
Example: Create a new partition using fdisk
sudo fdisk /dev/sdb
# Press 'n' to create a new partition
# Press 'p' for primary partition
# Enter partition number (1-4)
# Enter first sector (default is OK)
# Enter last sector (default is OK)
# Press 'w' to write changes and exit
| Command | Description | Notes |
|---|---|---|
| fdisk /dev/sdX | Launch fdisk for device X | Replace X with the target disk (e.g., sdb) |
| n | Create new partition | Choose primary or extended |
| p | Primary partition | Partition type (1-4) |
| e | Extended partition | Used for logical partitions |
| w | Write changes and exit | Commits partition table to disk |
| q | Quit without saving | Discards all changes |
Note: Always backup important data before modifying partitions.
2. Creating Partitions with parted
Example: Create a new partition table and add a partition
sudo parted /dev/sdb mklabel gpt
sudo parted /dev/sdb mkpart primary 1MiB 100%
| Command | Description | Notes |
|---|---|---|
| parted /dev/sdb mklabel gpt | Makes a new GPT partition table on /dev/sdb | Use msdos for MBR |
| parted /dev/sdb mkpart primary 1MiB 100% | Creates a new primary partition from 1MiB to end of disk | Start and end can be in MiB, GiB, or sectors |
Note: Always verify the partition table with
parted /dev/sdb print before making changes.Warning: Incorrect use of
parted can destroy data. Ensure the correct device is targeted.3. Creating GPT Partitions
| Command | Description | Usage |
|---|---|---|
| gdisk | Interactive GPT partitioning tool | sudo gdisk /dev/sdX |
| parted | Partitioning tool supporting GPT | sudo parted /dev/sdX |
| fdisk | Legacy partitioning tool (MBR only) | sudo fdisk /dev/sdX |
Example: Using gdisk to create a new GPT partition
sudo gdisk /dev/sda
Command: o
 Create a new GPT partition table
Command: n
 Create a new partition
Partition number: 1
 Use partition 1
First sector: 2048
 Start at 2048 sectors
Last sector: +1G
 1GB size
Command: t
 Change partition type
Partition type: 8300
 Linux filesystem (ext4)
Command: w
 Write changes to disk
Note: Ensure the device path is correct to avoid data loss.
Warning: Always backup important data before partitioning.
| Partition Type | Code | Description |
|---|---|---|
| EFI System Partition (ESP) | EF00 |
Used for UEFI boot loaders |
| Linux filesystem | 8300 |
Standard Linux ext4 partition |
| Linux swap | 8200 |
Swap space for virtual memory |
4. Creating ext4 File System
| Option | Values | Description |
|---|---|---|
| -t ext4 | ext4 |
Specify the file system type as ext4 |
| /dev/sdb1 | /dev/sdb1 |
Target device or partition to format |
Note: Ensure the target device is unmounted before formatting to avoid data loss.
Warning: Formatting a device will erase all existing data on it.
5. Creating XFS File System
Example: Create XFS File System
# Create XFS file system on /dev/sdb1
mkfs.xfs /dev/sdb1
# Create XFS file system with specific block size
mkfs.xfs -b size=4096 /dev/sdb1
# Create XFS file system with custom log size
mkfs.xfs -l size=128m /dev/sdb1
| Option | Values | Description |
|---|---|---|
| -b size=<size> | 512 to 65536 |
Block size in bytes (default: 4096) |
| -l size=<size> | 1m to 512m |
Log size in bytes or megabytes |
| -d size=<size> | 1m to 256g |
Data section size (default: auto) |
| -f | - |
Force overwrite existing filesystem |
Note: XFS is a high-performance journaling file system designed for large storage systems. It supports very large files and can handle up to 500TB of data per filesystem.
Warning: Use
mkfs.xfs only on unmounted partitions. Data will be lost if used incorrectly.Key Features of XFS
- High performance for large files
- Supports up to 500TB filesystems
- Journaling for data integrity
- Online defragmentation capability
6. Creating Btrfs File System
Example: Create Btrfs File System
# Create a Btrfs file system on /dev/sdb1
sudo mkfs.btrfs /dev/sdb1
# Create Btrfs with specific options
sudo mkfs.btrfs -f /dev/sdb1
# Create Btrfs with multiple devices
sudo mkfs.btrfs /dev/sdb1 /dev/sdc1
| Option | Description | Notes |
|---|---|---|
| -f, --force | Force creation even if device is not empty | Use with caution as it overwrites existing data |
| -d, --data | Set data profile (e.g., single, raid1) | Default is single |
| -m, --metadata | Set metadata profile (e.g., single, raid1) | Default is single |
| -l, --label | Set label for the file system | Useful for identification |
| -L, --label | Same as -l | Alias for setting label |
Note: Btrfs supports various RAID levels for data and metadata redundancy. Use
mkfs.btrfs to format a device or set of devices into a Btrfs file system.Warning: Formatting a device with Btrfs will erase all existing data on that device. Ensure you have backups before proceeding.
7. Creating FAT32 File System
| Command | Description | Notes |
|---|---|---|
| mkfs.fat | mkfs.fat [options] <device> |
Creates a FAT32 file system on the specified device |
Note: Ensure the device is unmounted before creating the file system to avoid data corruption.
Warning: This operation will erase all data on the target device.
| Option | Description | Default |
|---|---|---|
| -F <size> | Specify FAT type (12, 16, or 32) | 16 |
| -n <label> | Set volume label | None |
| -C | Create a new file system without formatting | False |
8. Resizing ext File Systems
| Command | Description | Notes |
|---|---|---|
| resize2fs | resize2fs /dev/sda1 5G |
Resize ext2/ext3/ext4 filesystem to 5GB |
| resize2fs | resize2fs /dev/sda1 |
Resize filesystem to maximum available space |
Note: Ensure the partition is unmounted or in read-only mode before resizing to prevent data corruption.
Warning: Resizing a filesystem that is mounted and in use can lead to data loss. Always backup important data before resizing.
9. Resizing XFS File Systems
Note: XFS supports online resizing for growing filesystems, but shrinking requires unmounting the filesystem.
| Command | Description | Notes |
|---|---|---|
| xfs_growfs | Resize XFS filesystem to use all available space on the underlying block device | Can be run while the filesystem is mounted (online) |
| xfs_admin | Modify XFS filesystem parameters (including size limits) | Use with caution; can affect filesystem behavior |
Example: Growing an XFS Filesystem
# Grow the filesystem to use all available space
xfs_growfs /mount/point
# Verify new size
df -h /mount/point
Example: Checking XFS Filesystem Size
# Check current filesystem size and usage
xfs_info /mount/point
# Display detailed filesystem information
xfs_admin -l /dev/sdX
Warning: Resizing XFS filesystems should be done carefully to avoid data loss. Always backup critical data before resizing.
Key Points
- XFS supports online growth using
xfs_growfs - Shrinking XFS requires unmounting the filesystem
- Use
xfs_infoto check current size and usage - Ensure sufficient free space on underlying storage before resizing
10. Tuning File System Parameters
| Parameter | Description | Default Value | Notes |
|---|---|---|---|
| fs.file-max | Maximum number of file handles | 1048576 | System-wide limit for open file handles |
| fs.nr_open | Maximum number of file handles per process | 2097152 | Per-process limit, can be higher than fs.file-max |
| vm.swappiness | How aggressively the kernel swaps memory pages | 60 | Lower values mean less swapping, higher means more |
| vm.dirty-ratio | Percentage of system memory that can be filled with dirty pages before synchronous writeback begins | 10 | Controls how much dirty data can accumulate before flushing to disk |
| vm.dirty-background-ratio | Percentage of system memory that can be filled with dirty pages before background writeback begins | 5 | Controls how much dirty data can accumulate before background flushing starts |
Note: Changes made with
sysctl are temporary and will reset after reboot. To make them permanent, add the settings to /etc/sysctl.conf or /etc/sysctl.d/99-custom.conf.
Warning: Setting
vm.swappiness too low can cause performance issues due to excessive memory usage. Setting fs.file-max too high can lead to resource exhaustion.
11. Checking File System Type
| Command | Description | Example |
|---|---|---|
| file | Identifies the type of file system for a given device or file | file -s /dev/sda1 |
| lsblk | Lists block devices and their file system types | lsblk -f |
| df | Shows disk space usage and file system types | df -T |
| blkid | Displays UUID and file system type for block devices | blkid /dev/sda1 |
Example: Identifying File System Type
# Using file command
$ file -s /dev/sda1
/dev/sda1: Linux filesystem, ext4
# Using blkid command
$ blkid /dev/sda1
/dev/sda1: UUID="550e8400-e29b-41d4-a716-446655440000" TYPE="ext4"
# Using df command
$ df -T /dev/sda1
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda1 ext4 10240000 2048000 7680000 20% /mnt/data
12. Using Btrfs Subvolumes
Btrfs (B-tree file system) is a modern copy-on-write filesystem that supports subvolumes — independent filesystems within a single Btrfs filesystem. Subvolumes are useful for organizing data, creating snapshots, and managing system states.
| Command | Description | Example |
|---|---|---|
| btrfs subvolume create | Create a new subvolume | btrfs subvolume create /mnt/btrfs/subvol1 |
| btrfs subvolume list | List all subvolumes in a Btrfs filesystem | btrfs subvolume list /mnt/btrfs |
| btrfs subvolume delete | Delete a subvolume (must be unmounted) | btrfs subvolume delete /mnt/btrfs/subvol1 |
| btrfs subvolume get-default | Get the default subvolume ID | btrfs subvolume get-default /mnt/btrfs |
| btrfs subvolume set-default | Set the default subvolume ID | btrfs subvolume set-default 5 /mnt/btrfs |
Example: Creating and Using Subvolumes
btrfs subvolume create /mnt/btrfs/root
btrfs subvolume create /mnt/btrfs/home
btrfs subvolume set-default 5 /mnt/btrfs
Note: Subvolumes are not mountable directly like regular directories. They must be mounted via the Btrfs filesystem root.