DevX Skillbuilding for IBM DeveloperWorks
DevX Skillbuilding for IBM DeveloperWorks
DevX Skillbuilding for IBM DeveloperWorks
Get regular email alerts when we publish new features!
DevX Update for IBM developerWorks

More Newsletters
 Print Print

IBM developerWorks Teaches the Fine Art of Linux Hacking

Customizing Linux may not be for everyone in a working environment but, even if you only "hack" the OS for fun, the lessons learned can lead to valuable skills. 


Linux has made tremendous headway toward becoming a mainstream operating system for a number of reasons. Its stability is exemplary, its scalability is exceptional, it is relatively easy to maintain, it doesn't have the same unrealistic hardware requirements needed by some other operating systems, and—in its open source incarnations—it's inexpensive or even free. In addition to these distinctive characteristics, it also has another feature that isn't readily available in many other platforms: it can be molded and customized to fit the needs of both end users and the devices on which it runs.

Those interested in learning about customizing Linux to suit their needs may want to check out the IBM developerWorks tutorials on the subject: "Hacking the Linux 2.6 kernel," Part 1 and Part 2. Both tutorials are written for Linux or UNIX developers, at the beginning to intermediate levels, who have a general familiarity with using a UNIX command-line shell and some knowledge of C programming. They're authored by Valerie Henson, an IBM Software Engineer with the Linux Technology Center, and Lina Martensson, a freelance writer at Chalmers University of Technology, Sweden. Both tutorials include a list of system requirements as well as resources that can be used to further your educational foray into Linux. The tutorials themselves can even be used as academic tools. Best of all, they're free.

The Advantages of Kernel Hacking
The old adage that says "if it ain't broke, don't fix it," rarely holds true for anything that can be improved upon. Whether you're upgrading your car stereo or replacing an outdated stove in your house, the obvious shortsightedness of this way of thinking is painfully obvious. The unfortunate thing about many operating systems is that they offer little or no room for customization. Even if they're broken, the best you can do is to download a patch or reinstall the same faulty system you started out with. What's even more unfortunate is that those who use Linux, an environment that provides for customization, don't have the skills to use its potential to their advantage. The environment itself may not be broken, but it can almost always be improved.

"There are a number of reasons that you might want to customize Linux," says Valerie Henson, coauthor of the tutorials. "You may want to hack into the kernel to mold it into a tool for work or you may want to learn about it simply out of intellectual curiosity. Most of us involved in information technology are interested in learning new things, so increasing your knowledge is an obvious benefit. Raising the level of your job skills is an added bonus."

Henson says that one of the reasons you might want to hack the Linux kernel is to create a hardware driver that isn't supported in the environment. Another reason is to make Linux perform tasks that may not have been considered when the more generic kernel was created. Hacking the kernel to alter a driver can also allow it do things that aren't officially supported. For example, a lot of wireless cards include hardware that can allow them to be used as a wireless access point, but the companies don't expect you to use them that way, since they sell the exact same card for more money in their wireless access point product. The driver released by the company won't support access point mode, but often a programmer will write a driver to support it.

Changing the kernel can also be useful in tracking down bugs. "If you have a bug in your system, you can often recompile the kernel with information that will help you find it so that you're not at the mercy of a support contract," Henson says.

Custom kernels for small microprocessor-based hardware appliances are also frequently needed. Because there's no licensing fee, many companies are interested in using stripped-down versions of Linux as an operating system for their technology products. "You can definitely recompile the Linux kernel to make it more streamlined," Henson says. "You can take functionality out and you can add it in. Usually, kernels are shipped with the ability to do everything that you might want them to even though you rarely need all of that functionality. If you really wanted to, you could even completely eliminate the network stack."

Other reasons for customization include making Linux installations appropriate for academic and scientific applications. If you're performing tasks that require massive computation, learning about computer architecture and operating systems is a must. "A friend of mine was doing computational tasks in Mathematica that were taking forever to complete," Henson says. "After enlisting the help of someone who had an in-depth understanding of how the system worked, they managed to cut the time taken to complete the computations to minutes instead of days. So much research is now being done through computer simulations that you really want to know the characteristics of the system to make your program run as fast as possible."

Henson adds that anyone maintaining a Linux cluster should have a working knowledge of the kernel because working with a large number of machines often tickles seldom seen kernel-level bugs. For example, a bug that appears once per year on a developer's computer might show up once a week in a medium-sized cluster. In addition, anyone working with a custom module should know about the inner workings of the kernel in order to avoid breaking kernel programming rules and corrupting or crashing the entire system.

While it is possible to cause problems with a system in hacking the kernel, the tutorials provide precautions that can be taken to ease the minds of would-be kernel hackers. "Save your old kernel, don't let anyone else compile it, and you should be fine. You could have the same level of problems that you might have in editing the registry on your Windows machine without saving the old one. If you back up the kernel so that you can restore the machine to its previous state or work through the tutorials on a machine that you can tinker with, the potential problems are minimal. You just need to use some common sense."

Part 1: Getting Ready to Hack
The first tutorial in developerWorks' Linux hacking series is called, appropriately, "Hacking the Linux 2.6 Kernel, Part 1: Getting Ready." The 23-page document deals primarily with learning about system and environment requirements, the best ways to acquire Linux source code, how to configure and boot a new kernel, and how to use the printk function to print messages during bootup. By working though the steps in the tutorial, users learn about setting up or obtaining the correct tools and software they need in preparing to make a hack, including the compilation environment and kernel source. The tutorial goes on to describe the steps necessary to configure, compile, alter, recompile, boot, and copy the kernel to a new piece of hardware. If the kernel doesn't boot, troubleshooting suggestions are also given.

Part 2: Hacking for Fun and Functionality
Once all of the pieces are in place, it's time to actually alter the Linux kernel to see how it's done. This topic is addressed in Henson and Martensson's "Hacking the Linux 2.6 Kernel, Part 2: Making Your First Hack." While Part 1 offered an overview of the kernel source and the tools necessary to get to the heart of Linux hacking, Part 2 looks at the different parts of the kernel, their location, the order in which they execute, and how to locate a particular piece of code. It then delves into system calls and the procedure for building modules, and wraps up with creating, applying, and submitting the patches you've made. Every step of the way, explanations and instructions are both clear and concise.

Distribution is important in having your patch considered for use in your organization's overall Linux system, and the means by which this can be accomplished is discussed in detail. Having that patch accepted, however, is another story. This task is included in an important section called "Political Considerations." Here, you'll be introduced to the kind of social engineering that you may have to undertake to prove that your patch is relevant and meaningful without stepping on the toes of the system's gatekeepers.

Hacking: Form and Function
When many people envision a "hacker," the first thing that comes to mind is the image of "black hat" evil-doers who destroy system functionality. Here, however, this often-derogatory term is shown in a whole new light. By learning how to manipulate the Linux kernel, you have the opportunity to make the environment more valuable, whether it's by paring it down for use in embedded systems, building drivers, or simply making appropriate use of its power. In this series, Henson and Martensson provide a pair of tutorials that will help you gain skills and alleviate any fears you may have about delving deeply into the Linux kernel—one of the most malleable environments available. Work your way through them, and you're on your way toward not only increasing the system's functionality, but increasing your knowledge of Linux. "It's extremely valuable to gain experience in customizing Linux," Henson says. "You can go from doing it at home just for fun to addressing the specific needs of your company. Whatever your goals, it's a cool thing to learn."

   
George Walsh is a veteran tech editor and writer with experience in fields ranging from embedded systems programming to CAD. As a freelance researcher and writer he has provided his expertise to more than 35 clients in a wide variety of technical markets. He is currently a contributing editor to SD Times.
Submit article to:
Featured Resources from IBM