<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>brool &#187; ext3</title>
	<atom:link href="http://www.brool.com/index.php/tag/ext3/feed" rel="self" type="application/rss+xml" />
	<link>http://www.brool.com</link>
	<description>brool \brool\ (n.) : a low roar; a deep murmur or humming</description>
	<lastBuildDate>Fri, 20 Jan 2012 07:58:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Recovering A Trashed Ext3 System</title>
		<link>http://www.brool.com/index.php/recovering-a-trashed-ext3-system</link>
		<comments>http://www.brool.com/index.php/recovering-a-trashed-ext3-system#comments</comments>
		<pubDate>Tue, 10 Feb 2009 07:56:16 +0000</pubDate>
		<dc:creator>tim</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[ext3]]></category>
		<category><![CDATA[recovery]]></category>

		<guid isPermaLink="false">http://www.brool.com/?p=149</guid>
		<description><![CDATA[My 500GB backup drive started making horrible, horrible noises when spinning up, akin to a lawnmower trying to mow a rock, so I decided that it was time get another backup drive so that I would be safe. I picked up another drive at Fry&#8217;s (not a Maxtor this time, thank you very much; I [...]]]></description>
			<content:encoded><![CDATA[<p>My 500GB backup drive started making horrible, horrible noises when spinning up, akin to a lawnmower trying to mow a rock, so I decided that it was time get another backup drive so that I would be safe.  I picked up another drive at Fry&#8217;s (not a Maxtor this time, thank you very much;  I haven&#8217;t had much luck with Maxtor) and hooked them both up to my computer so that I could completely copy from the old drive to the new drive.</p>

<p>The old drive was in /dev/sdb1, while the new drive was in /dev/sdc1, so it was the work of a few moments to type:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1"><span class="kw2">dd</span> <span class="re2">if</span>=<span class="sy0">/</span>dev<span class="sy0">/</span>sdc1 <span class="re2">of</span>=<span class="sy0">/</span>dev<span class="sy0">/</span>sdb1</pre></div></div></div></div></div></div></div>



<p>See what I did there?  Yup, I <i>copied the new drive onto the old drive</i>, and, yes, I sometimes do incredibly stupid things.  I stopped it just a couple of seconds later, but I had lost the first 100MB of my drive.  </p>

<p>Most of my stuff is backed up to at least two places, but there was probably about 200GB of stuff that I had lost, non-critical non-financial files that I would have preferred to get back.  I eventually got it all back in the hackiest way possible.</p>

<p>How did I get it back?</p>

<p>1. Ran mke2fs -n to find some good superblocks.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1">$ mke2fs <span class="re5">-n</span> <span class="sy0">/</span>dev<span class="sy0">/</span>sdc1
Filesystem <span class="re2">label</span>=
OS type: Linux
Block <span class="re2">size</span>=<span class="nu0">4096</span> <span class="br0">&#40;</span><span class="re2">log</span>=<span class="nu0">2</span><span class="br0">&#41;</span>
Fragment <span class="re2">size</span>=<span class="nu0">4096</span> <span class="br0">&#40;</span><span class="re2">log</span>=<span class="nu0">2</span><span class="br0">&#41;</span>
<span class="nu0">30515200</span> inodes, <span class="nu0">122035756</span> blocks
<span class="nu0">6101787</span> blocks <span class="br0">&#40;</span><span class="nu0">5.00</span><span class="sy0">%</span><span class="br0">&#41;</span> reserved <span class="kw1">for</span> the super user
First data <span class="re2">block</span>=<span class="nu0">0</span>
Maximum filesystem <span class="re2">blocks</span>=<span class="nu0">0</span>
<span class="nu0">3725</span> block <span class="kw2">groups</span>
<span class="nu0">32768</span> blocks per group, <span class="nu0">32768</span> fragments per group
<span class="nu0">8192</span> inodes per group
Superblock backups stored on blocks: 
	<span class="nu0">32768</span>, <span class="nu0">98304</span>, <span class="nu0">163840</span>, <span class="nu0">229376</span>, <span class="nu0">294912</span>, <span class="nu0">819200</span>, <span class="nu0">884736</span>, <span class="nu0">1605632</span>, <span class="nu0">2654208</span>, 
	<span class="nu0">4096000</span>, <span class="nu0">7962624</span>, <span class="nu0">11239424</span>, <span class="nu0">20480000</span>, <span class="nu0">23887872</span>, <span class="nu0">71663616</span>, <span class="nu0">78675968</span>, 
	<span class="nu0">102400000</span></pre></div></div></div></div></div></div></div>




<p>2. Verified that the superblocks were good. All of the &#8220;good&#8221; superblocks should be identical.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1"><span class="kw2">dd</span> <span class="re2">if</span>=<span class="sy0">/</span>dev<span class="sy0">/</span>sdc1 <span class="re2">skip</span>=<span class="nu0">32768</span> <span class="re2">count</span>=<span class="nu0">1</span> <span class="re2">bs</span>=<span class="nu0">4096</span> <span class="sy0">|</span> hex <span class="sy0">|</span> <span class="kw2">head</span>
<span class="nu0">1</span>+<span class="nu0">0</span> records <span class="kw1">in</span>
<span class="nu0">1</span>+<span class="nu0">0</span> records out
<span class="nu0">4096</span> bytes <span class="br0">&#40;</span><span class="nu0">4.1</span> kB<span class="br0">&#41;</span> copied, <span class="nu0">0.00194333</span> s, <span class="nu0">2.1</span> MB<span class="sy0">/</span>s
0x00000000: 00 <span class="nu0">40</span> a3 03 2c 1e <span class="nu0">46</span> 07 - 05 9f <span class="nu0">12</span> 00 d5 c4 <span class="nu0">28</span> 07 .<span class="sy0">@</span>..,.F.....<span class="br0">&#40;</span>.
0x00000010: f5 3f a3 03 00 00 00 00 - 02 00 00 00 02 00 00 00 .?..............
0x00000020: 00 <span class="nu0">80</span> 00 00 00 <span class="nu0">80</span> 00 00 - 00 <span class="nu0">40</span> 00 00 00 00 00 00 .........<span class="sy0">@</span>......
0x00000030: 4c db <span class="nu0">85</span> <span class="nu0">47</span> 00 00 <span class="nu0">15</span> 00 - <span class="nu0">53</span> ef 00 00 01 00 00 00 L..G....S.......
0x00000040: c2 6f 0d 3a 00 4e <span class="kw2">ed</span> 00 - 00 00 00 00 01 00 00 00 .o.:.N..........
0x00000050: 00 00 00 00 0b 00 00 00 - <span class="nu0">80</span> 00 01 00 04 00 00 00 ................
0x00000060: 02 00 00 00 01 00 00 00 - e6 9f <span class="nu0">75</span> ff 9c 5d <span class="nu0">49</span> 0a ..........u..<span class="br0">&#93;</span>I.
0x00000070: <span class="nu0">95</span> cb a2 fb e0 <span class="nu0">39</span> ce 9c - 00 00 00 00 00 00 00 00 .?<span class="nu0">9</span>..........
0x00000080: 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
0x00000090: 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................</pre></div></div></div></div></div></div></div>



 
<p>I compared it with one of the later superblocks.  I was lucky and had only lost the last one. </p>

<p>3. Get a temporary root directory onto the drive.  For my particular case, I copied the start of an identically sized drive to the damaged drive so that I could get enough of a file system to work with.  </p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1"><span class="kw2">dd</span> <span class="re2">if</span>=<span class="sy0">/</span>dev<span class="sy0">/</span>sdd1 <span class="re2">of</span>=<span class="sy0">/</span>dev<span class="sy0">/</span>sdc1 <span class="re2">count</span>=<span class="nu0">2000</span> <span class="re2">bs</span>=<span class="nu0">4096</span></pre></div></div></div></div></div></div></div>




<p>4. Scan the drive and figure out what directories you want to try to get back.  I ended up writing <a href="http://github.com/brool/util/tree/master">a small Python program</a> to scan the drive directly, finding the subdirectories that I wanted.</p>

<p>You&#8217;re looking for directories that are as high level as possible &#8212; in my case, I had a directory to_archive that contained all the things that I hadn&#8217;t redundantly backed up.  It was at block 22839809. </p>

<p>5. Given a block number, it&#8217;s possible to convert it into an inode&#8230; but it&#8217;s much easier to just go to the directory in question and take a look at the inode for it.   </p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1"><span class="kw2">dd</span> <span class="re2">if</span>=<span class="sy0">/</span>dev<span class="sy0">/</span>sdc1 <span class="re2">skip</span>=<span class="nu0">22839810</span> <span class="re2">count</span>=<span class="nu0">1</span> <span class="re2">bs</span>=<span class="nu0">4096</span> <span class="sy0">|</span> hex <span class="sy0">|</span> <span class="kw2">head</span> <span class="re5">-n</span> <span class="nu0">1</span></pre></div></div></div></div></div></div></div>




<p>The first four bytes are the inode.   Copy them down.</p>
 
<p>5. I found the temporary root directory that I copied from the other drive.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1">python scan_directory.py <span class="sy0">/</span>dev<span class="sy0">/</span>sdc1
block <span class="co0"># 1539</span>
<span class="br0">&#91;</span><span class="st_h">'.'</span>, <span class="st_h">'..'</span>, <span class="st_h">'lost+found'</span>, <span class="st_h">'var'</span>, <span class="st_h">'etc'</span>, <span class="st_h">'media'</span>, <span class="st_h">'cdrom'</span>, <span class="st_h">'bin'</span>, <span class="st_h">'boot'</span>, <span class="st_h">'dev'</span>, <span class="st_h">'home'</span>, <span class="st_h">'lib'</span>, <span class="st_h">'mnt'</span>, <span class="st_h">'opt'</span>, <span class="st_h">'proc'</span>, <span class="st_h">'root'</span>, <span class="st_h">'sbin'</span>, <span class="st_h">'srv'</span>, <span class="st_h">'sys'</span>, <span class="st_h">'tmp'</span>, <span class="st_h">'usr'</span>, <span class="st_h">'initrd.img'</span>, <span class="st_h">'vmlinuz'</span>, <span class="st_h">'initrd.img.old'</span>, <span class="st_h">'vmlinuz.old'</span><span class="br0">&#93;</span></pre></div></div></div></div></div></div></div>




<p>This directory is mostly pointing into garbage, but we just want to point one directory to our stuff.</p>

<p>6. Now we want to point the inode of a directory to the one that contains our files.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1"><span class="kw2">dd</span> <span class="re2">if</span>=<span class="sy0">/</span>dev<span class="sy0">/</span>sdc1 <span class="re2">of</span>=root_directory.bin <span class="re2">count</span>=<span class="nu0">1</span> <span class="re2">bs</span>=<span class="nu0">4096</span> <span class="re2">skip</span>=<span class="nu0">1539</span></pre></div></div></div></div></div></div></div>




The root directory looks something like this.


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1">0x00000000: 02 00 00 00 0c 00 01 02 - 2e 00 00 00 02 00 00 00 ................
0x00000010: 0c 00 02 02 2e 2e 00 00 - 0b 00 00 00 <span class="nu0">14</span> 00 0a 02 ................
0x00000020: 6c 6f <span class="nu0">73</span> <span class="nu0">74</span> 2b <span class="nu0">66</span> 6f <span class="nu0">75</span> - 6e <span class="nu0">64</span> 00 00 01 e0 0a 00 lost+found......
0x00000030: 0c 00 03 02 <span class="nu0">76</span> <span class="nu0">61</span> <span class="nu0">72</span> 00 - 01 <span class="nu0">40</span> <span class="nu0">17</span> 00 0c 00 03 02 ....var..<span class="sy0">@</span>......
0x00000040: <span class="nu0">65</span> <span class="nu0">74</span> <span class="nu0">63</span> 00 01 e0 0c 00 - <span class="nu0">10</span> 00 05 02 6d <span class="nu0">65</span> <span class="nu0">64</span> <span class="nu0">69</span> etc.........medi
0x00000050: <span class="nu0">61</span> 00 00 00 0c 00 00 00 - <span class="nu0">10</span> 00 05 07 <span class="nu0">63</span> <span class="nu0">64</span> <span class="nu0">72</span> 6f a...........cdro
0x00000060: 6d 00 00 00 01 00 <span class="nu0">12</span> 00 - 0c 00 03 02 <span class="nu0">62</span> <span class="nu0">69</span> 6e 00 m...........bin.
0x00000070: 01 <span class="nu0">60</span> <span class="nu0">20</span> 00 0c 00 04 02 - <span class="nu0">62</span> 6f 6f <span class="nu0">74</span> 01 <span class="nu0">40</span> <span class="nu0">13</span> 00 .<span class="sy0">`</span> .....boot.<span class="sy0">@</span>..
0x00000080: 0c 00 03 02 <span class="nu0">64</span> <span class="nu0">65</span> <span class="nu0">76</span> 00 - 01 <span class="nu0">20</span> 1b 00 0c 00 04 02 ....dev.. ......
0x00000090: <span class="nu0">68</span> 6f 6d <span class="nu0">65</span> 01 <span class="nu0">40</span> 03 00 - 0c 00 03 02 6c <span class="nu0">69</span> <span class="nu0">62</span> 00 home.<span class="sy0">@</span>......lib.</pre></div></div></div></div></div></div></div>




<p>The name of a file is preceded by a a) one byte type, b) a one byte name length, c) two bytes of record length, and d) four bytes of an inode.  In this case, we&#8217;re going to change lost+found from inode 0x0b to the one that we got in step 4.  Easiest way is to use dd and tweak:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1">tweak root_directory.bin
... edit it ...
<span class="kw2">dd</span> <span class="re2">if</span>=root_directory.bin <span class="re2">of</span>=<span class="sy0">/</span>dev<span class="sy0">/</span>sdc1 <span class="re2">count</span>=<span class="nu0">1</span> <span class="re2">bs</span>=<span class="nu0">4096</span> <span class="re2">seek</span>=<span class="nu0">1539</span></pre></div></div></div></div></div></div></div>




<p>7. Now, mount it and cross your fingers.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1"><span class="kw2">mount</span> <span class="re5">-o</span> ro <span class="sy0">/</span>dev<span class="sy0">/</span>sdc1 <span class="sy0">/</span>mnt<span class="sy0">/</span>external</pre></div></div></div></div></div></div></div>




<p>In my case, I was lucky &#8212; I was able to get <i>everything</i> back in the directories that I cared about. </p>]]></content:encoded>
			<wfw:commentRss>http://www.brool.com/index.php/recovering-a-trashed-ext3-system/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

