Computer lessons

Fsck disk check. Linux: disk check

Any computer is a complex device that consists of many components and no one is immune from failures of any of them. In this article, we will look at how to promptly recognize one of the serious problems with information storage devices, be it a hard drive or a flash drive, and how the disk is checked for bad sectors in Linux.

Any drive consists of many small blocks (sectors) that store information in the form of zeros or ones (bits). If, for some reason, the operating system cannot write a bit of information to a certain sector, then it can be considered “broken”.

A sector can become damaged for various reasons:

  • Manufacturing defects
  • Turn off the computer's power while recording information.
  • Physical wear and tear of the drive.

A small number of bad sectors are found on almost any drive. But it is worth paying attention if their number increases over time. This may indicate the imminent physical death of the drive and it’s time for you to think about replacing it.

Let's look at which Linux utilities we can use to check a disk for bad Linux sectors.

Checking the drive for bad sectors using badblocks.

Badblocks is a standard Linux utility for checking for bad sectors. It is installed by default in almost any distribution and can be used to check both a hard drive and an external drive.

First, let's look at what drives are connected to our system and what partitions they have. To do this, we need another standard Linux utility - fdisk.

Naturally, you need to execute commands with superuser rights:

Parameter -l we tell fdisk that we need to show the list of partitions and exit.

Now that we know what partitions we have, we can check them for bad sectors. To do this we will use the badblocks utility as follows:

$ sudo badblocks -v /dev/sda1 > badsectors.txt

To check, we specify the following parameters:

  • -v— detailed output of information about the test results.
  • /dev/sda1- the section that we want to check for bad sectors.
  • > badsectors.txt— output the result of the command to the badsectors.txt file.

If, as a result, bad sectors were found, then we need to instruct the operating system not to write information to them in the future. To do this, we need Linux utilities for working with file systems:

  • e2fsck. If we fix a partition with Linux file systems (ext2,ext3,ext4).
  • fsck. If we patch a file system other than ext.

Enter the following commands:

$ sudo e2fsck -l badsectors.txt /dev/sda1

Or, if our file system is not ext:

$ sudo fsck -l badsectors.txt /dev/sda1

Parameter -l we tell the utility to use the list of bad sectors from the file badsectors.txt, which we obtained earlier when checking with the badblocks utility.

Checking a drive for bad sectors in Linux in smartmontools

Now let's look at a more modern and reliable way to check a disk for bad linux sectors. Modern ATA/SATA, SCSI/SAS, SSD drives have a built-in self-monitoring system S.M.A.R.T (Self-Monitoring, Analysis and Reporting Technology, Self-monitoring, analysis and reporting technology), which monitors drive parameters and will help determine the deterioration of drive parameters in the early stages. To work with S.M.A.R.T in Linux there is a smartmontools utility.

Let's install it first. If your distribution is based on Debian\Ubuntu, then enter:

$ sudo apt install smartmontools

If you have a distribution based on RHEL\CentOS, then enter:

$ sudo yum install smartmontools

From time to time you need to check your hard drive. I believe that there is nothing more valuable than information on a hard drive, well, of course, not counting our lives, and it will be oh, what a shame when your family photos, videos, necessary abstracts and work reports, passwords and any other important data disappear. How to check a hard drive in Linux, and in our case in Ubuntu, and what programs exist for testing our helpers and saviors - hard drives? You should check the hard drive not from the system installed on it, but from LiveCD/USB. One such valuable build would be Parted Magic, although this can also be done from an Ubuntu CD/USB. This is a complete ammunition kit for working with hard drives. Here you have GParted, for resizing HDD partitions (analogous to Acronis Disc Director), and CloneZilla, for creating exact copies of your system disks or partitions with subsequent recovery, and GSmartControl - for reporting on the status of your disk and much more. So let's begin the review of programs for checking the hard drive in Ubuntu.

Console program Badblocks.

To find out how your hard drive or drives are partitioned and select a partition to check, run the command:

sudo fdisk -l

To start scanning for bad sectors, just run the command in the Terminal:

sudo badblocks -sv /dev/sdb1

Where:

/dev/sdb1- this is the section being checked,

-s— will display information about scanning as a percentage, the presence or absence of “bad” sectors, etc.,

-v— will display detailed information about the check.

If you need to get a text report, then you need to run the following command:

sudo badblocks -s /dev/sdb1 > errors.txt

Instead of /dev/sdb1 you must indicate the desired partition of your hard drive, and a text file will appear in your Home directory errors.txt with a report. If there are still bad things, then it is advisable to mark them so that the system does not access them while working with the disk. To mark bad sectors, run the command:

sudo e2fsck -l errors.txt /dev/sdb1

Key -l allows the program to use the errors.txt file to work with bad sectors. But you can avoid the above two commands and run just one:

sudo e2fsck -ct /dev/sdb1

The e2fsck program is part of the E2fsprogs software package, which includes badblock, and the key -c makes it possible to use the badblock utility to search for bad sectors.

To check the file system (ext 2/ext 3/ext 4) run the following command:

e2fsck -y /dev/partition of your disk or the entire disk

Key -y tells the utility to answer all questions positively.

Other commonly used options:

-p,-a automatically “repair” the file system without any questions asked.
-f forced (forced) check. The check will happen in any case, even if the file system did not need it.
-c launches the badblocks program to find and mark “bad” sectors on the disk;
-v Detailed information about the verification will be displayed.

Although fsck can be used instead of e2fsck. But everyone is free to choose what is better or more convenient for him.

Disks program.

Ubuntu has a great program Discs, which provides information on all connected devices in the system (hard drives, flash drives, CD/DVD drives, etc.) By running it, you can find out the S.M.A.R.T. data. on the disk of interest.

Program GSmartControl

And finally, I want to recommend the program GSmartControl, which is a graphical shell (GUI) for the console program - smartctl. You can find it in the Ubuntu Application Center, or install it through the Terminal with the command:

sudo apt-get install gsmartcontrol

The program shows complete information on S.M.A.R.T. data. You can find out more about each item .

Well, you’ve learned how to check a hard drive in Linux. May this information serve you well! Good luck!

A computer is a device whose operation is based on the interaction of many components. Over time, they can cause malfunctions. One of the common reasons for the machine to not work properly is bad sectors on the disk, so it needs to be tested periodically. Linux provides all the possibilities for this.

What are broken blocks and why do they appear?

A block (sector) is a small disk cell on which information is stored in the form of bits (0 and 1). When the system fails to write the next bit into a cell, it is called a bad sector. There can be several reasons for the occurrence of such blocks:

  • manufacturing defects;
  • turning off the power while recording information;
  • physical wear of the disk.

Initially, almost all media contain violations. Over time, their number may increase, which indicates that the device will soon fail. In Linux, there are several ways to test a disk for errors.

Linux disk check

The Linux kernel runs several operating systems, including Ubuntu and Debian. The disk check procedure is universal and suitable for each of them. It’s worth considering that it’s time to test the media when the disk system is under heavy load, the speed of working with the media (writing/reading) has decreased significantly, or these procedures even cause errors.

Many people are familiar with the program on Windows – Victoria HDD. The developers took care of writing its analogues for Linux.

Badblocks

Badblocks is a disk utility available by default on Ubuntu and other Linux distributions. The program allows you to test both the hard drive and external drives.

Important! All terminal commands given in this article begin with the sudo parameter, since they require superuser rights to execute.

Before testing a disk in Linux, you should check which drives are connected to the system using the fdisk-l utility. It will also show the partitions available on them.

Now you can begin direct testing for bad sectors. The work of Badblocks is organized as follows:

badblocks -v /dev/sdk1 > bsector.txt

The following commands and operands are used in the entry:

  • -v – displays a detailed report on the scan performed; ·
  • /dev/sdk 1– section to be checked;
  • bsector.txt – writes the results to a text file.

If bad blocks are found when checking the disk, you need to run the fsck or e2fsck utility, depending on the file system used. They will limit the recording of information in non-working sectors. For ext2, ext3, or ext4 file systems, run the following command:

fsck -l bsector.txt /dev/sdk1

Otherwise:

fsck -l bsector.txt /dev/sdk1

The -l parameter tells the program that bad blocks are listed in the bsector.txt file, and these are the ones that need to be excluded.

GParted

The utility checks the Linux file system without resorting to a text interface.

The tool is not natively included in operating system distributions, so it must be installed by running the command:

apt-get install gparted

The main application window displays available drives. The fact that it’s time to test the carrier is clear from the exclamation mark located next to its name. The check is started by clicking on the “Check for errors” item in the “Section” submenu located on the panel at the top. The required disk is pre-selected. Once the scan is complete, the utility will display the result.

Checking HDDs and other storage devices with the GParted application is available for users of Ubuntu, FreeBSD, Centos, Debian and other distributions running on the Linux kernel.

Smartmontools

The tool allows you to test the file system with greater reliability. Modern hard drives have a built-in self-monitoring module S.M.A.R.T., which analyzes drive data and helps identify a malfunction at the initial stage. Smartmontools is designed to work with this module.

The installation is started via the terminal:

  • apt install smartmontools – for Ubuntu/Debian;
  • yum install smartmontools – for CentOS.

To view information about the status of the hard drive, enter the following line:

smartctl –H /dev/sdk1

Checking for errors takes varying amounts of time, depending on the disk size. Upon completion, the program will display a result about the presence of bad sectors or their absence.

The utility has other parameters: -a, --all, -x, --xall. For additional information, call up help:

Safecopy

When the need arises to test a hard drive in Linux, you should be prepared for any result.

The Safecopy application copies data from a damaged device to a working one. The source can be either hard drives or removable media. This tool ignores I/O errors, read errors, and bad blocks while continuing to work continuously. The execution speed is the maximum possible that the computer provides.

Comment! The utility is not intended to recover deleted files. It retrieves information stored in bad sectors.

To install Safecopy on Linux, enter the following line into the terminal:

The scan is started with the command:

safecopy /dev/sdk1 /home/files/

Here the first path denotes the damaged disk, the second - the directory where the files will be saved.

The program is able to create an image of the file system of an unstable storage device.

What to do if an error is detected in the Ubuntu system program

Installing new software or changing system settings may cause a "System program error detected" message. Many people ignore it because it does not affect their overall work.

The problem is usually encountered by users of Ubuntu version 16.04. In this case, there is no need to test the HDD, since the problem is most likely a software failure. The message notifies you of an unexpected termination of the program and prompts you to send a report to the developers. If you agree, a browser window will open where you need to fill out a 4-step form. This option causes difficulties and does not guarantee that the error will disappear.

The second method will help avoid the message from appearing only if it is called by the same program. To do this, at the next notification, you need to check the “Do not show again for this program” option.

The third method is to disable the Apport utility, which in Linux is responsible for collecting information and sending reports. This approach will completely eliminate pop-up windows with errors. It is possible to disable only the display of notifications, leaving the collection service running. To do this you need to do:

gsettings set com.ubuntu.update-notifier show-apport-crashes false

Data will continue to be collected in the /var/crash folder. They need to be cleaned periodically to prevent them from filling up disk space:

To completely disable Apport services, enter the following entry into the terminal:

gksu gedit /etc/default/apport

In the text that appears, the value of the enable field changes from 1 to 0. In the future, to enable the service again, the default settings are returned.

Conclusion

To prevent file loss, it is recommended to periodically test your hard drive and removable media. Linux offers several approaches to solving the problem. You can choose from a list of utilities that identify bad sectors and ensure the transfer of information to a normally functioning device.

If, during boot, the operating system reports that there are errors in the file system on one of the partitions, then you should immediately check the disks and correct the file system errors.

Any self-respecting user should not forget that periodically checking hard drives for bad sectors and checking disks for errors is an example of common sense.

Important! Launch and Execution FSCK on a mounted file system may cause data corruption, soUse this material at your own risk. The author is not responsible for any damage you may cause.

In order to protect yourself you need to:

  • Switch to Single user mode and unmount the file system
  • Boot your computer into recovery mode using the installation CD

So, you need to check the disks and fix file system errors, let's get started.

1) Single user mode

Change the initialization level and unmount the file system:

# init 1
# umount /home

Search for connected partitions:

# fsck /dev/sda1

2) Recovery mode from installation CD

Insert the installation CD into the drive and reboot the system:

Wait a while and after booting from the installation CD, run the command:

# linux rescue nomount

The NOMOUNT directive will disable mounting, so you can safely use FSCK.

After this, run FSCK on the partition with errors:

# fsck -yvf /dev/sda1

LVM (Logical Volume Manager)

The situation with LVM (Logical Volume Manager) sections are a little more complicated. For start FSCK For LVM partitions must first be found PV (Physical Volume), VG (Volume Group), LV (Logical Extension) and activate them, to do this, run the following commands in sequence:

# lvm pvscan
# lvm vgscan
# lvm lvchange -ay VolGroup00
# lvm lvscan

# fsck -yfv /dev/VolGroup00/LogVol00

Upon execution, FSCK will return the result in the form of a code, this code is a unique number representing the sum of the following values:

0 - No errors;
1 - Filesystem errors corrected;
2 - System should be rebooted;
4 - Filesystem errors left uncorrected;
8 - Operational error;
16 - Usage or syntax error;
32 - Fsck canceled by user request;
128 - Shared-library error.

Any computer is a complex device that consists of many components and no one is immune from failures of any of them. In this article, we will look at how to promptly recognize one of the serious problems with information storage devices, be it a hard drive or a flash drive, and how the disk is checked for bad sectors in Linux.

Any drive consists of many small blocks (sectors) that store information in the form of zeros or ones (bits). If, for some reason, the operating system cannot write a bit of information to a certain sector, then it can be considered “broken”.

A sector can become damaged for various reasons:

  • Manufacturing defects
  • Turn off the computer's power while recording information.
  • Physical wear and tear of the drive.

A small number of bad sectors are found on almost any drive. But it is worth paying attention if their number increases over time. This may indicate the imminent physical death of the drive and it’s time for you to think about replacing it.

Let's look at which Linux utilities we can use to check a disk for bad Linux sectors.

Checking the drive for bad sectors using badblocks.

Badblocks is a standard Linux utility for checking for bad sectors. It is installed by default in almost any distribution and can be used to check both a hard drive and an external drive.

First, let's look at what drives are connected to our system and what partitions they have. To do this, we need another standard Linux utility - fdisk.

Naturally, you need to execute commands with superuser rights:

Parameter -l we tell fdisk that we need to show the list of partitions and exit.

Now that we know what partitions we have, we can check them for bad sectors. To do this we will use the badblocks utility as follows:

sudo badblocks -v /dev/sda1 > badsectors.txt

To check, we specify the following parameters:

  • -v- detailed output of information about the test results.
  • /dev/sda1- the section that we want to check for bad sectors.
  • > badsectors.txt- output the result of the command to the badsectors.txt file.

If, as a result, bad sectors were found, then we need to instruct the operating system not to write information to them in the future. To do this, we need Linux utilities for working with file systems:

  • e2fsck. If we fix a partition with Linux file systems (ext2,ext3,ext4).
  • fsck. If we patch a file system other than ext.

Enter the following commands:

sudo e2fsck -l badsectors.txt /dev/sda1

Or, if our file system is not ext:

sudo fsck -l badsectors.txt /dev/sda1

Parameter -l we tell the utility to use the list of bad sectors from the file badsectors.txt, which we obtained earlier when checking with the badblocks utility.

Checking a drive for bad sectors in Linux in smartmontools

Now let's look at a more modern and reliable way to check a disk for bad linux sectors. Modern ATA/SATA, SCSI/SAS, SSD drives have a built-in self-monitoring system S.M.A.R.T (Self-Monitoring, Analysis and Reporting Technology, Self-monitoring, analysis and reporting technology), which monitors drive parameters and will help determine the deterioration of drive parameters in the early stages. To work with S.M.A.R.T in Linux there is a smartmontools utility.

Let's install it first. If your distribution is based on Debian\Ubuntu, then enter:

sudo apt install smartmontools

If you have a distribution based on RHEL\CentOS, then enter:

sudo yum install smartmontools

Now that we have installed smartmontools we can view the help page using the command:

Let's move on to working with the utility. Enter the following command with the parameter -H so that the utility shows us information about the status of the drive:

sudo smartctl -H /dev/sda1

As you can see, checking the disk for bad linux sectors is completed and the utility tells us that everything is fine with the drive!

Additionally, you can specify the following parameters -a or --all to get even more information about the drive, or -x And --xall to view information, including other drive parameters.

conclusions

In this article, we looked at ways to check drives for bad sectors under Linux in order to anticipate possible failures in time and not lose data.