mkfs Command

Create file systems on disk partitions and storage devices in Linux.

Syntax

mkfs [OPTIONS] [-t FSTYPE] DEVICE mkfs.FSTYPE [OPTIONS] DEVICE

The mkfs command creates file systems on disk partitions or storage devices. It acts as a front-end to various filesystem-specific utilities.

Common Options

Option Description
-t, --type Specify filesystem type
-V, --verbose Verbose output
-n, --no-action Show what would be done without executing
-f, --force Force creation (filesystem-specific)
-L, --label Set filesystem label
-U, --uuid Set filesystem UUID
-c Check for bad blocks before creating
-q, --quiet Quiet mode

Supported Filesystem Types

Type Command Description Use Cases
ext4 mkfs.ext4 Fourth extended filesystem General purpose, default on many Linux distributions
xfs mkfs.xfs High-performance journaling filesystem Large files, high-performance applications
btrfs mkfs.btrfs B-tree filesystem with advanced features Snapshots, compression, RAID
ext3 mkfs.ext3 Third extended filesystem Legacy systems, journaling support
ext2 mkfs.ext2 Second extended filesystem Simple, no journaling
vfat mkfs.vfat FAT32 filesystem USB drives, compatibility with Windows
ntfs mkfs.ntfs NTFS filesystem Windows compatibility

Basic Examples

Creating ext4 filesystems

# Create ext4 filesystem sudo mkfs.ext4 /dev/sdb1 # Create ext4 with label sudo mkfs.ext4 -L "MyData" /dev/sdb1 # Create ext4 with custom UUID sudo mkfs.ext4 -U 12345678-1234-1234-1234-123456789012 /dev/sdb1 # Create ext4 with bad block check sudo mkfs.ext4 -c /dev/sdb1

Create ext4 filesystems with various options

Creating other filesystem types

# Create XFS filesystem sudo mkfs.xfs /dev/sdb1 # Create Btrfs filesystem sudo mkfs.btrfs /dev/sdb1 # Create FAT32 filesystem sudo mkfs.vfat -F 32 /dev/sdb1 # Create NTFS filesystem sudo mkfs.ntfs /dev/sdb1 # Using generic mkfs with type specification sudo mkfs -t ext4 /dev/sdb1

Create different types of filesystems

Filesystem with custom parameters

# Create ext4 with custom block size sudo mkfs.ext4 -b 4096 /dev/sdb1 # Create ext4 with reserved blocks percentage sudo mkfs.ext4 -m 1 /dev/sdb1 # Create XFS with custom block size sudo mkfs.xfs -b size=4096 /dev/sdb1 # Create Btrfs with compression sudo mkfs.btrfs -f /dev/sdb1

Create filesystems with custom parameters and optimizations

Advanced Usage

Ext4 advanced options

# Create ext4 with journal on separate device sudo mkfs.ext4 -J device=/dev/sdc1 /dev/sdb1 # Create ext4 with specific inode count sudo mkfs.ext4 -N 1000000 /dev/sdb1 # Create ext4 with lazy initialization disabled sudo mkfs.ext4 -E lazy_itable_init=0,lazy_journal_init=0 /dev/sdb1 # Create ext4 with specific features sudo mkfs.ext4 -O ^has_journal,extent /dev/sdb1 # Create ext4 optimized for SSDs sudo mkfs.ext4 -E discard /dev/sdb1

Advanced ext4 filesystem creation options

XFS advanced options

# Create XFS with custom sector size sudo mkfs.xfs -s size=4096 /dev/sdb1 # Create XFS with specific allocation group size sudo mkfs.xfs -d agsize=128m /dev/sdb1 # Create XFS with log on separate device sudo mkfs.xfs -l logdev=/dev/sdc1 /dev/sdb1 # Create XFS with metadata on separate device sudo mkfs.xfs -m rtdev=/dev/sdd1 /dev/sdb1 # Force XFS creation (overwrite existing) sudo mkfs.xfs -f /dev/sdb1

Advanced XFS filesystem creation options

Btrfs advanced options

# Create Btrfs with RAID1 sudo mkfs.btrfs -m raid1 -d raid1 /dev/sdb1 /dev/sdc1 # Create Btrfs with compression sudo mkfs.btrfs -O compress-force=zstd /dev/sdb1 # Create Btrfs with specific node size sudo mkfs.btrfs -n 32k /dev/sdb1 # Create Btrfs with mixed block groups sudo mkfs.btrfs -M /dev/sdb1 # Create Btrfs with specific UUID sudo mkfs.btrfs -U 12345678-1234-1234-1234-123456789012 /dev/sdb1

Advanced Btrfs filesystem creation options

Preparation and Safety

Before creating filesystems

# List all block devices lsblk # Show partition information sudo fdisk -l # Check if device is mounted mount | grep /dev/sdb1 # Unmount if necessary sudo umount /dev/sdb1 # Check for existing filesystem sudo blkid /dev/sdb1 # Wipe existing filesystem signatures sudo wipefs -a /dev/sdb1

Prepare and verify devices before filesystem creation

Dry run and verification

# Dry run (show what would be done) sudo mkfs.ext4 -n /dev/sdb1 # Verbose output sudo mkfs.ext4 -V /dev/sdb1 # Verify filesystem after creation sudo fsck.ext4 -n /dev/sdb1 # Check filesystem information sudo tune2fs -l /dev/sdb1 # Test mount the new filesystem sudo mkdir /mnt/test sudo mount /dev/sdb1 /mnt/test df -h /mnt/test sudo umount /mnt/test

Verify operations and test new filesystems

Filesystem-Specific Examples

USB drive formatting

# Format USB drive as FAT32 (Windows compatible) sudo mkfs.vfat -F 32 -n "USB_DRIVE" /dev/sdb1 # Format USB drive as exFAT (large file support) sudo mkfs.exfat -n "USB_DRIVE" /dev/sdb1 # Format USB drive as ext4 (Linux only) sudo mkfs.ext4 -L "USB_DRIVE" /dev/sdb1 # Format USB drive as NTFS (Windows compatible) sudo mkfs.ntfs -f -L "USB_DRIVE" /dev/sdb1

Format USB drives for different compatibility requirements

Server storage formatting

# Format large storage as XFS sudo mkfs.xfs -f -L "DataStorage" /dev/sdb1 # Format database storage with optimizations sudo mkfs.ext4 -L "Database" -E stride=32,stripe-width=64 /dev/sdb1 # Format backup storage with compression sudo mkfs.btrfs -L "Backup" -f /dev/sdb1 # Format log storage with minimal reserved space sudo mkfs.ext4 -L "Logs" -m 0 /dev/sdb1

Format storage for specific server use cases

RAID array formatting

# Format software RAID device sudo mkfs.ext4 -L "RAID_Array" /dev/md0 # Format hardware RAID with optimizations sudo mkfs.xfs -f -d su=64k,sw=4 /dev/sdb1 # Format Btrfs RAID1 sudo mkfs.btrfs -L "RAID1_Array" -m raid1 -d raid1 /dev/sdb1 /dev/sdc1 # Format with alignment for RAID sudo mkfs.ext4 -E stride=16,stripe-width=64 /dev/md0

Format RAID arrays with appropriate optimizations

Troubleshooting

Common Issues
  • Device busy - Device is mounted or in use
  • Permission denied - Need root privileges
  • Invalid device - Device doesn't exist or wrong path
  • Filesystem exists - Existing filesystem signatures present

Resolving common problems

# Check if device is busy sudo lsof /dev/sdb1 sudo fuser -v /dev/sdb1 # Force unmount if stuck sudo umount -f /dev/sdb1 sudo umount -l /dev/sdb1 # Lazy unmount # Clear existing filesystem signatures sudo wipefs -a /dev/sdb1 # Check device health sudo smartctl -a /dev/sdb # Verify device is writable sudo dd if=/dev/zero of=/dev/sdb1 bs=1M count=1

Diagnose and resolve filesystem creation issues

Recovery and verification

# Check filesystem after creation sudo fsck -n /dev/sdb1 # Get detailed filesystem information sudo dumpe2fs /dev/sdb1 | head -20 # For ext filesystems sudo xfs_info /dev/sdb1 # For XFS sudo btrfs filesystem show /dev/sdb1 # For Btrfs # Test filesystem performance sudo hdparm -tT /dev/sdb1 # Monitor filesystem creation progress sudo mkfs.ext4 -v /dev/sdb1

Verify and test newly created filesystems

Best Practices

Safety Guidelines
  • Always backup important data before formatting
  • Double-check device paths to avoid formatting wrong drives
  • Unmount devices before formatting
  • Use appropriate filesystem types for intended use
  • Test new filesystems before putting into production
  • Consider alignment for SSDs and RAID arrays
Performance Considerations
  • Choose block size based on typical file sizes
  • Reserve appropriate space for system use
  • Enable discard for SSDs
  • Use appropriate stripe settings for RAID
  • Consider journal placement for high I/O workloads
  • Optimize inode count for expected file count

See also