Ubuntu: How to recover an HDD after using it for SWAP



Question:

Last night I was working on my linux box using a CFD solver where I needed more than the 16 GB RAM that I already have installed. So I took one of my external HDDs (total of 360G capacity with about 100G remaining) and issued the command to let it be used as a SWAP space.

The program ran till about midnight and I left machine running as the job was still going. In the morning, I saw the machine was frozen (maybe still busy) so I rebooted the machine.

Then I checked my external drive I used as SWAP and it was not accessible. I disconnected it and checked on Windows and it asked me to reformat. Since then I have used "testdisk" and I see that it appears to be OK but I cannot access the original contents (windows files).

How could I access or recover the data left on that disk?

Thanks.


Solution:1

You wrote:

So I took one of my external HDDs (total of 360G capacity with about 100G remaining) and issued the command to let it be used as a SWAP space.

The antecedent of "it" is unclear, and the answer to your question depends on that detail:

"It" = 100GiB Free Space

If "it" refers to the 100GiB of free space you mention, then the appropriate commands would have been:

  1. Use fdisk, parted, GParted, or some other tool to create a new partition in that unpartitioned space.
  2. Use mkswap or GParted to prepare the new partition as swap space.
  3. Use swapon to activate the swap space.

There are variants of this. For instance, you might have created a swap file rather than a swap partition. In any event, the key point here is that only part of the disk was used as swap space, leaving the used portions of the disk untouched by Linux's swap tools.

In this case, your original data should be accessible. If it's not, further diagnostics are required, as noted below....

"It" = Whole Disk

If you did not prepare a swap partition or swap file, and simply issued mkswap on the whole-disk device (/dev/sdb or whatever it is) or on the existing partition(s) on the disk (/dev/sdb1, for example), then you've destroyed at least some of your existing data. You might be able to recover some file using a tool such as PhotoRec, but how many files you'll be able to recover depends on how much swap space your software consumed while you left it running. If little swap space was used, you might be able to get back most of your files; but if your software chewed up most of the available disk space, or if you were simply unlucky in what specific sectors it used, you could have already lost most or all of your files.

If the damaged disk used NTFS, note that there are Windows-specific programs similar to PhotoRec that do the same job, and may do it better. The times I've used it, PhotoRec has never recovered filenames for me; but some Windows-specific tools may do so. Thus, you may want to look into such tools.

In Either Case....

There are two commands that you may want to run if you're not sure what you've done:

  • Type sudo parted -l to learn how your disks are partitioned.
  • Type sudo blkid to learn how each partition is prepared (what filesystem it uses, or if it's swap space).

If you're not sure how to interpret the data, edit your question to add it or post a link to a pastebin site that holds the output.

These commands may turn up the cause of your problems if you did not wipe it all out as swap space. You might need to do something more in that case, though. For instance, if your external disk uses NTFS, you might need to run CHKDSK on it from Windows. (There are no adequate NTFS-repair tools in Linux, so a system crash when NTFS is mounted requires repair in Windows.)


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »