📜 ⬆️ ⬇️

Copying partitions on a hard disk using GNU / Linux: how to manage a bootable USB flash drive where Acronis was previously needed

Since the good news of the existence of an operating system called GNU / Linux, which is used by all real programmers, I have tried several times, I tried to replace the brainchild of Bill Gates and failed several times.

There were no gurus nearby, Internet access was tightly restricted and therefore any non-trivial task became completely unsolvable at that time.

Then, when the unlimited Internet appeared, as a result, with the receipt of software and knowledge, it became much easier, I set up a dual boot and began to master Ubuntu.
')
The main difficulties in the process of relocation were created primarily because of the need to drop familiar programs almost from childhood, which are simply not available in the operating system for real programmers.

In most cases this was not fatal. Instead of the usual foobar, you can always listen to music on Rhythmbox. Instead of Microsoft Office, there is OpenOffice. Yes, its compatibility with the product of the corporation of evil sometimes leaves much to be desired, but it is quite possible to use it for solving everyday problems of the average user. About any Firefox, Opera and Chrome, I generally keep quiet.

But in some cases it was necessary to reboot the computer, select the Microsoft Windows item in the bootloader menu and do everything in the old fashioned way. Such a task was for example the removal of the image of a logical disk in order to transfer to another physical medium, because the current one was pretty dumb.

The program that was used in such situations was Acronis. Intuition suggested that it would take at least a week to figure out how to do without him, and the result usually needed an edge in half an hour, so asking was postponed, postponed and postponed.

But after another friendly joke about nobody needed and not knowing anything, and therefore a free Linux, I decided that the next time I would need nothing more than a bootable USB flash drive and the command line to create a disk image. And he went to google.

Google created a clear impression that each dog knows how to clone disks using open source operating systems. Alas, clear instructions on the implementation of well-known and elementary things are usually the most difficult to find. Some manuals as a result of stubborn searches eventually came to light, but still they were tight, because they were clearly written by literate people who mess around with the topic not for the first or even the second day, and simply don’t remember that the knowledge that they honored for the required minimum, not everyone owns.

Now that the horrors of googling are over, I understand that everything is really quite simple and I want to formalize the results of my research in the form of a brief guide on working with disk images in GNU / Linux, addressed primarily to those who are used to Acronis or another the program. In due time such a document would help me very much, I hope that it will help someone else.

Acronis


In Acronis, everything was simple and clear: the window interface, hints and previous experience with images of CDs made the process simple. He took off the image, put in a new screw, unrolled the image on it - there was nothing even to tell about it. In the case of Linux, the process choked at the stage of removing the image - how to do this was absolutely unclear. There was no special program with prompts, a program that added a virtual DVD to the device list - like Daemon Tools too. Yes, and the list of devices turned out to be quite problematic, so you can start right from it.

View the list of drives


For each connected drive (hard drive, flash drive) in the / dev directory you can find the corresponding file. Its name consists of the letters sd and the letter designating the device number. That is, the hard drive connected to the first channel will match the sda ​​file, the second sdb, and so on. Accordingly, the easiest way to view the list of devices is ls -1 / dev / sd [az] . This command will show the list of drives, but will not give us any information about them. And we need to at least see the list of sections.

View the list of sections on the drive


Each section in the dev directory also has a file. Its name is built from the name of the file corresponding to the drive and the partition number. That is, for the sda ​​drive, on which there are 2 partitions, 2 files will be created in the / dev directory - sda1 and sda2. You can view the list of partitions on the sda ​​drive using the ls -1 / dev / sda [0-9] command; the full list of partitions on all devices is obtained at the output of the ls -1 / dev / sd [az] [0-9] command , and if necessary include the devices themselves in the list, then you can simply write ls -1 / dev / sd * , which is much less meaningful, but briefly and most importantly works. Or, as suggested in the comments, there is another option cat / proc / partitions

Often, the owner can identify the disk simply by the following list (this is when it does not have 452 disks), but if this is not enough, then you can use the hdparm program.

Specifically, hdparm -I / dev / sda will print detailed information about the device / dev / sda.

The same information, but in a form scattered across different files, can be found in the / sys / block / sda directory. For example, the disk model is in the / dev / block / sda / device / model file.

If this is not enough, then it remains only to mount the devices in turn and watch what is recorded on them. But that's another story.

Image removal


So, the device is recognized and the section from which you want to remove the image is found. Now the process itself.

General recommendations


A disk image is what we usually call a disk copy at the partition level or the entire device. And if the Windows user in response to the question of how to make this copy, usually hears a recommendation to use a specialized software, then in Linux this is not the case.

As I said, here each disk and each disk partition is represented by a specific file. And, since the image is a file with a copy of the disk, it is logical to assume that the operation of removing the image and the operation of copying the partition file are one and the same.

The way it is. Suppose we need to remove the image of the / dev / sda2 partition (on it, Windows 7 usually holds the C drive :) and save it to a file called win_c.img.

To do this, just write cp / dev / sda2 win_c.img . Or cat / dev / sda2> win_c.img . In short, you can use any program or combination of programs that copies files.

The right way


As you probably already guess, you can achieve the desired not in one or even in two, witty and not so ways, but in my opinion it is more correct and easiest to use the ddrescue utility. What is essentially important is referring to GNU ddrescue , and not long ago the outdated original.

She, like cp, is copying files, but if errors are found, ddrescue will not stop working with the joyful message that the patient is more dead than alive, and will remember the place with an error to return to it later and try to read again . Now slowly and gently.

Ddrescue is used as follows:
ddrescue <keys> <file which is copied> <new file, into which we copy old> <log file>

ddrescue is not able to receive input data from the pipeline, or transmit the result of the work to the conveyor, which, however, is not surprising. The ability to skip places with errors during the first iteration and return to them in subsequent cycles implies the presence of files on the output and input.

The command that performs the operation similar to that described above will look like this:

ddrescue / dev / sda2 win_c.img win_c.img.log

By the way, at the moment I silently mean that the hard disk from which we remove the copy is more or less intact and does not throw out the tricks. If there are serious problems with the disk, it is better to get acquainted with the details of using ddrescue, for example, here . and apply this knowledge when removing the image.

Wrong way


You can often find recommendations to use a program called dd to copy a device file or partition. As a rule, to remove the image it is proposed to do something like this:

dd if = / dev / sda2 of = win_c.img

Do not do this! Although the semantic approach is correct (copying one file to another will occur), the consequences can be very sad. Yes, dd exceeds cp in the sense that if an error is detected, cp stops working and dd does not stop, but if there are broken or badly readable sectors on the disk, then dd will continue to try to read their contents until the hard drive starts to smoke.

Yes, dd has the argument noerror , but when using it, copying can be performed with errors, no attempt to restore them will be made. ddrescue in a similar situation after the first pass will return to the missing places and try to read them in small pieces. And leave a log file with which you can continue to try to subtract bad places in the future.

In short, use ddrescue. And if there is nothing except dd, do not forget about the argument noerror.

The procedure described above can be used, for example, to save a disk image with a newly installed operating system for later recovery. Yes, a real Linux user will not do that, but this is a fairly common practice among users of the operating system with a different name. And for periodic thoughtful repair of a regularly breaking computer of some kind of pretty girl, even penguin lovers can safely use it. And if there are more girls than some, strictly individual for each self-made quantity, then this method is simply irreplaceable. The main thing is not to confuse the names of files with images.

View the contents of the partition image (logical drive)


Since each disk partition is represented by a file, it can be assumed that there should be a regular way of connecting image files to the file system. From a certain point of view, it is. Specifically, the mount utility is used for this, with which you can place the tree of files contained in the image in any directory of your choice. This process is called mount.

So, we already have an image taken from the C: drive of the Windows 7 operating system. We called the image win_c.img and want to see its contents in the previously created directory / mnt / win_c . To do this, enter the command mount -o loop win_c.img / mnt / win_c .

This is how you can make sure that the image that you are going to roll onto the hard drive of the next pink laptop is the image of that C: drive that is expected to be seen on this particular machine. Well, or you can simply copy files from it without which you can’t do anything and send this image into outer darkness. And on the laptop to put the last Ubuntu or Fedor.

But we can go deeper.

View the contents of the physical disk image


But with the mounting of the disk image is not so simple. The terrible truth is that the kernel does not know how to mount files from an arbitrary location in the file system and the argument -o loop, the mount command, signals that you first need to associate the image with the virtual device file in the / dev directory, and then attach the contents of this devices to the file system.

Virtual device files are created in advance (at the system boot stage) and have the names loop0, loop1, loop2, and so on in ascending order.

You can associate a partition image with one of these files with the losetup command. The mount command from the previous section is actually equivalent to the following two commands.

losetup / dev / loop0 win_c.img
mount / dev / loop0 / mnt / win_c

But in order to view the contents of the image in which there are several sections, this is not enough. The fact is that if you run the losetup command for such a file, then the entire image will be associated with the / dev / loop0 device. That is, this device will be equivalent to / dev / sda, and we need equivalents of / dev / sda1 and / dev / sda2.

Holders of the latest versions of losetup (read gents and archwingers) can execute losetup with the --partscan argument, which will automatically create files in the / dev directory corresponding to the image sections. That is / dev / loop0p1, / ​​dev / loop0p2 and so on up to the horizon. And now these files can be given to the mount command.

losetup --partscan / dev / loop0 drive.img
mount / dev / loop0p2 / mnt / win_c

Those who are not so lucky with the distribution can use the kpartx program, which will do the same, but put the files corresponding to the sections not in the / dev directory, but in the / dev / mapper directory, from where they can be mounted and viewed.

kpartx -a / dev / loop0 drive.img
mount / dev / mapper / loop0p2 / mnt / win_c

But we can go even deeper than that ...

Automatic creation of partition files when running the losetup command


In fact, the kernel (and specifically the loop module) has been able to look for a partition table in the image file for a long time and create the corresponding files, but this feature is disabled by default.

It is turned on if the parameter of the loop max_part module is not zero. This parameter can only be set when the module is loaded, so if the system is already running, the module must be unloaded from memory and loaded again with the parameter already set. To do this, run the following two commands.

modprobe -r loop
modprobe loop max_part = 63

However, in some distributions (for example, in Ubunt), the loop module is compiled tightly into the kernel and therefore, to set the parameter, loop.max_part = 63 will have to be entered into the kernel command line and the system will be rebooted.

Deploy image to physical media


And now about how to actually roll the image to another disk. As before, we must be guided by the fact that the disks and partitions are presented in the form of files. And if for removing the image it was necessary to create a copy of the device file or partition, then in order to roll up this image back, you must perform a copy operation in the opposite direction. That is, instead of cp / dev / sda2 win_c.img write cp win_c.img / dev / sda2 . Well, it is better to remember the advice outlined in the guide to removing the image. Namely, use ddrescue and not use dd.

ddrescue --force win_c.img / dev / sda2 win_c_restore.img.log
Of course, it should be remembered that the partition in which we restore the image (in this case / dev / sda2) must be no less than the file with the image. If the partition is larger than this file, then there will be no problems during recovery, but the partition will have unpartitioned space. And you have to either come to terms with this fact, or increase the size of the file system to the size of a partition with some specialized software (it is usually much easier and faster to increase the size than to reduce it).

Compressing a partition image (logical drive)


A particularly nice feature of Acronis is the ability not to write disk or partition chunks that do not contain files to an image file. This allows you to shrink the image to the actual amount of data in a partition or on disk.

The approach outlined earlier has fundamental limitations that prevent such a feature from being implemented - sector-based copying programs do not know anything about the structure of file systems, and even more, they don’t know what it is - a file system.

However, there is a way out. True, to use it, the file system on which the image will be stored must support such a specific thing as sparse files .

Sparse files


A sparse file is a file in which pieces containing zeroes are not written to disk. That is, if half of the file is filled with data, and the other half with zeros, only half of this file will be written to the disk and additional information about areas of the file filled with zeros will be recorded. If the area with zeros is continuous and starts in the middle, then the actual file on the disk will replace half of its theoretical volume.

Preparing a partition or device for compression


In order to take advantage of this opportunity in order to compress the image, you need to write zeros in the free space before removing. It is easy to do this - just mount the file system to a directory and create a binary zero file in this directory whose size will be equal to the size of free space on the partition.

But, fortunately, it is not necessary to find out the amount of free space and transfer it to the program that creates the file, and you will not need to look for the program that creates the file with zeros. There is a dimensionless zero file in the / dev directory, which, as the name implies, is an inexhaustible source of binary zeros. It remains only to copy it to the right place.

Since the file is dimensionless, copying will continue until the copy fills all the space available to it, that is, all the free space in the section that we actually needed.

About how to copy files using standard operating system tools, it’s written here so many times that the manual could well be called “The Art of Copying Files in the GNU Environment”, but since the truth does not get stuck from repetition, I’ll probably write again.

To create a file with binary zeros, you can run the command cp / dev / zero zerofile or cat / dev / zero> zerofile .

Creating a sparse device image file or partition


Most utilities for working with files are aware of the existence of sparse files and are able to create them. The ddrescue mentioned earlier, in order for the image to turn out to be a sparse file, you need to pass the key --sparse . Now the resulting file will take up as much space as it needs, and no more.

How to be in the case of compression of the image of a damaged disk


As you know, it’s scary to take many disks and take it into your hands, what can be the conversation about recording multi-gigabyte files there. If you just got such a disc in your hands, you must first remove the image, and then make a sparse file out of it. To do this, you need to mount the resulting image, then fill the free space with zeros and make a copy from the image, which will already be a full-fledged sparse file. The original image can then be deleted.

Copying a file can be done perfectly by cp, which can make sparse files as good as ddrescue. cp --sparse win_c.img win_c_sparse.img

Of course, for these operations a lot of free space will be required, but it is still much better than nothing at all.

Conclusion


And now in a concise form about what can be learned from this guide.

  1. In Linux, each drive and each partition on the drive corresponds to a file.
  2. Since an image is a byte copy of the contents of a disk or partition, to create it, it is enough to make a copy of the corresponding file with any program that can make copies.
  3. The GNU ddrescue program is best suited for making a copy of the file corresponding to the partition or drive.
  4. To restore the image just copy it back.
  5. To view the contents of an image, it is enough to mount it in a directory of your choice, just as the system mounts devices at boot.
  6. Partition images and drive images are completely mounted differently and not quite in the same way as real drives and partitions are mounted.
  7. To obtain a compressed image of a partition, you must specifically copy the previously prepared partition into a file system that supports sparse files.

And the fact that this guide can not be gleaned.

  1. There is a specialized software package called Clonezilla , as well as a distribution based on it, designed to back up and restore partitions on a hard disk.
  2. There are ntfsclone and partclone utilities that have knowledge of the structure of file systems and use this knowledge, for example, in order not to write empty space to backup.

PS Just in case I give a link to Slax - a distribution kit, which I put on bootable flash drives. He can copy himself into RAM and there is a ddrescue by default.

Source: https://habr.com/ru/post/233961/


All Articles