<?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"
	>

<channel>
	<title>Not really a blog</title>
	<atom:link href="http://blog.notreally.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.notreally.org</link>
	<description>or is it?</description>
	<pubDate>Mon, 11 Feb 2008 00:05:29 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>Tricks to diagnose processes blocked on strong I/O in linux</title>
		<link>http://blog.notreally.org/articles/2008/02/10/tricks-to-diagnose-processes-blocked-on-strong-io-in-linux/</link>
		<comments>http://blog.notreally.org/articles/2008/02/10/tricks-to-diagnose-processes-blocked-on-strong-io-in-linux/#comments</comments>
		<pubDate>Sun, 10 Feb 2008 23:59:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[linux]]></category>

		<category><![CDATA[sysadmin]]></category>

		<category><![CDATA[linux tricks hack]]></category>

		<guid isPermaLink="false">http://blog.notreally.org/articles/2008/02/10/tricks-to-diagnose-processes-blocked-on-strong-io-in-linux/</guid>
		<description><![CDATA[There&#8217;s one aspect of the linux kernel and the GNU operating system and related tools in which it might be lacking behind, specially with kernel 2.4 series. I&#8217;m talking about I/O accounting or how to know what&#8217;s going on with the hard disc or other devices which are used to write and read data.
The thing [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s one aspect of the linux kernel and the GNU operating system and related tools in which it might be lacking behind, specially with kernel 2.4 series. I&#8217;m talking about I/O accounting or how to know what&#8217;s going on with the hard disc or other devices which are used to write and read data.</p>
<p>The thing is that linux provides you with a few tools with which you can tell what&#8217;s going on with the box and its set of discs. Say <strong>vmstat</strong> provides you with a lot of information and various other files scattered in the <tt>/proc</tt> filesystem. But that information only tells us about the system globally, so it&#8217;s good for diagnosing if a high load on a box is due to some process chewing CPU cycles away or because of the hard disc being hammered and being painfully slow. But what about if you want to know what exactly is going on, which process or processes are responsible for the situation, how do you know? The answer is that Linux doesn&#8217;t provide you with tools for that, as far as I know (If you know of any, please leave a comment). There&#8217;s no such thing as a top utility for process I/O accounting. The situation is better in Linux 2.6 provided you activate the <a href="http://www.mjmwired.net/kernel/Documentation/accounting/taskstats.txt">taskstats accounting module</a> with which you can query information about the processes. The user-space utilities are <a href="http://search.cpan.org/~scottw/Linux-Taskstats-Read-4.00/">somewhat scarce</a>, but at least there&#8217;s something with which you can start playing.</p>
<p>However there are some tricks you can use to try to find which is the culprit when things go wrong. As usually, many of these tricks come from work where I keep learning from my colleagues, who, by the way, are  much more intelligent than I am ;-), when things go wrong and some problem arises that needs immediate action.</p>
<p>So, let&#8217;s define the typical scenario on which we could apply these tricks. You&#8217;ve got a Linux box which has a high load average. Say 15, 20, etc. As you may know, the load average measures the number of processes that are waiting to be executed on the process queue. That doesn&#8217;t necessarily mean that the CPU is loaded when, for example, processes are blocked because of I/O, say a read from the disc because this is slow or something. The CPU would just sit there most of the time being idle. This number makes sense when you know the number of CPU the box has. If you have a <em>loadavg</em> of 2 in a two-CPU box, then you are just fine, ideally.</p>
<p>The number one tool for  identifying what&#8217;s going on is <a href="http://gd4.tuwien.ac.at/.vhost/linuxcommand.org/man_pages/vmstat8.html">vmstat</a>, which would tell you a lot of things going on in the box, specially when you execute it periodically as <tt>vmstat 1</tt>. If you read the man page (and I do recommend you to read it), you can get an idea of all the information what would be going through the screen :-). <a href="http://www.flickr.com/photos/golan/464063579/sizes/o/">Click here to see a screen shot of the output of <em>vmstat</em> on 4 different boxes</a>. Almost all of its output is useful for diagnosis, except the last column in the case of a Linux 2.4 box (that value is added to the idle column).</p>
<p>With this tool we can find out if the system is busy on I/O and how. For example by looking at the <strong>bo</strong> and <strong>bi</strong> columns. Swapping, when it&#8217;s happening, could also imply that the hard disc is being hammered but that would also mean that there&#8217;s not enough memory in the system for all the processes running at that very moment. Well, all of its output can be useful for identifying what&#8217;s going on.</p>
<p>Ok, so back to our problem, how do we start? Well, the first thing to do is to try to find out what&#8217;s on execution that could be causing this. Who are the usual suspects. By looking at <tt>ps</tt> output we could get an idea of which processes and/or application could be causing the disc I/O. The problem with this is that sometimes an application runs tens or hundreds of processes, each of which is serving a remote client (say apache prefork) and maybe only some of them are causing the havoc, so killing all possible processes is not an option in a production environment (possibly killing the processes causing the problem is feasible because they might be wedged or something).</p>
<p><strong>Finding the suspects</strong></p>
<p>One way to find what processes are causing the writes and reads  is to have a look at the processes in  <a href="http://en.wikipedia.org/wiki/Uninterruptible_sleep">uninterruptible sleep</a> state. As the box has a high load average because of I/O, surely there must be processes in such a state because they are waiting for the disc to return back the data and return from their system calls. And these processes are likely to be involved in the high load of the system. If you think that uninterruptible sleep processes cannot be killed you are right, but we are assuming that they are in this state briefly again and again because of reading and writing to the disc non-stop. If you have read the vmstat man page, you must have noticed that the column <strong>b</strong> tells us the number of processes in such a state.</p>
<pre>golan@kore:~$ ps aux | grep " D"
root     27351  2.9  0.2 11992 9160 ?        DN   23:06   0:08 /usr/bin/python /usr/bin/rdiff-backup -v 5 --restrict-read-only /disk --server
mail     28652  0.5  0.0  4948 1840 ?        D    23:11   0:00 exim -bd -q5m
golan    28670  0.0  0.0  2684  804 pts/23   S+   23:11   0:00 grep --color  D</pre>
<p>Here we can see two processes in such a state (noted by <strong>D </strong>on ps output). Normally we don&#8217;t get to see many of these at the same time and if we issue the same command again, we are probably not going to see it again unless there is a problem which is why I&#8217;m writing this in the first place :-).</p>
<p><strong>Examining the suspects</strong></p>
<p>Well, we now need to examine the suspects and filter them, because there might be perfectly valid processes that are in uninterruptible sleep state but are not responsible of the high load, so we need to find out. One thing that we could do is attach <a href="http://sourceforge.net/projects/strace/">strace</a> to a specific process and see how it&#8217;s doing. This can be easily achieved this way:</p>
<pre>golan@kore:~$ strace -p 12766
Process 12766 attached - interrupt to quit
write(1, "y\n", 2)                      = 2</pre>
<p>Here we see the output of a process executing <strong>yes</strong>. So, what does this output tell us? It shows us all the system calls that the process is doing, so we can effectively see if it is reading or writing.</p>
<p>But all this can be very time consuming if we have quite a few processes to examine. What we could do is strace all of them and save their output to different files and then examine them later:</p>
<p>If what we are examining is a process called <strong>command</strong>, we could  do it this way:</p>
<pre>
# mkdir /tmp/strace
# cd /tmp/strace
# for i in `ps axuwf | grep command | awk '{ print $2 }'`; do (strace -p $i > command-$i.strace 2>&#038;1)&#038;  done
</pre>
<p>What this would do is create a series of files called <tt>command-PID-strace</tt>, one for each of the processes that match the regular expression in the grep command. If we set this running for a while, we can now examine the contents of all the files. Even better if we display the files ordered by <strong>size</strong> we would have a pointer to the process that are doing the most system calls. All we would need to do is verify that those system calls are actually read and write system calls. And also, don&#8217;t forget to kill all the strace processes that we sent to the background by issuing a <tt>killall strace</tt> :-)</p>
<p>So now we have a list of processes that are causing lots of reads and writes in the hard disc. What to do next depends on the situation and what you want to do. You might want to kill the processes, or find who (the person) who started them, in case they were started by someone. Or which network connection, IP address, etc etc. There are a bunch of utilities that you can use including strace, <a href="http://en.wikipedia.org/wiki/Netstat">netstat</a>, <a href="http://en.wikipedia.org/wiki/Lsof">lsof</a>, etc. It&#8217;s up to you what to do next.  </p>
<p><strong>And&#8230;</strong></p>
<p>Well, This is me learning from my colleagues and from problems that arise when you don&#8217;t expect them. My understanding of the linux kernel is not that good, but now many of the things that I studied in the Operating System class start to make a little bit more sense. So please, if you have experience with this, know of other ways to get this kind of information, please share it with me (as a comment or otherwise). I&#8217;m still learning :)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.notreally.org/articles/2008/02/10/tricks-to-diagnose-processes-blocked-on-strong-io-in-linux/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Installing From Source, The Easy Way</title>
		<link>http://blog.notreally.org/articles/2008/01/21/installing-from-source-the-easy-way/</link>
		<comments>http://blog.notreally.org/articles/2008/01/21/installing-from-source-the-easy-way/#comments</comments>
		<pubDate>Mon, 21 Jan 2008 00:40:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[linux]]></category>

		<category><![CDATA[sysadmin]]></category>

		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.notreally.org/articles/2008/01/21/installing-from-source-the-easy-way/</guid>
		<description><![CDATA[Installing software in any unix-like operating system these days has become very easy. Package managers such as dpkg, the one used by Debian or Ubuntu, take most of the hassle by dealing with all the dependencies and intricacies that modern software  has nowadays. It&#8217;s just a matter of getting the package that some hard-working [...]]]></description>
			<content:encoded><![CDATA[<p>Installing software in any unix-like operating system these days has become very easy. Package managers such as dpkg, the one used by <a href="http://www.debian.org" title="Debian">Debian</a> or <a href="http://www.ubuntu.com" title="Ubuntu">Ubuntu</a>, take most of the hassle by dealing with all the dependencies and intricacies that modern software  has nowadays. It&#8217;s just a matter of getting the package that some hard-working and/or generous developer has made and install it in our system. It&#8217;s straightforward compared with how things were a mere few years ago.</p>
<p>We&#8217;ve always had the possibility of installing from the software, provided we have resolved all the needed dependencies. Installing from the source can be handy and useful at times. We might want to change some options on compile time or we might want to have two versions of the same package installed on different locations for example.</p>
<p>The problem lies when we want to upgrade the software and we have different versions of it installed, we could end up in a very cluttered scenario, say, with files installed across the file system from different versions of it. Even more, we might not have an easy way to track down which files belong to which version, let alone uninstalling the software.</p>
<p><strong>The Easy Way ® ;-)</strong></p>
<p>As always there are simple solutions for complex problems. There is a nice piece of software which helps us to keep track of software packages installed from source in a clean way. It doesn&#8217;t work for all cases, but it does a pretty good job for most of them. I&#8217;m talking about <a href="http://www.encap.org/epkg/">epkg</a>,<em> The Encap Package Manager</em>.</p>
<p>I&#8217;ll try to describe how it works in a not very technical or detailed way, just to get you going with it and then I&#8217;ll install it on my system so you&#8217;ll be able to see how handy it is.</p>
<p>Basically, all you have to do is install all software packages on a directory on <code>/usr/local/encap</code>, creating a directory for each of them  in a <code>package-version.revision</code> fashion. Then we will use epkg to just create symlinks to the appropriate places, usually <code>/usr/local</code>.</p>
<p>So, say we&#8217;ve got:</p>
<pre>
/usr/local/encap/mysoft-1.1/bin/mysoft
/usr/local/encap/mysoft-1.1/lib/mysoft.so</pre>
<p>epkg would create symlinks such as:</p>
<pre>
/usr/local/bin/mysoft  ->  /usr/local/encap/mysoft-1.1/bin/mysoft
/usr/local/lib/mysoft.so  -> /usr/local/encap/mysoft-1.1/lib/mysoft.so</pre>
<p>and that&#8217;s it, pretty much. With more complex packages it can get more difficult, but you get the idea.</p>
<p><strong>Let&#8217;s just see an example </strong></p>
<p>First of all, we need to install epkg on our system. I will be using an Ubuntu 7.10 system, which, to date, doesn&#8217;t have epkg on it. So I will install it from source in the usual way, to <code>/usr/local</code></p>

<div class="wp_syntax"><div class="code"><pre>root@kore:/usr/local/src# wget ftp://ftp.encap.org/pub/encap/epkg/epkg-2.3.9.tar.gz
--23:47:17--  ftp://ftp.encap.org/pub/encap/epkg/epkg-2.3.9.tar.gz
           =&gt; `epkg-2.3.9.tar.gz'
...
23:47:32 (85.66 KB/s) - `epkg-2.3.9.tar.gz' saved [237232]
&nbsp;
root@kore:/usr/local/src# tar xfz epkg-2.3.9.tar.gz
root@kore:/usr/local/src# cd epkg-2.3.9/
root@kore:/usr/local/src/epkg-2.3.9#
root@kore:/usr/local/src/epkg-2.3.9# ./configure --prefix=/usr/local
checking for epkg... no
checking for mkencap... no
checking for Encap source directory... /usr/local/encap
checking for Encap target directory... /usr/local
checking for Encap package directory... /usr/local/encap/epkg-2.3.9
checking for gcc... gcc
...
config.status: creating epkg/Makefile
config.status: creating mkencap/Makefile
config.status: creating mkencap/mkencap_environment
config.status: creating doc/Makefile
config.status: creating config.h</pre></div></div>

<p>As we see, we&#8217;ll install the package with its default options, pointing to to <code>/usr/local/encap</code> as the <em>encap</em> directory. Please, see the help for more options.</p>
<p>We install it:</p>

<div class="wp_syntax"><div class="code"><pre>root@kore:/usr/local/src/epkg-2.3.9# make &amp;&amp; make install
...
epkg: installing package epkg-2.3.9...
  &gt; reading Encap source directory...
  &gt; installing package epkg-2.3.9
    !  man: not an Encap link
    &gt; executing postinstall script
installing: /usr/local/etc/mkencap_environment
    &gt; installation partially successful
root@kore:/usr/local/src/epkg-2.3.9#</pre></div></div>

<p>If we have a look at <code>/usr/local/bin</code> and <code>/usr/local/encap</code> it has installed itself as an <em>encapped</em> package :), and now we are ready to use it with a real example.</p>
<p><strong>Installing GLE</strong></p>
<p>Say we wanted to install <a href="http://glx.sourceforge.net/">The Graphics Layout Engine, or GLE</a> on our computer and we don&#8217;t have a binary package at hand, or we want to control it, or whatever :), let&#8217;s just do it with epkg.</p>
<ol>
<li><strong>Get the source</strong>

<div class="wp_syntax"><div class="code"><pre>root@kore:/usr/local/src# wget http://surfnet.dl.sourceforge.net/sourceforge/glx/GLE-4.1.1-src.zip
root@kore:/usr/local/src# unzip GLE-4.1.1-src.zip
root@kore:/usr/local/src# cd gle4/</pre></div></div>

</li>
<li><strong>Configure</strong>: We will be configuring the software to make it believe it is going to be installed on <code>/usr/local</code> but we will actually install it on <code>/usr/local/encap/</code> instead, so epkg can deal with it. This is an <strong>important</strong> step, so let&#8217;s just do it by configuring it with those options and with any other that we might want to use:

<div class="wp_syntax"><div class="code"><pre>root@kore:/usr/local/src/gle4# aptitude install libpng12-dev libpng12-0 libtiff4-dev libtiff4 libjpeg62-dev libjpeg62
root@kore:/usr/local/src/gle4# ./configure --with-qt=no --prefix=/usr/local
...
root@kore:/usr/local/src/gle4# make
...</pre></div></div>

<p>As you can see, I installed some dev packages (using debian&#8217;s aptitude) because they are dependencies for GLE. After that, I configure the package without any graphical environment (based on <a href="http://www.trolltech.no">Qt</a>) and pointing to <code>/usr/local</code>. Then we compile it.</li>
<li><strong>Installing</strong>. Now, we will be installing it on <code>/usr/local/encap</code>. Bear with me now and I&#8217;ll explain what I did after doing it :)

<div class="wp_syntax"><div class="code"><pre>root@kore:/usr/local/src/gle4# make DESTDIR=/usr/local/encap/GLE-4.1.1 install
root@kore:/usr/local/encap/GLE-4.1.1#
root@kore:/usr/local/encap/GLE-4.1.1# mv usr/local/* .
root@kore:/usr/local/encap/GLE-4.1.1# rm -rf usr
root@kore:/usr/local/encap/GLE-4.1.1# ls
bin  lib  share</pre></div></div>

<p>Ok, we what I&#8217;ve done is execute <em><code>make install</code></em> but setting the <code>DESTDIR</code> variable (which is supported by GLE&#8217;s <code>Makefile</code>) to install it on <code>/usr/local/encap/GLE-4.1.1</code>. But there, it is usually installed within its own &#8220;<code>usr/local</code>&#8221; directory, so to make it be as if it were installed on <code>/usr/local</code> hierarchy, we move it to the right place so now we have:</p>
<pre>
/usr/local/encap/GLE-4.1.1/bin
/usr/local/encap/GLE-4.1.1/lib
/usr/local/encap/GLE-4.1.1/share</pre>
<p>and so on.</li>
<li><strong>Install it with epkg</strong> Now the final step is to call epkg to actually create the proper symlinks and that&#8217;s it:

<div class="wp_syntax"><div class="code"><pre>root@kore:/usr/local/encap/GLE-4.1.1# epkg GLE
epkg: installing package GLE...
  &gt; reading Encap source directory...
  &gt; installing package GLE-4.1.1
    &gt; installation successful
root@kore:/usr/local/encap/GLE-4.1.1#
root@kore:/usr/local/encap/GLE-4.1.1# gle
GLE version 4.1.1
Usage: gle [options] filename.gle
More information: gle -help
root@kore:/usr/local/encap/GLE-4.1.1#</pre></div></div>

</li>
</ol>
<p>And that&#8217;s it, really. Now, two things,</p>
<ul>
<li> If we install a newer version, say 4.2.0 whenever that&#8217;s ready, we just install it on <code>/usr/local/encap/GLE-4.2.0</code> as we&#8217;ve seen before, and simple calling again
<pre>
# epkg GLE</pre>
<p>would create the right symlinks (that&#8217;s it, &#8220;deinstall&#8221; the previous version and install the new one.</li>
<li> If we want to uninstall it, that&#8217;s it, remove the symlinks, we simply issue this command:
<pre>
# epkg -r GLE</pre>
<p>and that&#8217;s all.</li>
</ul>
<p><strong>Summary</strong></p>
<ol>
<li> <strong>epkg</strong> lets us install software from source having control over it, ie. Installing it in a clean way, being able to deinstall it and upgrade it without cluttering the file system</li>
<li> All you have to do is install the software on <code>/usr/local/encap/package-version</code> as if were <code>/usr/local</code>. The variable <strong><code>DESTDIR</code></strong> on <a href="http://en.wikipedia.org/wiki/Makefile"><code>Makefiles</code></a> helps us to do it in an easy way. If the software is too simple, you&#8217;ll have to do it manually.</li>
<li> Remember <code>/usr/local/encap/package-version/usr/local/bin</code> must end up as <code>/usr/local/encap/package-version/bin</code>.</li>
<li> Execute <code>epkg package</code> to install it and <code>epkg -r package</code> to uninstall it.</li>
<li>If you have problems with the libraries, try executing <a href="http://linux.die.net/man/8/ldconfig"><code>ldconfig</code></a>.</li>
<li> <strong>Be Careful</strong> I usually make mistakes, overwrite things and delete files, so take care with what you do and do it under your own responsibility ;-).</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://blog.notreally.org/articles/2008/01/21/installing-from-source-the-easy-way/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Modifying a live linux kernel</title>
		<link>http://blog.notreally.org/articles/2007/12/19/modifying-a-live-linux-kernel/</link>
		<comments>http://blog.notreally.org/articles/2007/12/19/modifying-a-live-linux-kernel/#comments</comments>
		<pubDate>Wed, 19 Dec 2007 17:22:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[linux]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://blog.notreally.org/articles/2007/12/19/modifying-a-live-linux-kernel/</guid>
		<description><![CDATA[Before reading this, I just need to say something:
 I&#8217;ve no idea of linux, I&#8217;ve no idea of programming, I&#8217;ve no idea of computers&#8230; Everything you read here might have been invented, so, please, do not reproduce what I write here. If you do, bear in mind that you do it under your own responsibility. [...]]]></description>
			<content:encoded><![CDATA[<p>Before reading this, I just need to say something:</p>
<blockquote><p> I&#8217;ve no idea of linux, I&#8217;ve no idea of programming, I&#8217;ve no idea of computers&#8230; Everything you read here might have been invented, so, please, do not reproduce what I write here. If you do, bear in mind that you do it under your own responsibility. In fact, what is a computer anyway?</p></blockquote>
<p>The other day we were having issues with a box that was used as a NFS box among other things. These issues appeared on upgrading this box from kernel 2.4 to <a href="http://www.kernel.org">kernel 2.6.22.1</a>. These issues were related to locking on the NFS server, because of changing the behaviour of the <a href="http://linux.die.net/man/2/flock">flock system call</a> on linux 2.6.11-18. From the<a href="http://nfs.sourceforge.net/"> NFS FAQ</a>:</p>
<blockquote><p> The NFS client in 2.6.12 provides support for <tt>flock()</tt>/BSD locks on NFS files by emulating the BSD-style locks in terms of POSIX byte range locks. Other NFS clients that use the same emulation mechanism, or that use <tt>fcntl()</tt>/POSIX locks, will then see the same locks that the Linux NFS client sees.</p></blockquote>
<p>The problems we had are related to using NFS and Samba for exporting the same file system and locking not working properly.</p>
<blockquote><p>
SMB supports two types of locks - file-wide locks and byte-range locks.</p>
<ul>
<li>File-wide locks
<ul>
<li> Called &#8217;share modes&#8217; in SMB parlance</li>
<li> Also known as &#8216;BSD-style locks&#8217;
<li>
<li> provided by flock() in Linux</li>
<li> provided by a &#8217;share mode&#8217; flag when opening a file under Win32</li>
<li>Supported primarily by samba within samba itself by storing in a TDB - get listed under &#8216;Locked Files&#8217; at the bottom of smbstatus</li>
<li>May also be enforced in the kernel using flock() if HAVE_KERNEL_SHARE_MODES is 1.</li>
</ul>
</li>
<li>Byte-range locks
<ul>
<li> Called &#8216;POSIX-style&#8217; locks.</li>
<li> provided by fcntl(fd, F_GETLK) in POSIX.</li>
<li> provided by _locking() in Win32</li>
<li> lockf() is a Linux wrapper around fcntl() for locking the whole file.</li>
<li> Supported by samba by a &#8216;Windows to POSIX byte-range overlap conversion layer&#8217; and then fcntl().</li>
</ul>
</li>
</ul>
<p>Windows applications appear to use both share modes and byterange locks.</p>
<p>In Linux, flock() and fcntl() locks are oblivious to each other, as per <a href="http://lxr.linux.no/source/Documentation/locks.txt"> http://lxr.linux.no/source/Documentation/locks.txt</a>. </p>
<p>NFSv3 (as a protocol) only supports byte-range locks.  However, nfsd does flock() locks on files on the server taken out by other processes - although clients cannot set them themselves.  See <a href="http://nfs.sourceforge.net/#faq_d10"> http://nfs.sourceforge.net/#faq_d10</a> </p>
<p>Unfortunately, linux 2.6.12 adds flock() emulation to the Linux NFS client by translating it into a file-wide fcntl().  This means that flock()s and fcntl()s *do collide* on remote NFS shares, which introduces all the potential application race conditions which Linux avoided by having them oblivious to each other locally.  The practical upshot of this is that if you re-share an NFS share via samba, then if a Windows client (e.g. Outlook opening a PST file) opens a file with a share mode, then byte-range locking operations will fail as the lock has already been acquired.  (The fact that NFS doesn&#8217;t realise the same PID has both locks and allow them both is probably an even bigger problem).  The solution for this is to revert bits of the patch responsible: <a href="http://www.linux-nfs.org/Linux-2.6.x/2.6.11/linux-2.6.11-18-flock.dif"> http://www.linux-nfs.org/Linux-2.6.x/2.6.11/linux-2.6.11-18-flock.dif</a>.  Disabling share modes in samba is not an option, as it also disables the application-layer TDB support for them - and disabling HAVE_KERNEL_SHARE_MODES will stop other programs (e.g. nfsd) on dump being aware of what&#8217;s been flock()ed. </p>
</blockquote>
<p>So our solution for our server was reverting this patch on 2.6.11-18 and apply this patch:</p>

<div class="wp_syntax"><div class="code"><pre class="diff"><span style="color: #888822;">--- fs/nfs/file.c       <span style="">2007</span><span style="">-07</span><span style="">-10</span> <span style="">19</span>:<span style="">56</span>:<span style="">30.000000000</span> <span style="">+0100</span></span>
<span style="color: #888822;">+++ fs/nfs/file.c.nfs_flock_fix <span style="">2007</span><span style="">-11</span><span style="">-13</span> <span style="">13</span>:<span style="">40</span>:<span style="">06.000000000</span> <span style="">+0000</span></span>
<span style="color: #440088;">@@ <span style="">-543</span>,<span style="">10</span> <span style="">+543</span>,<span style="">24</span> @@</span>
         * Not sure whether that would be unique, though, or whether
         * that would break in other places.
         */
<span style="color: #991111;">-       if <span style="">&#40;</span>!<span style="">&#40;</span>fl-&gt;fl_flags &amp; FL_FLOCK<span style="">&#41;</span><span style="">&#41;</span></span>
<span style="color: #00b000;">+</span>
<span style="color: #00b000;">+       /**</span>
<span style="color: #00b000;">+        * Don't simulate flock<span style="">&#40;</span><span style="">&#41;</span> using posix locks, as they appear to collide with</span>
<span style="color: #00b000;">+        * legitimate posix locks from the same process.</span>
<span style="color: #00b000;">+        */</span>
<span style="color: #00b000;">+       if <span style="">&#40;</span>fl-&gt;fl_flags &amp; FL_FLOCK<span style="">&#41;</span></span>
                return -ENOLCK;
&nbsp;
        /* We're simulating flock<span style="">&#40;</span><span style="">&#41;</span> locks using posix locks on the server */
<span style="color: #00b000;">+       /* ...except we shouldn't get here, due to the above patch. */</span>
        fl-&gt;fl_owner = <span style="">&#40;</span>fl_owner_t<span style="">&#41;</span>filp;
        fl-&gt;fl_start = <span style="">0</span>;
        fl-&gt;fl_end = OFFSET_MAX;</pre></div></div>

<p>So, for us, recompiling the kernel with such patch on the production server fixes all our problems. But, what if we wanted to do this live, could such a subtle change be done without rebooting? You might be thinking right now about the different options that you have on /proc about changing the behaviour of the kernel live, but what if you don&#8217;t have such option? What if we wanted to change something and there was no way to do this because it is not implemented or not possible?</p>
<p><strong>Let&#8217;s see.</strong></p>
<p>So, <strong>from an academic point of view</strong> we wanted to see if this could really be done. If the linux kernel would let us do that, if it was feasible. So, we set up a testing box in which we would try to modify the running kernel. How hard would be to do it on the testing box?</p>
<p>It seems there&#8217;s a way to do it which my colleague Matthew came up with (all credit to him, I&#8217;m just telling the story). Let&#8217;s examine the piece of code that we want to change. The offending code lives on the file fs/nfs/file.c of the linux kernel</p>

<div class="wp_syntax"><div class="code"><pre class="c"><span style="color: #808080; font-style: italic;">/*
 * Lock a (portion of) a file
 */</span>
<span style="color: #993333;">static</span> <span style="color: #993333;">int</span> nfs_flock<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">struct</span> file *filp, <span style="color: #993333;">int</span> cmd, <span style="color: #993333;">struct</span> file_lock *fl<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
        dprintk<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;NFS: nfs_flock(f=%s/%ld, t=%x, fl=%x)n&quot;</span>,
                        filp-&gt;f_path.<span style="color: #202020;">dentry</span>-&gt;d_inode-&gt;i_sb-&gt;s_id,
                        filp-&gt;f_path.<span style="color: #202020;">dentry</span>-&gt;d_inode-&gt;i_ino,
                        fl-&gt;fl_type, fl-&gt;fl_flags<span style="color: #66cc66;">&#41;</span>;
         <span style="color: #808080; font-style: italic;">/*
         * No BSD flocks over NFS allowed.
         * Note: we could try to fake a POSIX lock request here by
         * using ((u32) filp | 0x80000000) or some such as the pid.
         * Not sure whether that would be unique, though, or whether
         * that would break in other places.
         */</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #66cc66;">&#40;</span>fl-&gt;fl_flags &amp; FL_FLOCK<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #b1b100;">return</span> -ENOLCK;
&nbsp;
        <span style="color: #808080; font-style: italic;">/* We're simulating flock() locks using posix locks on the server */</span>
        fl-&gt;fl_owner = <span style="color: #66cc66;">&#40;</span>fl_owner_t<span style="color: #66cc66;">&#41;</span>filp;
        fl-&gt;fl_start = <span style="color: #cc66cc;">0</span>;
        fl-&gt;fl_end = OFFSET_MAX;
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>fl-&gt;fl_type == F_UNLCK<span style="color: #66cc66;">&#41;</span>
                <span style="color: #b1b100;">return</span> do_unlk<span style="color: #66cc66;">&#40;</span>filp, cmd, fl<span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #b1b100;">return</span> do_setlk<span style="color: #66cc66;">&#40;</span>filp, cmd, fl<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>What we wanted to do is to change the behaviour of the previous function such that the if condition would be:</p>

<div class="wp_syntax"><div class="code"><pre class="c">        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>fl-&gt;fl_flags &amp; FL_FLOCK<span style="color: #66cc66;">&#41;</span>
                <span style="color: #b1b100;">return</span> -ENOLCK;</pre></div></div>

<p>That means that the change is fairly trivial and that it would result in switching an operation, basically changing the way the branching (in the machine code) is done on the <strong>if</strong> instruction. If we disassemble such object file, file.o, we get something like</p>

<div class="wp_syntax"><div class="code"><pre># objdump -d fs/nfs/file.o
...
00000885 &lt;nfs_flock&gt;:
 885:   57                      push   %edi
 886:   89 d7                   mov    %edx,%edi
 888:   56                      push   %esi
 889:   89 c6                   mov    %eax,%esi
 88b:   53                      push   %ebx
 88c:   89 cb                   mov    %ecx,%ebx
 88e:   83 ec 14                sub    $0x14,%esp
 891:   f6 05 00 00 00 00 40    testb  $0x40,0x0
 898:   74 3b                   je     8d5 &lt;nfs_flock+0x50&gt;
 89a:   0f b6 41 2c             movzbl 0x2c(%ecx),%eax
 89e:   89 44 24 10             mov    %eax,0x10(%esp)
 8a2:   0f b6 41 2d             movzbl 0x2d(%ecx),%eax
 8a6:   89 44 24 0c             mov    %eax,0xc(%esp)
 8aa:   8b 56 0c                mov    0xc(%esi),%edx
 8ad:   8b 42 0c                mov    0xc(%edx),%eax
 8b0:   8b 40 20                mov    0x20(%eax),%eax
 8b3:   89 44 24 08             mov    %eax,0x8(%esp)
 8b7:   8b 42 0c                mov    0xc(%edx),%eax
 8ba:   8b 80 9c 00 00 00       mov    0x9c(%eax),%eax
 8c0:   c7 04 24 20 01 00 00    movl   $0x120,(%esp)
 8c7:   05 40 01 00 00          add    $0x140,%eax
 8cc:   89 44 24 04             mov    %eax,0x4(%esp)
 8d0:   e8 fc ff ff ff          call   8d1 &lt;nfs_flock+0x4c&gt;
 8d5:   f6 43 2c 02             testb  $0x2,0x2c(%ebx)
 8d9:   74 47                   je     922 &lt;nfs_flock+0x9d&gt;
 8db:   80 7b 2d 02             cmpb   $0x2,0x2d(%ebx)
 8df:   89 73 14                mov    %esi,0x14(%ebx)
 8e2:   c7 43 30 00 00 00 00    movl   $0x0,0x30(%ebx)
 8e9:   c7 43 34 00 00 00 00    movl   $0x0,0x34(%ebx)
 8f0:   c7 43 38 ff ff ff ff    movl   $0xffffffff,0x38(%ebx)
 8f7:   c7 43 3c ff ff ff 7f    movl   $0x7fffffff,0x3c(%ebx)
 8fe:   75 11                   jne    911 &lt;nfs_flock+0x8c&gt;
 900:   83 c4 14                add    $0x14,%esp
 903:   89 d9                   mov    %ebx,%ecx
 905:   89 f0                   mov    %esi,%eax
...</pre></div></div>

<p>We can actually have a look at the disassembled code of the function flock(). If you have a look at address 0&#215;8d9, there&#8217;s an instruction that looks suspiciously similar to the test carried on the if instructions. If you know assembly and know how a compiler works, you could find out that this jump instruction (JE) is just the one we want to change, exactly to a JNE instruction (I am not going to extend here on assembly and compilers and the like. I guess that if you wanted to do this, you should  already know this. And, by the way, I&#8217;ve no idea of those concepts either, seriously).</p>
<blockquote><p>If you are not sure if that&#8217;s the right instruction, you could recompile the kernel, get the assembly out of the same file.o object and compare it to see what is what changed. </p>
<p>Also, if you look at address 0&#215;898, there another JE instruction which may look like the one we are looking, but this belongs to dprintk as we have debug enabled on that kernel.
</p></blockquote>
<p>If we have a look at the instructions on the <a href="http://www.intel.com/products/processor/manuals/index.htm">IA32 manual</a>, we see that the opcodes for the interesting instructions are:</p>
<ul>
<li> <strong>JE</strong>: 74</li>
<li> <strong>JNE</strong>: 75</li>
</ul>
<p>Ok, so right now, we know that we want to change an JE (with opcode 74) instruction for a JNE instruction (with opcode 75) on address 0&#215;8d9 of the object file &#8220;file.o&#8221;.</p>
<p>The problem now is to find out where on the kernel memory this piece of code lives. One approach that you might think of doing is grepping the whole memory for a particular sequence of instructions. This is not recommended and I will explain why later on. First, let&#8217;s see how we can have access to the kernel memory, where we could possibly modify the data&#8230;</p>
<p>If you have a look at your unix system, you&#8217;ll see that you have a <strong>/dev/kmem</strong> special file, with which you can access the memory from the kernel&#8217;s point of view. This is quite useful as you can access it in read mode and, more interestingly, write mode. However, doing stuff with it might be a bit dangerous, as you might have guessed. It even seems  <a href="http://developer.apple.com/DOCUMENTATION/Darwin/Conceptual/KernelProgramming/security/chapter_3_section_7.html">that some vendors will disable this special file</a>.</p>
<p>Anyway, as I said before, you cannot and don&#8217;t want to read or grep the whole memory at /dev/kmem. It seems that <a href="http://ftp.cerias.purdue.edu/pub/doc/misc/crashing_your_system.txt">the reason is having write-only registers mapped into memory</a>, so a read would crash the system. (You might think that that message is very old, from a distant time, but believe me, it crashes linux if you do so. We tried reading the whole of /dev/mem and the network driver crashed among other little pieces, so don&#8217;t bother).</p>
<p>So basically the thing boils down to:</p>
<ol>
<li> Finding out exactly where we have to change the kernel (ie, at which memory address)</li>
<li> Open /dev/kmem and changing it with the right tool</li>
<li> Hope everything went fine :-)</li>
</ol>
<p><strong> Finding out where </strong></p>
<p>We need to find out where on the kernel we want to change it. This can be easily do by using tools and information the kernel provides us.</p>
<p>First, we need to find out where the kernel function <strong>nfs_flock</strong> starts, and that can be done by having a look at the System.map file that is generated every time we compile a kernel. The <a href="http://dirac.org/linux/system.map/">System.map</a> file is a file that helps kernel developers to debug their code by mapping kernel functions to memory addresses, so it is actually much easier to find  stuff. It contains the kernel symbol table with all symbols.</p>
<p>Ours looks like:</p>
<pre>
...
c01a9b3f t do_unlk
c01a9b97 t do_setlk
c01a9c2b t nfs_lock
c01a9d21 t nfs_flock
c01a9dcc T nfs_get_root
c01a9f3c T nfs_write_inode
...</pre>
<p>So now, we know that <strong>nfs_flock</strong> is located at <strong>0xc01a9d21</strong>. We&#8217;ll use this in a minute.</p>
<p>We saw that we were having a look at the instruction located at <strong>0&#215;8D9</strong> on the object file file.o (got by using objdump before). We also know that, on such object file, nfs_flock starts at <strong>0&#215;885</strong>, right?. That means that, the byte we want to change, is located exactly:</p>
<pre>
0x8D9 - 0x885 = 0x54</pre>
<p>at <strong>nfs_flock + 0&#215;54</strong>.</p>
<p>Well, as you might know, those adresses (on the object file) are relative to such file, and that, when being linked into the actual kernel, the addresses are all relocated and recalculated. So, basically the right point is on</p>
<pre>
0xc01a9d21 + 0x54</pre>
<p><strong>Opening /dev/kmem and modifying it</strong></p>
<p>Opening /dev/kmem needs to be done with the right tools. This is basically because we need LARGE_FILE support on whichever tool we use to modify it, as the /dev/kmem special file is a representation of the kernel memory and we need access it with a tool that supports large files.</p>
<p>In our system, the easiest way to do it is with perl. First, we double check that perl was compiled with LARGE_FILE support:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">root@devbox:~<span style="color: #808080; font-style: italic;"># <span style="color: #c20cb9; font-weight: bold;">perl</span> -V  | <span style="color: #c20cb9; font-weight: bold;">grep</span> LARGE</span>
    <span style="color: #007800;">cc=</span><span style="color: #ff0000;">'cc'</span>, ccflags =<span style="color: #ff0000;">'-fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64'</span>,
  Compile-<span style="color: #000000; font-weight: bold;">time</span> options: USE_LARGE_FILES
root@devbox:~<span style="color: #808080; font-style: italic;">#</span></pre></div></div>

<p>and, now, we can modify the kernel in a oneliner such as:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">root@devbox:~<span style="color: #808080; font-style: italic;"># <span style="color: #c20cb9; font-weight: bold;">perl</span> -e </span><span style="color: #ff0000;">'open (KERNEL, &quot;+&lt;/dev/kmem&quot;) || die $!; seek(KERNEL, 0xc01a9d21 + 0x54, 0); syswrite(KERNEL, chr(0x75)); close(KERNEL);'</span>
root@devbox:~<span style="color: #808080; font-style: italic;">#</span></pre></div></div>

<p>where we are writing <strong>0&#215;75</strong> in the right position (calculated previously).</p>
<p>And that&#8217;s all there is to it. If everthing went fine, the kernel behavior has been modified and nothing has crashed.</p>
<blockquote><p>
Again, only do this under a dev box, under your responsibility and if you really really know what you are doing. And only only only if you want to play with a live kernel. Remember, I know nothing about computers.
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.notreally.org/articles/2007/12/19/modifying-a-live-linux-kernel/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Multitail</title>
		<link>http://blog.notreally.org/articles/2007/07/23/multitail/</link>
		<comments>http://blog.notreally.org/articles/2007/07/23/multitail/#comments</comments>
		<pubDate>Mon, 23 Jul 2007 13:24:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[programming]]></category>

		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://blog.notreally.org/articles/2007/07/23/multitail/</guid>
		<description><![CDATA[Some time ago I discovered Multitail, a tool for displaying in a tail-like fashion any kind of information. I works by splitting the console  window in many parts and displaying the info you want on each of those screens, whether it is tailing a file or the output of a command via a ssh [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago I discovered <a href="http://www.vanheusden.com/multitail/">Multitail</a>, a tool for displaying in a tail-like fashion any kind of information. I works by splitting the console <a href="http://www.flickr.com/photo_zoom.gne?id=464063579&amp;size=o"><img src="http://farm1.static.flickr.com/226/464063579_dbfda54e3e_t.jpg" class="alignright" alt="multitail" align="right" height="70" width="100" /></a> window in many parts and displaying the info you want on each of those <em>screens</em>, whether it is tailing a file or the output of a command via a ssh session. It also has coloring support (which you can extend using regular expressions) to tailor your needs.</p>
<p>I found it really handy when I have to monitor many servers. Just by using some bash power, you can get very nice outputs just by using something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #808080; font-style: italic;">#!/bin/bash</span>
<span style="color: #007800;">rest=</span>$*
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> -z <span style="color: #007800;">$rest</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> ;then
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;You need to specify at least one server&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #007800;">command=</span><span style="color: #ff0000;">&quot;multitail -s 2 &quot;</span>
<span style="color: #000000; font-weight: bold;">for</span> server <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$rest</span>
<span style="color: #000000; font-weight: bold;">do</span>
  <span style="color: #007800;">command=</span><span style="color: #ff0000;">&quot;$command -CS vmstat -t $server -l  <span style="color: #000099; font-weight: bold;">\&quot;</span>ssh $server vmstat 1 <span style="color: #000099; font-weight: bold;">\&quot;</span> &quot;</span>
<span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #7a0874; font-weight: bold;">eval</span> <span style="color: #007800;">$command</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.notreally.org/articles/2007/07/23/multitail/feed/</wfw:commentRss>
		</item>
		<item>
		<title>update</title>
		<link>http://blog.notreally.org/articles/2007/06/10/update/</link>
		<comments>http://blog.notreally.org/articles/2007/06/10/update/#comments</comments>
		<pubDate>Sun, 10 Jun 2007 21:55:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[general]]></category>

		<guid isPermaLink="false">http://blog.notreally.org/articles/2007/06/10/update/</guid>
		<description><![CDATA[Hi all,
This is just a short note to let you know that I have changed this blog over this weekend. Not only I have migrated it to a different box, but also I have (sort of) migrated it to wordpress. I hope that this will prevent some errors that have been around lately (like getting [...]]]></description>
			<content:encoded><![CDATA[<p>Hi all,</p>
<p>This is just a short note to let you know that I have changed this blog over this weekend. Not only I have migrated it to a different box, but also I have (sort of) migrated it to wordpress. I hope that this will prevent some errors that have been around lately (like getting old posts as news, etc).</p>
<p>I hope to get everything fixed (formatting, theme, etc) in the following weeks.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.notreally.org/articles/2007/06/10/update/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Firefox trick</title>
		<link>http://blog.notreally.org/articles/2007/01/26/firefox-trick/</link>
		<comments>http://blog.notreally.org/articles/2007/01/26/firefox-trick/#comments</comments>
		<pubDate>Fri, 26 Jan 2007 10:53:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[computers]]></category>

		<category><![CDATA[internet]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Try this in firefox:

 Go to any page with lots of images.
 Delete the URL bar
 Substitute it with:


javascript:R=0; x1=.1; y1=.05; x2=.25; y2=.24; x3=1.6; y3=.24; x4=300;y4=200; x5=300; y5=200; DI=document.images; DIL=DI.length; function A(){for(i=0; i-DIL; i++){DIS=DI[ i ].style; DIS.position=&#8217;absolute&#8217;; DIS.left=(Math.sin(R*x1+i*x2+x3)*x4+x5)+&#8221;px&#8221;; DIS.top=(Math.cos(R*y1+i*y2+y3)*y4+y5)+&#8221;px&#8221;}R++}setInterval(&#8217;A()&#8217;,5); void(0)
Press Enter and enjoy!!!
:-)
]]></description>
			<content:encoded><![CDATA[<p>Try this in firefox:</p>
<ul>
<li> Go to any page with lots of images.</li>
<li> Delete the URL bar</li>
<li> Substitute it with:</li>
</ul>
<pre>
javascript:R=0; x1=.1; y1=.05; x2=.25; y2=.24; x3=1.6; y3=.24; x4=300;y4=200; x5=300; y5=200; DI=document.images; DIL=DI.length; function A(){for(i=0; i-DIL; i++){DIS=DI[ i ].style; DIS.position=&#8217;absolute&#8217;; DIS.left=(Math.sin(R*x1+i*x2+x3)*x4+x5)+&#8221;px&#8221;; DIS.top=(Math.cos(R*y1+i*y2+y3)*y4+y5)+&#8221;px&#8221;}R++}setInterval(&#8217;A()&#8217;,5); void(0)</pre>
<p>Press Enter and enjoy!!!</p>
<p>:-)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.notreally.org/articles/2007/01/26/firefox-trick/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to monitor packets from a remote interface in real time</title>
		<link>http://blog.notreally.org/articles/2007/01/24/how-to-monitor-packets-from-a-remote-interface/</link>
		<comments>http://blog.notreally.org/articles/2007/01/24/how-to-monitor-packets-from-a-remote-interface/#comments</comments>
		<pubDate>Wed, 24 Jan 2007 13:42:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[computers]]></category>

		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I was trying to debug a connection to the internet that should be controlled by tc, the tool linux uses to do traffic shaping. The scenario is this:

 All outbound traffic through one interface using a specific IP port should not block the rest of the traffic.
 Some other ports and protocols are given priority [...]]]></description>
			<content:encoded><![CDATA[<p>I was trying to debug a connection to the internet that should be controlled by <a href="http://www.rns-nis.co.yu/~mps/linux-tc.html">tc</a>, the tool linux uses to do traffic shaping. The scenario is this:</p>
<ul>
<li> All outbound traffic through one interface using a specific IP port should not block the rest of the traffic.</li>
<li> Some other ports and protocols are given priority (ie: interactive traffic using ssh)</li>
</ul>
<p>But it was failing. tc is quite complicated to get it properly working. For testing purposes, we were using a combination of <a href="http://www.wireshark.org/">wireshark</a> (or ethereal), <a href="http://netcat.sourceforge.net">netcat</a> and <a href="http://iptraf.seul.org/">iptraf</a> to see how the bandwidth was used. My boss showed me a way to get all this working together which is quite handy. I hope he doesn&#8217;t mind at all if I share it with you :-).Imagine we have 3 computers:</p>
<ul>
<li>hostA: our personal computer</li>
<li>hostB: where we are doing traffic shaping</li>
<li>hostC: a test computer where we are going to send data so we can test everything.</li>
</ul>
<p>right, imagine we want to control port 3000 on hostB. For that purpose, we will set up a netcat instance listening on hostC on such port:</p>
<pre>
golan@hostC:~$ nc -l -p 3000</pre>
<p>So we can now send data to such port using netcat as well:</p>
<pre>
golan@hostB:~$ cat /dev/urandom | nc hostC 3000</pre>
<p>This way we can send data and test the connection.</p>
<p>Capturing traffic on hostC</p>
<p>We can use wireshark on hostC in a tricky way to see what&#8217;s going on. So, we use something like this:</p>
<pre>
golan@hostA:~$ rm capture; mkfifo capture;

golan@hostA:~$ ssh root@hostC tcpdump -s 0 -U -n -w - -i eth0 tcp port 3000 &gt; capture</pre>
<p>We create a FIFO named capture and we capture traffic on hostC, on port 3000 and write it to the standard output which in fact is redirected to the FIFO we have just created.</p>
<h4>Wireshark</h4>
<p>So now, we launch wireshark, and configure it to capture data from the FIFO we created as the interface file:</p>
<p><img src="http://roncero.org/varios/wireshark2.png" /></p>
<p>If we now click on <strong>statistics</strong>-&gt;<strong>IO Graph</strong> we can get a nice traffic graph</p>
<p><img src="http://roncero.org/varios/wiresharkgraph.png" /></p>
<p>which can help us to debug the application</p>
<h4>IPTraf</h4>
<p>Alternately we can use iptraf, where we can have a window like this one:</p>
<p><img src="http://roncero.org/varios/wireshark3.png" /></p>
<h4>Measuring bandwidth consumption</h4>
<p>Now, you can test the connection by sending data using netcat as seen above.</p>
<p>I guess there are many more ways to do this, but found this one to be nice. Just drop a comment if you think there are nicer ways to do this :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.notreally.org/articles/2007/01/24/how-to-monitor-packets-from-a-remote-interface/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Last FM</title>
		<link>http://blog.notreally.org/articles/2007/01/17/last-fm/</link>
		<comments>http://blog.notreally.org/articles/2007/01/17/last-fm/#comments</comments>
		<pubDate>Wed, 17 Jan 2007 23:05:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[internet]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[
I first knew about internet radio by listening to Soma FM back in 2001 or 2002. It was pretty cool by then.
Lately I&#8217;ve been using Last FM quite a lot. I find it quite nice the way they have everything set up. They have a nice mp3 player with which you can listen to the [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://static.last.fm/depth/advertising/lastfm/mini_red.gif"></p>
<p>I first knew about internet radio by listening to <a href="http://somafm.com">Soma FM</a> back in 2001 or 2002. It was pretty cool by then.</p>
<p>Lately I&#8217;ve been using <a href="http://last.fm">Last FM</a> quite a lot. I find it quite nice the way they have everything set up. They have a <a href="http://www.lastfm.com/tools">nice mp3 player</a> with which you can listen to the music they are constantly streaming. Not only that, you can listen to different kinds of music at any time, searching it by artists or by music tags. It&#8217;s also a social website where you can see what your friends have been listening to lately or even get recommendations based on the music you&#8217;ve been listening to.</p>
<p>Anyway, I believe that it is pretty cool and I&#8217;ve just subscribed for 6 months so I can test it, but primarily to support them. Keep on with the good work :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.notreally.org/articles/2007/01/17/last-fm/feed/</wfw:commentRss>
		</item>
		<item>
		<title>More on setting a subversion mirror repository</title>
		<link>http://blog.notreally.org/articles/2006/12/13/more-on-setting-a-subversion-mirror-repository/</link>
		<comments>http://blog.notreally.org/articles/2006/12/13/more-on-setting-a-subversion-mirror-repository/#comments</comments>
		<pubDate>Wed, 13 Dec 2006 22:34:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[internet]]></category>

		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[A few days ago I wrote about setting up a subversion repository using  svnsync. On that entry I was using svn+ssh authentication, but there are some gotchas to take into account to avoid having a security issue.If you recall correctly, we had a unix user with which people will access the repository. If we [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago I wrote about <a href="http://blog.notreally.org/articles/2006/11/30/setting-up-a-subversion-mirror-repository-using-svnsync">setting up a subversion repository using  svnsync</a>. On that entry I was using svn+ssh authentication, but there are some gotchas to take into account to avoid having a security issue.If you recall correctly, we had a unix user with which people will access the repository. If we haven&#8217;t taken any extra protection, that means that anyone that has their ssh key on such a user&#8217;s ~/.ssh/authorized_keys file can log into the system. If such user uses public authentication to access the remote account used for the remote repository, any commiter can have access to the private key and thus access the remote machine and have write permissions on it.</p>
<p>To avoid this scenario we need to set things up in a way that users can commit to the main repository but cannot have access to the remote repository. And that, again, can be done by creating a different user (<strong>Remember, all this applies to the main repository box</strong>, not the remote box).<br />
Say:</p>
<ul>
<li> <strong>svn</strong>: The user used for the repository. It will let any user have write permission on the repository. It will also have all the public keys of all the commiters, to allow them to access the server using the svn+ssh authentication under the generic user svn, in case it needs to be retrieved from a remote location, which can be easily used by forwarding your credentials.</li>
<li> <strong>svnsync</strong>: A user which will have access to the repository (read access is enough) and will have a couple of wrapper scripts to svnsync. It will use a ssh key to access the remote repository. The rest of the users will have <strong>no read permissions</strong> on this user&#8217;s home directory.</li>
<li> <strong>The rest of the users</strong> for the commiters. They will have write access to the repository. As you may know, if you use svn+ssh authentication, whenever you do a commit, you basically are executing svnserve on the repositoy under your user ID (That&#8217;s why you need write permissions).</li>
</ul>
<p>This way, commiters won&#8217;t have access to the svnsync private key. But we must grant them access to the wrapper scripts, so whenever they do a commit, svnserve is able to execute those scripts by triggering a post-commit hook.</p>
<p>All these can be achieved by using <a href="http://www.gratisoft.us/sudo/">sudo</a>. So, say that we have two different wrapper scripts:</p>
<ul>
<li>/home/svnsync/bin/synccommit.sh

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #808080; font-style: italic;">#!/bin/bash</span>
<span style="color: #808080; font-style: italic;"># this is to synchronize the repostory</span>
<span style="color: #808080; font-style: italic;"># $<span style="color: #000000;">1</span> will be the repository</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> -z $<span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
  <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #808080; font-style: italic;"># The repository should be a parameter</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">DATE=</span>`<span style="color: #c20cb9; font-weight: bold;">date</span> +%Y%m%d-%H%M `
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;[$DATE] Commiting to repository $1 . (output below if any)&quot;</span> &gt;&gt; /home/svnsync/svnsync-commit.log <span style="color: #000000;">2</span>&gt;&amp;<span style="color: #000000;">1</span>
/usr/<span style="color: #7a0874; font-weight: bold;">local</span>/bin/svnsync synchronize --username svnsync <span style="color: #ff0000;">&quot;$1&quot;</span> &gt;&gt; /home/svnsync/svnsync-commit.log <span style="color: #000000;">2</span>&gt;&amp;<span style="color: #000000;">1</span></pre></div></div>

</li>
<li>/home/svnsync/bin/syncprop.sh

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #808080; font-style: italic;">#!/bin/bash</span>
<span style="color: #808080; font-style: italic;"># this is to synchronize the a property</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># $<span style="color: #000000;">1</span> will be the repository</span>
<span style="color: #808080; font-style: italic;"># $<span style="color: #000000;">2</span> will be the revision number</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> -z $<span style="color: #000000;">1</span> || -z $<span style="color: #000000;">2</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
  <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #808080; font-style: italic;"># The repository should be a parameter</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">DATE=</span>`<span style="color: #c20cb9; font-weight: bold;">date</span> +%Y%m%d-%H%M `
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;[$DATE] Updating property (Revision $2) to repository $1 . (output below if any)&quot;</span> &gt;&gt; /home/svnsync/svnsync-properties.log <span style="color: #000000;">2</span>&gt;&amp;<span style="color: #000000;">1</span>
/usr/<span style="color: #7a0874; font-weight: bold;">local</span>/bin/svnsync copy-revprops --username svnsync <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #ff0000;">&quot;$2&quot;</span>  &gt;&gt; /home/svnsync/svnsync-properties.log <span style="color: #000000;">2</span>&gt;&amp;<span style="color: #000000;">1</span></pre></div></div>

</li>
</ul>
<p>We can call them by setting up the hook scripts as:</p>
<li> <strong>post-commit</strong> hook:
<pre>
# Propagate the data to the remote repository
sudo -u svnsync /home/svnsync/bin/synccommit.sh svn+ssh://svnsync@remote/home/svnsync/svn &amp;</pre>
<p>* *post-rev-change*:</p>
<pre>

# Propagating changes to the remote repository. Putting it to the background
sudo -u svnsync /home/svnsync/bin/syncprop.sh svn+ssh://svnsync@remote/home/svnsync/svn $REV  &amp;</pre>
<p>All we need to do now is grant access to the commiters to execute both wrapper scripts by adding the following to the /etc/sudoers file (remember, by using visudo)</p>
<pre>
%commiters  ALL= (svnsync) NOPASSWD: /home/svnsync/bin/syncprop.sh
svn         ALL= (svnsync) NOPASSWD: /home/svnsync/bin/syncprop.sh</pre>
<p>Where commiters is a common group where all commiters belong to.</li>
]]></content:encoded>
			<wfw:commentRss>http://blog.notreally.org/articles/2006/12/13/more-on-setting-a-subversion-mirror-repository/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Speeding up trac&#8217;s response time</title>
		<link>http://blog.notreally.org/articles/2006/12/04/speeding-up-tracs-response-time/</link>
		<comments>http://blog.notreally.org/articles/2006/12/04/speeding-up-tracs-response-time/#comments</comments>
		<pubDate>Mon, 04 Dec 2006 18:30:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[internet]]></category>

		<category><![CDATA[python]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I&#8217;ve been trying to speed up an installation of trac over the last few days. The web interface took ages to display each of the directories or files within the subversion repository. But this one wasn&#8217;t too big. The only change to the subversion repository is that we started using a vendor branch imported into [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been trying to speed up an installation of <a href="http://trac.edgewall.org/">trac</a> over the last few days. The web interface took ages to display each of the directories or files within the subversion repository. But this one wasn&#8217;t too big. The only change to the subversion repository is that we started using a vendor branch imported into our main repository using <a href="http://search.cpan.org/~clkao/SVN-Mirror-0.72/bin/svm">svm</a></p>
<p>So, after a few hours trying different solutions, and reading trac&#8217;s source code, I think I got where the bottleneck was.Well, it was <a href="sqlite">http://www.sqlite.org/download.html</a> which was causing the bottleneck. Trac uses an object <em>CachedRepository</em> to access the repositories. Whenever we want to get the <em>chagesets</em>, a function to synchronize the repository is called:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">class</span> CachedRepository<span style="color: black;">&#40;</span>Repository<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">def</span> get_changeset<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, rev<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">synced</span>:
      <span style="color: #008000;">self</span>.<span style="color: black;">sync</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
      <span style="color: #008000;">self</span>.<span style="color: black;">synced</span> = <span style="color: #ff4500;">1</span>
      <span style="color: #ff7700;font-weight:bold;">return</span> CachedChangeset<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">repos</span>.<span style="color: black;">normalize_rev</span><span style="color: black;">&#40;</span>rev<span style="color: black;">&#41;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">db</span>, <span style="color: #008000;">self</span>.<span style="color: black;">authz</span><span style="color: black;">&#41;</span></pre></div></div>

<p>and such method, sync(), makes a call to:</p>

<div class="wp_syntax"><div class="code"><pre class="python">youngest_stored = <span style="color: #008000;">self</span>.<span style="color: black;">repos</span>.<span style="color: black;">get_youngest_rev_in_cache</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">db</span><span style="color: black;">&#41;</span></pre></div></div>

<p>which is all this:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> get_youngest_rev_in_cache<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, db<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Get the latest stored revision by sorting the revision strings
    numerically
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    cursor = db.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SELECT rev FROM revision ORDER BY -LENGTH(rev), rev DESC LIMIT 1&quot;</span><span style="color: black;">&#41;</span>
    row = cursor.<span style="color: black;">fetchone</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> row <span style="color: #ff7700;font-weight:bold;">and</span> row<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #008000;">None</span></pre></div></div>

<p>And that SQL query was taking around 1-2 seconds each time it was executed. It happened that we were running an old version of <a href="http://www.sqlite.org/">sqlite</a> and <a href="http://www.initd.org/pub/software/pysqlite/">pysqlite</a>, so a <strong>./cofigure &amp;&amp; make &amp;&amp; make install</strong> using the recommended installation saved my day :-)</p>
<p>Hope it is useful to anybody if it gets indexed by <a href="http://www.google.com">Google</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.notreally.org/articles/2006/12/04/speeding-up-tracs-response-time/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
