HardDisk Management

Managing and repairing hard disks and filesystems in Linux involves a range of tools and commands. Here’s a comprehensive guide covering tools, commands, and tricks:

Tools and Commands

Disk Utilities

  1. fdisk - Partition table manipulator for Linux.

  2. parted - A more advanced partitioning tool.

  3. lsblk - Lists information about block devices.

  4. blkid - Locate/print block device attributes.

  5. mkfs - Create a filesystem.

  6. fsck - File system consistency check and repair.

  7. tune2fs - Adjust tunable filesystem parameters on ext2/ext3/ext4 filesystems.

  8. e2fsck - Check a Linux ext2/ext3/ext4 filesystem.

  9. xfs_repair - Repair XFS filesystems.

  10. resize2fs - Resize ext2/ext3/ext4 filesystems.

  11. btrfs - Commands for managing Btrfs filesystems.

  12. smartctl - Control and monitor storage systems using S.M.A.R.T.

Disk Usage and Monitoring

  1. df - Report file system disk space usage.

  2. du - Estimate file space usage.

  3. iotop - Display I/O usage by processes.

  4. dstat - Versatile resource statistics.

  5. blktrace - Trace block I/O operations.

Tricks

  1. Mounting Filesystems:

  2. Creating Partitions:

  3. Automount:

  4. Check Disk Usage Over Time:

  5. Disk Health Monitoring:

Enterprise Information and Commands

  1. LVM (Logical Volume Manager):

  2. RAID:

  3. Backup Solutions:

  4. Enterprise Monitoring Tools:

Fdisk

fdisk is a powerful command-line utility used to manipulate disk partitions in Linux. It allows you to create, delete, and manage disk partitions. Here’s a comprehensive guide to using fdisk.

1. Checking Disk Partitions

Before making changes, you should list the current partitions on your disk.

sudo fdisk -l
      

2. Starting fdisk

To start fdisk for a specific disk, use:

sudo fdisk /dev/sdX
      

Replace /dev/sdX with the identifier of your disk (e.g., /dev/sda).

When you start fdisk, you’ll be presented with a prompt where you can enter commands. Here’s a summary of the available commands:

4. Creating a New Partition

  1. Start fdisk:

    sudo fdisk /dev/sdX
          
  2. Enter n (new partition) and follow the prompts:

  3. Example:

5. Deleting a Partition

  1. Start fdisk:

    sudo fdisk /dev/sdX
          
  2. Enter d (delete partition) and specify the partition number to delete.

  3. Example:

6. Changing a Partition’s Type

  1. Start fdisk:

    sudo fdisk /dev/sdX
          
  2. Enter t (change partition type) and specify the partition number.

  3. Specify the type (e.g., 83 for Linux, 82 for swap).

  4. Example:

7. Setting a Bootable Flag

  1. Start fdisk:

    sudo fdisk /dev/sdX
          
  2. Enter a (toggle bootable flag) and specify the partition number.

  3. Example:

8. Writing Changes to Disk

  1. Start fdisk:

    sudo fdisk /dev/sdX
          
  2. Make your changes (create, delete, modify partitions).

  3. Enter w (write) to save changes and exit.

  4. Example:

9. Quitting Without Saving Changes

  1. Start fdisk:

    sudo fdisk /dev/sdX
          
  2. Make your changes (if any).

  3. Enter q (quit) to exit without saving changes.

  4. Example:

10. Expert Mode

  1. Start fdisk:

    sudo fdisk /dev/sdX
          
  2. Enter x (expert mode) for advanced options.

  3. Explore options carefully as expert mode can be risky.

11. Updating the Partition Table

After modifying partitions with fdisk, you may need to reboot or run the following command to update the partition table:

sudo partprobe /dev/sdX
      

This command notifies the OS of partition table changes without rebooting.

Example Workflow

Here’s an example of creating and formatting a new partition:

  1. List current partitions:

    sudo fdisk -l
          
  2. Start fdisk on the disk:

    sudo fdisk /dev/sda
          
  3. Create a new partition:

  4. Write changes:

  5. Format the new partition:

    sudo mkfs.ext4 /dev/sda1
          
  6. Mount the new partition:

    sudo mount /dev/sda1 /mnt
          

This guide covers the basics of using fdisk to manage disk partitions. For more complex tasks, refer to the fdisk man page:

man fdisk
      

Parted

parted is a command-line tool for managing disk partitions, supporting both MBR (Master Boot Record) and GPT (GUID Partition Table) partitioning schemes. It provides more advanced partitioning features than fdisk.

1. Installing parted

On Debian-based systems (e.g., Ubuntu):

sudo apt-get install parted
      

On Red Hat-based systems (e.g., CentOS):

sudo yum install parted
      

On Fedora:

sudo dnf install parted
      

2. Starting parted

To start parted for a specific disk, use:

sudo parted /dev/sdX
      

Replace /dev/sdX with your disk identifier (e.g., /dev/sda).

3. Understanding the parted Commands

Once you start parted, you’ll enter its interactive mode. Here are some useful commands:

4. Viewing the Partition Table

To view the partition table of a disk, use the print command:

(parted) print
      

This command displays details about the partitions, including their sizes, file systems, and partition types.

5. Creating a New Partition Table

  1. Start parted:

    sudo parted /dev/sdX
          
  2. Create a new partition table:

    (parted) mklabel gpt
          

    or

    (parted) mklabel msdos
          

6. Creating a New Partition

  1. Start parted:

    sudo parted /dev/sdX
          
  2. Create a new partition:

    (parted) mkpart primary ext4 1MiB 100%
          

7. Removing a Partition

  1. Start parted:

    sudo parted /dev/sdX
          
  2. List partitions:

    (parted) print
          
  3. Remove a partition:

    (parted) rm PARTITION_NUMBER
          

    Replace PARTITION_NUMBER with the number of the partition you want to remove.

8. Resizing a Partition

  1. Start parted:

    sudo parted /dev/sdX
          
  2. Resize a partition:

    (parted) resizepart PARTITION_NUMBER END
          

9. Moving a Partition

  1. Start parted:

    sudo parted /dev/sdX
          
  2. Move a partition:

    (parted) move PARTITION_NUMBER START END
          

10. Setting a Partition Flag

  1. Start parted:

    sudo parted /dev/sdX
          
  2. Set a partition flag:

    (parted) set PARTITION_NUMBER boot on
          

11. Quitting parted

To exit parted, use the quit command:

(parted) quit
      

Example Workflow

Here’s an example workflow for creating a new GPT partition table and a new ext4 partition:

  1. Start parted:

    sudo parted /dev/sda
          
  2. Create a new GPT partition table:

    (parted) mklabel gpt
          
  3. Create a new partition:

    (parted) mkpart primary ext4 1MiB 100%
          
  4. Verify the partition:

    (parted) print
          
  5. Quit parted:

    (parted) quit
          

Additional Notes

Refer to the parted man page for more detailed information and options:

man parted
      

lsblk

lsblk is a command-line utility used to list information about all available block devices in a Linux system. It provides a clear view of the storage devices, their partitions, and their mount points.

1. Basic Usage

To list all block devices:

lsblk
      

This command provides a tree-like view of block devices, showing their names, sizes, types, and mount points.

2. Common Options

3. Detailed Examples

  1. Basic Listing

    To display a simple list of all block devices:

    lsblk
          

    Output Example:

    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
          sda      8:0    0  100G  0 disk 
          ├─sda1   8:1    0  512M  0 part /boot
          ├─sda2   8:2    0   50G  0 part /
          └─sda3   8:3    0   49G  0 part /home
          
  2. List with Filesystem Information

    To show filesystem information (like UUID, type, and label):

    lsblk -f
          

    Output Example:

    NAME   FSTYPE LABEL UUID                                 MOUNTPOINT
          sda
          ├─sda1 ext4         1234-5678-90AB-CDEF                 /boot
          ├─sda2 ext4         ABCD-1234-5678-90EF                 /
          └─sda3 ext4         FEDC-BA98-7654-3210                 /home
          
  3. List in Simple Format

    To display block devices in a simple format without tree structure:

    lsblk -l
          

    Output Example:

    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
          sda      8:0    0  100G  0 disk 
          sda1     8:1    0  512M  0 part /boot
          sda2     8:2    0   50G  0 part /
          sda3     8:3    0   49G  0 part /home
          
  4. Custom Columns

    To display specific columns, use the -o option:

    lsblk -o NAME,SIZE,TYPE,MOUNTPOINT
          

    Output Example:

    NAME   SIZE TYPE MOUNTPOINT
          sda   100G  disk 
          sda1  512M  part /boot
          sda2   50G  part /
          sda3   49G  part /home
          
  5. Show Only Specific Devices

    To list specific devices, you can use grep:

    lsblk | grep sda
          

    Output Example:

    sda      8:0    0  100G  0 disk 
          ├─sda1   8:1    0  512M  0 part /boot
          ├─sda2   8:2    0   50G  0 part /
          └─sda3   8:3    0   49G  0 part /home
          
  6. List Devices with Partitions Only

    To list only devices with partitions:

    lsblk -p -o NAME,TYPE | grep part
          

    Output Example:

    /dev/sda1  part
          /dev/sda2  part
          /dev/sda3  part
          

7. Practical Uses

Example Workflow

Here’s a typical workflow for checking disk partitions and filesystems:

  1. List all devices:

    lsblk
          
  2. Show detailed filesystem info:

    lsblk -f
          
  3. Display specific columns:

    lsblk -o NAME,SIZE,TYPE,MOUNTPOINT
          
  4. Check if a new disk is recognized:

    lsblk
          

    Look for new devices or partitions that match your expectations.

Man Page

For more detailed information and options, refer to the lsblk man page:

man lsblk
      

This guide should help you effectively use lsblk to manage and monitor your block devices and their partitions in Linux.

blkid

blkid is a command-line utility used to locate and display information about block devices. It provides details about device labels, UUIDs (Universally Unique Identifiers), filesystem types, and more.

1. Basic Usage

To list all block devices and their attributes:

sudo blkid
      

This command will display information about all detected block devices, including their UUIDs, labels, and filesystems.

2. Common Options

3. Detailed Examples

  1. Basic Listing

    To display information about all block devices:

    sudo blkid
          

    Output Example:

    /dev/sda1: UUID="1234-5678-90AB-CDEF" TYPE="ext4" PARTUUID="abcd1234-01"
          /dev/sda2: UUID="ABCD-1234-5678-90EF" TYPE="swap" PARTUUID="abcd1234-02"
          /dev/sdb1: LABEL="MyData" UUID="FEDC-BA98-7654-3210" TYPE="ntfs"
          
  2. Display Specific Attributes

    To show a specific attribute like UUID for a device:

    sudo blkid -o value -s UUID /dev/sda1
          

    Output Example:

    1234-5678-90AB-CDEF
          
  3. List Attributes for All Devices

    To display a list of all available attributes for all devices:

    sudo blkid -o list
          

    Output Example:

    device         UUID                                 TYPE
          /dev/sda1      1234-5678-90AB-CDEF                 ext4
          /dev/sda2      ABCD-1234-5678-90EF                 swap
          /dev/sdb1      FEDC-BA98-7654-3210                 ntfs
          
  4. Display Specific Attribute for All Devices

    To show only UUIDs for all devices:

    sudo blkid -s UUID
          

    Output Example:

    /dev/sda1: UUID="1234-5678-90AB-CDEF"
          /dev/sda2: UUID="ABCD-1234-5678-90EF"
          /dev/sdb1: UUID="FEDC-BA98-7654-3210"
          
  5. Find Devices with Specific Label

    To find devices with a specific label:

    sudo blkid -t LABEL="MyData"
          

    Output Example:

    /dev/sdb1: LABEL="MyData" UUID="FEDC-BA98-7654-3210" TYPE="ntfs"
          

4. Practical Uses

Example Workflow

Here’s a typical workflow for using blkid:

  1. List all block devices with detailed attributes:

    sudo blkid
          
  2. Find the UUID of a specific device:

    sudo blkid -o value -s UUID /dev/sda1
          
  3. Check devices with a specific label:

    sudo blkid -t LABEL="MyData"
          

Man Page

For more detailed information and options, refer to the blkid man page:

man blkid
      

This guide should help you effectively use blkid to manage and retrieve information about your block devices.

mkfs

mkfs (short for “make filesystem”) is a command-line utility used to create a filesystem on a block device or partition. It supports various filesystem types, such as ext4, xfs, and btrfs.

1. Basic Usage

The general syntax for mkfs is:

mkfs [OPTIONS] DEVICE
      

2. Common Filesystem Types and Commands

Here are some common mkfs commands for different filesystems:

3. Detailed Examples

  1. Creating an ext4 Filesystem

    To create an ext4 filesystem on /dev/sda1:

    sudo mkfs.ext4 /dev/sda1
          

    Options:

    Example with Options:

    sudo mkfs.ext4 -L mydata -m 2 /dev/sda1
          
  2. Creating an xfs Filesystem

    To create an xfs filesystem on /dev/sdb1:

    sudo mkfs.xfs /dev/sdb1
          

    Options:

    Example with Options:

    sudo mkfs.xfs -L mydata /dev/sdb1
          
  3. Creating a FAT32 Filesystem

    To create a FAT32 filesystem on /dev/sdc1:

    sudo mkfs.vfat /dev/sdc1
          

    Options:

    Example with Options:

    sudo mkfs.vfat -n MYFAT32 /dev/sdc1
          
  4. Creating a NTFS Filesystem

    To create an NTFS filesystem on /dev/sdd1:

    sudo mkfs.ntfs /dev/sdd1
          

    Options:

    Example with Options:

    sudo mkfs.ntfs -L myntfs -q /dev/sdd1
          
  5. Creating a Btrfs Filesystem

    To create a Btrfs filesystem on /dev/sde1:

    sudo mkfs.btrfs /dev/sde1
          

    Options:

    Example with Options:

    sudo mkfs.btrfs -L mybtrfs /dev/sde1
          

4. Practical Uses

Example Workflow

Here’s a typical workflow for creating a new ext4 filesystem:

  1. Check Existing Filesystems:

    sudo lsblk -f
          
  2. Create a Filesystem:

    sudo mkfs.ext4 /dev/sda1
          
  3. Verify Creation:

    sudo blkid /dev/sda1
          
  4. Mount the Filesystem:

    sudo mount /dev/sda1 /mnt
          
  5. Check Mount Point:

    df -h
          

Man Page

For more detailed information and options, refer to the mkfs man page:

man mkfs
      

This guide should help you effectively use mkfs to create and manage filesystems on your block devices.

fsck

fsck (File System Check) is a command-line utility used to check and repair inconsistencies in filesystems. It ensures that the filesystem’s structure is intact and can fix any errors it finds.

1. Basic Usage

The general syntax for fsck is:

fsck [OPTIONS] [FILESYSTEM]
      

2. Common Filesystem Types

fsck can be used with different filesystem types, including:

3. Common Options

4. Detailed Examples

  1. Check Filesystem

    To check a filesystem on /dev/sda1 without making changes:

    sudo fsck -n /dev/sda1
          

    Output Example:

    fsck from util-linux 2.36.1
          /dev/sda1: clean, 12345/12345678 files, 123456/12345678 blocks
          
  2. Force Check and Repair

    To force a check and automatically repair errors:

    sudo fsck -a /dev/sda1
          

    Output Example:

    fsck from util-linux 2.36.1
          /dev/sda1: recovering journal
          /dev/sda1: clean, 12345/12345678 files, 123456/12345678 blocks
          
  3. Interactive Check and Repair

    To check the filesystem and interactively repair errors:

    sudo fsck /dev/sda1
          

    You’ll be prompted to confirm each repair.

  4. Check and Repair All Filesystems in /etc/fstab

    To check and repair all filesystems listed in /etc/fstab:

    sudo fsck -A
          

    Options:

  5. Check for Bad Sectors

    To check for bad sectors (requires e2fsck):

    sudo fsck -c /dev/sda1
          

    Output Example:

    fsck from util-linux 2.36.1
          /dev/sda1: 1/12345678 files, 123456/12345678 blocks
          
  6. Force a Check on Boot

    To force a filesystem check on the next boot, create a file named /forcefsck:

    sudo touch /forcefsck
          

    The system will perform a filesystem check during the next boot.

5. Practical Uses

Example Workflow

Here’s a typical workflow for using fsck:

  1. Unmount the Filesystem (if necessary):

    sudo umount /dev/sda1
          
  2. Check the Filesystem:

    sudo fsck /dev/sda1
          
  3. Review and Repair Errors:

    Follow the prompts to repair any detected errors.

  4. Remount the Filesystem:

    sudo mount /dev/sda1 /mnt
          

Man Page

For more detailed information and options, refer to the fsck man page:

man fsck
      

tune2fs

tune2fs is a command-line utility used to adjust filesystem parameters on ext2, ext3, and ext4 filesystems. It allows you to modify filesystem features, change labels, adjust reserved blocks, and more.

1. Basic Usage

The general syntax for tune2fs is:

sudo tune2fs [OPTIONS] DEVICE
      

2. Common Options

3. Detailed Examples

  1. Change the Volume Label

    To change the volume label of a filesystem on /dev/sda1 to MYDISK:

    sudo tune2fs -L MYDISK /dev/sda1
          

    Output Example:

    tune2fs: Label changed to "MYDISK"
          
  2. Set Reserved Blocks Percentage

    To set the percentage of reserved blocks to 3%:

    sudo tune2fs -m 3 /dev/sda1
          

    Output Example:

    tune2fs: Reserved block percentage changed to 3%
          
  3. Adjust Filesystem Check Interval

    To set the interval between filesystem checks to 30 days:

    sudo tune2fs -i 30d /dev/sda1
          

    Output Example:

    tune2fs: Check interval changed to 30 days
          
  4. Set Maximum Mount Count

    To set the maximum number of mounts before a filesystem check to 20:

    sudo tune2fs -c 20 /dev/sda1
          

    Output Example:

    tune2fs: Mount count limit changed to 20
          
  5. Add or Remove a Journal (ext3/ext4 Only)

    To add a journal to an ext2 filesystem (converting it to ext3/ext4):

    sudo tune2fs -j /dev/sda1
          

    Output Example:

    tune2fs: Adding journal to /dev/sda1
          

    To remove a journal (if it exists):

    sudo tune2fs -O ^has_journal /dev/sda1
          

    Output Example:

    tune2fs: Removing journal from /dev/sda1
          
  6. Change Inode Size

    To change the inode size to 256 bytes:

    sudo tune2fs -E inode_size=256 /dev/sda1
          

    Output Example:

    tune2fs: Inode size changed to 256 bytes
          

4. Practical Uses

Example Workflow

Here’s a typical workflow for using tune2fs:

  1. Check Current Filesystem Settings:

    sudo tune2fs -l /dev/sda1
          
  2. Change Volume Label:

    sudo tune2fs -L MYDISK /dev/sda1
          
  3. Adjust Reserved Blocks Percentage:

    sudo tune2fs -m 3 /dev/sda1
          
  4. Set Maximum Mount Count:

    sudo tune2fs -c 20 /dev/sda1
          
  5. Add Journal to Filesystem (if converting from ext2 to ext3):

    sudo tune2fs -j /dev/sda1
          

Man Page

For more detailed information and options, refer to the tune2fs man page:

man tune2fs
      

e2fsck

e2fsck (Extended 2 File System Check) is a command-line utility used to check and repair ext2, ext3, and ext4 filesystems. It is a more advanced tool for filesystem checking and is generally used in scenarios where more detailed control over the filesystem check process is needed.

1. Basic Usage

The general syntax for e2fsck is:

sudo e2fsck [OPTIONS] DEVICE
      

2. Common Options

3. Detailed Examples

  1. Check Filesystem Without Making Changes

    To check a filesystem on /dev/sda1 without making any changes:

    sudo e2fsck -n /dev/sda1
          

    Output Example:

    e2fsck 1.46.5 (30-Dec-2020)
          /dev/sda1: clean, 12345/12345678 files, 123456/12345678 blocks
          
  2. Force a Filesystem Check

    To force a check and repair errors:

    sudo e2fsck -f /dev/sda1
          

    Output Example:

    e2fsck 1.46.5 (30-Dec-2020)
          /dev/sda1: recovering journal
          /dev/sda1: clean, 12345/12345678 files, 123456/12345678 blocks
          
  3. Automatically Repair Errors

    To automatically repair filesystem errors:

    sudo e2fsck -a /dev/sda1
          

    Output Example:

    e2fsck 1.46.5 (30-Dec-2020)
          /dev/sda1: recovering journal
          /dev/sda1: repaired, 12345/12345678 files, 123456/12345678 blocks
          
  4. Interactive Repair

    To interactively check and repair the filesystem (you will be prompted to confirm each repair):

    sudo e2fsck /dev/sda1
          

    Output Example:

    e2fsck 1.46.5 (30-Dec-2020)
          /dev/sda1: Checking journal
          /dev/sda1: Pass 1: Checking inode and block bitmaps
          /dev/sda1: Pass 2: Checking directory structure
          /dev/sda1: Pass 3: Checking directory entries
          /dev/sda1: Pass 4: Checking reference counts
          /dev/sda1: Pass 5: Checking group summary information
          
  5. Check for Bad Sectors

    To check for bad sectors:

    sudo e2fsck -c /dev/sda1
          

    Output Example:

    e2fsck 1.46.5 (30-Dec-2020)
          /dev/sda1: Checking for bad sectors
          /dev/sda1: 123456/12345678 files, 123456/12345678 blocks
          
  6. Log Output to a File

    To log the e2fsck output to a file:

    sudo e2fsck -f -l /path/to/logfile /dev/sda1
          

    Output Example:

    e2fsck 1.46.5 (30-Dec-2020)
          /dev/sda1: log file written to /path/to/logfile
          

4. Practical Uses

Example Workflow

Here’s a typical workflow for using e2fsck:

  1. Unmount the Filesystem (if necessary):

    sudo umount /dev/sda1
          
  2. Check the Filesystem:

    sudo e2fsck -f /dev/sda1
          
  3. Review and Repair Errors:

    Follow the prompts to repair any detected errors.

  4. Remount the Filesystem:

    sudo mount /dev/sda1 /mnt
          

Man Page

For more detailed information and options, refer to the e2fsck man page:

man e2fsck
      

XFS-repair

xfs_repair is a utility used to check and repair XFS filesystems. XFS is a high-performance filesystem commonly used in Linux environments. xfs_repair helps in fixing filesystem inconsistencies and corruption.

1. Basic Usage

The general syntax for xfs_repair is:

sudo xfs_repair [OPTIONS] DEVICE
      

2. Common Options

3. Detailed Examples

  1. Check Filesystem Without Making Changes

    To check a filesystem on /dev/sda1 without making any changes:

    sudo xfs_repair -n /dev/sda1
          

    Output Example:

    XFS filesystem being repaired
          No modifications made
          
  2. Perform a Full Filesystem Check

    To perform a complete check and repair the filesystem:

    sudo xfs_repair -d /dev/sda1
          

    Output Example:

    XFS file system being repaired
          Checking filesystem consistency
          Rebuilding XFS filesystem structures
          
  3. Clear the Log and Rebuild Metadata

    To clear the log and rebuild the filesystem metadata:

    sudo xfs_repair -L /dev/sda1
          

    Output Example:

    XFS file system being repaired
          Clearing log and rebuilding filesystem metadata
          
  4. Verbose Output

    To get detailed output during the repair process:

    sudo xfs_repair -v /dev/sda1
          

    Output Example:

    XFS file system being repaired
          Verbose mode enabled
          Checking filesystem consistency
          
  5. Print Repair Progress

    To print the progress of the repair process:

    sudo xfs_repair -P /dev/sda1
          

    Output Example:

    XFS file system being repaired
          Progress: 50% completed
          
  6. Check Filesystem Integrity Only

    To check the filesystem’s integrity and report issues without repairing:

    sudo xfs_repair -i /dev/sda1
          

    Output Example:

    XFS file system being checked
          Integrity check completed
          

4. Practical Uses

Example Workflow

Here’s a typical workflow for using xfs_repair:

  1. Unmount the Filesystem (if necessary):

    sudo umount /dev/sda1
          
  2. Check the Filesystem:

    sudo xfs_repair -d /dev/sda1
          
  3. Review and Repair Errors:

    Follow the prompts and messages to repair any detected errors.

  4. Remount the Filesystem:

    sudo mount /dev/sda1 /mnt
          

Man Page

For more detailed information and options, refer to the xfs_repair man page:

man xfs_repair
      

resize2fs

Full Tutorial on resize2fs

resize2fs is a command-line utility used to resize ext2, ext3, and ext4 filesystems. It allows you to increase or decrease the size of the filesystem without affecting the data stored on it. This tool is useful for managing disk space and adjusting filesystems to fit the available storage.

1. Basic Usage

The general syntax for resize2fs is:

sudo resize2fs [OPTIONS] DEVICE [SIZE]
      

2. Common Options

3. Detailed Examples

  1. Resize Filesystem to Fill the Partition

    To resize a filesystem on /dev/sda1 to use all available space on the partition:

    sudo resize2fs /dev/sda1
          

    Output Example:

    resize2fs 1.46.5 (30-Dec-2020)
          Resizing the filesystem on /dev/sda1 to 10240000 (4k) blocks
          
  2. Resize Filesystem to a Specific Size

    To resize the filesystem on /dev/sda1 to 10GB:

    sudo resize2fs /dev/sda1 10G
          

    Output Example:

    resize2fs 1.46.5 (30-Dec-2020)
          Resizing the filesystem on /dev/sda1 to 2621440 (4k) blocks
          
  3. Force Filesystem Resize

    To force resizing of the filesystem, even if it appears clean:

    sudo resize2fs -f /dev/sda1
          

    Output Example:

    resize2fs 1.46.5 (30-Dec-2020)
          Forced resize of the filesystem on /dev/sda1
          
  4. Resize Filesystem to Minimum Size

    To resize the filesystem to the minimum size required:

    sudo resize2fs -M /dev/sda1
          

    Output Example:

    resize2fs 1.46.5 (30-Dec-2020)
          Resizing the filesystem on /dev/sda1 to the minimum size
          
  5. Print Progress Information

    To print progress information during the resizing process:

    sudo resize2fs -p /dev/sda1
          

    Output Example:

    resize2fs 1.46.5 (30-Dec-2020)
          Resizing the filesystem on /dev/sda1
          Progress: 50% completed
          
  6. Shrink the Filesystem

    To shrink the filesystem (make sure to first reduce the size of the partition if shrinking):

    sudo resize2fs -s /dev/sda1
          

    Output Example:

    resize2fs 1.46.5 (30-Dec-2020)
          Shrinking the filesystem on /dev/sda1
          

4. Practical Uses

Example Workflow

Here’s a typical workflow for resizing a filesystem:

  1. Resize the Partition (if needed):

    Use partitioning tools like fdisk, parted, or gparted to adjust the partition size before resizing the filesystem.

  2. Resize the Filesystem

    sudo resize2fs /dev/sda1
          
  3. Verify the Filesystem Size

    After resizing, you can check the filesystem size using df or tune2fs:

    df -h /dev/sda1
          
    sudo tune2fs -l /dev/sda1
          

Man Page

For more detailed information and options, refer to the resize2fs man page:

man resize2fs
      

btrfs

Btrfs (B-tree filesystem) is a modern filesystem for Linux with advanced features like snapshots, volume management, and built-in RAID. It includes several utilities for managing and maintaining Btrfs filesystems.

1. btrfs Command Overview

The btrfs command is a versatile tool used for managing Btrfs filesystems. It has various subcommands for different operations.

Basic Syntax:

sudo btrfs [SUBCOMMAND] [OPTIONS] [ARGS]
      

2. Common Subcommands

Here’s a guide to the most commonly used btrfs subcommands:

a. btrfs filesystem
b. btrfs device
c. btrfs balance
d. btrfs subvolume
e. btrfs check
f. btrfs scrub

3. Practical Examples

  1. Creating a New Btrfs Filesystem

    sudo mkfs.btrfs /dev/sda1
          
  2. Mounting a Btrfs Filesystem

    sudo mount /dev/sda1 /mnt/myfs
          
  3. Adding a Device to a Btrfs Filesystem

    sudo btrfs device add /dev/sdb1 /mnt/myfs
          
  4. Creating a Snapshot

    sudo btrfs subvolume snapshot /mnt/myfs/@ /mnt/myfs/@snapshot
          
  5. Resizing a Filesystem

    sudo btrfs filesystem resize +10G /mnt/myfs
          
  6. Starting a Balance Operation

    sudo btrfs balance start /mnt/myfs
          
  7. Checking Filesystem Integrity

    sudo btrfs check /dev/sda1
          

4. Man Pages and Documentation

For more detailed information on each command and its options, you can refer to the man pages:

man btrfs
      man btrfs-device
      man btrfs-balance
      man btrfs-subvolume
      man btrfs-scrub
      man btrfs-check
      

Smartctl

Full Tutorial on smartctl

smartctl is a command-line utility used to control and monitor the Self-Monitoring, Analysis, and Reporting Technology (SMART) system built into modern hard drives and solid-state drives (SSDs). It provides detailed information about the health and status of drives, allowing for proactive maintenance and troubleshooting.

1. Basic Usage

The general syntax for smartctl is:

sudo smartctl [OPTIONS] [DEVICE]
      

2. Common Commands and Options

a. Display Drive Health Status
b. Perform SMART Self-Test
c. Check SMART Attributes
d. Enable/Disable SMART
e. Get Drive Information

3. Practical Examples

  1. Check Overall SMART Status

    sudo smartctl -a /dev/sda
          
  2. Run a Short Self-Test

    sudo smartctl -t short /dev/sda
          

    Check the results after a few minutes:

    sudo smartctl -a /dev/sda
          
  3. Run a Long Self-Test

    sudo smartctl -t long /dev/sda
          

    Check the results after the test completes:

    sudo smartctl -a /dev/sda
          
  4. Enable SMART Monitoring

    sudo smartctl -s on /dev/sda
          
  5. Disable SMART Monitoring

    sudo smartctl -s off /dev/sda
          

4. Man Page

For more detailed information and options, refer to the smartctl man page:

man smartctl
      

Remove Bad Sector

Removing bad sectors from a hard disk isn’t a straightforward process because once a sector is marked as bad, it can’t be physically repaired. However, you can manage bad sectors and prevent data loss by using several tools and techniques. Here’s a guide on how to handle bad sectors:

1. Identify Bad Sectors

First, you need to identify which sectors are bad. You can use the smartctl utility or badblocks for this purpose.

a. Using smartctl

  1. Check SMART Attributes:

    sudo smartctl -a /dev/sda
          

    Look for attributes like Reallocated_Sector_Ct or Current_Pending_Sector. These indicate bad sectors.

  2. Run a SMART Short or Long Test:

    sudo smartctl -t short /dev/sda
          

    Or

    sudo smartctl -t long /dev/sda
          

    Check the results:

    sudo smartctl -a /dev/sda
          

b. Using badblocks

  1. Run badblocks to Check for Bad Sectors:

    sudo badblocks -v /dev/sda
          

    This will list all bad sectors found.

2. Mark Bad Sectors

After identifying bad sectors, you can use e2fsck (for ext2/ext3/ext4 filesystems) or fsck (for other filesystems) to mark them as unusable.

a. For ext2/ext3/ext4 Filesystems

  1. Run e2fsck to Check and Fix Filesystem:

    sudo e2fsck -cf /dev/sda1
          

    Note: Replace /dev/sda1 with your partition.

b. For Other Filesystems

  1. Run fsck with Bad Sector Checking:

    sudo fsck -cf /dev/sda1
          

    Similar options apply as with e2fsck.

3. Remap Bad Sectors

When a bad sector is found, modern hard drives can remap these sectors to spare sectors. This is done automatically by the drive’s firmware, but you should ensure it’s enabled and functioning correctly.

a. Using hdparm

You can use hdparm to check if the drive is using its spare sectors:

  1. Check Drive Status:

    sudo hdparm -I /dev/sda | grep 'Reallocated Sector Count'
          

    This command will show if the drive has reallocated sectors.

4. Backup and Replace

Since bad sectors are often a sign of impending drive failure, it’s crucial to:

  1. Backup Important Data: Regularly back up your data to avoid loss.

  2. Consider Replacing the Drive: If bad sectors are frequent or the drive is older, consider replacing it with a new one.

5. Monitor Drive Health

Regularly monitor your drive’s health using tools like smartctl to catch issues early.

Example Workflow

  1. Check for Bad Sectors:

    sudo smartctl -a /dev/sda
          sudo badblocks -v /dev/sda
          
  2. Fix Filesystem and Mark Bad Sectors:

    sudo e2fsck -cf /dev/sda1
          
  3. Check Drive Status:

    sudo hdparm -I /dev/sda | grep 'Reallocated Sector Count'
          
  4. Backup and Replace Drive if Needed

    Regularly back up your data and monitor the drive’s health to decide on replacement.

Tape

Tape storage, often used for backups and archival purposes, is a reliable method for storing large amounts of data. Here’s a guide on basic information about tape storage and commonly used commands in Linux to manage tape drives.

1. Tape Storage Basics

a. What is Tape Storage?

b. Types of Tape Drives

c. Key Concepts

2. Common Tape Commands in Linux

Linux provides several utilities to work with tape drives. Below are some of the most commonly used commands:

a. mt (Magnetic Tape) Command

The mt command is used for controlling tape drives.

Basic Syntax:

mt [OPTIONS] [COMMAND]
      

Common Commands:

b. tar Command

The tar command is often used in conjunction with tape drives to create and extract backups.

Basic Syntax:

tar [OPTIONS] [ARCHIVE] [FILES]
      

Common Commands:

c. dd Command

The dd command can be used for low-level copying to and from tape devices.

Basic Syntax:

dd if=[SOURCE] of=[DESTINATION] [OPTIONS]
      

Common Commands:

d. file Command

The file command can be used to check the type of data on a tape device.

Basic Syntax:

file [FILE]
      

Common Command:

3. Managing Tape Drives

a. Checking Tape Drive Status

Use dmesg to check if the system recognizes the tape drive:

dmesg | grep st0
      

b. Configuring Tape Drives