Copying hard drive partitions using GNU / Linux: how to get around a bootable USB flash drive where Acronis was needed before
- From the sandbox
- Tutorial
Since the good news came to me about the existence of an operating system called GNU / Linux, which is used by all real programmers, I tried several times to replace her brainchild of Bill Gates and several times failed.
There were no gurus nearby, access to the Internet was severely limited, and therefore any non-trivial task became completely unsolvable in those days.
Then, when the unlimited Internet appeared, as a result of which it became much easier with getting software and knowledge, I set up a dual boot for myself and began to master Ubuntu.
The main difficulties in the process of moving were created primarily because of the need to quit familiar programs 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 evil corporation sometimes leaves much to be desired, but it can be used to solve the everyday tasks of the average user. About all kinds of Firefox, Opera and Lame I generally keep quiet.
But in some cases, it was necessary to restart the computer, select the Microsoft Windows item in the bootloader menu and do everything the old fashioned way. Such a task was, for example, removing the image of a logical drive in order to transfer it to another physical medium, because the current one has already become pretty frayed.
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 it, and the result usually needed an edge in half an hour, so the clarification was delayed, delayed, and delayed.
But after another friendly joke about nobody who needs and knows nothing, and therefore free Linux, I decided that the next time I would not need anything other than a bootable USB flash drive and command line to create a disk image. And went to google.
Google created a clear impression that every dog knows how to clone disks using open source OS tools. Alas, clear instructions for the implementation of well-known and elementary things are usually the most difficult to find. As a result of persistent searches, some manuals eventually came to light, but still it was a little tough for them, because they were clearly written by competent people who were busy with the topic for not the first or even the second day, and simply do not remember that the knowledge respected by them for the necessary minimum, far from everyone owns it.
Now that the horrors of googling are behind, I understand that everything is really quite simple and I want to present the results of my research in the form of a short guide to working with disk images in GNU / Linux, addressed primarily to those who are used to Akronis or another similar to him the program. Such a document would help me a lot in due time, I hope that it will help someone else.
In Akronis, everything was simple and clear: the window interface, prompts, and previous experience working with CD images made the process simple. He took off the image, inserted a new screw, turned the image on it - there’s nothing to even talk about. In the case of Linux, the process choked at the stage of removing the image - how to do it was absolutely unclear. There was no special program with hints, a program that added a virtual DVD to the list of devices - like Daemon Tools too. Yes, and the list of devices turned out to be quite problematic, so you can start right from it.
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 indicating the device number. That is, the hard drive connected to the first channel will correspond to the sda file, the second sdb, and so on. Accordingly, the easiest way to see the list of devices is ls -1 / dev / sd [az] . This command will show a list of drives, but will not give us any information about them. And we need to at least see the list of sections.
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 an 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 with the command ls -1 / dev / sda [0-9]; a complete list of partitions on all devices is obtained at the output of the command ls -1 / dev / sd [az] [0-9] , and if necessary If you include the devices themselves in the list, you can simply write ls -1 / dev / sd * , which is much less meaningful, but it works short and important. Or, as suggested in the comments, there is another option cat / proc / partitions
Often the owner can identify the drive simply by this list (this is when he does not have 452 drives), but if this is not enough, then you can use the hdparm program .
Specifically hdparm -I / dev / sda will display detailed information about the device / dev / sda.
The same information, but in the form scattered by different files, can be found in the / sys / block / sda directory . For example, the disk model is in the file / dev / block / sda / device / model .
If this is not enough, it remains only to take turns mounting the devices and see what is recorded on them. But this is a completely different story.
So, the device is recognized and the section from which you want to remove the image is found. Now the actual process.
We usually call a disk image a disk copy at the partition or device level. And if a Windows user, in response to a question about how to make this copy, usually hears a recommendation to use specialized software, then in Linux everything is different.
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.
As you probably already guessed, you can achieve what you want by not one or even two, witty and not very ways, but it’s more correct and easiest in my opinion to use the ddrescue utility . What is essentially important is the GNU ddrescue , not the outdated original.
She, like cp, is engaged in copying files, but if errors are found, ddrescue will not interrupt the work with a joyful message that the patient is more likely dead than alive, but will remember the place with the error in order to return to it later and try to read it again . Now slowly and gently.
Used by ddrescue as follows:
ddrescue <keys> <file to be copied> <new file to which we copy old> <log file>
ddrescue is not able to receive input from the pipeline, or transmit the result of work to the pipeline, which is not surprising, however. The ability to skip places with errors at the first iteration and returning to them in subsequent cycles implies the presence of files at the output and input.
A command that performs an 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 tacitly mean that the hard drive from which we are making a copy is more or less healthy and tricks do not throw out. If there are serious problems with the disk, then it is better to familiarize yourself with the details of using ddrescue, for example here . and apply this knowledge when taking an image.
Often you can find recommendations to use a program called dd to copy a device or section file. 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 semantical approach is correct (one file will be copied to another), the consequences can be very sad. Yes, dd is superior to cp in that in the event that an error is detected, cp will stop working and dd will not stop, but if there are bad or badly readable sectors on the disk, then dd will continue to try to read their contents until smoke comes out of the hard drive.
Yes, dd has a noerror argument, but when using it, copying may be performed with errors, attempts to restore which will not be made. ddrescue in a similar situation after the first pass will return to the missed places and try to read them in small pieces. And it will leave a log file, with the help of which it will be possible to continue attempts to subtract bad places in the future.
In short, use ddrescue. And if there is nothing besides dd, do not forget about the noerror argument.
The procedure described above can be used, for example, to save a disk image with a newly installed operating system for subsequent recovery. Yes, a real Linuxoid will not do this, but among users of the operating system with a different name, this is a fairly common practice. And for periodic thoughtful repair of a regularly breaking computer of some pretty girl, even penguin lovers can safely use it. And if there are more than a few girls, a purely individual quantity for each homemade product, then this method is simply irreplaceable. The main thing is not to confuse the names of the image files.
Since each disk partition is represented by a file, it can be assumed that there should be a regular way to connect image files to the file system. From a certain point of view, the way it is. Specifically, the mount utility is used for this , with which you can put the file tree contained in the image into any directory of your choice. This process is called mounting.
So, we already have the image taken from the C: drive of the Windows 7 operating system. We named the image win_c.img and want to see its contents in the previously created directory / mnt / win_c . To do this, enter the mount -o loop win_c.img / mnt / win_c command .
This is how you can make sure that the image that you are going to roll onto the next pink laptop’s hard drive is the image of that drive C: which is expected to be seen on this particular machine. Well, or you can simply copy files from it without which you can’t do without it and send this image into outer darkness. And on the laptop to put the last Ubuntu or Fedor.
But we can go deeper.
But mounting a disk image as a whole is not so simple. The terrible truth is that the kernel does not know how to mount files from an arbitrary place in the file system, and the -o loop argument of mount indicates that you first need to link the image to the virtual device file in the / dev directory, and then attach the contents of this devices to the file system.
Files of virtual devices are created in advance (at the stage of system boot) and have the names loop0, loop1, loop2 and so on in increasing 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 see the contents of an image in which there are several sections, this is not enough. The fact is that if you execute the losetup command for such a file, then the whole image will be associated with the device / dev / loop0. That is, this device will be the equivalent of / dev / sda, and we need the equivalents / dev / sda1 and / dev / sda2.
Owners of the latest versions of the losetup program (read gentushniki and archevodi) can run losetup with the argument --partscan , which will lead to the automatic creation of files in the / dev directory that correspond to the image sections. That is, / dev / loop0p1, / dev / loop0p2 and so on 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 partitions 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 ...
Automatically create partition files when running the losetup command
In fact, the kernel (specifically, the loop module ) has long been able to search the partition table in the image file and create the corresponding files, but this function is disabled by default.
It turns on if the module parameter loop max_part is not equal to 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 tightly compiled into the kernel and therefore you need to enter loop.max_part = 63 to set the parameter to the kernel command line and reboot the system.
And now about how to actually roll the image onto another disk. As before, you must be guided by the fact that disks and partitions are presented in the form of files. And if you had to create a copy of the device or partition file to take the image, then in order to roll this image back you need to carry out the 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's better to remember the tips outlined in the image removal guide. 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 into which we are restoring the image (in this case / dev / sda2) should be no less than the image file. If the partition is larger than this file, then there will be no problems during recovery, but the partition will have unallocated space. And you have to either come to terms with this fact, or increase the size of the file system to the size of the partition with some specialized software (increasing, however, is usually much easier and faster than decreasing).
A particularly nice feature of Acronis is the ability to not write pieces of a disk or partition that do not contain files to the image file. This allows you to shrink the image to the real amount of data in the partition or on the disk.
The approach outlined above has fundamental limitations that do not allow such a feature to be implemented - sector-by-sector copying programs do not know anything about the structure of file systems and even more than that, they don’t know what it really is - a file system.
However, there is a way out. True, for its use, the file system on which the image will be stored must support such a specific thing as sparse files .
A sparse file is a file in which chunks containing zeros are not written to disk. That is, if half of the file is filled with data, and the other half is zeros, only half of this file will be written to disk and even additional information about areas of the file filled with zeros. If the region with zeros is continuous and starts in the middle, then in reality the file on the disk will replace half of its theoretical volume.
In order to take advantage of this opportunity in order to compress the image, you need to write zeros to the free space before removing. It is not difficult to do this - just mount the file system in a directory and create a file with binary zeros in this directory whose size will be equal to the size of free space on the partition.
But to find out the amount of free space and transfer it to the program that creates the file, fortunately, it is not necessary, and you do not need to look for the program that creates the file with zeros. In the / dev directory there is a dimensionless zero file, which, as the name suggests, 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, which we actually needed.
About how to copy files using standard operating system tools, it has been written so many times that the guide could well be called “The Art of Copying Files in the GNU Environment,” but since the truth of repetition does not stutter, I will probably write again.
To create a file with binary zeros, you can run the command cp / dev / zero zerofile or cat / dev / zero> zerofile .
Most file utilities are aware of the existence of sparse files and are able to create them. The ddrescue mentioned earlier, in order for the image to be a sparse file, you need to pass the --sparse switch . Now the resulting file will take up as much space as it needs, and no more.
As you know, it’s scary to take many disks into your hands, what talk can there be about writing multi-gigabyte files there. If such a disk fell into your hands, you should first remove the image, and then make a sparse file from it. To do this, mount the resulting image, then fill the free space with zeros and make a copy from the image, which will already be a full sparse file. The original image can then be deleted.
Copying a file is handled perfectly by cp, which can make sparse files no worse than ddrescue. cp --sparse win_c.img win_c_sparse.img
Of course, these operations will require a lot of free space, but still it is much better than not at all.
And now in a concise form about what can be gleaned from this guide.
And that cannot be gleaned from this manual.
PS Just in case, I give a link to Slax - the distribution that I put on bootable flash drives. He knows how to copy himself into RAM and by default there is ddrescue.
There were no gurus nearby, access to the Internet was severely limited, and therefore any non-trivial task became completely unsolvable in those days.
Then, when the unlimited Internet appeared, as a result of which it became much easier with getting software and knowledge, I set up a dual boot for myself and began to master Ubuntu.
The main difficulties in the process of moving were created primarily because of the need to quit familiar programs 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 evil corporation sometimes leaves much to be desired, but it can be used to solve the everyday tasks of the average user. About all kinds of Firefox, Opera and Lame I generally keep quiet.
But in some cases, it was necessary to restart the computer, select the Microsoft Windows item in the bootloader menu and do everything the old fashioned way. Such a task was, for example, removing the image of a logical drive in order to transfer it to another physical medium, because the current one has already become pretty frayed.
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 it, and the result usually needed an edge in half an hour, so the clarification was delayed, delayed, and delayed.
But after another friendly joke about nobody who needs and knows nothing, and therefore free Linux, I decided that the next time I would not need anything other than a bootable USB flash drive and command line to create a disk image. And went to google.
Google created a clear impression that every dog knows how to clone disks using open source OS tools. Alas, clear instructions for the implementation of well-known and elementary things are usually the most difficult to find. As a result of persistent searches, some manuals eventually came to light, but still it was a little tough for them, because they were clearly written by competent people who were busy with the topic for not the first or even the second day, and simply do not remember that the knowledge respected by them for the necessary minimum, far from everyone owns it.
Now that the horrors of googling are behind, I understand that everything is really quite simple and I want to present the results of my research in the form of a short guide to working with disk images in GNU / Linux, addressed primarily to those who are used to Akronis or another similar to him the program. Such a document would help me a lot in due time, I hope that it will help someone else.
Acronis
In Akronis, everything was simple and clear: the window interface, prompts, and previous experience working with CD images made the process simple. He took off the image, inserted a new screw, turned the image on it - there’s nothing to even talk about. In the case of Linux, the process choked at the stage of removing the image - how to do it was absolutely unclear. There was no special program with hints, a program that added a virtual DVD to the list of devices - like Daemon Tools too. Yes, and the list of devices turned out to be quite problematic, so you can start right from it.
View Drive List
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 indicating the device number. That is, the hard drive connected to the first channel will correspond to the sda file, the second sdb, and so on. Accordingly, the easiest way to see the list of devices is ls -1 / dev / sd [az] . This command will show a list of drives, but will not give us any information about them. And we need to at least see the list of sections.
View a list of partitions on a 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 an 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 with the command ls -1 / dev / sda [0-9]; a complete list of partitions on all devices is obtained at the output of the command ls -1 / dev / sd [az] [0-9] , and if necessary If you include the devices themselves in the list, you can simply write ls -1 / dev / sd * , which is much less meaningful, but it works short and important. Or, as suggested in the comments, there is another option cat / proc / partitions
Often the owner can identify the drive simply by this list (this is when he does not have 452 drives), but if this is not enough, then you can use the hdparm program .
Specifically hdparm -I / dev / sda will display detailed information about the device / dev / sda.
The same information, but in the form scattered by different files, can be found in the / sys / block / sda directory . For example, the disk model is in the file / dev / block / sda / device / model .
If this is not enough, it remains only to take turns mounting the devices and see what is recorded on them. But this is a completely different story.
Image removal
So, the device is recognized and the section from which you want to remove the image is found. Now the actual process.
General recommendations
We usually call a disk image a disk copy at the partition or device level. And if a Windows user, in response to a question about how to make this copy, usually hears a recommendation to use specialized software, then in Linux everything is different.
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 guessed, you can achieve what you want by not one or even two, witty and not very ways, but it’s more correct and easiest in my opinion to use the ddrescue utility . What is essentially important is the GNU ddrescue , not the outdated original.
She, like cp, is engaged in copying files, but if errors are found, ddrescue will not interrupt the work with a joyful message that the patient is more likely dead than alive, but will remember the place with the error in order to return to it later and try to read it again . Now slowly and gently.
Used by ddrescue as follows:
ddrescue <keys> <file to be copied> <new file to which we copy old> <log file>
ddrescue is not able to receive input from the pipeline, or transmit the result of work to the pipeline, which is not surprising, however. The ability to skip places with errors at the first iteration and returning to them in subsequent cycles implies the presence of files at the output and input.
A command that performs an 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 tacitly mean that the hard drive from which we are making a copy is more or less healthy and tricks do not throw out. If there are serious problems with the disk, then it is better to familiarize yourself with the details of using ddrescue, for example here . and apply this knowledge when taking an image.
Wrong way
Often you can find recommendations to use a program called dd to copy a device or section file. 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 semantical approach is correct (one file will be copied to another), the consequences can be very sad. Yes, dd is superior to cp in that in the event that an error is detected, cp will stop working and dd will not stop, but if there are bad or badly readable sectors on the disk, then dd will continue to try to read their contents until smoke comes out of the hard drive.
Yes, dd has a noerror argument, but when using it, copying may be performed with errors, attempts to restore which will not be made. ddrescue in a similar situation after the first pass will return to the missed places and try to read them in small pieces. And it will leave a log file, with the help of which it will be possible to continue attempts to subtract bad places in the future.
In short, use ddrescue. And if there is nothing besides dd, do not forget about the noerror argument.
The procedure described above can be used, for example, to save a disk image with a newly installed operating system for subsequent recovery. Yes, a real Linuxoid will not do this, but among users of the operating system with a different name, this is a fairly common practice. And for periodic thoughtful repair of a regularly breaking computer of some pretty girl, even penguin lovers can safely use it. And if there are more than a few girls, a purely individual quantity for each homemade product, then this method is simply irreplaceable. The main thing is not to confuse the names of the image files.
Viewing the contents of a partition image (logical drive)
Since each disk partition is represented by a file, it can be assumed that there should be a regular way to connect image files to the file system. From a certain point of view, the way it is. Specifically, the mount utility is used for this , with which you can put the file tree contained in the image into any directory of your choice. This process is called mounting.
So, we already have the image taken from the C: drive of the Windows 7 operating system. We named the image win_c.img and want to see its contents in the previously created directory / mnt / win_c . To do this, enter the mount -o loop win_c.img / mnt / win_c command .
This is how you can make sure that the image that you are going to roll onto the next pink laptop’s hard drive is the image of that drive C: which is expected to be seen on this particular machine. Well, or you can simply copy files from it without which you can’t do without it 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 a physical disk image
But mounting a disk image as a whole is not so simple. The terrible truth is that the kernel does not know how to mount files from an arbitrary place in the file system, and the -o loop argument of mount indicates that you first need to link the image to the virtual device file in the / dev directory, and then attach the contents of this devices to the file system.
Files of virtual devices are created in advance (at the stage of system boot) and have the names loop0, loop1, loop2 and so on in increasing 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 see the contents of an image in which there are several sections, this is not enough. The fact is that if you execute the losetup command for such a file, then the whole image will be associated with the device / dev / loop0. That is, this device will be the equivalent of / dev / sda, and we need the equivalents / dev / sda1 and / dev / sda2.
Owners of the latest versions of the losetup program (read gentushniki and archevodi) can run losetup with the argument --partscan , which will lead to the automatic creation of files in the / dev directory that correspond to the image sections. That is, / dev / loop0p1, / dev / loop0p2 and so on 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 partitions 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 ...
Automatically create partition files when running the losetup command
In fact, the kernel (specifically, the loop module ) has long been able to search the partition table in the image file and create the corresponding files, but this function is disabled by default.
It turns on if the module parameter loop max_part is not equal to 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 tightly compiled into the kernel and therefore you need to enter loop.max_part = 63 to set the parameter to the kernel command line and reboot the system.
Deploying an image to physical media
And now about how to actually roll the image onto another disk. As before, you must be guided by the fact that disks and partitions are presented in the form of files. And if you had to create a copy of the device or partition file to take the image, then in order to roll this image back you need to carry out the 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's better to remember the tips outlined in the image removal guide. 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 into which we are restoring the image (in this case / dev / sda2) should be no less than the image file. If the partition is larger than this file, then there will be no problems during recovery, but the partition will have unallocated space. And you have to either come to terms with this fact, or increase the size of the file system to the size of the partition with some specialized software (increasing, however, is usually much easier and faster than decreasing).
Compressing a partition image (logical drive)
A particularly nice feature of Acronis is the ability to not write pieces of a disk or partition that do not contain files to the image file. This allows you to shrink the image to the real amount of data in the partition or on the disk.
The approach outlined above has fundamental limitations that do not allow such a feature to be implemented - sector-by-sector copying programs do not know anything about the structure of file systems and even more than that, they don’t know what it really is - a file system.
However, there is a way out. True, for its use, 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 chunks containing zeros are not written to disk. That is, if half of the file is filled with data, and the other half is zeros, only half of this file will be written to disk and even additional information about areas of the file filled with zeros. If the region with zeros is continuous and starts in the middle, then in reality the 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 to the free space before removing. It is not difficult to do this - just mount the file system in a directory and create a file with binary zeros in this directory whose size will be equal to the size of free space on the partition.
But to find out the amount of free space and transfer it to the program that creates the file, fortunately, it is not necessary, and you do not need to look for the program that creates the file with zeros. In the / dev directory there is a dimensionless zero file, which, as the name suggests, 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, which we actually needed.
About how to copy files using standard operating system tools, it has been written so many times that the guide could well be called “The Art of Copying Files in the GNU Environment,” but since the truth of repetition does not stutter, I will 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 file utilities are aware of the existence of sparse files and are able to create them. The ddrescue mentioned earlier, in order for the image to be a sparse file, you need to pass the --sparse switch . Now the resulting file will take up as much space as it needs, and no more.
What to do when compressing a damaged disk image
As you know, it’s scary to take many disks into your hands, what talk can there be about writing multi-gigabyte files there. If such a disk fell into your hands, you should first remove the image, and then make a sparse file from it. To do this, mount the resulting image, then fill the free space with zeros and make a copy from the image, which will already be a full sparse file. The original image can then be deleted.
Copying a file is handled perfectly by cp, which can make sparse files no worse than ddrescue. cp --sparse win_c.img win_c_sparse.img
Of course, these operations will require a lot of free space, but still it is much better than not at all.
Conclusion
And now in a concise form about what can be gleaned from this guide.
- In Linux, each drive and each partition on the drive corresponds to a file.
- Since the image is a byte copy of the contents of the disk or partition, to create it, just make a copy of the corresponding file with any program that can make copies.
- Best of all, the GNU ddrescue program is suitable for making a copy of a file corresponding to a partition or drive.
- To restore the image, just copy it back.
- To view the contents of an image, just mount it in a directory of your choice, just as the system mounts the devices at boot time.
- Partition images and drive images are mounted completely differently and not quite in the same way as real drives and partitions are mounted.
- To get a compressed image of a partition, you need to specially copy a previously prepared partition to a file system that supports sparse files.
And that cannot be gleaned from this manual.
- There is a specialized software package called Clonezilla , as well as a distribution based on it, designed to backup and restore hard disk partitions.
- There are ntfsclone and partclone utilities that have knowledge about the file system design and use this knowledge, for example, so as not to write empty space in the backup.
PS Just in case, I give a link to Slax - the distribution that I put on bootable flash drives. He knows how to copy himself into RAM and by default there is ddrescue.