<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>.:: Marcos Dione/StyXman's glob ::. (Posts about kernel)</title><link>https://www.grulic.org.ar/~mdione/glob/</link><description></description><atom:link href="https://www.grulic.org.ar/~mdione/glob/categories/kernel.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2026 &lt;a href="mailto:mdione@grulic.org.ar"&gt;Marcos Dione&lt;/a&gt; </copyright><lastBuildDate>Thu, 01 Jan 2026 15:38:03 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Recovering partitions with pen and paper</title><link>https://www.grulic.org.ar/~mdione/glob/posts/recovering-partitions-with-pen-and-paper/</link><dc:creator>Marcos Dione</dc:creator><description>&lt;p&gt;&lt;em&gt;Note: this is a translation of an old post. I decided to translate it because
now I'm looking for a SysAdmin position (tell your friends!) and I would like
this post to show how I work.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Last Saturday I received an email from one of the guys from work with the
subject «urgennnnnnnnt: heeeeeeeeeelp»[sic]. He says he was idling on Friday
night when his machine stopped emiting sound via the soundcard and then it behaved
erratically. When he tried rebooting it, it didn't boot anymore. «It says
something about disk not bootable...».&lt;/p&gt;
&lt;p&gt;Monday morning I go to work and go to see the machine. Precisely, it said
something about «disk not bootable». I boot with a USB key with
&lt;a href="http://grml.org/"&gt;GRML&lt;/a&gt; and I find that the disk has no partitions.&lt;/p&gt;
&lt;p&gt;Panic.&lt;/p&gt;
&lt;p&gt;The guy is doing a PostDoc in something astronomical (literally) and all his
work is in that machine. No backups, as usual, so I prepare myself to rescue
the partitions.&lt;/p&gt;
&lt;p&gt;In that same USB key I have a system with
&lt;a href="http://www.gnu.org/software/parted/"&gt;parted&lt;/a&gt;. I boot with it and I try using
&lt;code&gt;parted&lt;/code&gt;'s rescue tool. Nothing. I ask the guy how the disk was partitioned, etc.
He tells me that he only installed Kubuntu clicking 'Next'. Kubuntu by default
creates a swap partition and an ext3 partition for / and that's it, which made
what was coming relatively easy.&lt;/p&gt;
&lt;p&gt;I reboot in GRML and I use &lt;code&gt;hexdump -C /dev/sda | more&lt;/code&gt; to see the disk's
content. This is not the first time that I juggle with partitions and MBRs,
but last time I did it, I used a tool that is now discontinued (the tool was
called DiskEdit, included in The Norton Utilities), which had special edit modes
for MBRs, FATs, and a lot of useful things... in MS universe.&lt;/p&gt;
&lt;p&gt;First I confirm that, yes, the first sector is a MBR (at least it has the
&lt;code&gt;0x55aa&lt;/code&gt; signature at the end), and that the whole &lt;a href="http://en.wikipedia.org/wiki/Mbr#MBRs_and_disk_partitioning"&gt;partition
table&lt;/a&gt; is empty,
but in the second sector of the disk there seems to be a copy. I take pen and
paper, write down what I found, but it turns out not only I have half the data,
the partition I thought I found was too small.&lt;/p&gt;
&lt;p&gt;So I decide to look for the partition by hand. To do that I needed to find out
first how does the ext3 kernel code know wether a partition is ext3 or not.
I knew it would be some kind of magic signature, but I had no idea which. So I
installed the sources for 2.6.29 in my laptop and started to look at ext3's
code. After going around a lot, including reading the code that is excuted when
you &lt;a href="http://lxr.linux.no/linux+v2.6.29/fs/super.c#L917"&gt;mount&lt;/a&gt;
&lt;a href="http://lxr.linux.no/linux+v2.6.29/fs/super.c#L779"&gt;a&lt;/a&gt;
&lt;a href="http://lxr.linux.no/linux+v2.6.29/fs/super.c#L357"&gt;filesystem&lt;/a&gt; of type
&lt;a href="http://lxr.linux.no/linux+v2.6.29/fs/ext3/super.c#L1546"&gt;ext3&lt;/a&gt;, where we can
&lt;a href="http://lxr.linux.no/linux+v2.6.29/fs/ext3/super.c#L1614"&gt;see that it uses&lt;/a&gt; a
&lt;a href="http://lxr.linux.no/linux+v2.6.29/include/linux/magic.h#L16"&gt;magic signature&lt;/a&gt;&lt;sup id="fnref:3"&gt;&lt;a class="footnote-ref" href="https://www.grulic.org.ar/~mdione/glob/posts/recovering-partitions-with-pen-and-paper/#fn:3"&gt;3&lt;/a&gt;&lt;/sup&gt;
&lt;a href="http://lxr.linux.no/linux+v2.6.29/include/linux/ext3_fs.h#L454"&gt;and&lt;/a&gt;
the &lt;a href="http://lxr.linux.no/linux+v2.6.29/include/linux/ext3_fs.h#L454"&gt;structure of the ext3
superblock&lt;/a&gt;,
where we can see &lt;a href="http://lxr.linux.no/linux+v2.6.29/include/linux/ext3_fs.h#L470"&gt;the magic's
offset&lt;/a&gt;
is 0x38.&lt;/p&gt;
&lt;p&gt;So the problem of finding an ext3 partition is reduced to the problem of finding
0x53ef (damn little endian) at a sector's offset 0x38 in the disk. Luckily
&lt;code&gt;more&lt;/code&gt; has a find tool, so I sit down to search &lt;em&gt;every&lt;/em&gt; occurrence of &lt;code&gt;53 ef&lt;/code&gt;,
hoping that the address at the left ends in &lt;code&gt;30&lt;/code&gt; and that they would be the
9th and 10th bytes in the line (damn 0 based offsets).&lt;/p&gt;
&lt;p&gt;A few 'next' after, I get my first candidate. It looks good, because I was also
comparing my findings with a similar dump from my USB key (which I have
formatted as &lt;code&gt;ext2&lt;/code&gt;, and luckily &lt;code&gt;ext2&lt;/code&gt; and &lt;code&gt;ext3&lt;/code&gt; share those structures), and
also I spot something that looks like a
&lt;a href="http://lxr.linux.no/linux+v2.6.29/include/linux/ext3_fs.h#L499"&gt;&lt;code&gt;uuid&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This candidate's address is &lt;code&gt;0x80731038&lt;/code&gt;. I substract &lt;code&gt;0x38&lt;/code&gt; and I get the
address &lt;code&gt;0x80731000&lt;/code&gt;, a nice round number for a superblock. Converted to decimal
that's &lt;code&gt;2.155.024.384&lt;/code&gt;, some 2GiB from the disk's begginning. Looks really good!
The swap partition could be before the root one, and could have that size.&lt;/p&gt;
&lt;p&gt;I use &lt;code&gt;fdisk /dev/sda&lt;/code&gt; to see the (still empty) partition table. It says there's
&lt;code&gt;16.065&lt;/code&gt; sectors per cylinder, times &lt;code&gt;512&lt;/code&gt; bytes per sector, equals &lt;code&gt;8.225.280&lt;/code&gt;
bytes per cylinder. Almost all distros (actually I think all of them) partition
disks at cylinder boundaries&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="https://www.grulic.org.ar/~mdione/glob/posts/recovering-partitions-with-pen-and-paper/#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;, so if the sector I found is right at the
beginning of a cylinder...&lt;/p&gt;
&lt;p&gt;I divide &lt;code&gt;2.155.024.384/8.225.280=...&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;(suspense pause)&lt;sup id="fnref:2"&gt;&lt;a class="footnote-ref" href="https://www.grulic.org.ar/~mdione/glob/posts/recovering-partitions-with-pen-and-paper/#fn:2"&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;262.000124494...&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;¡Damn! I almost had it... Hmm, how much is the factional part?
&lt;code&gt;(262.000124494-262)*8.225.280=...&lt;/code&gt; ¡&lt;code&gt;1024&lt;/code&gt;! ¿Is it that...?&lt;/p&gt;
&lt;p&gt;I run &lt;code&gt;strace debugfs -R show_super_stats /dev/sdb1&lt;/code&gt; (the partition
in my USB key) and I see that it actually seeks &lt;code&gt;1024&lt;/code&gt; bytes within the
partition for reading the superblock!&lt;/p&gt;
&lt;p&gt;This is it. With 262 in my head, I fire &lt;code&gt;fdisk /dev/sda&lt;/code&gt; and I create two
partitions: swap in cylinders 1-261 and root from cylinder 262 till the end. I
save, cross my fingers and I run &lt;code&gt;debugfs -R show_super_stats
/dev/sda1&lt;/code&gt;. It fails! What's wrong? I reboot and I try again, just in case the
kernel did not re-read correctly the partition table. It fails again. WTF?&lt;/p&gt;
&lt;p&gt;Ah, duh, it's &lt;code&gt;sda2&lt;/code&gt;, where do I have my head... Ok, &lt;code&gt;debugfs -R
show_super_stats /dev/sda2&lt;/code&gt;... it works, the sonofabitch works! I can't believe
it. I risk it: &lt;code&gt;fsck -n /dev/sda2&lt;/code&gt;. «Filesystem is clean». Damn, I try harder:
&lt;code&gt;fsck -n -f /dev/sda2&lt;/code&gt;...&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sda2 etc etc...
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It's fine! But the MBR doesn't have GURB installed, so I do the usual GRUB
reinstall process, I reboot...&lt;/p&gt;
&lt;p&gt;It boots like nothing has happened, and it finishes in a beautiful login.
Satisifed, I pat myself in the back, pack my things and I start my weekend&lt;sup id="fnref:4"&gt;&lt;a class="footnote-ref" href="https://www.grulic.org.ar/~mdione/glob/posts/recovering-partitions-with-pen-and-paper/#fn:4"&gt;4&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;div class="footnote"&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;... wasting some 8MiB between the MBR and the first partition. &lt;a class="footnote-backref" href="https://www.grulic.org.ar/~mdione/glob/posts/recovering-partitions-with-pen-and-paper/#fnref:1" title="Jump back to footnote 1 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:2"&gt;
&lt;p&gt;The sharp ones reading this will notice that this can not give an integer by
  no means. &lt;a class="footnote-backref" href="https://www.grulic.org.ar/~mdione/glob/posts/recovering-partitions-with-pen-and-paper/#fnref:2" title="Jump back to footnote 2 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:3"&gt;
&lt;p&gt;Reiser magics are funny. Looks like he started the fad that now AdOlEsCeNtS
  use. &lt;a class="footnote-backref" href="https://www.grulic.org.ar/~mdione/glob/posts/recovering-partitions-with-pen-and-paper/#fnref:3" title="Jump back to footnote 3 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:4"&gt;
&lt;p&gt;Update from 2023: yes, you got the math right. It took me a whole week of
  trial and error between other pressing tasks. &lt;a class="footnote-backref" href="https://www.grulic.org.ar/~mdione/glob/posts/recovering-partitions-with-pen-and-paper/#fnref:4" title="Jump back to footnote 4 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</description><category>disks</category><category>kernel</category><category>rescue</category><category>sysadmin</category><guid>https://www.grulic.org.ar/~mdione/glob/posts/recovering-partitions-with-pen-and-paper/</guid><pubDate>Thu, 07 Apr 2011 21:49:04 GMT</pubDate></item></channel></rss>