Created
November 11, 2025 04:50
-
-
Save chapmanjacobd/dcb3b046c66538a1816a0fba514f9595 to your computer and use it in GitHub Desktop.
eantoranz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| I just thought this was interesting--so copying here. Not my words: | |
| In this article I (Edmundo Carmona) will describe an experience I had that began with the failure of some RAID5 disks at the Hospital of Pediatric Especialties, where I work. While I wouldn’t wish such an event on my worst enemy, it was something that made me learn about the power of knowledge—a deep knowledge, which is so important in the hacking culture. | |
| Are you in dire straights with your hard drive? | |
| We at Free Software Magazine had a major hardware crash. The good guys at DTI DATA performed hard drive recovery and saved our magazine! | |
| Friday, April 29, 2005 | |
| This article has downloads! | |
| A 5-disk (18GB each) RAID5 was mounted on a HP Netserver Rack Storage/12. Due to a power outage yesterday, it would no longer recognize the RAID. As a matter of fact, there were two more RAIDs on the rack that were recovered... but this one (holding about 60GB of data) just wouldn’t work. | |
| The IT manager decided to call in some “gurus” to try to get the data back on-line. I (the only GNU/Linux user at the IT department) thought that something could be done with GNU/Linux. My first thought was: “If I get images of the separate disks, maybe I can start a software RAID on GNU/Linux. All I need is enough disk space to handle all of the images”. I told my crazy (so far) idea to the IT manager and he decided to give it a try... but only once the gurus gave up. | |
| Monday, May 2, 2005 | |
| The gurus are still trying to get the data back on-line. | |
| Tuesday, May 3, 2005 | |
| The gurus are still trying to get the data back on-line. | |
| Wednesday, May 4, 2005 | |
| These guys are stubborn, aren’t they? | |
| Thursday, May 5, 2005 | |
| The IT manager called me late in the afternoon. I was given the chance to Save the Republic. One of the disks of the array had been removed. I put the disks on a computer as separate disks (no RAID), booted with Knoppix (the environment of the IT department is Windows based, apart for my desktop, which has the XP that came with the HP box and Mandriva, which is where the computer normally stays) and made the four images of the four disks left from the original five: | |
| # for i in a b c d; do dd if=/dev/sd$i of=image$i.dat bs=4k; done | |
| I got all the files in a single HD and left the office. | |
| Friday, May 6, 2005 | |
| I wanted to start a software RAID, fooling the kernel into thinking that the files where HDs. Just having the images was not enough to bring the RAID on-line. RAID5 has a number of options: algorithm (left/right parity, synchronous/asynchronous), chunk (strip) size, but most important: the order of the images in the RAID. I had to tell the kernel how the RAID controller had mounted them so it could replicate the RAID. | |
| I had already been given the hint that the chunks were 64KB long. By the end of the day, the software RAID idea hadn’t worked at all. I started thinking about rebuilding the data the “hard” way: Making a single image of the RAID from the separate images. | |
| Weekend, May 7 and May 8, 2005 | |
| I did some research during the weekend, plus a little study of the images. The images didn’t look encrypted at all. The first “chunk” of the four images looked like garbage, but one of the disks showed a Partition Table right on the second chunk and the other chunks appeared to have other kind of data: | |
| # fdisk -lu discoa1 | |
| You must set cylinders. | |
| You can do this from the extra functions menu. | |
| Disk discoa1: 0 MB, 0 bytes | |
| 255 heads, 63 sectors/track, 0 cylinders, total 0 sectors | |
| Units = sectors of 1 * 512 = 512 bytes | |
| Device Boot Start End Blocks Id System | |
| discoa1p1 63 142175249 71087593+ 7 HPFS/NTFS | |
| Partition 1 has different physical/logical endings: | |
| phys=(1023, 254, 63) logical=(8849, 254, 63) | |
| fdisk was complaining because it was a 64KB file, not the expected 72GB one (written in the partition table). I studied the images and noticed that the data chunks and the parity chunks were distinguishable from each other, and that they seemed to follow a plain RAID5 distribution and algorithm... I was hopeful. | |
| Disk 1 Disk 2 Disk 3 Disk 4 Disk 5 | |
| 1 2 3 4 P | |
| 5 6 7 P 8 | |
| 9 10 P 11 12 | |
| 13 P 14 15 16 | |
| P 17 18 19 20 | |
| 21 22 23 24 P | |
| 25 26 27 P 28 | |
| Table 1 - RAID5’s chunk disposition (in a 5-disk array) | |
| I made a java class that could rebuild the RAID content from the separate images (Had I used C/C++, I would still be coding!). It was all about placing the right chunk from the right disk (image of disk) at the right place of the final image. I was missing one image, but it could be calculated with the help of the parity chunks spread all over the disks (see Textbox 1). The class was no big deal: selecting the right chunks from the disks, and using XORs to calculate the missing chunks. I guess it took about three or four hours at most to code it. I was finally ready to give it a try. The problem I hit was that while testing the software RAID at home I had damaged the images. So, I have to wait until Monday to test the class with the images of the RAID. | |
| RAID stands for Redundant Array of Independent Disks. All it does is make a number of disks “look” like they are one to improve throughput or fault-tolerance. There are a number of ways to put them together. Some of them are: | |
| Mirroring: in this case, each disk has exactly the same content. Size of the array: the size of the smallest disk. Redundancy: There must be at least a disk working for the data to remain intact. | |
| Linear: one disk follows the other. The size of each disk doesn’t matter at all. Size of the array: the sum of the size of the separate disks. Redundancy: If you remove one disk, you will lose the information on that disk and potentially all of the data in the array. | |
| RAID5: The information is spread in all of the available devices in a manner different from linear. Size of the array: the size of the smallest disk multiplied by the number of the disks minus one. Redundancy: At most one disk can be removed/replaced from the array without data loss. Instead of having disks that follow each other, the information is written in “chunks” of data, one disk at a time (see Table 1). | |
| In Table 1, the numbers represent the order in which the chunks are written on the disks (in this example, it’s left parity, asynchronous). There is a parity chunk per every n – 1 chunks of data. That is done for redundancy. | |
| It works like this: parity is calculated by XORing the n – 1 chunks of data in a row. This logical operator has a very interesting property for redundancy. If you remove one of the data chunks and use the parity chunk instead for the XOR operation, you will get the missing chunk of data: | |
| a xor b xor c xor d xor e = p | |
| If you remove c, then: | |
| a xor b xor d xor e xor p = c | |
| What does this mean for the RAID? It means that if you remove a whole disk from the array, the RAID can still work... though with a little overhead to calculate the missing chunks. Furthermore, if you replace a missing disk with a new one, the data that was in the removed disk can be rewritten to the new disk. There will be no loss of data (provided that no more than a single disk is missing at any given moment). | |
| The process of making a RAID image wasn’t complicated. I started the Java class by telling it the conditions of the run: algorithm, images, order of the disks, chunk size, skipped chunks (remember there were 64KB of garbage at the beginning of every image), and output file. | |
| I started thinking about rebuilding the data the “hard” way: making a single image of the RAID from the separate images | |
| Monday, May 9 2005 | |
| I made some attempts at rebuilding the RAID content. Each try took roughly two or three hours. After a run, I had a RAID.dat file (about 72GB in size) that was the “supposed” image of a HD, just like doing: | |
| # dd if=/dev/hda of=ata.dat | |
| Please notice the lack** **of partition number in the input file (a raw HD block device). | |
| Then I had to use that image as a hard drive. First, I had to use fdisk to know the “partitioning” of the hard drive (it had no problem handling the file at all). At that point, just as I had thought, I discovered that the file was the image of a HD and I could see a partition starting from sector 63. I was more than happy! There were no complaints from fdisk this time. Unfortunately, I can’t give you console output from now on, because the files have already been erased. Instead, I’ll show the commands that were involved: | |
| # fdisk -lu RAID.dat | |
| Then mounting. How could I make the kernel think that this file was a hard drive? Well... it took me some more research to learn that losetup is used to link loop devices to files. It felt like the solution was at hand! I had to link the file to a loop device starting from byte 32256 (I had to skip the first 63 sectors 512 bytes each, according to fdisk): | |
| #losetup -o 32256 /dev/loop0 RAID.dat | |
| It linked, no problem! Then mounted: | |
| #mount -t ntfs /dev/loop0 /mnt/tmp | |
| There was no complaint when mounting. All of the pieces were fitting together after all. | |
| I just forgot to take into consideration one very important factor in the IT world: Murphy’s Law. The RAID was not going to give itself away so easily after all. | |
| When I ran ls, in the mount point, I could see a few of the directories, but the information wasn’t usable. I couldn’t cd to those directories and dmesg said there were problems with the NTFS indexes. I guessed I must have made a mistake ordering the disks... or used the wrong algorithm. I tried twice (with different options), but failed. | |
| Tuesday, May 10 2005 | |
| I had left another attempt working when I left the office. That one failed too. I was getting frustrated at the time. Three of the developers at the IT department offered their help and started analysing the whole thing with me. I made another class that rebuilt the missing image, which I felt would help us in the analysis—no matter what the algorithm, order or strip size, according to RAID theory, the missing image’s content would always be the same. | |
| We noticed that I had indeed made a mistake when ordering the disks! (Hey, I can’t always be right, can I?) We studied the images a little further to make sure, and started the whole thing again. It was already getting late, so we had to wait until the next morning to see the results. | |
| I just forgot to take into consideration one very important factor in the IT world: Murphy’s Law | |
| Wednesday, May 11 2005 | |
| First thing in the morning (and I didn’t sleep very well because of the wait), I did a ls and... | |
| Eureka! It worked. | |
| All of the directories were there (otherwise, I wouldn’t have written this article in the first place, right?). I tried to work with some of the files in the partition... and it was perfect. | |
| I suddenly became the spoiled kid of the IT department! I got a big chocolate cake—that’s what I call a bargain! | |
| Even better, the experience caused some of the guys from the IT department to install GNU/Linux on their own personal computers. That’s quite an achievement! | |
| Conclusion | |
| I want to finish saying that I did nothing miraculous... but definitely clever! I certainly used the resources I had at hand... plus Knoppix. I also got a lot of help from the GNU/Linux community (through www.linuxquestions.org mostly). Thank you people! | |
| It’s very important that you make sure backups be made on a regular basis to avoid this kind of situation. I don’t think it’s likely you will find yourself in the same situation we got ourselves into. But, if you do find yourself in the same boat, I hope this information allows you to not lose the data the Microsoft way (just format the disk, and forget about your data). Don’t freak out, get a Knoppix CD (if you can get a GNU/Linux guru along with it, all the better!), and with a little programming you will most likely solve the problem. | |
| Thanks | |
| I’d like to thank Simon Carreno, Heberto Ramos and Javier Machado, for their help in analysing the way the images of the RAID had to be put together. I’d also like to thank the IT crew as a whole for their support. | |
| License | |
| Verbatim copying and distribution of this entire article are permitted worldwide, without royalty, in any medium, provided this notice is preserved. | |
| https://www.linuxquestions.org/questions/linux-kernel-70/kernel-module-development-how-to-open-block-devices-and-use-them-open-seek-read-848766/page3.html | |
| GENERAL | |
| ------- | |
| This material I'm including in thsi jar is by no means a bullet proof project. It was intended to solve a | |
| problem we had with a Array of Disks that its HP controller didn't want to recognize. | |
| I'm doing this readme by memory, so there could be a mistake here or there. | |
| The class that does the recovery job is Raid5Redcovery | |
| That class will only get one argument: the name of a properties file having all the information | |
| necessary for a run. For example: | |
| chunk=65536 | |
| disks=6 | |
| 1=./disk1 | |
| 2=./disk2 | |
| 4=./disk4 | |
| 5=./disk5 | |
| 6=./disk6 | |
| algorithm=left asymmetric | |
| skip=2 | |
| In this example I just created: | |
| 6 disks. Image 3 is missing. Left Asymmetric algorithm and skip the first 2 chunks from all the images. | |
| Recognizing the order of the images and the algorithm. | |
| ----------------------------------------------------- | |
| Well... that took a little bit of over thinking to achieve it. | |
| First I saw a Partition Table on one of the first chunks of the images. I saw it because of the classical 55aa signature. | |
| When I used fdisk on that single chunk, I saw it really was a partition table: | |
| # fdisk -lu discoa1 | |
| You must set cylinders. | |
| You can do this from the extra functions menu. | |
| Disk discoa1: 0 MB, 0 bytes | |
| 255 heads, 63 sectors/track, 0 cylinders, total 0 sectors | |
| Units = sectors of 1 * 512 = 512 bytes | |
| Device Boot Start End Blocks Id System | |
| discoa1p1 63 142175249 71087593+ 7 HPFS/NTFS | |
| Partition 1 has different physical/logical endings: | |
| phys=(1023, 254, 63) logical=(8849, 254, 63) | |
| As that HAS to be the first sector of the disk, then that chunk would have to be the first chunk of the first image (in case | |
| of a left algorithm) or the second image (a right asymmetric algorithm. It can't be a right symmetric, because then the partition | |
| table would be on the SECOND chunk of the first image). | |
| The other thing was learning how to recognize parity chunks from data chunks. If you look at the chunks that I provide with this | |
| zip file, you will see that data chunks they have a structure present for the NTFS file system. | |
| a piece from discoc1: | |
| 000001F4 00 00 00 00 00 00 00 00 00 00 19 00 46 49 4C 45 2A 00 03 00 .......... ..FILE*... | |
| 00000208 EA 32 60 4E 04 00 00 00 02 00 02 00 30 00 03 00 58 02 00 00 .2`N...... ..0...X... | |
| I had 5 disks running in the array, right? if you look at the chunks, the ones carrying data have this | |
| FILE* thing exactly at the same position. If you have 4 chunks having FILE* exactly at the same position..... | |
| guess what a parity chunk will say in that position. ;-) You said 0s? Then you got it right. That's how we were | |
| able to see where the parity chunks were. That will set only two possible positions | |
| for the images (depending on right or left algorithm). | |
| If I had had an even number of disks in the array, it would have been a little more difficult to recognize the parity chunks | |
| because they would have the same FILE* thing in exactly the same position in the parity chunks... We would have been forced to | |
| analize the chunks with a little more attention (and having a missing chunk wouldn't have been of great help ;-))... | |
| but (thankfully) we didn't. | |
| That leaves you with three possible attempts to "getting it right". | |
| Doing a little more research of the chunks to see if you can discover the secuence of chunks with algorithm x, y or z, would | |
| lead to discovering which of the three is THE algorithm. | |
| Any other questions? email me: eantoranz at gmail dot com | |
| Cheers! | |
| GNU GENERAL PUBLIC LICENSE | |
| Version 2, June 1991 | |
| Copyright (C) 1989, 1991 Free Software Foundation, Inc. | |
| 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA | |
| Everyone is permitted to copy and distribute verbatim copies | |
| of this license document, but changing it is not allowed. | |
| Preamble | |
| The licenses for most software are designed to take away your | |
| freedom to share and change it. By contrast, the GNU General Public | |
| License is intended to guarantee your freedom to share and change free | |
| software--to make sure the software is free for all its users. This | |
| General Public License applies to most of the Free Software | |
| Foundation's software and to any other program whose authors commit to | |
| using it. (Some other Free Software Foundation software is covered by | |
| the GNU Library General Public License instead.) You can apply it to | |
| your programs, too. | |
| When we speak of free software, we are referring to freedom, not | |
| price. Our General Public Licenses are designed to make sure that you | |
| have the freedom to distribute copies of free software (and charge for | |
| this service if you wish), that you receive source code or can get it | |
| if you want it, that you can change the software or use pieces of it | |
| in new free programs; and that you know you can do these things. | |
| To protect your rights, we need to make restrictions that forbid | |
| anyone to deny you these rights or to ask you to surrender the rights. | |
| These restrictions translate to certain responsibilities for you if you | |
| distribute copies of the software, or if you modify it. | |
| For example, if you distribute copies of such a program, whether | |
| gratis or for a fee, you must give the recipients all the rights that | |
| you have. You must make sure that they, too, receive or can get the | |
| source code. And you must show them these terms so they know their | |
| rights. | |
| We protect your rights with two steps: (1) copyright the software, and | |
| (2) offer you this license which gives you legal permission to copy, | |
| distribute and/or modify the software. | |
| Also, for each author's protection and ours, we want to make certain | |
| that everyone understands that there is no warranty for this free | |
| software. If the software is modified by someone else and passed on, we | |
| want its recipients to know that what they have is not the original, so | |
| that any problems introduced by others will not reflect on the original | |
| authors' reputations. | |
| Finally, any free program is threatened constantly by software | |
| patents. We wish to avoid the danger that redistributors of a free | |
| program will individually obtain patent licenses, in effect making the | |
| program proprietary. To prevent this, we have made it clear that any | |
| patent must be licensed for everyone's free use or not licensed at all. | |
| The precise terms and conditions for copying, distribution and | |
| modification follow. | |
| GNU GENERAL PUBLIC LICENSE | |
| TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
| 0. This License applies to any program or other work which contains | |
| a notice placed by the copyright holder saying it may be distributed | |
| under the terms of this General Public License. The "Program", below, | |
| refers to any such program or work, and a "work based on the Program" | |
| means either the Program or any derivative work under copyright law: | |
| that is to say, a work containing the Program or a portion of it, | |
| either verbatim or with modifications and/or translated into another | |
| language. (Hereinafter, translation is included without limitation in | |
| the term "modification".) Each licensee is addressed as "you". | |
| Activities other than copying, distribution and modification are not | |
| covered by this License; they are outside its scope. The act of | |
| running the Program is not restricted, and the output from the Program | |
| is covered only if its contents constitute a work based on the | |
| Program (independent of having been made by running the Program). | |
| Whether that is true depends on what the Program does. | |
| 1. You may copy and distribute verbatim copies of the Program's | |
| source code as you receive it, in any medium, provided that you | |
| conspicuously and appropriately publish on each copy an appropriate | |
| copyright notice and disclaimer of warranty; keep intact all the | |
| notices that refer to this License and to the absence of any warranty; | |
| and give any other recipients of the Program a copy of this License | |
| along with the Program. | |
| You may charge a fee for the physical act of transferring a copy, and | |
| you may at your option offer warranty protection in exchange for a fee. | |
| 2. You may modify your copy or copies of the Program or any portion | |
| of it, thus forming a work based on the Program, and copy and | |
| distribute such modifications or work under the terms of Section 1 | |
| above, provided that you also meet all of these conditions: | |
| a) You must cause the modified files to carry prominent notices | |
| stating that you changed the files and the date of any change. | |
| b) You must cause any work that you distribute or publish, that in | |
| whole or in part contains or is derived from the Program or any | |
| part thereof, to be licensed as a whole at no charge to all third | |
| parties under the terms of this License. | |
| c) If the modified program normally reads commands interactively | |
| when run, you must cause it, when started running for such | |
| interactive use in the most ordinary way, to print or display an | |
| announcement including an appropriate copyright notice and a | |
| notice that there is no warranty (or else, saying that you provide | |
| a warranty) and that users may redistribute the program under | |
| these conditions, and telling the user how to view a copy of this | |
| License. (Exception: if the Program itself is interactive but | |
| does not normally print such an announcement, your work based on | |
| the Program is not required to print an announcement.) | |
| These requirements apply to the modified work as a whole. If | |
| identifiable sections of that work are not derived from the Program, | |
| and can be reasonably considered independent and separate works in | |
| themselves, then this License, and its terms, do not apply to those | |
| sections when you distribute them as separate works. But when you | |
| distribute the same sections as part of a whole which is a work based | |
| on the Program, the distribution of the whole must be on the terms of | |
| this License, whose permissions for other licensees extend to the | |
| entire whole, and thus to each and every part regardless of who wrote it. | |
| Thus, it is not the intent of this section to claim rights or contest | |
| your rights to work written entirely by you; rather, the intent is to | |
| exercise the right to control the distribution of derivative or | |
| collective works based on the Program. | |
| In addition, mere aggregation of another work not based on the Program | |
| with the Program (or with a work based on the Program) on a volume of | |
| a storage or distribution medium does not bring the other work under | |
| the scope of this License. | |
| 3. You may copy and distribute the Program (or a work based on it, | |
| under Section 2) in object code or executable form under the terms of | |
| Sections 1 and 2 above provided that you also do one of the following: | |
| a) Accompany it with the complete corresponding machine-readable | |
| source code, which must be distributed under the terms of Sections | |
| 1 and 2 above on a medium customarily used for software interchange; or, | |
| b) Accompany it with a written offer, valid for at least three | |
| years, to give any third party, for a charge no more than your | |
| cost of physically performing source distribution, a complete | |
| machine-readable copy of the corresponding source code, to be | |
| distributed under the terms of Sections 1 and 2 above on a medium | |
| customarily used for software interchange; or, | |
| c) Accompany it with the information you received as to the offer | |
| to distribute corresponding source code. (This alternative is | |
| allowed only for noncommercial distribution and only if you | |
| received the program in object code or executable form with such | |
| an offer, in accord with Subsection b above.) | |
| The source code for a work means the preferred form of the work for | |
| making modifications to it. For an executable work, complete source | |
| code means all the source code for all modules it contains, plus any | |
| associated interface definition files, plus the scripts used to | |
| control compilation and installation of the executable. However, as a | |
| special exception, the source code distributed need not include | |
| anything that is normally distributed (in either source or binary | |
| form) with the major components (compiler, kernel, and so on) of the | |
| operating system on which the executable runs, unless that component | |
| itself accompanies the executable. | |
| If distribution of executable or object code is made by offering | |
| access to copy from a designated place, then offering equivalent | |
| access to copy the source code from the same place counts as | |
| distribution of the source code, even though third parties are not | |
| compelled to copy the source along with the object code. | |
| 4. You may not copy, modify, sublicense, or distribute the Program | |
| except as expressly provided under this License. Any attempt | |
| otherwise to copy, modify, sublicense or distribute the Program is | |
| void, and will automatically terminate your rights under this License. | |
| However, parties who have received copies, or rights, from you under | |
| this License will not have their licenses terminated so long as such | |
| parties remain in full compliance. | |
| 5. You are not required to accept this License, since you have not | |
| signed it. However, nothing else grants you permission to modify or | |
| distribute the Program or its derivative works. These actions are | |
| prohibited by law if you do not accept this License. Therefore, by | |
| modifying or distributing the Program (or any work based on the | |
| Program), you indicate your acceptance of this License to do so, and | |
| all its terms and conditions for copying, distributing or modifying | |
| the Program or works based on it. | |
| 6. Each time you redistribute the Program (or any work based on the | |
| Program), the recipient automatically receives a license from the | |
| original licensor to copy, distribute or modify the Program subject to | |
| these terms and conditions. You may not impose any further | |
| restrictions on the recipients' exercise of the rights granted herein. | |
| You are not responsible for enforcing compliance by third parties to | |
| this License. | |
| 7. If, as a consequence of a court judgment or allegation of patent | |
| infringement or for any other reason (not limited to patent issues), | |
| conditions are imposed on you (whether by court order, agreement or | |
| otherwise) that contradict the conditions of this License, they do not | |
| excuse you from the conditions of this License. If you cannot | |
| distribute so as to satisfy simultaneously your obligations under this | |
| License and any other pertinent obligations, then as a consequence you | |
| may not distribute the Program at all. For example, if a patent | |
| license would not permit royalty-free redistribution of the Program by | |
| all those who receive copies directly or indirectly through you, then | |
| the only way you could satisfy both it and this License would be to | |
| refrain entirely from distribution of the Program. | |
| If any portion of this section is held invalid or unenforceable under | |
| any particular circumstance, the balance of the section is intended to | |
| apply and the section as a whole is intended to apply in other | |
| circumstances. | |
| It is not the purpose of this section to induce you to infringe any | |
| patents or other property right claims or to contest validity of any | |
| such claims; this section has the sole purpose of protecting the | |
| integrity of the free software distribution system, which is | |
| implemented by public license practices. Many people have made | |
| generous contributions to the wide range of software distributed | |
| through that system in reliance on consistent application of that | |
| system; it is up to the author/donor to decide if he or she is willing | |
| to distribute software through any other system and a licensee cannot | |
| impose that choice. | |
| This section is intended to make thoroughly clear what is believed to | |
| be a consequence of the rest of this License. | |
| 8. If the distribution and/or use of the Program is restricted in | |
| certain countries either by patents or by copyrighted interfaces, the | |
| original copyright holder who places the Program under this License | |
| may add an explicit geographical distribution limitation excluding | |
| those countries, so that distribution is permitted only in or among | |
| countries not thus excluded. In such case, this License incorporates | |
| the limitation as if written in the body of this License. | |
| 9. The Free Software Foundation may publish revised and/or new versions | |
| of the General Public License from time to time. Such new versions will | |
| be similar in spirit to the present version, but may differ in detail to | |
| address new problems or concerns. | |
| Each version is given a distinguishing version number. If the Program | |
| specifies a version number of this License which applies to it and "any | |
| later version", you have the option of following the terms and conditions | |
| either of that version or of any later version published by the Free | |
| Software Foundation. If the Program does not specify a version number of | |
| this License, you may choose any version ever published by the Free Software | |
| Foundation. | |
| 10. If you wish to incorporate parts of the Program into other free | |
| programs whose distribution conditions are different, write to the author | |
| to ask for permission. For software which is copyrighted by the Free | |
| Software Foundation, write to the Free Software Foundation; we sometimes | |
| make exceptions for this. Our decision will be guided by the two goals | |
| of preserving the free status of all derivatives of our free software and | |
| of promoting the sharing and reuse of software generally. | |
| NO WARRANTY | |
| 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY | |
| FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN | |
| OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES | |
| PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED | |
| OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
| MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS | |
| TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE | |
| PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, | |
| REPAIR OR CORRECTION. | |
| 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING | |
| WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR | |
| REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, | |
| INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING | |
| OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED | |
| TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY | |
| YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER | |
| PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE | |
| POSSIBILITY OF SUCH DAMAGES. | |
| END OF TERMS AND CONDITIONS | |
| How to Apply These Terms to Your New Programs | |
| If you develop a new program, and you want it to be of the greatest | |
| possible use to the public, the best way to achieve this is to make it | |
| free software which everyone can redistribute and change under these terms. | |
| To do so, attach the following notices to the program. It is safest | |
| to attach them to the start of each source file to most effectively | |
| convey the exclusion of warranty; and each file should have at least | |
| the "copyright" line and a pointer to where the full notice is found. | |
| <one line to give the program's name and a brief idea of what it does.> | |
| Copyright (C) <year> <name of author> | |
| This program is free software; you can redistribute it and/or modify | |
| it under the terms of the GNU General Public License as published by | |
| the Free Software Foundation; either version 2 of the License, or | |
| (at your option) any later version. | |
| This program is distributed in the hope that it will be useful, | |
| but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| GNU General Public License for more details. | |
| You should have received a copy of the GNU General Public License | |
| along with this program; if not, write to the Free Software | |
| Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA | |
| Also add information on how to contact you by electronic and paper mail. | |
| If the program is interactive, make it output a short notice like this | |
| when it starts in an interactive mode: | |
| Gnomovision version 69, Copyright (C) year name of author | |
| Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. | |
| This is free software, and you are welcome to redistribute it | |
| under certain conditions; type `show c' for details. | |
| The hypothetical commands `show w' and `show c' should show the appropriate | |
| parts of the General Public License. Of course, the commands you use may | |
| be called something other than `show w' and `show c'; they could even be | |
| mouse-clicks or menu items--whatever suits your program. | |
| You should also get your employer (if you work as a programmer) or your | |
| school, if any, to sign a "copyright disclaimer" for the program, if | |
| necessary. Here is a sample; alter the names: | |
| Yoyodyne, Inc., hereby disclaims all copyright interest in the program | |
| `Gnomovision' (which makes passes at compilers) written by James Hacker. | |
| <signature of Ty Coon>, 1 April 1989 | |
| Ty Coon, President of Vice | |
| This General Public License does not permit incorporating your program into | |
| proprietary programs. If your program is a subroutine library, you may | |
| consider it more useful to permit linking proprietary applications with the | |
| library. If this is what you want to do, use the GNU Library General | |
| Public License instead of this License. | |
| /* | |
| * Copyright 2005 Edmundo Carmona | |
| * | |
| * This file is part of the raid recovery project. | |
| * | |
| * This project is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License | |
| * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. | |
| * | |
| * This project is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | |
| * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | |
| * | |
| * You should have received a copy of the GNU General Public License along with this project; if not, write to the | |
| * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA | |
| * | |
| * Created on May 9, 2005 | |
| * | |
| */ | |
| package org.antix.raid; | |
| import java.io.FileInputStream; | |
| import java.io.FileOutputStream; | |
| import java.io.IOException; | |
| import java.util.Properties; | |
| /** | |
| * @author antoranz | |
| * | |
| */ | |
| public class Raid5Recovery { | |
| private final static int LEFT_ASYMMETRIC = 0; | |
| private final static int LEFT_SYMMETRIC = 1; | |
| private final static int RIGHT_SYMMETRIC = 2; | |
| private final static int RIGHT_ASYMMETRIC = 3; | |
| private int chunkSize; | |
| private int algorithm; | |
| private int disks; | |
| private FileInputStream[] inputs; | |
| private FileOutputStream output; | |
| private Properties properties; | |
| private Chunk[] chunks; | |
| private int skipChunks; | |
| private Raid5Recovery(int chunk, int disks, FileInputStream[] inputs, | |
| FileOutputStream output, int algorithm, int skipChunks) { | |
| chunkSize = chunk; | |
| this.disks = disks; | |
| this.inputs = inputs; | |
| this.output = output; | |
| this.algorithm = algorithm; | |
| this.skipChunks = skipChunks; | |
| } | |
| public static void main(String[] args) { | |
| Properties properties = new Properties(); | |
| try { | |
| properties.load(new FileInputStream(args[0])); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| System.err.println("Can't get properties due to " | |
| + e.getMessage()); | |
| System.exit(0); | |
| } | |
| int chunkSize = 0; | |
| try { | |
| chunkSize = Integer.parseInt(properties.getProperty("chunk")); | |
| System.out.println("Chunk size: " + chunkSize + " bytes"); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| System.err.println("Can't get chunk size due to " | |
| + e.getMessage()); | |
| System.exit(0); | |
| } | |
| // Hoy many disks are there? | |
| int disks = 0; | |
| int missingDisks = 0; | |
| try { | |
| disks = Integer.parseInt(properties.getProperty("disks")); | |
| System.out.println("Number of disks: " + disks); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| System.err.println("Can't get the number of disks due to " | |
| + e.getMessage()); | |
| System.exit(0); | |
| } | |
| if (disks < 3) { | |
| System.err.println("There can't be less than 3 disks"); | |
| System.exit(0); | |
| } | |
| FileInputStream[] readers = new FileInputStream[disks]; | |
| for (int i = 0; i < disks; i++) { | |
| String disk = properties.getProperty(Integer.toString(i + 1)); | |
| System.out.println("Disk " + (i + 1) + ": " + disk); | |
| if (disk == null || disk.length() == 0) { | |
| // This disk is missing | |
| missingDisks++; | |
| if (missingDisks > 1) { | |
| System.err.println("No more than one disk can be missing"); | |
| System.exit(0); | |
| } | |
| } else { | |
| // Let's try to open the file | |
| try { | |
| readers[i] = new FileInputStream(disk); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| System.err | |
| .println("Couldn't open specified file due to " | |
| + e.getMessage()); | |
| System.exit(0); | |
| } | |
| } | |
| } | |
| FileOutputStream output = null; | |
| try { | |
| String temp = properties.getProperty("output"); | |
| System.out.println("output: " + temp); | |
| output = new FileOutputStream(temp); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| System.err.println("Couldn't open output stream due to " | |
| + e.getMessage()); | |
| System.exit(0); | |
| } | |
| // algorithm | |
| int algorithm = LEFT_ASYMMETRIC; | |
| String temp = properties.getProperty("algoritmh"); | |
| if (temp != null && temp.length() > 0) { | |
| try { | |
| temp = temp.toUpperCase(); | |
| if (temp.equals("LEFT ASYMMETRIC")) { | |
| algorithm = LEFT_ASYMMETRIC; | |
| } else if (temp.equals("LEFT SYMMETRIC")) { | |
| algorithm = LEFT_SYMMETRIC; | |
| } else if (temp.equals("RIGHT SYMMETRIC")){ | |
| algorithm = RIGHT_SYMMETRIC; | |
| } else if (temp.equals("RIGHT ASYMMETRIC")){ | |
| algorithm = RIGHT_ASYMMETRIC; | |
| } else { | |
| System.out.println("Unknown algorithm: " + temp); | |
| System.exit(0); | |
| } | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| System.err.println("Couldn't determine algorithm due to " | |
| + e.getMessage()); | |
| System.exit(0); | |
| } | |
| } | |
| System.out.print("Algorithm: "); | |
| switch (algorithm) { | |
| case LEFT_ASYMMETRIC: | |
| System.out.println("Left Asymmetric"); | |
| break; | |
| case LEFT_SYMMETRIC: | |
| System.out.println("Left Symmetric"); | |
| break; | |
| case RIGHT_ASYMMETRIC: | |
| System.out.println("Right Asymmetric"); | |
| break; | |
| case RIGHT_SYMMETRIC: | |
| System.out.println("Right Symmetric"); | |
| break; | |
| default: | |
| System.out.println("Unknown (" + temp + ")"); | |
| System.exit(0); | |
| } | |
| int skipChunks = 0; | |
| temp = properties.getProperty("skip"); | |
| if (temp != null && temp.length() > 0) { | |
| try { | |
| skipChunks = Integer.parseInt(temp); | |
| System.out.println("skip " + skipChunks + " chunks"); | |
| } catch (Exception e) { | |
| System.err | |
| .println("Don't understand hoy many chunks to skip (" | |
| + temp + ")"); | |
| System.exit(0); | |
| } | |
| } | |
| Raid5Recovery recovery = new Raid5Recovery(chunkSize, disks, readers, | |
| output, algorithm, skipChunks); | |
| recovery.recover(); | |
| System.out.println("Finished!"); | |
| // TODO close the files | |
| } | |
| private void recover() { | |
| chunks = new Chunk[disks]; | |
| //int chunksRead = 0; | |
| for (int i = 0; i < disks; i++) { | |
| if (inputs[i] == null) | |
| continue; | |
| Chunk unChunk = new Chunk(chunkSize, inputs[i]); | |
| chunks[i] = unChunk; | |
| // I skip the chunks stipulated in the properties file | |
| for (int j = 0; j < skipChunks; j++) { | |
| try { | |
| if (unChunk.read() != chunkSize) { | |
| System.out | |
| .println("Not enouch data to read from disk " | |
| + i + 1); | |
| System.exit(0); | |
| } | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| System.err.println("Couldn't read from disk " + i + 1 | |
| + " due to " + e.getMessage()); | |
| System.exit(0); | |
| } | |
| } | |
| } | |
| int row = 0; | |
| boolean cont = true; | |
| int bytesRead = Integer.MAX_VALUE; | |
| while (cont) { | |
| for (int i = 0; i < disks; i++) { | |
| // Read a chunk from all streams | |
| Chunk aChunk = chunks[i]; | |
| if (aChunk != null) { | |
| // read | |
| try { | |
| bytesRead = Math.min(bytesRead, aChunk.read()); | |
| // always have to get a full chunk | |
| cont = cont && bytesRead == chunkSize; | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| System.out.println("Couldn't read chunk " + row | |
| + " from disk " + i + " due to " + e.getMessage()); | |
| System.exit(0); | |
| } | |
| } | |
| } | |
| // have read all the chunks | |
| // Now, let's print | |
| if (bytesRead > 0) | |
| printRow(row, bytesRead); | |
| row++; | |
| } | |
| } | |
| /** | |
| * Print this row of data (on the output stream) | |
| * | |
| * @param row | |
| * @param numberOfBytes number of bytes to print from the chunks | |
| */ | |
| private void printRow(int row, int numberOfBytes) { | |
| int parityDisk = 0; | |
| switch (algorithm) { | |
| case LEFT_ASYMMETRIC: | |
| case LEFT_SYMMETRIC: | |
| parityDisk = disks - row % disks - 1; | |
| break; | |
| case RIGHT_ASYMMETRIC: | |
| case RIGHT_SYMMETRIC: | |
| parityDisk = row % disks; | |
| break; | |
| } | |
| int from = 0; | |
| switch (algorithm) { | |
| case LEFT_SYMMETRIC: | |
| case RIGHT_SYMMETRIC: | |
| from = (disks - row % disks) % disks; | |
| } | |
| int to = from == 0 ? disks - 1 : from - 1; | |
| int i = from; | |
| byte[] data = null; | |
| boolean cont = true; | |
| while (cont) { | |
| // Take the data form the chucnk (if it's not empty) | |
| if (i != parityDisk) { | |
| Chunk aChunk = chunks[i]; | |
| if (aChunk != null) { | |
| data = aChunk.getChunk(); | |
| } else { | |
| // calculate the data from all teh chunks | |
| data = new byte[chunkSize]; | |
| // just XORs | |
| for (int j = 0; j < disks; j++) { | |
| if (j == i) | |
| continue; // if it's the same image, skip it (it's empty) | |
| byte[] temp = chunks[j].getChunk(); | |
| for (int w = 0; w < numberOfBytes; w++) { | |
| data[w] ^= temp[w]; | |
| } | |
| } | |
| } | |
| try { | |
| output.write(data, 0, numberOfBytes); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| System.out | |
| .println("Couldn't write to output file on row " | |
| + row | |
| + " of disk " | |
| + i | |
| + " due to " | |
| + e.getMessage()); | |
| System.exit(0); | |
| } | |
| } else { | |
| // If it's a parity disk, we won't "print" it | |
| } | |
| if (i == to) { | |
| cont = false; | |
| } | |
| i++; | |
| if (i >= disks) | |
| i = 0; // Reached the end. Let's go back | |
| } | |
| } | |
| } | |
| --- image recovery | |
| /* | |
| * Copyright 2005 Edmundo Carmona | |
| * | |
| * This file is part of the raid recovery project. | |
| * | |
| * This project is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License | |
| * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. | |
| * | |
| * This project is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | |
| * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | |
| * | |
| * You should have received a copy of the GNU General Public License along with this project; if not, write to the | |
| * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA | |
| * | |
| * Created on May 10, 2005 | |
| * | |
| */ | |
| package org.antix.raid; | |
| import java.io.FileInputStream; | |
| import java.io.FileOutputStream; | |
| import java.io.IOException; | |
| /** | |
| * @author antoranz | |
| * | |
| */ | |
| public class RaidImageRecovery { | |
| private Chunk[] inputs; | |
| private FileOutputStream output; | |
| private RaidImageRecovery(FileInputStream[] inputs, FileOutputStream output) { | |
| this.inputs = new Chunk[inputs.length]; | |
| for (int i = 0; i < inputs.length; i++) { | |
| this.inputs[i] = new Chunk(64 * 1024, inputs[i]); | |
| } | |
| this.output = output; | |
| } | |
| /** | |
| * Execution of this class.<p> | |
| * | |
| * As arguments, the user has to specify the filenames of all the images | |
| * that the user has and the name of the file for the missing image last.<p> | |
| * | |
| * Example:<p> | |
| * RaidImageRecovery ./image1 ./image2 ./image4 ./missingimage | |
| * | |
| * @param args | |
| */ | |
| public static void main(String[] args) { | |
| // open all the files, the output is where the data will be written to | |
| // there can't be less than 3 disks | |
| if (args.length < 3) { | |
| System.out.println("Not enough disks"); | |
| System.exit(0); | |
| } | |
| FileInputStream inputs[] = new FileInputStream[args.length - 1]; | |
| for (int i = 0; i < args.length - 1; i++) { | |
| System.out.println("Input " + i + 1 + ": " + args[0]); | |
| try { | |
| inputs[i] = new FileInputStream(args[i]); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| System.out.println("Couldn't open file due to " | |
| + e.getMessage()); | |
| System.exit(0); | |
| } | |
| } | |
| FileOutputStream output = null; | |
| try { | |
| System.out.println("Output: " + args[args.length - 1]); | |
| output = new FileOutputStream(args[args.length - 1]); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| System.out.println("Couldn't open output file due to " | |
| + e.getMessage()); | |
| System.exit(0); | |
| } | |
| RaidImageRecovery raidRecovery = new RaidImageRecovery(inputs, output); | |
| try { | |
| raidRecovery.recover(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| System.out.println("Couldn't complete recovery due to " | |
| + e.getMessage()); | |
| System.exit(0); | |
| } | |
| } | |
| private void recover() throws IOException { | |
| // let's read all inputs, until there's nothing left | |
| boolean cont = true; | |
| while (cont) { | |
| // read all input streams | |
| int bytesRead = Integer.MAX_VALUE; | |
| for (int i = 0; i < inputs.length; i++) { | |
| bytesRead = Math.min(inputs[i].read(), bytesRead); | |
| cont = cont && bytesRead != -1; | |
| } | |
| // calculate the missing chunk | |
| if (cont) { | |
| byte[] data = new byte[64 * 1024]; | |
| // There's data left | |
| for (int i = 0; i < inputs.length; i++) { | |
| byte[] temp = inputs[i].getChunk(); | |
| for (int j = 0; j < temp.length; j++) { | |
| data[j] ^= temp[j]; | |
| } | |
| } | |
| output.write(data, 0, bytesRead); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment