Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I realised some files on my /home partition were unreadable and caused system to crash, so I supposed something was broken on my hard disk.
I just went to single-user mode, unmounted the partition, and tried e2fsck, but it reported no errors. Then I ran badblocks, which got freezed at the same block the files were supposed to be.
I read a bit the man pages and found a "mke2fs" program which had an option to check partition headings. I asked it to do this check, but a few seconds later I realised I had mispelled some option and it was actually overriding the partition.
I tried to mount the partition again, but its header was corrupted. So I ran mke2fs again, telling it to create only the partition header. As suggested by documentation, next I ran e2fsck, which repported some errors and fixed them. But the resulting filesystem had only 11 used inodes and was still unmountable.
Since I have some very important data in this partition, there's no backup of it. (I've only seen backups of games data!). But assuming that most of the contents were just stuff, there's a high chance of this important data being still unmodified.
Now, the question is:
What could I try to restore this partition (or, at least, rescue most of its data)?
I first tried to make a image with dd, but dd got locked at some block "x" on the disc. Same for dls tool of sleuthkit.
Then I managed to recover some files from the partition by running magicrescue. It also hanged at that "x" sector, but I could rerun it from a greater block to skip the bad one(s). (After a reset-button reboot). Unfortunately, it didn't look able to find the most important files; it just returned some unnecessary images and stuff like that.
Finally I supposed I could try to grep filenames in the device and then expect to find contents near, but obviously grep reached that "x" damaged block and the system got locked for the nth time.
So what am I expected to do? Throw a curse on the hard disk vendor? The moral you can extract from my case is "buy a CDRW and copy all those important files on it before they abandon you".
Anyway, does anyone know some tool to make a copy of the partition which is capable to skip errors? (blocks will do)
Then I wrote some code to search the raw data on my partition by a keyword. It's quite fast if you are searching for some specific data, which was my case. It compiles with gcc and must be edited for an specific raw input (sorry, it's only a small tool, not a full program). Someone might find it helpful, but it has no warranty.
This is the code listing:
Code:
#include <stdio.h>
#define SIZE 4096
FILE* buffer;
FILE* registre;
FILE* output;
char name[1024];
char* stuff;
int block;
int a,b,c;
int equal;
char chars[1024];
char chars1;
int NCHARS;
int keeponreading;
int realsize;
char outname[1024];
int main(int argc, char* argv[])
{
if (argc < 2) return 1;
fprintf(stderr,"%i",argc);
if (sscanf(argv[1],"%s1023",chars) != 1) return 1;
NCHARS = strlen(chars);
stuff = malloc(SIZE+NCHARS);
registre = fopen("./log.txt","w");
chars1 = chars[0];
for(a=0;a<=11;a++)
{
sprintf(name,"/shared/sda72.img.%i",a);
buffer = fopen(name,"rb");
block =0;
fprintf(stderr,
"Started reading file %i\n",a);
keeponreading = -1;
while (keeponreading)
{
block ++; /* We start counting at 1 */
realsize = fread(stuff,1,SIZE+NCHARS,buffer);
keeponreading = (realsize==SIZE+NCHARS);
if ((block % 10240) == 1) fprintf(stderr,
"Now on block %i\n",block);
for(b=0;b<realsize-NCHARS;b++)
{
if (stuff[b] == chars1)
{
equal = 1;
for(c=1;c<NCHARS;c++)
{
if (stuff[b+c] != chars[c])
{
equal = 0;
break;
}
}
if (equal == 1)
{
fprintf(stderr,
"Found '%s' at block %i of file %i !!!\n",chars,block,a);
fprintf(registre, "%i %i\n",block,a);
sprintf(outname, "./%i.%i.bin",a,block);
output = fopen(outname,"wb");
fwrite(stuff,1,SIZE,output);
fclose(output);
}
}
}
fseek(buffer,-NCHARS,SEEK_CUR);
}
fclose(buffer);
}
fclose(registre);
free(stuff);
return 0;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.