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:
fdisk
- Partition table manipulator for Linux.
sudo fdisk -l
sudo fdisk /dev/sdX
(replace sdX
with your disk identifier)parted
- A more advanced partitioning tool.
sudo parted -l
sudo parted /dev/sdX
lsblk
- Lists information about block devices.
lsblk
blkid
- Locate/print block device attributes.
sudo blkid
mkfs
- Create a filesystem.
sudo mkfs.ext4 /dev/sdX1
sudo mkfs.xfs /dev/sdX1
fsck
- File system consistency check and repair.
sudo fsck /dev/sdX1
tune2fs
- Adjust tunable filesystem parameters on ext2/ext3/ext4 filesystems.
sudo tune2fs -l /dev/sdX1
e2fsck
- Check a Linux ext2/ext3/ext4 filesystem.
sudo e2fsck /dev/sdX1
xfs_repair
- Repair XFS filesystems.
sudo xfs_repair /dev/sdX1
resize2fs
- Resize ext2/ext3/ext4 filesystems.
sudo resize2fs /dev/sdX1
btrfs
- Commands for managing Btrfs filesystems.
sudo btrfs check /dev/sdX1
sudo btrfs balance start /mount/point
smartctl
- Control and monitor storage systems using S.M.A.R.T.
sudo smartctl -a /dev/sdX
sudo smartctl -t short /dev/sdX
df
- Report file system disk space usage.
df -h
du
- Estimate file space usage.
du -sh /path/to/directory
iotop
- Display I/O usage by processes.
sudo iotop
dstat
- Versatile resource statistics.
dstat
blktrace
- Trace block I/O operations.
sudo blktrace -d /dev/sdX
Mounting Filesystems:
sudo mount /dev/sdX1 /mnt
sudo mount -o defaults,noatime /dev/sdX1 /mnt
Creating Partitions:
fdisk
or parted
to create and format partitions.Automount:
/etc/fstab
for automatic mounting on boot.Check Disk Usage Over Time:
dstat
or iostat
to monitor disk performance trends.Disk Health Monitoring:
smartctl
checks using cron jobs.LVM
(Logical Volume Manager):
sudo pvcreate /dev/sdX
sudo vgcreate vgname /dev/sdX
sudo lvcreate -n lvname -L size vgname
sudo lvresize -L +10G /dev/vgname/lvname
RAID:
mdadm
- Manage Linux software RAID.
sudo mdadm --create --verbose /dev/md0 --level=5 --raid-devices=2 /dev/sdX /dev/sdY
sudo mdadm --detail /dev/md0
Backup Solutions:
rsync
- Sync files and directories.
rsync -av /source /destination
dd
- Create disk images or clone disks.
sudo dd if=/dev/sdX of=/path/to/image.img
sudo dd if=/path/to/image.img of=/dev/sdX
Enterprise Monitoring Tools:
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
.
Before making changes, you should list the current partitions on your disk.
sudo fdisk -l
-l
(list): Lists all partitions on all disks.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
).
fdisk
MenuWhen you start fdisk
, you’ll be presented with a prompt where you can enter commands. Here’s a summary of the available commands:
m
: Print the help menu.p
: Print the partition table.n
: Add a new partition.d
: Delete a partition.t
: Change a partition’s type.a
: Toggle a bootable flag on a partition.w
: Write changes to the disk and exit.q
: Quit without saving changes.x
: Expert mode (advanced options).Start fdisk
:
sudo fdisk /dev/sdX
Enter n
(new partition) and follow the prompts:
p
: Primary partitione
: Extended partition (for creating logical partitions)Example:
/dev/sda
, choose n
, then p
, and follow the prompts to create the partition.Start fdisk
:
sudo fdisk /dev/sdX
Enter d
(delete partition) and specify the partition number to delete.
Example:
d
and then 1
.Start fdisk
:
sudo fdisk /dev/sdX
Enter t
(change partition type) and specify the partition number.
Specify the type (e.g., 83
for Linux, 82
for swap).
Example:
t
, then 1
, and enter 83
.Start fdisk
:
sudo fdisk /dev/sdX
Enter a
(toggle bootable flag) and specify the partition number.
Example:
a
and then 1
.Start fdisk
:
sudo fdisk /dev/sdX
Make your changes (create, delete, modify partitions).
Enter w
(write) to save changes and exit.
Example:
w
to write them to disk.Start fdisk
:
sudo fdisk /dev/sdX
Make your changes (if any).
Enter q
(quit) to exit without saving changes.
Example:
q
to quit without saving.Start fdisk
:
sudo fdisk /dev/sdX
Enter x
(expert mode) for advanced options.
Explore options carefully as expert mode can be risky.
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.
Here’s an example of creating and formatting a new partition:
List current partitions:
sudo fdisk -l
Start fdisk
on the disk:
sudo fdisk /dev/sda
Create a new partition:
n
for a new partition.p
for a primary partition.1
).Write changes:
w
to write changes and exit.Format the new partition:
sudo mkfs.ext4 /dev/sda1
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
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
.
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
parted
To start parted
for a specific disk, use:
sudo parted /dev/sdX
Replace /dev/sdX
with your disk identifier (e.g., /dev/sda
).
parted
CommandsOnce you start parted
, you’ll enter its interactive mode. Here are some useful commands:
print
: Displays the partition table.mklabel
: Creates a new partition table (MBR or GPT).mkpart
: Creates a new partition.rm
: Removes a partition.resize
: Resizes a partition.move
: Moves a partition.name
: Sets a partition name.set
: Sets partition flags.quit
: Exits parted
.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.
Start parted
:
sudo parted /dev/sdX
Create a new partition table:
(parted) mklabel gpt
or
(parted) mklabel msdos
gpt
: Creates a GPT partition table (recommended for disks larger than 2TB).msdos
: Creates an MBR partition table.Start parted
:
sudo parted /dev/sdX
Create a new partition:
(parted) mkpart primary ext4 1MiB 100%
primary
: Type of partition (can be primary
, logical
, or primary
depending on the partition table type).ext4
: File system type (optional).1MiB
: Start position of the partition.100%
: End position of the partition (can be specified in MiB, GiB, or as a percentage).Start parted
:
sudo parted /dev/sdX
List partitions:
(parted) print
Remove a partition:
(parted) rm PARTITION_NUMBER
Replace PARTITION_NUMBER
with the number of the partition you want to remove.
Start parted
:
sudo parted /dev/sdX
Resize a partition:
(parted) resizepart PARTITION_NUMBER END
PARTITION_NUMBER
: Number of the partition to resize.END
: New end position of the partition (e.g., 100GiB
).Start parted
:
sudo parted /dev/sdX
Move a partition:
(parted) move PARTITION_NUMBER START END
PARTITION_NUMBER
: Number of the partition to move.START
: New start position.END
: New end position.Start parted
:
sudo parted /dev/sdX
Set a partition flag:
(parted) set PARTITION_NUMBER boot on
PARTITION_NUMBER
: Number of the partition.boot
: Flag to set (other flags include lba
, raid
, etc.).parted
To exit parted
, use the quit
command:
(parted) quit
Here’s an example workflow for creating a new GPT partition table and a new ext4 partition:
Start parted
:
sudo parted /dev/sda
Create a new GPT partition table:
(parted) mklabel gpt
Create a new partition:
(parted) mkpart primary ext4 1MiB 100%
Verify the partition:
(parted) print
Quit parted
:
(parted) quit
parted
can handle large disks and multiple partition schemes (GPT or MBR) more flexibly than fdisk
.mkfs.ext4
) and mount them as needed.Refer to the parted
man page for more detailed information and options:
man parted
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.
To list all block devices:
lsblk
This command provides a tree-like view of block devices, showing their names, sizes, types, and mount points.
-a
: Show all devices, including empty ones.-f
: Show filesystem information, including labels and UUIDs.-l
: List in a simple, one-line-per-device format.-o
: Specify columns to display.-n
: Suppress the header row.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
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
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
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
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
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
lsblk
to view how disks are partitioned and where filesystems are mounted.lsblk
with other tools like df
to monitor disk usage.Here’s a typical workflow for checking disk partitions and filesystems:
List all devices:
lsblk
Show detailed filesystem info:
lsblk -f
Display specific columns:
lsblk -o NAME,SIZE,TYPE,MOUNTPOINT
Check if a new disk is recognized:
lsblk
Look for new devices or partitions that match your expectations.
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
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.
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.
-o
: Output format (e.g., value
, list
, full
).-s
: Specific attribute to query (e.g., UUID
, LABEL
).-l
: List all devices, including those without filesystems.-c
: Specify the cache file to use.-t
: Display specific attributes.-p
: Print device names and attributes, including devices that are not mounted.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"
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
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
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"
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"
blkid
to obtain UUIDs and labels of disks and partitions for mounting or configuration in /etc/fstab
.Here’s a typical workflow for using blkid
:
List all block devices with detailed attributes:
sudo blkid
Find the UUID of a specific device:
sudo blkid -o value -s UUID /dev/sda1
Check devices with a specific label:
sudo blkid -t LABEL="MyData"
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
(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.
The general syntax for mkfs
is:
mkfs [OPTIONS] DEVICE
DEVICE
: The block device or partition where the filesystem will be created (e.g., /dev/sda1
).Here are some common mkfs
commands for different filesystems:
ext2:
sudo mkfs.ext2 /dev/sdXn
ext3:
sudo mkfs.ext3 /dev/sdXn
ext4:
sudo mkfs.ext4 /dev/sdXn
xfs:
sudo mkfs.xfs /dev/sdXn
btrfs:
sudo mkfs.btrfs /dev/sdXn
vfat (FAT32):
sudo mkfs.vfat /dev/sdXn
ntfs:
sudo mkfs.ntfs /dev/sdXn
Creating an ext4 Filesystem
To create an ext4 filesystem on /dev/sda1
:
sudo mkfs.ext4 /dev/sda1
Options:
-L
: Set a volume label.-m
: Specify the reserved block percentage (default is 5%).Example with Options:
sudo mkfs.ext4 -L mydata -m 2 /dev/sda1
Creating an xfs Filesystem
To create an xfs filesystem on /dev/sdb1
:
sudo mkfs.xfs /dev/sdb1
Options:
-L
: Set a volume label.-d size=
: Specify the size of the data section.Example with Options:
sudo mkfs.xfs -L mydata /dev/sdb1
Creating a FAT32 Filesystem
To create a FAT32 filesystem on /dev/sdc1
:
sudo mkfs.vfat /dev/sdc1
Options:
-n
: Set a volume label.-F
: Specify the FAT type (12, 16, or 32).Example with Options:
sudo mkfs.vfat -n MYFAT32 /dev/sdc1
Creating a NTFS Filesystem
To create an NTFS filesystem on /dev/sdd1
:
sudo mkfs.ntfs /dev/sdd1
Options:
-L
: Set a volume label.-q
: Perform a quick format.Example with Options:
sudo mkfs.ntfs -L myntfs -q /dev/sdd1
Creating a Btrfs Filesystem
To create a Btrfs filesystem on /dev/sde1
:
sudo mkfs.btrfs /dev/sde1
Options:
-L
: Set a volume label.-d
: Specify the data RAID level (e.g., raid0
, raid1
).Example with Options:
sudo mkfs.btrfs -L mybtrfs /dev/sde1
mkfs
to format a partition before mounting it.Here’s a typical workflow for creating a new ext4 filesystem:
Check Existing Filesystems:
sudo lsblk -f
Create a Filesystem:
sudo mkfs.ext4 /dev/sda1
Verify Creation:
sudo blkid /dev/sda1
Mount the Filesystem:
sudo mount /dev/sda1 /mnt
Check Mount Point:
df -h
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
(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.
The general syntax for fsck
is:
fsck [OPTIONS] [FILESYSTEM]
FILESYSTEM
: The device or partition to check (e.g., /dev/sda1
).fsck
can be used with different filesystem types, including:
-a
: Automatically repair filesystem without prompting (use with caution).-f
: Force check, even if the filesystem seems clean.-n
: No modifications; just check and report errors.-y
: Assume “yes” to all prompts (automatically repair without asking).-c
: Check for bad sectors (requires e2fsck
).-t
: Print timing stats.-l
: Log file to write the fsck output.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
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
Interactive Check and Repair
To check the filesystem and interactively repair errors:
sudo fsck /dev/sda1
You’ll be prompted to confirm each repair.
Check and Repair All Filesystems in /etc/fstab
To check and repair all filesystems listed in /etc/fstab
:
sudo fsck -A
Options:
-A
: Check all filesystems listed in /etc/fstab
.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
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.
fsck
to check and repair filesystems.fsck
to repair corrupted filesystems and recover data.Here’s a typical workflow for using fsck
:
Unmount the Filesystem (if necessary):
sudo umount /dev/sda1
Check the Filesystem:
sudo fsck /dev/sda1
Review and Repair Errors:
Follow the prompts to repair any detected errors.
Remount the Filesystem:
sudo mount /dev/sda1 /mnt
For more detailed information and options, refer to the fsck
man page:
man fsck
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.
The general syntax for tune2fs
is:
sudo tune2fs [OPTIONS] DEVICE
DEVICE
: The block device or partition where the ext2/ext3/ext4 filesystem is located (e.g., /dev/sda1
).-L
: Set or change the volume label.-m
: Set the percentage of reserved blocks.-o
: Set filesystem options (e.g., journal_data
).-i
: Set the interval between filesystem checks.-c
: Set the maximum number of mounts before a check.-j
: Add or remove a journal (ext3/ext4 only).-E
: Extended options (e.g., changing inode size).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"
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%
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
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
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
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
Here’s a typical workflow for using tune2fs
:
Check Current Filesystem Settings:
sudo tune2fs -l /dev/sda1
Change Volume Label:
sudo tune2fs -L MYDISK /dev/sda1
Adjust Reserved Blocks Percentage:
sudo tune2fs -m 3 /dev/sda1
Set Maximum Mount Count:
sudo tune2fs -c 20 /dev/sda1
Add Journal to Filesystem (if converting from ext2 to ext3):
sudo tune2fs -j /dev/sda1
For more detailed information and options, refer to the tune2fs
man page:
man tune2fs
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.
The general syntax for e2fsck
is:
sudo e2fsck [OPTIONS] DEVICE
DEVICE
: The block device or partition to check (e.g., /dev/sda1
).-a
: Automatically repair the filesystem without prompting (use with caution).-f
: Force a check, even if the filesystem appears clean.-n
: No modifications; just check and report errors.-y
: Assume “yes” to all prompts (automatically repair without asking).-c
: Check for bad sectors.-l
: Log file to write the e2fsck
output.-t
: Print timing stats.-D
: Optimize directories.-E
: Extended options (e.g., adjusting inode sizes).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
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
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
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
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
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
e2fsck
to repair corrupted filesystems and recover data.Here’s a typical workflow for using e2fsck
:
Unmount the Filesystem (if necessary):
sudo umount /dev/sda1
Check the Filesystem:
sudo e2fsck -f /dev/sda1
Review and Repair Errors:
Follow the prompts to repair any detected errors.
Remount the Filesystem:
sudo mount /dev/sda1 /mnt
For more detailed information and options, refer to the e2fsck
man page:
man e2fsck
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.
The general syntax for xfs_repair
is:
sudo xfs_repair [OPTIONS] DEVICE
DEVICE
: The block device or partition where the XFS filesystem is located (e.g., /dev/sda1
).-n
: No modifications; just check and report errors.-d
: Perform a full check, including inode checks and data block verification.-L
: Clear the log and rebuild the filesystem metadata.-P
: Print the progress of the repair process.-v
: Verbose output.-i
: Check the filesystem’s integrity and report issues without repairing.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
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
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
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
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
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
xfs_repair
to ensure XFS filesystem integrity.Here’s a typical workflow for using xfs_repair
:
Unmount the Filesystem (if necessary):
sudo umount /dev/sda1
Check the Filesystem:
sudo xfs_repair -d /dev/sda1
Review and Repair Errors:
Follow the prompts and messages to repair any detected errors.
Remount the Filesystem:
sudo mount /dev/sda1 /mnt
For more detailed information and options, refer to the xfs_repair
man page:
man xfs_repair
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.
The general syntax for resize2fs
is:
sudo resize2fs [OPTIONS] DEVICE [SIZE]
DEVICE
: The block device or partition where the ext2/ext3/ext4 filesystem is located (e.g., /dev/sda1
).SIZE
: The new size of the filesystem (optional). If omitted, resize2fs
will resize the filesystem to fill the available space on the partition.-f
: Force resizing even if the filesystem appears to be clean.-M
: Resize the filesystem to the minimum size required.-p
: Print progress information during resizing.-s
: Shrink the filesystem.-i
: Check and display the filesystem information.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
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
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
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
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
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
Here’s a typical workflow for resizing a filesystem:
Resize the Partition (if needed):
Use partitioning tools like fdisk
, parted
, or gparted
to adjust the partition size before resizing the filesystem.
Resize the Filesystem
sudo resize2fs /dev/sda1
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
For more detailed information and options, refer to the resize2fs
man page:
man resize2fs
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.
btrfs
Command OverviewThe 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]
Here’s a guide to the most commonly used btrfs
subcommands:
btrfs filesystem
btrfs filesystem show
: Display information about mounted Btrfs filesystems.
sudo btrfs filesystem show
Output Example:
Label: 'myfs' uuid: a1b2c3d4-e5f6-7890-abcd-ef1234567890
Total devices 1 FS bytes used 10.00GiB
devid 1 size 20.00GiB used 15.00GiB path /dev/sda1
btrfs filesystem df
: Show space usage of a Btrfs filesystem.
sudo btrfs filesystem df /mnt/myfs
Output Example:
Data, single: total=10.00GiB, used=5.00GiB
Metadata, single: total=1.00GiB, used=500.00MiB
System, single: total=32.00MiB, used=16.00MiB
btrfs filesystem resize
: Resize a Btrfs filesystem.
sudo btrfs filesystem resize 20G /mnt/myfs
Output Example:
Resize: 20.00GiB
btrfs device
btrfs device add
: Add a device to a Btrfs filesystem.
sudo btrfs device add /dev/sdb1 /mnt/myfs
btrfs device delete
: Remove a device from a Btrfs filesystem.
sudo btrfs device delete /dev/sdb1 /mnt/myfs
btrfs balance
btrfs balance start
: Start a balance operation to redistribute data across devices.
sudo btrfs balance start /mnt/myfs
btrfs balance status
: Check the status of a balance operation.
sudo btrfs balance status /mnt/myfs
btrfs balance cancel
: Cancel a running balance operation.
sudo btrfs balance cancel /mnt/myfs
btrfs subvolume
btrfs subvolume list
: List all subvolumes in a Btrfs filesystem.
sudo btrfs subvolume list /mnt/myfs
Output Example:
ID 256 gen 128 top level 5 path @
ID 257 gen 130 top level 5 path @home
btrfs subvolume snapshot
: Create a snapshot of a subvolume.
sudo btrfs subvolume snapshot /mnt/myfs/@ /mnt/myfs/@snapshot
btrfs subvolume delete
: Delete a subvolume.
sudo btrfs subvolume delete /mnt/myfs/@snapshot
btrfs check
btrfs check
: Check the integrity of a Btrfs filesystem. Use with caution as it can be destructive.
sudo btrfs check /dev/sda1
btrfs check --repair
: Attempt to repair a damaged Btrfs filesystem (requires --force
).
sudo btrfs check --repair /dev/sda1
btrfs scrub
btrfs scrub start
: Start a scrub operation to check for and correct data errors.
sudo btrfs scrub start /mnt/myfs
btrfs scrub status
: Check the status of a scrub operation.
sudo btrfs scrub status /mnt/myfs
btrfs scrub cancel
: Cancel a running scrub operation.
sudo btrfs scrub cancel /mnt/myfs
Creating a New Btrfs Filesystem
sudo mkfs.btrfs /dev/sda1
Mounting a Btrfs Filesystem
sudo mount /dev/sda1 /mnt/myfs
Adding a Device to a Btrfs Filesystem
sudo btrfs device add /dev/sdb1 /mnt/myfs
Creating a Snapshot
sudo btrfs subvolume snapshot /mnt/myfs/@ /mnt/myfs/@snapshot
Resizing a Filesystem
sudo btrfs filesystem resize +10G /mnt/myfs
Starting a Balance Operation
sudo btrfs balance start /mnt/myfs
Checking Filesystem Integrity
sudo btrfs check /dev/sda1
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
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.
The general syntax for smartctl
is:
sudo smartctl [OPTIONS] [DEVICE]
DEVICE
: The block device you want to monitor (e.g., /dev/sda
).-a
: Show all SMART information, including health status and attributes.
sudo smartctl -a /dev/sda
Output Example:
smartctl 7.2 2019-12-30 r5022 [x86_64-linux-5.4.0-54-generic] (local build)
Copyright (C) 2002-19, Bruce Allen, Christian Franke, www.smartmontools.org
=== START OF INFORMATION SECTION ===
Model Family: Samsung SSD 860
Device Model: Samsung SSD 860 EVO 500GB
Serial Number: S3Z5NB0M321547P
Firmware Version: RVT04B6Q
User Capacity: 500,107,862,016 bytes [500 GB]
Sector Size: 512 bytes logical/physical
Rotation Rate: Solid State Device
-t short
: Perform a short self-test (usually takes a few minutes).
sudo smartctl -t short /dev/sda
Output Example:
Short self-test routine recommended polling every 1 minute.
-t long
: Perform a long self-test (takes longer, typically an hour or more).
sudo smartctl -t long /dev/sda
Output Example:
Long self-test routine recommended polling every 1 minute.
-t offline
: Perform an offline self-test (non-destructive, can be done in the background).
sudo smartctl -t offline /dev/sda
-A
: Display detailed SMART attribute data.
sudo smartctl -A /dev/sda
Output Example:
SMART Attribute Data Structure revision number: 1
Vendor Specific SMART Attributes with Thresholds:
ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE
1 Raw_Read_Error_Rate 0x000f 100 100 006 Pre-fail Always - 0
5 Reallocated_Sector_Ct 0x0033 100 100 010 Pre-fail Always - 0
-s on
: Enable SMART monitoring.
sudo smartctl -s on /dev/sda
-s off
: Disable SMART monitoring.
sudo smartctl -s off /dev/sda
-i
: Display general information about the drive.
sudo smartctl -i /dev/sda
Output Example:
smartctl 7.2 2019-12-30 r5022 [x86_64-linux-5.4.0-54-generic] (local build)
Model Family: Samsung SSD 860
Device Model: Samsung SSD 860 EVO 500GB
Serial Number: S3Z5NB0M321547P
Firmware Version: RVT04B6Q
Check Overall SMART Status
sudo smartctl -a /dev/sda
Run a Short Self-Test
sudo smartctl -t short /dev/sda
Check the results after a few minutes:
sudo smartctl -a /dev/sda
Run a Long Self-Test
sudo smartctl -t long /dev/sda
Check the results after the test completes:
sudo smartctl -a /dev/sda
Enable SMART Monitoring
sudo smartctl -s on /dev/sda
Disable SMART Monitoring
sudo smartctl -s off /dev/sda
For more detailed information and options, refer to the smartctl
man page:
man smartctl
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:
First, you need to identify which sectors are bad. You can use the smartctl
utility or badblocks
for this purpose.
smartctl
Check SMART Attributes:
sudo smartctl -a /dev/sda
Look for attributes like Reallocated_Sector_Ct
or Current_Pending_Sector
. These indicate bad sectors.
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
badblocks
Run badblocks
to Check for Bad Sectors:
sudo badblocks -v /dev/sda
This will list all bad sectors found.
After identifying bad sectors, you can use e2fsck
(for ext2/ext3/ext4 filesystems) or fsck
(for other filesystems) to mark them as unusable.
Run e2fsck
to Check and Fix Filesystem:
sudo e2fsck -cf /dev/sda1
-c
: Check for bad sectors.-f
: Force check even if filesystem appears clean.Note: Replace /dev/sda1
with your partition.
Run fsck
with Bad Sector Checking:
sudo fsck -cf /dev/sda1
Similar options apply as with e2fsck
.
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.
hdparm
You can use hdparm
to check if the drive is using its spare sectors:
Check Drive Status:
sudo hdparm -I /dev/sda | grep 'Reallocated Sector Count'
This command will show if the drive has reallocated sectors.
Since bad sectors are often a sign of impending drive failure, it’s crucial to:
Backup Important Data: Regularly back up your data to avoid loss.
Consider Replacing the Drive: If bad sectors are frequent or the drive is older, consider replacing it with a new one.
Regularly monitor your drive’s health using tools like smartctl
to catch issues early.
Check for Bad Sectors:
sudo smartctl -a /dev/sda
sudo badblocks -v /dev/sda
Fix Filesystem and Mark Bad Sectors:
sudo e2fsck -cf /dev/sda1
Check Drive Status:
sudo hdparm -I /dev/sda | grep 'Reallocated Sector Count'
Backup and Replace Drive if Needed
Regularly back up your data and monitor the drive’s health to decide on replacement.
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.
Linux provides several utilities to work with tape drives. Below are some of the most commonly used commands:
mt
(Magnetic Tape) CommandThe mt
command is used for controlling tape drives.
Basic Syntax:
mt [OPTIONS] [COMMAND]
Common Commands:
Rewind the Tape
sudo mt -f /dev/st0 rewind
Forward the Tape
sudo mt -f /dev/st0 fsf N
Where N
is the number of files to skip.
Backspace (Rewind to Previous File)
sudo mt -f /dev/st0 bsf N
Where N
is the number of files to skip backwards.
Check the Status of the Tape
sudo mt -f /dev/st0 status
Eject the Tape
sudo mt -f /dev/st0 eject
tar
CommandThe tar
command is often used in conjunction with tape drives to create and extract backups.
Basic Syntax:
tar [OPTIONS] [ARCHIVE] [FILES]
Common Commands:
Create a Backup on Tape
tar -cvf /dev/st0 /path/to/files
-c
: Create a new archive-v
: Verbose output (lists files being archived)-f
: Specifies the file (in this case, the tape device)Extract Files from Tape
tar -xvf /dev/st0
-x
: Extract files from the archiveList Files on Tape
tar -tvf /dev/st0
-t
: List contents of the archivedd
CommandThe dd
command can be used for low-level copying to and from tape devices.
Basic Syntax:
dd if=[SOURCE] of=[DESTINATION] [OPTIONS]
Common Commands:
Backup a Filesystem to Tape
dd if=/dev/sda1 of=/dev/st0 bs=64K
if
: Input file (or device)of
: Output file (or device)bs
: Block sizeRestore from Tape
dd if=/dev/st0 of=/dev/sda1 bs=64K
file
CommandThe file
command can be used to check the type of data on a tape device.
Basic Syntax:
file [FILE]
Common Command:
Identify Tape Archive Type
file /dev/st0
Use dmesg
to check if the system recognizes the tape drive:
dmesg | grep st0
/dev/
(e.g., /dev/st0
).sudo
or adjust device permissions.