<?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>logIt &#187; backup/restore</title>
	<atom:link href="https://lakm.us/logit/tag/backuprestore/feed/" rel="self" type="application/rss+xml" />
	<link>https://lakm.us/logit</link>
	<description>Log Around The Clock</description>
	<lastBuildDate>Sat, 06 Jun 2015 14:17:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Duplicate/Restore ARM Linux Image to MMC/SD Card (BeagleBoard and DevKit ARM)</title>
		<link>https://lakm.us/logit/2013/03/duplicate-restore-arm-linux-image-mmc-sd-card-beagleboard-devkit-board/</link>
		<comments>https://lakm.us/logit/2013/03/duplicate-restore-arm-linux-image-mmc-sd-card-beagleboard-devkit-board/#comments</comments>
		<pubDate>Mon, 25 Mar 2013 10:02:45 +0000</pubDate>
		<dc:creator>Arif</dc:creator>
				<category><![CDATA[ARM Linux]]></category>
		<category><![CDATA[ARMv7]]></category>
		<category><![CDATA[backup/restore]]></category>
		<category><![CDATA[BeagleBoard]]></category>
		<category><![CDATA[DevKit8500D]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[rootfs]]></category>

		<guid isPermaLink="false">http://lakm.us/logit/?p=530</guid>
		<description><![CDATA[By booting from MMC/SD card, you can bring up your BeagleBoard-xM or DevKit8500D (both are ARMv7). It is the only way for the xM (microSD card to be exact), while with DevKit, you have the option to flash it to the board for NAND-boot once you&#8217;re sure that it works with SD card. eLinux wiki [...]]]></description>
				<content:encoded><![CDATA[<p>By booting from MMC/SD card, you can bring up your <a href="http://beagleboard.org/hardware-xM" target="_blank">BeagleBoard-xM</a> or <a href="http://www.armkits.com/product/devkit8500d.asp" target="_blank">DevKit8500D</a> (both are <a href="http://en.wikipedia.org/wiki/ARM_Cortex-A8" target="_blank">ARMv7</a>). It is the only way for the xM (microSD card to be exact), while with DevKit, you have the option to flash it to the board for NAND-boot once you&#8217;re sure that it works with SD card. <a href="http://elinux.org" target="_blank">eLinux wiki</a> has some sections about the card setup e.g. <a href="http://elinux.org/BeagleBoardDebian" target="_blank">Debian setup</a>, how to <a href="http://elinux.org/BeagleBoard#U-Boot_booting" target="_blank">start from U-boot prompt</a>, etc.</p>
<p>In the Debian example, the wiki introduced <a href="http://elinux.org/BeagleBoardDebian#Debian_NetInstall" target="_blank">NetInstall</a>. Later I find out the <code>mk_mmc.sh</code> script shown there to be useful for (1) duplicating and (2) restoring working Linux backup to new or corrupted MMC/SD card. I break down that <a href="https://github.com/RobertCNelson/netinstall/blob/bd32dd39732ccf33bfec8895c809f0d4fa79c95c/mk_mmc.sh" title="mk_mmc.sh (tree bd32dd3973)" target="_blank">Robert C. Nelson&#8217;s <code>mk_mmc.sh</code> script at GitHub</a> to small routines for setting up the card.</p>
<div class="wp-caption aligncenter" style="width: 460px"><img alt="typical-sd-card-partition-arm-linux-board.png" src="../../../../images/typical-sd-card-partition-arm-linux-board.png" title="Typical boot-rootfs partitions of SD card to boot ARM-Linux boards" width="334" height="131" /><p class="wp-caption-text">Typical boot-rootfs partitions of SD card to boot ARM-Linux boards</p></div>
<p>It&#8217;s an elegant script automating the process of preparing SD card for embedded Linux, but requires large downloading on the run (mostly root file system and then <a href="http://www.denx.de/wiki/U-Boot" title="Das U-Boot" target="_blank">U-Boot</a> binaries and loader config). Duplicating/restoring means we already have those and probably only need to do elementary process rather than full card preparation. Below are some processes that can be repeatedly used.</p>
<p><span id="more-530"></span><a id="Checking.Version" href="#Checking.Version" rel="bookmark" title="Checking Tool Versions"><br />
<h4>Checking Tool Versions</h4>
<p></a></p>
<p>The tools to work with are common in Linux distribution (I used Ubuntu) and require <code>root</code> privilege. The script checked for <code>fdisk</code> and <code>parted</code> installed. My versions are</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ fdisk <span style="color: #660033;">-v</span>
fdisk <span style="color: #7a0874; font-weight: bold;">&#40;</span>util-linux-ng 2.17.2<span style="color: #7a0874; font-weight: bold;">&#41;</span>
$ parted <span style="color: #660033;">-v</span>
parted <span style="color: #7a0874; font-weight: bold;">&#40;</span>GNU parted<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">2.2</span></pre></div></div>

<p>Originally <code>mk_mmc.sh</code> will exit If &#8220;<code>GNU Fdisk</code>&#8221; found instead. Also for the above <code>parted</code> version, &#8220;<code>--align cylinder</code>&#8221; argument will be used.</p>
<p><a id="Creating.Partitions" href="#Creating.Partitions" rel="bookmark" title="Creating Two Partitions"><br />
<h4>Creating Two Partitions</h4>
<p></a></p>
<p>Setting up two partitions which are the boot image (<code>FAT 16</code>) and root file system (<code>ext4</code>). The SD card plugged in through USB card reader found as <code>/dev/sdb</code> in this example (<strong>we&#8217;re formatting be sure of this!</strong>). If you plug MMC card to laptop slot, it may be found as <code>/dev/mmcblk0</code>. </p>
<ol>
<li>Format card into a single disk</li>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ parted <span style="color: #660033;">--script</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdb mklabel msdos
$ fdisk <span style="color: #660033;">-l</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdb</pre></div></div>

<li>Create the boot partition (allocating 64MB)</li>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ fdisk <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdb <span style="color: #cc0000; font-style: italic;">&lt;&lt; END
n
p
1
1
+64M
t
e
p
w
END</span></pre></div></div>

<p>then make sure it&#8217;s done.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sync</span></pre></div></div>

<li>Flag it as <code>boot</code></li>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ parted <span style="color: #660033;">--script</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdb <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #000000;">1</span> boot on</pre></div></div>

<li>Create the root file system by first determining space left after the first partition</li>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #007800;">END_BOOT</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">LC_ALL</span>=C parted <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdb unit mb print <span style="color: #c20cb9; font-weight: bold;">free</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> primary <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $3}'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot;M&quot;</span> -f1<span style="color: #7a0874; font-weight: bold;">&#41;</span>
$ <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$END_BOOT</span> 
<span style="color: #000000;">69.7</span>
$ <span style="color: #007800;">END_DEVICE</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">LC_ALL</span>=C parted <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdb unit mb print <span style="color: #c20cb9; font-weight: bold;">free</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> Free <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #660033;">-n</span> <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $2}'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot;M&quot;</span> -f1<span style="color: #7a0874; font-weight: bold;">&#41;</span>
$ <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$END_DEVICE</span> 
<span style="color: #000000;">3951</span></pre></div></div>

<p>In this case 4GB SD card is used and the above calculation will stretch space left from 69.7 to 3951 of as <code>rootfs</code> partition.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ parted <span style="color: #660033;">--script</span> <span style="color: #660033;">--align</span> cylinder <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdb mkpart primary ext4 <span style="color: #007800;">$END_BOOT</span> <span style="color: #007800;">$END_DEVICE</span></pre></div></div>

<li>Format <code>boot</code> and <code>rootfs</code> according to each file system type with those naming also. They&#8217;re already accessible as <code>/dev/sdb1</code> and <code>/dev/sdb2</code> (previously didn&#8217;t exist) or relevant <code>/dev/mmcblk0p(some index)</code>.</li>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ mkfs.vfat <span style="color: #660033;">-F</span> <span style="color: #000000;">16</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdb1 <span style="color: #660033;">-n</span> boot
$ mkfs.ext4 <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdb2 <span style="color: #660033;">-L</span> rootfs</pre></div></div>

<li>Finally we have something like the output of <code>parted</code> below</li>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #007800;">LC_ALL</span>=C parted <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdb unit mb print <span style="color: #c20cb9; font-weight: bold;">free</span> 
Model: HUAWEI MMC Storage <span style="color: #7a0874; font-weight: bold;">&#40;</span>scsi<span style="color: #7a0874; font-weight: bold;">&#41;</span>
Disk <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdb: 4023MB
Sector <span style="color: #c20cb9; font-weight: bold;">size</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>logical<span style="color: #000000; font-weight: bold;">/</span>physical<span style="color: #7a0874; font-weight: bold;">&#41;</span>: 512B<span style="color: #000000; font-weight: bold;">/</span>512B
Partition Table: msdos
&nbsp;
Number  Start   End     Size    Type     File system  Flags
 <span style="color: #000000;">1</span>      0.03MB  70.9MB  70.8MB  primary  fat16        boot, lba
        70.9MB  4023MB  3953MB           Free Space</pre></div></div>

<p><em>(yes I used Huawei modem as the microSD card reader)</em>
</ol>
<p><a id="Restoring.Linux" href="#Restoring.Linux" rel="bookmark" title="Restoring Linux"><br />
<h4>Restoring Linux</h4>
<p></a></p>
<p>For the <code>boot</code> partition content we can simply copy-paste the backup files, while for <code>rootfs</code> normally compressed file (preserving path information) is used for backup. Restore this by mounting <code>rootfs</code> and then unpacking the file (again <code>/dev/sdb2</code> in this case).</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>host<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>rootfs
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>host<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">mount</span> <span style="color: #660033;">-t</span> ext4 <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdb2 <span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>rootfs
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>host<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ pv armel-rootfs-201110131018.tar <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">--numeric-owner</span> <span style="color: #660033;">--preserve-permissions</span> <span style="color: #660033;">-xf</span> - <span style="color: #660033;">-C</span> <span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>rootfs</pre></div></div>

<p>Below is an example of how to backup <code>rootfs</code> from the mounted SD card (<code>/media/rootfs</code>).</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>host<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>rootfs
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>host<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>rootfs<span style="color: #666666; font-style: italic;"># tar --numeric-owner --preserve-permissions -cvf /home/arif/Documents/arm-linux/ubuntu/rootstock-rootfs/armel-rootfs-201202141421.tar ./*</span></pre></div></div>

<p>(Check <a href="../../../2011/10/running-ubuntu-on-devkit8500d-natty-kernel-and-lucid-rootfs/" title="Running Ubuntu on DevKit8500D (Natty Kernel and Lucid RootFS)" target="_blank">my other post related to creating <code>rootfs</code></a>)</p>
<p><a id="Creating.Swap" href="#Creating.Swap" rel="bookmark" title="Creating Swap"><br />
<h4>Creating Swap</h4>
<p></a></p>
<p><code>mk_mmc.sh</code> also offers optional <code>swap</code> creation. It is a &#8220;file&#8221; inside <code>rootfs</code> partition created by</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>host<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">dd</span> <span style="color: #007800;"><span style="color: #000000; font-weight: bold;">if</span></span>=<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>zero <span style="color: #007800;">of</span>=<span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>rootfs<span style="color: #000000; font-weight: bold;">/</span>SWAP.swap <span style="color: #007800;">bs</span>=1M <span style="color: #007800;">count</span>=<span style="color: #000000;">250</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>host<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mkswap <span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>rootfs<span style="color: #000000; font-weight: bold;">/</span>SWAP.swap</pre></div></div>

<p>(the above will create 262MB swap inside <code>rootfs</code> SD card currently mounted to <code>/media/rootfs</code> of the laptop)</p>
<p>It will be mounted as <code>swap</code> by the ARM-Linux board via <code>fstab</code> setting configured below.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>host<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;/SWAP.swap  none  swap  sw  0 0&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>rootfs<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>fstab</pre></div></div>

<p>Synchronize and safely remove the card</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>host<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">sync</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>host<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">umount</span> <span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>rootfs</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>https://lakm.us/logit/2013/03/duplicate-restore-arm-linux-image-mmc-sd-card-beagleboard-devkit-board/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
