Category Archives: Kernel

dmesg

Remember the Linux Kernel and Device Drivers?  They log like everything else.  They also write their log messages to a special file and there’s a command dmesg that lets you see what’s in the file.

The special file has a name and it’s the ring buffer.

The One Ring

The One Ring

This doesn’t mean you have a ring in your computer.  No hobbits are going to go on a quest to retrieve the one ring, sorry.  Go watch The Lord of the Rings to see that ring.

Instead, the ring buffer is a way of storing the messages.  Think of it like a pile of blocks.

A pile of blocks

A pile of blocks

We only want the pile of blocks to be four blocks high, so every time we add a block to the bottom, we must remove one of the blocks from the top.  That’s how the ring buffer works.  It only has a certain amount of information it can store so when that amount is reached, the oldest message in the ring buffer gets removed and the newest one is added.

Next question, what do the messages look like?  Well, you can run the command dmesg  and find out.  Some systems have that command restricted because the people who run the system think it’s a security leak.  It can be, if a device driver is complaining a lot a malicious person can take advantage of that.

I’m here to save the day, though, and show you what an output can look like:

___slab_alloc+0x37a/0x4a0
 ? __es_insert_extent+0x84/0x2f0
 ? ext4_ind_map_blocks+0x102/0xc70
 ? ext4_ind_map_blocks+0x584/0xc70
 __slab_alloc+0x27/0x40
 ? __slab_alloc+0x27/0x40
 ? __es_insert_extent+0x84/0x2f0
 kmem_cache_alloc+0xd5/0x180
 ? __es_insert_extent+0x84/0x2f0
 ? __es_insert_extent+0x84/0x2f0
 __es_insert_extent+0x84/0x2f0
 ext4_es_insert_extent+0xbf/0x190
 ext4_map_blocks+0x262/0x630
 ? __add_to_page_cache_locked+0xb0/0x170
 ext4_mpage_readpages+0x282/0xa20
 ? __raw_callee_save___pv_queued_spin_unlock+0x9/0x10
 ? queue_unplugged+0x33/0xa0
 ? alloc_pages_current+0x58/0xb0
 ext4_readpages+0x29/0x40
 ? ext4_readpages+0x29/0x40
 ? ext4_journalled_zero_new_buffers+0x110/0x110
 __do_page_cache_readahead+0x131/0x1e0

That’s not very helpful, is it?  It requires a lot of knowledge to understand the output of dmesg and it’s usually used when the person running the system notes a problem.  When a system is restarted, the ring buffer is full of information about starting processes, so that can be useful then as well.

Swap on/Swap off/Swap!

In the last post we talked about computer memory and there was that weird line in the free command results called swap.  I also promised to talk about it this week.

So there you are in Math class one day and your teacher writes the most complex equation on the board you’ve ever seen and says ‘SOLVE IT!’.  What do you do?  Well, you pull out a piece of paper and get to work.

swap is kind of like that piece of paper.  The computer wants to do all its thinking in the memory it has installed for that, but sometimes that doesn’t work.  So instead of pulling out a piece of paper and doing work on that, it uses swap.  It would be more fun if your computer could stop what its doing and pull out paper, but it wouldn’t work as well.

So what exactly is swap anyway?  Well, when you set up your system, you designate a part of your hard drive to be the swap space.  This means that the computer is using its remembering memory for thinking memory.  Sounds weird, but it works.  You remember when you think sometimes, right?  You remember how to do the math problem while you’re thinking about the math problem.  Or you’re thinking about how Iron Man would solve it rather than actually solving it, but hey, whatever keeps the math teacher happy!

So let’s look at the result of that free command again:

Alpha:~ computerlamp$ free -g
             total       used       free     shared    buffers     cached
Mem:            31         29          1          0          0         12
-/+ buffers/cache:         17         13
Swap:            2          2          0

According to this, I have 2 gigabytes of swap to use and I’ve used both of them.  In other words, I’ve used up all the paper I had free to do my work on and there’s no more available.  Not exactly the best spot to be in, but I’m sure I can borrow some from the guy next to me… except that doesn’t really work with computers.    But your administrator can add swap if she decides she needs it.

The Kernel Version

Over the years, Iron Man has had a lot of suits.  The suit he had when he started out looks nothing like the suit he wore in the Civil War movie.  Linux® kernels are something like that, the first kernels don’t look anything like the kernels of today.  There are different kernel versions that depend on what the person who put it together wanted and what hardware you’re running.

In other words, the kernel version of your Linux® system probably doesn’t look anything like the kernel version of the system I’m running.  But you can’t tell just by looking at the system what kernel version you’re running, so it’s not like Iron Man’s suit.  It would be neat if it was, imagine if your system put on a new suit every time you put a new kernel on it.  I think it would be awesome for it to change color at least every time I put on a new kernel, but it doesn’t work like that.

Luckily, there’s a command to find out what kernel version you’re running and it’s called uname.  The man page for it

uname man page

uname man page

Tells you exactly what it’s designed for,  it’s to print system information.

So, what information do I want?  I want the kernel version!

That’s:

Alpha:~ computerlamp$ uname -v
#1 SMP Tue Aug 12 09:58:26 UTC 2014

What else can I find out?

Well, I can find out what machine name I’m running, that’s uname -m

I can find out the name of my computer, that’s uname -b.  I can also find out the name of my computer using a different command.  That one is

Alpha:~ computerlamp$ hostname
Alpha
Alpha:~ computerlamp$ uname -b 
Alpha

Either way I run the command, I get the same result out, which means as usual there’s more than one way to do it!

I can also find out my processor, or my hardware platform, or the operating system.

But wait, you say, of course I know my operating system, I’m using Linux®.  Well, it’s always good to double check that using our friendly comman.

Alpha:~ computerlamp$ uname -o 
GNU/Linux

If you have multiple systems, it’s always useful to find out what kernel version you’re running on each one.  It’s a good idea to keep it consistent, but that’s up to the administrator.

Kernel Modules or Putting it all Together

The Linux® kernel is not one huge thing that can run every hardware ever made and talk to every application ever created.  Can you imagine that? It would be huge and every time something new was added, you’d have to add it on.  Even if it wasn’t anything you needed.  Instead, the kernel is a modular kernel, which means you can add pieces on as needed.  These pieces are called modules.

It’s like Lego® blocks.  You add the blocks on that you need but you don’t have to use every single one.

Lego Blocks

Lego Blocks

Just like that picture.  If I’m making a wall, I don’t need to add this head:

Lego Head

Lego Head

It doesn’t make any sense to add that to a wall, does it?  But if the rule is ‘use every single Lego® block you own no matter what’, then even if you’re making a wall, you have to add that on .  Maybe to the top, it would be good at scaring intruders, or at least making them laugh.

With the Linux® kernel, you don’t have to do that.  You can use modules to build your kernel so it does exactly what you need and nothing more.

It’s hard to find computing hardware that Linux® won’t support.  It runs motherboards, networking connections, webcams, security systems, CDs, DVDs, mice, keyboards, card readers, I could go on and on.  In fact, it might be easier to list things it doesn’t support, but… I really can’t think of anything.  If someone really wants to use Linux® to run a piece of hardware, they’ll either write the kernel module or find someone to write it for them.

You may not use all of the modules in Linux® but when you download the source code for it from https://kernel.org (that’s where the most recent source code is) you do get almost all of the modules along with it.  You can pick and choose which parts you want when you build it, which is something we’ll talk about in the future.  That’s because it’s about how to compile a program from source code, a more advanced topic than this.

I said ‘almost all’ because sometimes hardware manufacturers offer the modules separately.  It depends on the manufacturer and the people that maintain the kernel source code.

Kernel of the Matter

We’ve talked about how your computer gets places on the Internet.  Now let’s talk about how your computer, well, works!  How does Linux® do its thing, aside from  typing commands at the command line?  It’s called the kernel and it’s the core (or kernel) of the operation of your system.

So it’s not like the kernel of your popcorn, it’s more like the thing in the middle.  Like Nick Fury.  He’s always in the middle of the Avengers keeping things going.  Or making things go.  He’s always up to something.

Nick Fury

Nick Fury

He showed up with that helicarrier at the end of The Age of Ultron, didn’t he? He made that happen and got the Avengers (and the towns people) from the flying in the sky town (something I never want to see) and on to safety.

That’s what the kernel does.  It’s in between the hardware and the software.  Like this picture:

What the Kernel Does

What the Kernel Does

Linus Torvalds made the first Linux® kernel in 1991 for his own system. These days it runs on all sorts of things, but back then, he just wanted something for his own computer.

So how does it control your hardware?  It uses things called device drivers.  It lets you type on your keyboard and have it show up on a screen, or print to a printer, or read a CD, or read a USB, or… you get the idea.  It’s really flexible.

The kernel is also  modular.  That means that it isn’t one huge thing that keeps things going, but it’s made up of lots of little systems that put it all together and make it go.

It’s kind of like a Death Star built from Legos.  It needs all the parts together to make it a Death Star, but you can leave pieces off and still mostly have a Death Star.  You can add pieces to it and it’ll be a Death Star with more abilities.  Like if someone added an ice cream maker to the Death Star.  Or a spa for the emperor.  It’s still the Death Star and still acts like it, it’s just more!