<?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/"
	xmlns:georss="http://www.georss.org/georss" >

<channel>
	<title>Even the wrong words seem to rhyme &#187; udev</title>
	<atom:link href="http://www.kevinblake.co.uk/tag/udev/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kevinblake.co.uk</link>
	<description>a blog-life by Kevin Blake</description>
	<lastBuildDate>Thu, 13 May 2010 22:27:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Auto running commands when plugging in usb drives with udev in Linux</title>
		<link>http://www.kevinblake.co.uk/auto-running-commands-when-plugging-in-usb-drives-with-udev-in-linux/732/</link>
		<comments>http://www.kevinblake.co.uk/auto-running-commands-when-plugging-in-usb-drives-with-udev-in-linux/732/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 22:53:52 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[backups]]></category>
		<category><![CDATA[Fedora]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[udev]]></category>

		<guid isPermaLink="false">http://www.kevinblake.co.uk/?p=732</guid>
		<description><![CDATA[My backup strategy for my machines at home can be effectively described as &#8220;scatty, but thorough&#8220;.  Or at least until I actually have a major crash and need to recover that important file that was outside of my normal documents, code repositories, and archived folders. I am one of those sorts of people that has [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_745" class="wp-caption alignright" style="width: 160px"><a href="http://www.kevinblake.co.uk/wp-content/uploads/2010/01/backups-of-backups.jpg" rel="lightbox"><img class="size-thumbnail wp-image-745" title="Backups of Backups" src="http://www.kevinblake.co.uk/wp-content/uploads/2010/01/backups-of-backups-150x150.jpg" alt="" width="150" height="150" /></a><p class="wp-caption-text">Backups of Backups</p></div>
<p>My backup strategy for my machines at home can be effectively described as &#8220;<em>scatty, but thorough</em>&#8220;.  Or at least until I actually have a major crash and need to recover that important file that was outside of my normal documents, code repositories, and archived folders.</p>
<p>I am one of those sorts of people that has backups everywhere.  Folders stacked away with old dusty (and probably now useless) dvds.  Old hard drives filled with duplicates and archived files, stacked up in the back of cupboards and flung next to jam jars.  SD cards, USB keys, backups to other drives, backups to the machines on the other side of my flat, backups off-site.  Backups of backups backed up during the last backup.</p>
<p>So I decided to add one more with an external hard drive that I could plug in every now and then.  But backups aren&#8217;t really the point of this post.</p>
<p>Linux has a really powerful device manager called udev which detects when things are plugged into your machine (including hard drives), which you can write rules against, and have commands automagically executed.  This is really cool for my new hard drive, which can now automatically start backing up without regular cron jobs checking to see if it&#8217;s plugged in or not.  This is not the same as autorun files, it relies on <strong>that </strong>hard drive being plugged into <strong>that </strong>machine.  So don&#8217;t start crying about all the security risks with autorun, please.</p>
<p>Here&#8217;s how you do it.</p>
<div id="wpshdo_1" class="wp-synhighlighter-outer"><div id="wpshdt_1" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_1"></a><a id="wpshat_1" class="wp-synhighlighter-title" href="#codesyntax_1"  onClick="javascript:wpsh_toggleBlock(1)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_1" onClick="javascript:wpsh_code(1)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.kevinblake.co.uk/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_1" onClick="javascript:wpsh_print(1)" title="Print code"><img border="0" style="border: 0 none" src="http://www.kevinblake.co.uk/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.kevinblake.co.uk/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.kevinblake.co.uk/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_1" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash" style="font-family:monospace;">udevadm info <span class="re5">-a</span> <span class="re5">-p</span>  $<span class="br0">&#40;</span>udevadm info <span class="re5">-q</span> path <span class="re5">-n</span> <span class="sy0">/</span>dev<span class="sy0">/</span>sdc<span class="br0">&#41;</span></pre></div></div>
<p>You&#8217;ll get a whole bunch of output from that, including (among a lot of other output) a line that might look a little like this:</p>
<div id="wpshdo_2" class="wp-synhighlighter-outer"><div id="wpshdt_2" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_2"></a><a id="wpshat_2" class="wp-synhighlighter-title" href="#codesyntax_2"  onClick="javascript:wpsh_toggleBlock(2)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_2" onClick="javascript:wpsh_code(2)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.kevinblake.co.uk/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_2" onClick="javascript:wpsh_print(2)" title="Print code"><img border="0" style="border: 0 none" src="http://www.kevinblake.co.uk/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.kevinblake.co.uk/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.kevinblake.co.uk/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_2" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash" style="font-family:monospace;">ATTR<span class="br0">&#123;</span>serial<span class="br0">&#125;</span>==<span class="st0">&quot;312581808&quot;</span></pre></div></div>
<p>There are a lot more you can use as well, but this will identify your device.</p>
<p>Now create a new file inside <em>/etc/udev/rules.d/</em>, such as <em>/etc/udev/rules.d/81-usb-drive.rules</em></p>
<div id="wpshdo_3" class="wp-synhighlighter-outer"><div id="wpshdt_3" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_3"></a><a id="wpshat_3" class="wp-synhighlighter-title" href="#codesyntax_3"  onClick="javascript:wpsh_toggleBlock(3)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_3" onClick="javascript:wpsh_code(3)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.kevinblake.co.uk/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_3" onClick="javascript:wpsh_print(3)" title="Print code"><img border="0" style="border: 0 none" src="http://www.kevinblake.co.uk/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.kevinblake.co.uk/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.kevinblake.co.uk/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_3" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash" style="font-family:monospace;"><span class="re2">KERNEL</span>==<span class="st0">&quot;sd?1&quot;</span>, ATTRS<span class="br0">&#123;</span>serial<span class="br0">&#125;</span>==<span class="st0">&quot;312581808&quot;</span>, SYMLINK+=<span class="st0">&quot;backup-drive&quot;</span>, RUN+=<span class="st0">&quot;/bin/sh /home/user/scripts/backup-to-drive.sh&quot;</span></pre></div></div>
<p>Now every time you plug in that drive, that command is going to be executed (so include for example, the script that is going to run your backups).  Cool, eh?</p>
<p>I started thinking about other applications; syncing podcasts, ebooks and music are the obvious choices. Even as a crude simple method of executing commands on a box you don&#8217;t even usually login to.  Or you could use this technique to build yourself some poor man usb-based security.  Keep decryption keys on your thumb drive, and have it auto decrypt volumes on your machine when you plug in that particular drive.</p>
<p>Just don&#8217;t forget to keep a backup of the thumb drive, yeah?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kevinblake.co.uk/auto-running-commands-when-plugging-in-usb-drives-with-udev-in-linux/732/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
