Friday, June 03, 2011

Home Nas Setup

I bought an iOmega iConnect.
Seeing some reliablity issues. In between a copy found and smb crash :

VFS: busy inodes on changed media or resized disk sdc
VFS: busy inodes on changed media or resized disk sdc
Unable to handle kernel paging request at virtual address d0000000
pgd = c2f68000
[d0000000] *pgd=00000000
Internal error: Oops: 5 [#1] PREEMPT
Modules linked in: sr_mod cdrom usblp usbhid rt3090sta(P) msdos udf crc_itu_t isofs ufsd(P)
CPU: 0 Tainted: P ( #1)
PC is at _ZN4UFSD14CMemoryManager7MemmoveEPvPKvj+0xfc/0x150 [ufsd]
LR is at _ZN4UFSD14CFileMFTRecord8RemoveEaEPKcj+0x164/0x1d8 [ufsd]
pc : [] lr : [] psr: 20000013
sp : cee41e10 ip : bf06faa0 fp : cee41e88
r10: cff4dd60 r9 : cfd140c0 r8 : 0000003c
r7 : c200f700 r6 : 000059a0 r5 : 0000003c r4 : c200f700
r3 : 000000e2 r2 : d0000000 r1 : d0000000 r0 : c2009d9c
Flags: nzCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment user
Control: 0005397f Table: 02f68000 DAC: 00000015
Process smbd (pid: 21561, stack limit = 0xcee40268)
Stack: (0xcee41e10 to 0xcee42000)
1e00: 0000000c 0000a69d ffffa69c c200f700
1e20: 00000000 c2200000 c2200008 cd13b780 cee41e88 cee41e88 cee40000 00539000
1e40: becbb5f4 bf005a34 00539000 bf003b68 00014000 c2200074 c2200008 00000000
1e60: cf627500 c00b9260 c23c0e70 00014000 cf627500 401cf83c 00138f7c 000000eb
1e80: c0027b24 c00b9324 74737973 702e6d65 7869736f 6c63615f 6665645f 746c7561
1ea0: c2f68000 00000001 00000400 00001fb6 00000001 c088c000 c23c0e70 cfd1b334
1ec0: cfd1b300 00000200 00014000 c00aea74 cee41ef0 c00b57b8 c088c000 cee41f28
1ee0: 00000000 c088c000 00000001 c00a91b4 cd449680 cf627500 cee40000 00000001
1f00: cee41f28 c00a9340 cee41f90 c0027b24 c088c000 00000001 c088c000 00000000
1f20: ffffff9c c00a9c78 cd449680 cf627500 00000001 00000000 becb94d8 00000001
1f40: 00000001 00000000 c2f68000 c0053b44 becbb0f4 ffffffff 0010f000 c00aea74
1f60: cee41f88 c00b57b8 005cb730 00000000 401cf878 005cb730 0000001c 401cbb3c
1f80: 401cf83c 00000000 00000000 c00b9430 cd449680 cf627500 00000000 401cbb3c
1fa0: 00000008 c00279a0 401cbb3c 00000008 005b8228 401cf83c 401d7a78 00000008
1fc0: 401cbb3c 00000008 00138f7c 000000eb 00000000 00000000 00539000 becbb5f4
1fe0: becbb5d0 becbb5c0 401c0cb0 402ed130 60000010 005b8228 00000000 00000000
unwind: Index not found bf061eac
Code: e4d23001 e4c13001 e4d23001 e4c13001 (e4d23001)
---[ end trace f870048942819ed1 ]---

Sunday, January 03, 2010

My Torrent downloading setup using beagle board

Prologue : dont get upset by the large maze of wires in the picture. I have included some additional componets like USB to Serial adapter and USB hard disk, which are not mandatory for the mission outlined here. I have included them for the sake of completeness. Please click on the image for a better quality one.
For the sake of dummies who has never thought a world beyond intel/x86 PCs- Beagle Board is a low-cost, fan-less single board embedded board with a heart made of 600 MHz TI's Arm Cortex A8 based OMAP 3530 processor. The board is said to consume less than 2 watts of power while running and is just a 2 inch square in size.

I bought beagle board mainly to have an arm board at my disposal at home for hobby purposes like learning arm assembly and arm/architecture dependent and ofcourse arch independent portions of linux kernel. Though i have access to a wide variety of high end arm boards at my workplace, at times i miss them at home. Despite these noble intentions while buying i have to confess that in the most part of the last 4 months while I was in possession of my beagle i have done no more kernel hacking than recompiling kernel and u-boot for beagle with some prints. I have to accept that i was practically spoilt by the amazing support provided by the community in terms of software support for this board, and i could get it running with practically zero effort.

After beagle board was lying practically idle in its small box for a little over 4 months, couple of weeks before, i got stricken by an idea to put it to real use. How about using it as a headless [monitor less] system which will keep downloading your torrents 24/7 ie you can leave it home and go for a week long vacation and it will download all the torrents you have queued, which means when the power goes off and comes back beagle will again start up and continue the download. You can also add more torrents if you wish to while you are away , via ssh , provided you have a static IP as is my case.

The main problem, in achieving this was that beagle board didnt have an ethernet port, but it has USB port. So I have bought one usb to ethernet adapter [believe me finding this adapter these days can be quite a challenge i was able to find one at computer ware house for around RS 200/ mg road / barton centre in bangalore] . You should also buy one self powered USB hub. I couldnt find it here in bangalore so i ordered one from eBay [Belkin 4 port for Rs 850] . The purpose of USB hub is two fold in case of beagle board.
1. The USB ethernet adapter is a Full Speed [USB 1.1] device and the USB host controller port on the beagle will support only High Speed [USB 2.0] devices. Since the hub is a USB 2.0 device, if you plug the adapter on the hubs port it will be detected - see the image for details

2. My 160GB USB ethernet drive was also not working [though it is USB 2.0] while connecting directly to the board[ though thumb drives were working while connected directly], but worked while connected under the hub. I guess this was because the beagle is unable to power the hard disk, but as the Hub is self powered it isable to power the hard disk.

3. I didnt order a power adapter for the beagle board while ordering from digikey since i had to shell out an extra 1000 bucks for that. Ideally you should be able to find it from some neighbourhood electrical shops - but i couldnt . As a work around i was powering the board from my laptop using a mini B cable to the USB OTG port on the board.
So if you are using a self powered usb hub, you can power the beagle board by taking power from one of its port. This explains the 3rd downstream connection to the usb hub in the picture.

That completes the hardware setup, now u have burn the Xloader and u-boot into the Beagle board flash as explained here. Then i got an SD card and partitioned and formatted it into two, the first a 50 MB FAT partition and the remaining as an ext3 partition. I used GParted utility available in desktop Linux. So put the SD card in a usb card reader and plug it onto a native linux machine. Since i dont have a native Linux machine i used my ubutnu VMWare virtual machine running inside my vista for this purpose. For more detailed instructions on this, please see this video. Now download one angstrom Linux kernel build [uImage file] from this link. Put that file in the FAT partition of the SD card. Though you can burn the kernel image into the FLASH, it is better to keep it on the SD card as you can easily replace it with another kernel, by plugging the SD card onto another system.

Now that we have the XLoader, u-boot and Kernel, in place, all that is remaining is the root file system. Here i used anstrom Linux online image builder. For our purposes please include the following packages rtorrent, samba, ntp - network time, dropbear -ssh server, screen - session manager and Busybox replacements. Other interesting packages you can select are powertop, mplayer, Alsa utils and Cheeroke - web server. That will give you a tar ball which you can extract into your ext3 partition in the sd card. That is about it you can put the SD card and boot power on the beagle board and you should get linux shell. If you are seeing kernel panic - unable to mount root fs, check kernel command line during linux boot up - it should have something like this
Kernel command line: console=ttyS2,115200n8 root=/dev/mmcblk0p2 rw rootfstype=ext3 rootwait

Otherwise go and edit u-boots bootargs environment variable and modify it to
bootargs=console=ttyS2,115200n8 console=tty0 root=/dev/mmcblk0p2 rootdelay=2 rootfstype=ext3 rw single

Now configure the network, and ntp client and then get hold of a torrent file and launch rtorrent and you can start downloading torrents like the son of Bram Cohen. Please check this article on how to configure rtorrent . Have a look at this article also while configuring rtorrent. From my experince i have found that rtorrent is a very robust ncurses based bit torrent client. You can launch it with screen so that every time you ssh you can get the same console. Dont forget to read starting rtorrent at startup in the above mentioned link which can be quite handy.

I have ran this set up close to two weeks[including one week where i travelled to my native place for new year and returned and found that rtorrent was still running in spite of the numerous power outages, i even added a few torrents from my home over ssh] and found the setup to be quite reliable, except for a few cases where in i have founf USB ethernet driver has crashed ie when i tried to copy large file via samba into my windows system, but those can be easily worked around.

Though not related i am just adding one powertop screen shot also, just in case for your reference

Finally a word of thanks to my friend Manu Thayyil who carried the beagle all the way from US.

Please feel free to post your queries and comments.

Thursday, February 12, 2009

Final (not the last) attempt at VIM

Being an ardent Open source / Linux fan believer, you might be surprised to know that i am still a newbie with vim. Perhaps the only commands i know on vi till now are (i, q, wq and q!).

The editor of my choice happens to be Source Insight with its cool features like context window(a dedicated window which takes you to the definition of the symbol, as soon as you place your cursor onto the symbol on the main window - Since the entire project source code is indexed periodically, this works amazingly fast even for a massive project like the Linux kernel which may span across many 10,000s of files) and call graphs. Though i didn't intend to make this blog an advertisement to Source Insight, i cant help the way it is coming out.

Instead this should be taken as a challenge by the open Source community including me to come up with a stunning GUI development suite which can challenge the mighty SourceInsight. But i seriously doubt whether any work in this direction will materialize in Open Source leave alone start. This is because as far as i know, in Open Source/ Linux community, any developer worth his salt is already addicted to either of the two stalwarts vi or Emacs, mostly the former one(i respect both of them - well aware of their capabilities , but never had the patience or memory to master the long list of commands and neither do i believe that, even if i manage to learn all the commands, it still would improve my development efficiency to any significant level above SI ... who knows after i may find myself disproved). Thanks to my friend Xavier who introduced me to this editor.

Till my first date with SI, i was wasting my time with some cheap and crappy editors like edit plus (where the only way to find the definition of a symbol was to do a find in files - it was okay there as the projects there never exceeded more than 100 files) at SCM (my previous employer).
I am deliberately taking this instant to take a dig at a couple of people at this juncture. These were the reactions of a couple of ppl who were mostly used to VC 6 ide / edit plus when i explained them about SI.
One of the wizards at SCM commented , this editor may have lot of fancy features, but frankly i don't think you need anything more than edit plus to code ... (what to say, i can only sympathize with his narrow vision and obsession to absurdity).

Coming back to our topic vi. One anecdote on vi which still makes me laugh goes like this : There is this guy called Sinu (SKJ) one of my class mates / project group mate from ModelEnggColl .... quite a knowledgeable and technically competent person with a good sense of humor ... Once i was at his place , so in between our conversation after i told him that i mostly work on Linux kernel based embedded devices, he casually asked me whether i used Vi. I replied in negative. Then came his reply that i am luckily spared , and that his hands are itching to thrash any one and every one who used vi and crossed his path.
The story goes like this Mr SKJ and his colleague - a self proclaimed vi expert (newbie) one day before some release finds that certain changes needs to be done to a file while testing on the target. so our expert showed all his VI expertise on the file and later confessed that the only way to get back the original file was to retype it once again in some other normal editor. (Dont ask me whether he didn't have a back of the original point - you are missing the point).

Once again coming back to the point. After umpteen attempts, i have again decided to try my hands at championing vi. Couple of plus points of vi which no one else can claim goes like this :
You will find it on any embedded Linux device , be it your mobile, router or anything .. in fact that will be the only editor that you will ever find on these.

So here I am start again - like the spider who tries to climb all the way up for the umpteenth time by writing a new .vimrc file , which i am copy pasting below :

set nocompatible

set backspace=indent,eol,start

set ignorecase " Case insensitive matching.

set incsearch " Incremental search.

set tabstop=4 " Number of spaces counts for.

set shiftwidth=4 " use 4 spaces when text is indented auto or manual"

set background=dark " use bright colors"

set cindent

set autoindent " use the current line's indent level to set the indent level of new lines"

set smartindent " intelligently guess the indent level of any new line based on the previous line"

set showmatch " setting will cause the cursor to very briefly jump to a brace"

set ruler


"" Function keys.



"" F1: Toggle hlsearch (highlight search matches).

nmap :set hls!

nmap :set ignorecase!

map q :q

Apart from this i have learned a handful of commands ... which i have already forgotten

b,w - move fwd / bckwd one word with cursor at word beginning
e, ge - same as the previous , but the cusor will be at the end of the word .. see the power of vi
n/N - search the word back and forth
Ctrl-O - cursor to the previous pos
gg / GG - go to the beginning or end of file .. or the other way ...i can ven remember big endian / little endian - leave alone this ... any ways life has to move on
2>> / << -- shift two lines either way
Ctrl -R - redo
gd - go to the local definition of the variable
[[ / ]] - next function / previous function
H, M,L - top / middle / bottom of the screen

How to move cursor Up / down n lines - shit i forgot ? any way life has to go on

That's enough for Today (Ctsy A. O sir)

Sunday, January 04, 2009

Synchronization, Delays and PreEmption in Linux

Pre emptible kernel,
Assume that you have a while (1) loop in a kernel thread, If you dont have a premptible kernel your CPU will freeze once the thread is scheduled in. But if you have a pre emptible kernel, your system will work though with a reduced through put

Atomic Context
There are certain contexts from which, you just cant invoke the scheduler (the current process cant give away the CPU) by either (sleeping mdelay (), waiting for a semaphore, waiting for a completions), the only way to delay execution from these contexts is to busy wait. If you try to use any of the above mentioned re scheduling mechanisms in an atomic context, you are sure to get
: "BUG: scheduling while atomic" error.
The idea behind atomic contexts is that you are in the middle of a transaction which should be executed atomically (critical sections) .... There is no way you can yield the processor to another process, lest it interferes with the atomic nature of transaction.

How do you see whether you are in an atomic context or not ? : in_atomic()

Apart from interrupt handlers ... When all will i be in atomic contexts ? (Some times in the kernel drivers we write : we may implement certain call back functions, which will be called by the upper layer subsystems :

Eg. Assume that the network subsystem (TCP /IP stack) has a packet to tranmit . The physical layer driver eg. the ethernet driver, or USB network gadget driver, will initially register with its packet transferring routine with the TCP IP sub system (net_device->start_hard_transmit).

Now when a packet becomes ready from upper layers , our driver's transmit function will be called. In this particular case if you print the value of
in_atomic() , it will be 1 - indicating that the network subsystem has called your driers call back in atomic context. Basically what it means is that you cannot attempt to reschedule (delay, wait for completion or semaphore - all these will internally call schedule ()) within this call back. The only way to wait if you are very particular is to busy wait, or :

while (time_before(jiffies, j1))
cpu_relax( );

Spin Locks
This is a synchronization mechanism in which sleeping is not involved. Just like mutex, Spin lock is a lock which has two states, but the difference here is that : suppose the first thread acquires the lock, now if the second thread also attempts to take the lock, it will busy wait (a mutex would have slept)

Spinlocks are, by their nature, intended for use on multiprocessor systems, althougha uniprocessor workstation running a preemptive kernel behaves like SMP, as far as concurrency is concerned.

If a nonpreemptive uniprocessor system ever went into a spin on a lock, it would spin forever; no other thread would ever be able to obtain the CPU to release the lock. For this reason, spinlock perations on uniprocessor systems without preemption enabled are optimized to do nothing, with the exception of the ones that change the IRQ masking status.

Spinlocks and Atomic Context

Imagine for a moment that your driver acquires a spinlockand goes about its business within its critical section. Somewhere in the middle, your driver loses the processor. Perhaps it has called a function (copy_from_user, say) that puts the process to sleep. Or, perhaps, kernel preemption kicks in, and a higher-priority process pushes your code aside. Your code is now holding a lockthat it will not release any time in the foreseeable future. If some other thread tries to obtain the same lock, it will, in the best case, wait (spinning in the processor) for a very long time. In the worst case, the system could deadlock entirely.

Most readers would agree that this scenario is best avoided.
Therefore, the core rule that applies to spinlocks is that any code must, while holding a spinlock, be atomic. It cannot sleep; in fact, it cannot relinquish the processor for any reason except to
service interrupts (and sometimes not even then).

The kernel preemption case is handled by the spinlock code itself. Any time kernel code holds a spinlock, preemption is disabled on the relevant processor. Even uniprocessorsystems must disable preemption in this way to avoid race conditions. That is why proper locking is required even if you never expect your code to run on a multiprocessor machine.

[ certain portions of this article are borrowe from ldd 3 ]

Wednesday, July 30, 2008

cramfs : bad compressed blocksize
cramfsck : file length too short

Last day i came across a strange problem with my embedded linux board. Though the boot messages had been showing this warning for some time I didn't take it seriously,

I had a rootfs partion of type cramfs, now starange behaviours were observed like, certain particular files in the file system could not be read, broken links were observed in certail folders. And sometimes when i did an ls on some particular directories, the following trace was displayed on the shell.

cramfs: bad compressed blocksize 4289646708

Googling didn't turn up much info, except that the particular check was added in 2.6.19 cramfs code, so that the kernel wouldn't oops and hang, for certain file system errors.

We initially suspected bad block's in the flash, but this was disproved when another target also showed the same error.

Now i started debbuging.

1. I mounted the file system images via loop back on my development host, and checked if the certain broken links were observed.

$ mount -t cramfs -o loop "OriginalImage.cramfs" "MountPt"

But every thing was perfect on the loop mount, no broken links

2. I read out the entire partition from the target using dd.

dd if=/dev/mtdblock2/ of=/ReadOutImage.cramfs

To my surprise dd completed succefully, if the issue was due to flash bad blocks, dd should have failed.

Now i mounted the readback image via loop back on to my development system.

$ mount -t cramfs -o loop "ReadBackImage.cramfs" "mntPt"

Now i browsed through to the folder, which had the sym link problem. The broken links were present now.

Hence i had to conclude that the read back image is not the same as the written image.

I suspected some cramfs filesystem inconsistency may be present in the read back image. So i installed cramfs tools and ran cramfsck on both the images.

cramfsck ran witout any error on . But on it exited with initially during super block checking with the error

"file length too short"

Now i checked out the sizes of both the images and found out that the read back size was smaller than the original image size.

Now i checked out the kernel's partition table

$cat /proc/mtd

and indeed found out the problem, the size mentioned as per the kernel partition table, was indeed smaller than , the size of the cramfs image.

This was immediately solved by increasing the flash partitions size in the kernel partition table accordingly. The entry was present in the Nand drivers code.

This happened in the first place, because my boot loader was assuming a different partition size, consequently it didnt tell any error while flashing the image.

If any one finds this helpful, kindly leave a comment.

Labels: , ,

Sunday, December 09, 2007 - 2007

Right from my College days i have been an ardent supporter of GNU/Linux system and OpenSource software. In retrospect, there are a number of factors which has kindled this interest.

First of all being a Computer Science student, i was quite surprised and obviously thrilled in knowing that the source code of an entire Operating System was at our disposal. The concept of "free as in freedom" was a tempting relief from the normally closed/restrictive and greedy attitude that we encounter in our day today life. Moreover all the systems in our college were running Linux, and we were constantly encouraged to try out free software by our Principal - Prof Jyothi John. This obviously led to my first tryst with LINUX. I was also fortunate in having a Linux wizard - Bimal - as my room mate. Right through the entire college days he trouble shooted my installation woes, Network configuration and what not. We even had an opportunity to hear about the harmful effects of Software Patents
from the guru - the iconic Richard M Stallman (RMS) himself - what else can i ask for ?

My current job also gave me some opportunity to tweak with some Linux Related stuff, like experimenting with some kernel modules.

So after all this history i was quite thrilled to attend 2007.
I felt that i shouldn't miss the opportunity to hear from legendary kernel developers. So after some pulling ropes i was permitted to attend the event officially. Though my manager was initially of the opinion that the session's are not directly related to SCM's, he finally relented.

I was surprised at the number of foreigners who were attending the event at least some 10 % of the delegates were foreigners and 90 % of the speakers were foreigners. There were speakers from Red Hat - Alan Morris (SE Linux) , Ulrich Drepper (C Runtime maintainer). IBM 's LTC - The inimitable Rusty Russell (IP Chains , IP Filters - known as Linus' right hand man), SUN Microsystems - Chief Open Source Officer Simon Philipps, Mozilla - including the CEO
Mitchell baker , Harald weite - Open Moko and lot of others. These were the prominent speakers whom i have heard, as the talks were going on in parallel i missed some good ones (like from debian Prolect Lead Sam Hocevar , Open Solaris Lead Jim Grisanzio , then some talks on Real time patches for Kernel etc) . Also attended sessions of some of the Indian speakers - the most prominent being Naba Kumar of Anjuta fame, Abhijit Vichare a researcher from IIT-Bombay who has done elaborate research on GCC internals.

The facility at NSSC (
National Science Symposium Center - Indian Institute of Science - Bangalore, India). was of international standards. The entire venue had WiFi.

No i wont continue my dramatic monologue - here are some of the snaps .