Debug with ‘tail -f dmesg’ Equivalent and ‘proc/sys/kernel/printk’ Logging

2010 January 20
by SendDerek

A situation presented itself today where I needed to monitor the ‘dmesg’ output continually.  This is normally easy for regular files because you can simply use the command tail -f thefile and it will always present you with the latest information.  Well, not so with the dmesg output.  If you try, you’ll be greeted with some sort of error message.  One way around this is to run the dmesg -c command in a while loop like so…

while true;do dmesg -c;done

Understand that running dmesg -c will clear the dmesg ring buffer contents after printing, so you will lose the contents of dmesg.

Now, I’m not 100% sure if that while loop is a totally elegant or acceptable solution, so I offer another solution — the /proc/sys/kernel/printk file. According to IBM.com

This holds four numeric values that define where logging messages are sent, depending on their importance. For more information on different log levels, read the manpage for syslog(2). The four values of the file are:

1. Console Log Level: messages with a higher priority than this value will be printed to the console
2. Default Message Log Level: messages without a priority will be printed with this priority
3. Minimum Console Log Level: minimum (highest priority) value that the Console Log Level can be set to
4. Default Console Log Level: default value for Console Log Level

Default setting: 6 4 1 7

By experimenting with these values, you can send printk messages directly to the terminal window instead of only to dmesg. You can set these values using

echo "9 9 9 9" > /proc/sys/kernel/printk

Happy debugging!

Source: http://www.linuxforums.org/forum
Additional Resource: http://www.de-brauwer.be/wiki

My First Makefile for Compiling GTK+ (Or Any) Apps

2010 January 19
by SendDerek

A Makefile provides an easy, fast way to compile an application that requires more than just a simple gcc myprogram.c -o myprogram command. For example, to compile a GTK+ app, the common, basic compile command is gcc -g -Wall myguiapp.c -o myguiapp -export-dynamic `pkg-config –cflags –libs gtk+-2.0`. It’s not terrible, but it would be a pain to have to retype that every time you need to recompile. The alternative is to create a Makefile and type make. This is my very first Makefile, and I believe it makes a great example:

NAME=myguiapp
CFLAGS=-g -Wall -o $(NAME)
GTKFLAGS=-export-dynamic `pkg-config --cflags --libs gtk+-2.0`
SRCS=main.c
CC=gcc

# top-level rule to create the program.
all: main

# compiling the source file.
main: $(SRCS)
 $(CC) $(CFLAGS) $(SRCS) $(GTKFLAGS)

# cleaning everything that can be automatically recreated with "make".
clean:
 /bin/rm -f $(NAME)

As you can see, when make is called, it will compile the source with all my options including the program name. When make clean is called, it will delete the compiled program.

One important thing to remember is that these Makefiles are very picky about spacing and tabs.  You MUST use a TAB at the beginning of commands and a TAB must not be at the beginning of blank lines. The first error causes the commands not to run. The second causes the “make” utility to complain that there is a “blank” command.

Obviously, this is an incredibly simple Makefile in comparison to a lot of them out there, but this is a good starting point. You can customize the Makefile to match your compiling needs. If you’d like to learn more, Google is your friend. I can point you in a couple of good directions with these links:
http://mrbook.org/tutorials/make/
http://www.cs.umd.edu/class/fall2002/cmsc214/Tutorial/makefile.html

Happy coding!

List Installed Packages by Size in Debian

2010 January 18

In an embedded system, space is a very valuable commodity.  Sometimes, there are packages that are installed needlessly and they take up space.  A quick way to see the largest installed files in sorted order is:

dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n

Source:commandlinefu.com

Clearing a Linux Password

2010 January 15
by SendDerek

If you have direct access to the Linux filesystem (like a mounted SD card for example), it’s really easy to clear the password from the system. Simply take a look at the /etc/passwd file. You’ll see a line for every user on the system in a syntax like this:

<username>:<password>:<UID>:<GID>:<user_id_info>:<home_dir>:<command/shell>

To clear the password, simply delete anything in the field. By the way, if you see an ‘x’ character in this field, you will also need to clear the field in the /etc/shadow file.

For example:

#change this:
root:$1$TeCMjrna$1lmHjj.1E9NAAW2am.c7./:0:0:root:/root:/bin/bash
#to this:
root::0:0:root:/root:/bin/bash

There are also ways to clear/change the password from GRUB or LILO if you don’t have direct access to the filesystem, but I’ll let ya’ll search for that one!

…and that’s what I learned today!

Using ‘dd’ in Conjunction with ‘fdisk’ Sectors, Heads, Cylinder Counts

2010 January 14
by SendDerek

I ran across a situation today where I wanted to get the first 2GB of a 4GB SD card and save it to my HDD. The only information that I had was

I had a disk that was partitioned in such a way that a large chunk at the end of the disk was unallocated space and the partition before it was where I wanted to tell ‘dd’ to stop copying. This is normally easy when I have the number of bytes I want to copy, but all I had were the the number of heads, sectors, and cylinder start/end values given by the output of “fdisk -l”. I knew that I wanted to end at cylinder 508 since that was where the particular partition ended, but I still didn’t know which byte it ended at. I found an easy way to tell ‘dd’ what I wanted by using the ‘bs=xxb’ option like this:

dd if=/dev/sdb of=sd_image.dd bs=124x508x62b count=1

Where 124=heads, 508=cylinders, and 62b=sectors at 512bytes each. This is really simple math, but it’s simpler just to tell ‘dd’ to calculate it instead (124*508*62*512 = 1999618048bytes).

And that’s what I learned today!