Chrome | GlusterFS | Salt Stack | Post' LINUX JOURNAL Since 1994: The Original Magazine of the Linux Community INTRO TO SALT STACK the Python-Built Configuration Management System r Incorporate Python into Your Bash Workflow Write Extensions for GlusterFS with Python L CREATE APPLICATIONS inside the Chrome Browser ! SYMBOLIC MATH with Python PostgreSl^jf Capabilities wix. PostGIS 2.0 NOVEMBER 2012 | ISSUE 223 | www.linuxjournal.com 1 TIPS AND TRICKS for People Stuck with Windows December 9-14, 2012 Register by November 19th San Diego, CA and SAVE! rt£D , CRISIS AVERTED! CRISIS AVER 2Siii iSryn #f ^SSmmSSSSSStl bUiifBrSROu ad Teeiinmaes int Cerf, Google www.usenix.org/lisa12 > Si j*m i Sponsored by in cooperation with LOPSA - ss fisP join nsjn', trg , a w9 **£5Lm*"* m ‘ gO lOO' , tt> \l*A'N are n toilin* \ th \ZN"^ are P/US 3-Hay •eMMcat Program: ■SSSSi^-*. Detweiler, Man Bla?" 9 ' Valer,e D^kelmann a2e ' ands ^"a '^i t y^"" sylVania Univ ersixy ’SEE. '*«—«»•* monitoring sS and da,a . management, and toots* s ^ s * e ms ^ P s^. puppe ‘ Ut>S 7 iwuis “Mo.. ° rS » Hallway Track > and more! CV/- w San Die9°’ visit us at www.s RACKMOUNT SER When you partner with Silicon Mechanics, you get more than stellar technology - you get an Expert like Pierre. CONTENTS 'SST PYTHON FEATURES 68 Python Scripts as a Replacement for Bash Utility Scripts Learn how to use Python and existing UNIX tools to improve your productivity in the shell. Richard Delaney 80 Extending GlusterFS with Python GlusterFS is a distributed filesystem with a strong emphasis on extensibility. Now extensions can be written in Python, bringing significant performance and other improvements within reach of even more programmers. Jeff Darcy 90 Getting Started with Salt Stack—the Other Configuration Management System Built with Python Install and configure software on multiple servers at once. Ben Hosmer ON THE COVER • Extend PostgreSQL's Capabilities with PostGIS 2.0, p. 102 • Intro to Salt Stack—the Python-Built Configuration Management System, p. 90 • Incorporate Python into Your Bash Workflow, p. 68 • Write Extensions for GlusterFS with Python, p. 80 • Create Applications inside the Chrome Browser, p. 32 • Symbolic Math with Python, p. 24 • Tips and Tricks for People Stuck with Windows, p. 54 4 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM INDEPTH 102 The Past, Present and Future of GIS: PostGIS 2.0 Is Here! Have the workhorse of GIS at your fingertips. Stefano lacovella COLUMNS 32 Reuven M. Lerner’s At the Forge Chrome Extensions 42 Dave Taylor’s Work the Shell SIGALRM Timers and Stdin Analysis 48 Kyle Rankin’s Hack and / What's Up Dock? 54 Shawn Powers’ The Open-Source Classroom People in Glass Houses Are Stuck with Windows 112 DocSearls’ EOF Playing Value Subtraction Games IN EVERY ISSUE 8 Current lssue.tar.gz 10 Letters 20 UPFRONT 30 Editors’ Choice 65 New Products 117 Advertisers Index FolderSync FolderSy nc enables easy sync of files • • ke w between cloud storage and Android devices 30 FOLDERSYNC LINUX JOURNAL (ISSN 1075-3583) is published monthly by Belltown Media, Inc., 2121 Sage Road, Ste. 310, Houston, TX 77056 USA. Subscription rate is $29.50/year. Subscriptions start with the next issue. WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 5 LINUX JOURNAL. Subscribe to Linux Journal Digital Edition for only $2.45 an issue. ENJOY: Timely delivery Off-line reading Easy navigation Phrase search and highlighting Ability to save, clip and share articles Embedded videos LINUX JOURNAL Executive Editor Jill Franklin jill@linuxjournal.com Senior Editor Doc Searls doc@linuxjournal.com Associate Editor Shawn Powers shawn@linuxjournal.com Art Director Garrick Antikajian garrick@linuxjournal.com Products Editor James Gray newproducts@linuxjournal.com Editor Emeritus Don Marti dmarti@linuxjournal.com Technical Editor Michael Baxter mab@cruzio.com Senior Columnist Reuven Lerner reuven@lerner.co.il Security Editor Mick Bauer mick@visi.com Hack Editor Kyle Rankin lj@greenfly.net Virtual Editor Bill Childers bill.childers@linuxjournal.com Contributing Editors Ibrahim Haddad • Robert Love • Zack Brown • Dave Phillips • Marco Fioretti • Ludovic Marcotte Paul Barry • Paul McKenney • Dave Taylor • Dirk Elmendorf • Justin Ryan Publisher Carlie Fairchild publisher@linuxjournal.com Advertising Sales Manager Rebecca Cassity rebecca@linuxjournal.com Associate Publisher Mark Irgang mark@linuxjournal.com Webmistress Katherine Druckman webmistress@linuxjournal.com Accountant Candy Beauchamp acct@linuxjournal.com Linux Journal is published by, and is a registered trade name of, Belltown Media, Inc. PO Box 980985, Houston, TX 77098 USA Editorial Advisory Panel Brad Abram Baillio • Nick Baronian • Hari Boukis • Steve Case Kalyana Krishna Chadalavada • Brian Conner • Caleb S. Cullen • Keir Davis Michael Eager • Nick Faltys • Dennis Franklin Frey • Alicia Gibb Victor Gregorio • Philip Jacob • Jay Kruizenga • David A. Lane Steve Marquez • Dave McAllister • Carson McDonald • Craig Oda Jeffrey D. Parent • Charnell Pugsley • Thomas Quinlan • Mike Roberts Kristin Shoemaker • Chris D. Stark • Patrick Swartz • James Walker Advertising E-MAIL: ads@linuxjournal.com URL: www.linuxjournal.com/advertising PHONE: +1 713-344-1956 ext. 2 Android & iOS apps, desktop and e-Reader versions Subscriptions E-MAIL: subs@linuxjournal.com URL: www.linuxjournal.com/subscribe MAIL: PO Box 980985, Houston, TX 77098 USA LINUX is a registered trademark of Linus Torvalds. SUBSCRIBE TODAY! TrueNAS™ Storage Appliances Harness the Cloud Unified. Scalable. Flexible. Thanks to the Intel® Xeon® Processor 5600 series and high- performance flash, every TrueNAS Storage appliance delivers the utmost in throughput and IOPS. As IT infrastructure becomes increasingly virtualized, effective storage has become a critical requirement. iXsystems' TrueNAS Storage appliances offer high-throughput, low-latency backing for popular virtualization programs such as Hyper-V, VMWare®, and Xen®. TrueNAS hybrid storage technology combines memory, NAND flash, and traditional hard disks to dramatically reduce the cost of operating a high performance storage infrastructure. Each TrueNAS appliance can also serve multiple types of clients simultaneously over both iSCSI and NFS, making TrueNAS a flexible solution for your enterprise needs. For growing businesses that are consolidating infrastructure, the TrueNAS Pro is a powerful, flexible entry-level storage appliance. iXsystems also offers the TrueNAS Enterprise, which provides increased bandwidth, IOPS and storage capacity for resource-intensive applications. ✓ Supports iSCSI and NFS exports simultaneously Compatible with popular S Virtualization programs such as Hyper-V, VMware, and Xen ✓ 128-bit ZFS file system with up to triple parity software RAID Call 1-855-GREP-4-IX, or go to www.iXsystems.com TrueNAS Pro Features TrueNAS Enterprise Features • One Six-Core Intel® Xeon® Processor • Two Six-Core Intel® Xeon® Processors 5600 Series 5600 Series • High Performance Write Cache • Extreme Performance Write Cache • Up to 480GB MLC SSD Cache • Up to 1.2TB High Performance ioMemory • Up to 220 TB SATA Capacity . Up to 500TB SATA or 320TB SAS Capacity • Quad Gigabit Ethernet • Dual Ten Gigabit Ethernet • 48GB ECC Memory • 96GB ECC Memory Intel, the Intel logo, and Xeon Inside are trademarks or registered trademarks of Intel Corporation in the U.S. and other countries. Current_lssue.tar.gz V Indiana was the Dog’s Name M y wife is afraid of snakes. Actually, "afraid" may not be a big enough word. My wife is terrifyingly and abundantly mortified of snakes. Like any good husband, I remind her that Indiana Jones also was afraid of snakes, so she's in good company. This month, our issue is all about vipers—no, wait. Python. Whether you're a new programmer or an old coder, Python is flexible, cross-platform and really quite robust. Joey Bernard gets the Python train rolling in our UpFront section. Sympy is a library for Python providing a full- featured computer algebra system. Although I have no problem with my kids learning long division, there certainly are some great advantages to using computers for complex maths. Reuven M. Lerner takes a trip into HTML5 land. He shows how to create Chrome extensions, which can be entire applications running inside your browser. With HTML5, CSS, JavaScript and so on, Chrome applications can be robust, complex and a far cry from the Web applications of just a few years ago. In fact, if you recall from last month, I use a Chrome extension for writing my Linux Journal articles. Our other resident programmer is Dave Taylor, who teaches how to use SIGALARM in scripts to add valuable complexity to scripts that need it. That might sound overwhelming, but Dave explains what he's doing along the way, and in the end, what seems like a complex and confusing idea makes sense. Speaking of confusing ideas, I had to do a double take when I read Kyle Rankin's article on his new Android device. Yes, you read that right, Kyle uses Android. Like most things Kyle does, however, it's more than just switching from his N900 to a new phone. He's never happy with just a phone; Kyle wants a communication device that doubles as an International Space Station. This month, he comes close. I haven't been happy with the lack of hate mail in my inbox recently, so I thought it would be a good time to write an article about Windows. Okay, to be honest, it's a little more complicated than that, but I fully expect to get hate 8 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM CURRENT ISSUE.TAR.GZ mail nonetheless! As a Linux user currently stuck in a job with a Windows infrastructure (not here at Linux Journal, of course). I'm working hard to feel as at home as possible. I share my struggles with you, and maybe make Windows a little easier to deal with. After my sacrilegious foray into the Windows world, Richard Delaney brings us back to topic with his article on replacing Bash scripts with Python. Since Bash scripting is the only form of programming I ever do, I'm both hesitant and excited about this topic. Learning a new language would be very beneficial for me, and if I can use it for the same purposes I use Bash, all the better! GlusterFS is a fascinating distributed filesystem, which can scale to enormous size. If you're a Python programmer and want to add functionality to GlusterFS, Jeff Darcy's article is perfect. Integrating code across languages can be a daunting task, but with the flexibility of Python, Jeff shows us it's worth the effort. Configuration management systems are all the rage. This is obviously because it makes managing large numbers of servers much easier to do. A part of me thinks it might be due to the funny sounding project names as well. Puppet and Chef are both fairly well known, and thanks to their names, they're easy to remember. This month, Ben Hosmer introduces us to a Python-based configuration management tool named Salt Stack. Apparently having interesting names is a requirement in the configuration management world, and Salt Stack lives up to that. Does it live up to the functionality of its competition? Ben lets us know. Stefano lacovella finishes off this issue with PostGIS. PostgreSQL is a great open- source database system, but for keeping track of spatial data (think maps), it really needs to run with an extension like PostGIS in order to handle that type of stuff. Not only does PostGIS allow for complex mapping of spatial data, but it also can handle four-dimensional information as well. Good luck to Indiana Jones if he tries to follow a four-dimensional treasure map though. It's hard enough to keep track of snakes in three dimensions! This month is a well-rounded issue, which is heavy on the Python. If you're not a programmer, or don't want to learn about programming, fear not. We still have a lineup of content sure to please. Oh, and before you think of sending a rubber snake to the Linux Journal office, keep in mind that I'm not afraid of them at all. Now bees? That's another story altogether.* Shawn Powers is the Associate Editor for Linux Journal. He’s also the Gadget Guy for LinuxJournal.com, and he has an interesting collection of vintage Garfield coffee mugs. Don’t let his silly hairdo fool you. he’s a pretty ordinary guy and can be reached via e-mail at shawn@linuxjournal.com. Or. swing by the #linuxjournal IRC channel on Freenode.net. WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 9 letters r PL/R | DNS | Arduino | Squeezebox | NVM | Telnet | ZaTab rn mm mm |1#' TELNET: A HANDY LINUX JOURNAL Sine* ITO. Tht Migum of U*Ima C«mmu<4t w mn iw itMgi i i ... I.— EMBEDDED * AN^fe pUlMjQSWS PlRED •HARDWARE PROJECT • • ! . . IGJN •• MAIN?MEM0RY. • IS GOfMG TO CHANGE * • , EVERYTHING STREAM Y0U8 MUSIC WITH LOGITECH SQUEEZEBOX’S OPEN PLATFORM Reviewed Bash Embed the R Language ZaReason’s Notational in PostgreSQL ZaTab Shortcuts for Powerful Statistical Analysis Short vs. Clear Regarding Dave Taylor's question to readers in his September 201 2 column, my vote is for both. I find the various short forms useful and like to discover them; however, I absolutely agree about the problem of obfuscation. I tend to avoid shortcuts in code I will have to maintain and use them only on the command line or in "throw-away" code. I suggest that you focus on clever ideas for the most part and maybe dedicate occasional columns to shortcuts. Alternatively, you could finish the column with a shortened version of your monthly script with some minimal notation to guide the advanced or the curious in deciphering the syntax. Love your column Dave, keep it up. —Keith Another Response for Dave Use new bash features even if some old servers don't understand them? My answer is yes! My clients want the best solution for their challenges. If they can't or don't want to upgrade an old server, they will pay the extra time to adapt the script to the ancient version of the shell. But they want me to be as efficient on the new servers as I (not the script language!) can be. So I learn everyday something new and use it in my daily work. Please do the same! —Eugen Bash Notational Shortcuts When efficiency is important, but gets in the way of clarity, I like to use a feature found not only in bash, but in sh, esh, zsh and many other scripting languages. If you start a line with a # character, the shell ignores the remainder of the line, which allows you to include human-readable explanatory material. You even can precede the # with white space, so the explanatory material aligns with the indentation level of the code! i 1 8n? No problem—the shell 10 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM [ LETTERS i automatically adjusts to whatever human-readable language you use in the remainder of the line! But wait—there's more! It doesn't even have to be a human language! If you replace a clunky but clear construct with a tight, abstruse one, you even can include the former as explanatory material, as long as you precede it with this almost-magical # character! Given how much time we all spend debugging or enhancing other people's code, I am continually astonished at how many shell programmers seem to be unaware of this universal feature of scripting languages. —Jenny Howard Linux-Based Security Camera Systems I have read in the Letters section that readers say they wish to see different articles or more home-use practical- type projects. May I suggest a possible article about Linux-based camera security systems? ZoneMinder is one that comes to mind, but I'd also like to read about what is available in the commercial product area that supports IP cameras as well as DVR cards with cameras attached. I have used several brands of DVR capture cards, and most ran on a Windows-based system. I purchased a 16-port card and experimented with ZoneMinder and then discovered a company called Bluecherry (http://www.bluecherry.net or http://store.bluecherry.net), and I purchased one of its hardware- compression DVR cards and use its Linux-based camera server system. The company has a newly released 2.0 version of its server software running on Ubuntu 12.04 Linux. Many people like the idea of being able to put up cameras around their homes or businesses. LJ might want to do a simple article of some of the commercial and open-source DVR-security-camera- type applications available for Linux. I have seen articles written for Asterisk and other IP phone/PBX systems that can be used in homes or businesses, so why not one for security cameras? I have been reading the magazine for more than seven years, and given the pros and cons of what others have said about the switch to the all-digital format, I am very pleased with it and the choices of formats available. —Chad Pauli WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 11 [ LETTERS i Setting up a Linux-based camera system certainly sounds like a cool project. I actually really like the folks at Bluecherry and I tried my best to purchase an entire 32-camera system from them. Sadly the red tape at my day job prevented it, but I did try the software, and it really was amazing. For some strange reason, it seems that many systems are Windows- only, or claim to be Web-enabled but then require Internet Explorer in order to work. Hopefully, someone will read this letter and offer an article on configuring a camera system. It certainly sounds interesting to me! — Ed. Comment to Jeff Shutt When reading the Letters section in the September 2012 issue of Linux Journal, I saw Jeff Shutt's message about shell scripting and .csv files, and I wondered if he'd already heard of csvdb (http://sandbox.ltmnet.com/csvdb). This tool allows one to manipulate .csv files with SQL (including UPDATE and ALTER TABLE), and it could be a good alternative to shell scripts. I don't know how to contact him, but maybe you could act as gateway and forward this info to him? Thanks in advance. —Frank Scheiner Even better, how about we print your letter? That way Jeff won't be the only one benefiting. Thanks! — Ed. Kyle's Raspberry Pi Beer Fridge Suggestion I received my Raspberry Pi a couple weeks ago, a month after I ordered it, but it seems that production times are improving and that the new boards are being manufactured in Britain. I started playing with it, and I have a suggestion for Kyle's beer fridge. My idea is to use a Tl LaunchPad board (http://www.ti.com/LaunchPad) with an MSP430 micro-controller that has an integrated temperature sensor. Tl provides all the development tools for free, and the price is less than $5. The idea is to control the temperature on the LaunchPad and use the Raspberry Pi to retrieve the information and display it or post it on a Web server. I had only one problem. The Raspberry Pi didn't boot with the LaunchPad connected to the USB port. I have no idea why, but the workaround is to connect them using the SPI port (a tutorial on how to do this can 12 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM [ LETTERS i be found at http://mitchtech.net/ raspberry-pi-msp430-spi). I really enjoy every issue of LJ, and read each of them from the BOF all the way to the EOF (no more cover to cover). —jschiavon Constructive Complaint, a Thank You and Further Ideas Here in Belgium, I bought LJ at the store for years, and now I have to cope with Texterity. I don't really enjoy the format, but the content is there. I restrained from protesting and gave this a lot of thought. Since you went digital, I finally realized what was bugging me the most: it is not the change of media, but that it wouldn't be possible to select "text mode by default" in some settings. The second issue is related to my media consumption habits. I read LJ when I'm not at home, thus, almost exclusively on mobile Internet, which often is very slow. Some kind of cache, allowing on-line sync and off-line reading would be a killer feature. Considering that this would not be possible with Texterity, you taught me there has to be another way, as I will explain now. Last week, I received a new keyboard with heaps of funky keys (Corsair K90). Upon connecting the keyboard to the computer and searching the Internet on how to get it to work, I discovered that so far, no one has gotten the funky keys to feed the penguin. I decided to dig a little deeper into the problem. Combining tricks I learned from Kyle Rankin's hacks and Dave Taylor's scripts, it took me four days to get a very functional script that should already be able to manage various types of custom inputs with minor tweaks. It is still far from perfect, but the code is on https://github.com/ jupiter126/k90-test. Have you considered crowdsourcing the LJ app? Again, I thought about it a lot, and it seems to be the best long-term solution. (No, I don't sound like Lennart!) The main argument is that since LJ went digital, there is no point in paying for copies of LJ anymore, and that most LJ readers have the technical know-how required to wget pirate versions, although they willingly choose to pay instead, in order to support LJ. Thus, piracy is not an issue, as nowadays we are not buying an app, but rather sponsoring WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 13 [ LETTERS i the writers of the content. Another important argument is security, it is normal that you don't want to run unknown code on a server, but hey, I never said I wanted to crowdsource the server! Setting standard procedures and protocols for authentication and issue retrieval are the key elements for successful adoption (like passing zipped XMLs after successful authentication). From that point on, users or groups of users will be able to code their own apps for their own devices, and instead of having users like me complaining all year long about the past, you will promote the *NIX way. If you don't like the application, stop losing time complaining and start writing one you like better. On the other hand, I am sure a lot of your readers are not only willing to code a killer app, but also have the skills to do so and prefer running code they know rather than an obscure app. Do I really need to convince you that the future is open source? What are you waiting for in setting up an "LJ Git"? I would love to find Dave's code there, for example! —Nelson Thank you for the feedback. I like the Idea of a Linux Journal Git repo. We'll have to see if it's feasible. Regarding your text-mode question, I find that the .mobi and .epub versions are much more text-friendly. My favorite way to read Linux Journal currently is either via PDF on a big tablet or with Amazon's Kindle, because the latter will sync my reading progress between devices. Even if there is not an official LJ repo on GitHub, I like the idea of a crowdsourced app to interface with the existing distribution model! — Ed. Configuration Management Article I've been one of your readers for about eight years, and since then, a lot of stuff has changed. As a long¬ term Linux hacker, I've been hacking on a configuration management system for some time. As it has matured, I think it could be time to make it more public. Thus, I was wondering whether you are interested in getting or writing an article about cdist—the new kid on the block of configuration management systems, cdist is kind of a revolution, because it is almost 14 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM [ LETTERS i completely dependency-free and has very well known DSL. Let me know what you think. —Nico By all means, submit an article query on the topic to ljeditor@linuxjournal.com. Configuration management is certainly something sysadmins are pretty much forced to use now. I'll admit I've never heard of cdist; at the very least, thank you for bringing it to my attention! — Ed. Readable Shell Scripting I just read Dave Taylor's Work the Shell column titled "Bash Notational Shortcuts: Efficiency over Clarity" in the September 2012 issue. I agree wholeheartedly. Bash code should be written for readability, not efficiency. If the code needs to be efficient, then use a compiled language to reduce processing time. If I use some clever Bash construct in a script, six months (or however much time) later, I may have to dig around to figure out what I did and how I did it. Not to mention that others coming after you may need to figure out what you did so they can make requisite changes. This holds for any moderately usable programming language that gives potentially many ways to solve a particular problem. I say keep your examples easily understandable and inefficient. You may want to pepper your examples with more efficient examples, but because your audience potentially could be at many different levels of knowledge, keeping the examples readable and understandable without having resort to research in order to understand them will benefit more of us who don't script on a daily basis. Thanks, and I look forward to your column next month! —Trey Blancher Thanks! Thanks so much for offering a digital version of Linux Journal in .epub format. I already had a NOOK (original) and a NOOK Color reader, so I didn't need any additional hardware to take advantage of your format change. In 2011, I was offered an early retirement, which I took. A short time later, I moved overseas. Linux Journal is the only magazine I have renewed my subscription to. At this point, I have two requirements: a digital-only WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 15 [ LETTERS i subscription option and .epub as a format option. Thankfully, Linux Journal fits both of my requirements. Thanks again for an affordable way to keep up with one of my favorite publications. —Gar That's great to hear, Gar! We decided to invest the time to produce the .epub and .mobi versions, because we wanted the switch to digital to offer more rather than less. I find the multiple formats very nice, because I don't always read from the same device. — Ed. Xoom: It Really Whips the ZaReason Tablet's Arse! Although I think it is a always a noble endeavor to try to produce hardware tailored for Linux/open source, I'm not so sure about Android's future in that it is so extremely limited and kludgey compared to any bona fide laptop/ desktop Linux distribution, but that is beyond the scope of a mere letter to the editor. (Yes, yes, I know it is possible to run a real Linux distribution using chroot and a VNC client on rooted Android devices. Go ahead and try it and see how much fun you have!) Err, getting back to the point, ZaReason's underpowered tablet is just not worth the price—not when there are used Motorola Xooms on eBay for about $200. The Xoom has a dual-core processor, a 1200x800 screen nearly big enough for comfortable magazine PDF reading, a surprisingly good quality camera, USB host support via a cheap $5 cable, and the latest version Android. What with all the competition, with the price of a used original iPad in freefall and $200 Nexus 7s, why would anybody buy this ZaReason thingy? (I am in no way affiliated with Motorola and have no love of big corporations. I just want a cheap tablet!) Come to think of it, don't most Linux-preinstalled laptops—still rare beasties—command a hefty premium? You often can buy a svelte used high- end Lenovo or MacBook Pro for the price of one of those $1,200 Emperor seven-pound Dell boat anchors. What is this, the Linux Tax?! Dirty word or not, I doubt rooting is such a big deal for most FUD-resistant, warranty-voiding, independent- minded Linux users. Rooting the Wi-Fi-only Xoom was trivial, after a 16 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM [ LETTERS i few hours of wading through Web fora. (Wading through semi-literate forum-geek nonsense is nothing new to anyone who has used Linux for any length of time.) Like most new Android devices, the Xoom sucked at first: a microSD card slot you couldn't use because there was no driver and a sluggish Ul that I was tempted to refer to as another sort of sandwich besides ice cream. But now, the Xoom is nearly mature. Although the Ul still looks like crap—a perennial problem of all Android devices (haven't the devs ever heard the old realtor's saying "light and bright"?)—it is definitely "buttery" smooth ugliness with plenty of mostly free apps: Netflix, eBay, Firefox, e-book readers and so on. If only the half-baked Android Ul didn't resemble something out of the Addams' Family School of Design.... —Mike "Zaphod” Grossman I understand where you're conning from, but in the case of ZaReason, the company's willingness to leave Its product open Is a philosophical mindset I really want to support. For me, it's sort of like shopping at a local farm market and paying more for vegetables, because I want to support those folks. ZaReason may not be able to match the price point of bigger companies, but it gets my hat tip for its business ideals. I'll admit, as an end user more than a developer, I'd be more likely to buy a Nexus 7, but if I were a developer, I could see the wide-open model being very refreshing. — Ed. Random Numbers and OpenMP I was reading the May 2012 issue when I came across "Parallel Programming in C and Python" by Amit Saha, which had relevance to my job supporting weather/climate modeling on HPC systems. OpenMP is a particularly nice way to handle threading, but you have to be careful what you use in your parallel code. The common example of calculating pi in pLopenmp.c is a nice way of showing parallelism, but I would have liked to have seen a comment about using rand() in your function called in the parallel region. Creating random numbers in threaded applications can be tricky due to the hidden state of the generator being stored across calls. rand() is probably not the best method to use in C due to WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 17 [ LETTERS i this, possibly rand_r(unsigned int) to store the state and be suitable for this example. There is also probably a race condition to decide which thread gets which random number due to using the same random state. Another aspect I would have liked to have seen is repeatability of answers, which is important in science where OpenMP can affect the order of calculations. Otherwise, I found the article to be a great way to introduce people to threading, and I especially found the Python aspect useful to add to my understanding. I hope to see more HPC-relevant articles in the future. —Thomas Green Amit Saha replies: Thank you for reading my article and writing in with your comments. I am glad you found the Python bits interesting. I find your comments about rand() and OpenMP enlightening, as I myself didn't think of it before. This blog post seems to be suggesting the same thing: http://software.intel.com/ en-us/blogs/2009/11/05/use-of- rand-in-openmp-parallel-sections. And, a number of other blog posts seem to suggest that the initialization of the generator be done separately in the individual threads. This means that in pi_openmp.c, the random number generator should be initialized using a random seed (such as current time) in the part_count function. Thanks again for writing in. Eight-Year-Old Linux User I have an eight-year-old son who is fond of computers, mainly because of games, naturally. Last week when I got home, I saw that my Xubuntu laptop was open. I asked my son who opened it. He replied that he did it. Once I defined a user for him on Xubuntu, so that he could Google his homework. It seems he remembered the user name and password, logged on, opened Chrome, and listened to music via YouTube. For basic stuff like this, Ubuntu is simple enough for a small kid like him. I wanted to share this for those who think Linux is only for geeks. —Kaan You are absolutely correct. It's weird for me to watch my kids use technology. They don't care at all what operating system they're using. In fact, my girls tend to do homework research on their 18 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM [ LETTERS i phones before cracking open their laptops. I think I'm too old to consider my smartphone my "primary computing device", but my kids don't have a problem with that mindset. They also grab my Dell D420 running Xubuntu and use the system without hesitation. Kids are amazing. — Ed. Photo of the Month Here is a photo of a pack made by a pizzeria on a beach in Palermo, Sicily, Italy. As you can read, Linux offers pizza, panini and other magnificent stuff, like "arancini". The content is excellent, like our preferred OS. —Giovanni Organtini Linux Pizza WRITE LJ A LETTER We love hearing from our readers. Please send us your comments and feedback via http://www.linuxjournal.com/contact. LINUX J 0 ■ R M AI Fit Your Service SUBSCRIPTIONS: Linux Journal is available in a variety of digital formats, including PDF, .epub, .mobi and an on-line digital edition, as well as apps for iOS and Android devices. Renewing your subscription, changing your e-mail address for issue delivery, paying your invoice, viewing your account details or other subscription inquiries can be done instantly on-line: http://www.linuxjournal.com/subs. E-mail us at subs@linuxjournal.com or reach us via postal mail at Linux Journal, PO Box 980985, Houston, TX 77098 USA. Please remember to include your complete name and address when contacting us. ACCESSING THE DIGITAL ARCHIVE: Your monthly download notifications will have links to the various formats and to the digital archive. To access the digital archive at any time, log in at http://www.linuxjournal.com/digital. LETTERS TO THE EDITOR: We welcome your letters and encourage you to submit them at http://www.linuxjournal.com/contact or mail them to Linux Journal, PO Box 980985, Houston, TX 77098 USA. Letters may be edited for space and clarity. WRITING FOR US: We always are looking for contributed articles, tutorials and real-world stories for the magazine. An author's guide, a list of topics and due dates can be found on-line: http://www.linuxjournal.com/author. FREE e-NEWSLETTERS: Linux Journal editors publish newsletters on both a weekly and monthly basis. Receive late-breaking news, technical tips and tricks, an inside look at upcoming issues and links to in-depth stories featured on http://www.linuxjournal.com. Subscribe for free today: http://www.linuxjournal.com/ enewsletters. ADVERTISING: Linux Journal is a great resource for readers and advertisers alike. Request a media kit, view our current editorial calendar and advertising due dates, or learn more about other advertising and marketing opportunities by visiting us on-line: http://ww.linuxjournal.com/ advertising. Contact us directly for further information: ads@linuxjournal.com or + 1 713-344-1956 ext. 2. V WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 19 FRONT NEWS+FUN diff -u WHAT’S NEW IN KERNEL DEVELOPMENT This edition of diff -u is dedicated to Andre Hedrick, who committed suicide in July 2012. He was best known for his work on the IDE driver and later the ATA subsystem, which provided essentially the same features. I knew Andre only from his e-mails to the kernel mailing list. He seemed supremely confident in his understanding of the intricacies of IDE hard drive technology. He had a deep understanding of the specifications and standards, but he also had a deep understanding of the way the different pieces of hardware violated those specs and standards. Sometimes his understanding derived from conversations with the manufacturers and engineers who developed the hardware. IDE/ATA was one of the biggest nightmares of the entire kernel, for years. It was one of those areas that just had to work right, but that was steeped in mystery and confusion. Even detecting which hard drive was on the system was a nightmarish puzzle in many cases. Andre really could lose his temper though when someone disagreed with him. Once upon a time in 2000, he discovered that the root user could physically damage the hard drive on a given system. To him, this was an important bug, because if malicious users gained root, he felt they shouldn't be able to do real physical harm. But when he submitted his patch to fix the problem, no one wanted to take it, on the grounds that if someone gains root privileges, it's already game-over. During the ensuing flame war, Andre famously said in multiple different e-mail messages, "Here is your SECURITY HOLE! JOE-SIX-PACK- HACKER can fry your butt." He may have been hard to deal with sometimes, but I believe his heart always was in the right place. The cantankerous rantings that seemed so abusive at times, all stemmed from a desire to help and protect users, in the face of a truly ugly yet absolutely essential hardware industry. The kernel configuration system 20 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM [ UPFRONT] is about to become much simpler for the great mass of users in the world. Linus Torvalds recently put out a call to action, of sorts. He'd noticed that Linux distributions often had odd and unpredictable kernel configurations, and the system might break in subtle ways if users compiled a kernel that lacked some obscure option or other. This typically was not a problem for kernel developers themselves who tend to have a deep understanding of configuration options, but for regular users, he felt it resulted in fewer people being comfortable compiling their own kernels. Linus asked the kernel developers in general to work on providing a minimal default kernel configuration for all versions of all main distributions. This would ensure a working kernel and give users a jumping-off point for their own explorations. Kernel development often is pretty crazy, and kernel developers like their little jokes. Recently when Alexandre Pereira da Silva suggested adding a "Tested-by:" signature to all of the kernel's git commits, it led to a discussion of all the different types of git signatures that have been accepted into the kernel. Some of the choicest were things like "Fatfingered-by:" and "Heckled-for- on-IRC-by:". These are actually in the kernel, or at least in the git log. On one level, it's a shame, because people doing data mining to analyze kernel development may have a harder time with their analysis, but on another level, it's all pretty funny. —zack brown T®ot ©®®5 [?aoo©[ • Vortex86DX 1 GHz Fanless CPU 9 Low Power Consumption • 1 RS232/422/485 serial port 9 Mini-PCI Expansion slot • 2 USB 2.0 Host Ports • 10/100 BaseT Ethernet Oudio ^^ • PS/2 mouse & keyboard 9 CompactFlash & MicroSD card sockets 9 Resolution/Colors: 1024 x 600 @ 256K 9 Resistive Touch Screen 9 Free EMACOE Linux 9 Free Eclipse IDE Setting up a Panel PC can be a Puzzling experience. However, the PDX-089T comes ready to run with the Operating System installed on flash disk. Apply power and watch the Linux X-Windows desktop user interface appear on the vivid color LCD. Interact with the PDX-089T using the responsive integrated touchscreen. Everything works out of the box, allowing you to concentrate on your application rather than building and configuring device drivers. Just Write-lt and Run-lt... Starting at $450 Qty 1. www.emacinc.com/panel_pc/pdx089.htm | Since 1985 | OVER 27 YEARS OF | SINGLE BOARD | SOLUTIONS 0=2, one. Phone:(618) 529-4525 • Fax:(618)457-0110 • www.emacinc.com WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 21 [UPFRONT] Steam Nukem Forever Although many thought Duke Nukem Forever was nothing more than vaporware meant to be used as a metaphor for "never gonna happen", we were all shocked when it was actually released. Linux users have a similar love/hate relationship with with Valve, the makers of the Steam platform for gaming. Rumors of Linux-native Steam have been circulating for years, but nothing apart from running Steam under Wine ever has brought the popular platform to Linux. Several months ago. Valve announced it was working on a client, and I excitedly wrote about it, only to look foolish when months went by and nothing happened. Based on communication from Valve, it looks like in October 2012 there will be 1,000 real-life Linux users chosen for an external beta release! Will those chosen few be sworn to secrecy? Is Valve messing with journalists and not even have a Linux plan? For news right from the horse's mouth, check out Valve's Linux blog: http://blogs.valvesoftware.com/linux. —SHAWN POWERS They Said It Let him who would enjoy a good future waste none of his present. —Roger Babson Happiness is not something you postpone for the future; it is something you design for the present. —Jim Rohn Change is the law of life. And those who look only to the past or present are certain to miss the future. —John F. Kennedy If you want a vision of the future, imagine a boot stamping on a human face—forever. —George Orwell The best way to predict the future is to create it. —Peter Drucker 22 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM [ UPFRONT] Space Is Big—See It All! I have a huge collection of NASA photos taken from the Astronomy Pic of the Day Web site (http://apod.nasa.gov/apod/astropix.html) stored in a folder in my Dropbox. No matter what computer system I'm using, I rotate those images on my background, getting a virtual tour of the universe on every screen. Oddly enough, it can be challenging to get that image rotation to work well in Linux. I've mentioned some wallpaper-rotating applications before, but Slidewall is really pretty cool. Wall Mode (s> Slideshow Mode Live Wallpaper Mode Timing for Slideshow Mode Slideshow Tinning: 20 20 — * minutes 0 ^ seconds Miscellaneous I^T Minimize to system tray Notify me when wallpaper changes ® Run Slid ewall when Ubuntu is starting Reset Preferences Save Preferences Quit Slidewall Close to tray Slidewall includes a small daemon process to change GNOME-based desktop wallpaper images. It's in the Ubuntu 12.04+ repositories, but it will work with any GNOME-based system. Rotating a wallpaper collection works well, but Slidewall goes the extra mile and will fetch collections live from the Internet, or it can display a live picture of the Earth showing which parts have sunlight and which don't. Slidewall is a huge step forward in desktop wallpaper management, and if you have a difficult time deciding what picture to use as your background, now you don't have to choose just one!— shawnpowers WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 23 [UPFRONT] Symbolic Math with Python Many programming languages include libraries to do more complicated math. You can do statistics, numerical analysis or handle big numbers. One topic many programming languages have difficulty with is symbolic math. If you use Python though, you have access to sympy, the symbolic math library. Sympy is under constant development, and it's aiming to be a full-featured computer algebra system (CAS). It also is written completely in Python, so you won't need to install any extra requirements. You can download a source tarball or a git repository if you want the latest and greatest. Most distributions also provide a package for sympy for those of you less concerned about being bleeding-edge. Once it is installed, you will be able to access the sympy library in two ways. You can access it like any other library with the import statement. But, sympy also provides a binary called isympy that is modeled after ipython. In its simplest mode, sympy can be used as a calculator. Sympy has built-in support for three numeric types: float, rational and integer. Float and integer are intuitive, but what is a rational? A rational number is made of a numerator and a denominator. So, Rational(5,2) is equivalent to 5/2. There is also support for complex numbers. The imaginary part of a complex number is tagged with the constant I. So, a basic complex number is: a + b*l You can get the imaginary part with "im", and the real part with "re". You need to tell functions explicitly when they need to deal with complex numbers. For example, when doing a basic expansion, you get: exp(I*x).expand() exp(I*x) To get the actual expansion, you need to tell expand that it is dealing with complex numbers. This would look like: exp(I*x).expand(complex=True) All of the standard arithmetic operators, like addition, multiplication and power are available. All of the usual functions also are available, like trigonometric functions, special functions and so on. Special constants, like e and pi, are treated symbolically in sympy. They won't actually evaluate 24 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM [ UPFRONT] to a number, so something like " 1+pi" remains "1+pi". You actually have to use evalf explicitly to get a numeric value. There is also a class, called oo, which represents the concept of infinity—a handy extra when doing more complicated mathematics. Although this is useful, the real power of a CAS is the ability to do symbolic mathematics, like calculus or solving equations. Most other CASes automatically create symbolic variables when you use them. In sympy, these symbolic entities exist as classes, so you need to create them explicitly. You create them by using: x = Symbol('x ') y = Symbol('y') If you have more than one symbol at a time to define, you can use: x,y = symbols (' x ' , 'y ') Then, you can use them in other operations, like looking at equations. For example: (x+y)* *2 You then can apply operations to these equations, like expanding it: ((x+y)**2).expand () x**2 + 2*x*y + y**2 You also can substitute these variables for other variables, or even numbers, using the substitution operator. For example: ((x+y)**2).subs (x , 1) (l+y)**2 You can decompose or combine more complicated equations too. For example, let's say you have the following: (x+l)/(x-l) Then, you can do a partial fraction decomposition with: apart((x+l)/(x-l),x) 1 + 2/(x-1) You can combine things back together again with: together(l + 2/(x-l)) (x+l)/(x-l) When dealing with trigonometric functions, you need to tell operators like expand and together about it. For example, you could use: sin(x+y).expand(trig=True) sin(x)*cos (y) + sin(y)*cos(x) The really big use case for a CAS is calculus. Calculus is the backbone WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 25 [UPFRONT] of scientific calculations and is used in many situations. One of the fundamental ideas in calculus is the limit. Sympy provides a function called limit to handle exactly that. You need to provide a function, a variable and the value toward which the limit is being calculated. So, if you wanted to calculate the limit of (sin(x)/x) as x goes to 0, you would use: limit(sin(x)/x , x, 0) 1 Because sympy provides an infinity object, you can calculate limits as they go to infinity. So, you can calculate: limit(l/x, x, oo) 0 Sympy also allows you to do differentiation. It can understand basic polynomials, as well as trigonometric functions. If you wanted to differentiate sin(x), then you could use: x = Symbol('x ') diff(sin(x), x) cos (x) You can calculate higher derivatives by adding an extra parameter to the diff function call. So, calculating the first derivative of (x**2) can be done with: diff(x**2, x, 1) 2*x While the second derivative can be done with: dif f(x * * 2, x, 2) 2 Sympy provides for calculating solutions to differential equations. You can define a differential equation with the di f f function. For example: f(x).diff(x,x) + f(x) where f (x) is the function of interest, and di ff(x,x) takes the second derivative of f(x) with respect to x. To solve this equation, you would use the function dsolve: dsolve(f(x).diff(x,x) + f(x), f(x)) f(x) = Cl*cos(x) + C2*sin(x) This is a very common task in scientific calculations. The opposite of differentiation is integration. Sympy provides support for both indefinite and definite integrals. You can integrate elementary functions with: i integrate (si n (x) , x) 26 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM [ UPFRONT] -cos(x) You can integrate special functions too. For example: i integrate (exp (-x**2) *erf (x) , x) Definite integrals can be calculated by adding limits to the integration. If you integrate sin(x) from 0 to pi/2, you would use: integrate(sin(x) , (x, 0, pi/2)) 1 Sympy also can handle some improper integrals. For example: i integrate (exp (x) , (x, 0, oo)) 1 Sometimes, equations are too complex to deal with analytically. In those cases, you need to generate a series expansion and calculate an approximation. Sympy provides the operator series to do this. For example, if you wanted a fourth-order series expansion of cos(x) about 0, you would use: cos(x).series(x, 0, 4) 1 - (x**2)/2 + (x* *4)/24 Sympy handles linear algebra through the use of the Matri xclass. If you are dealing with just numbers, you can use: Matrix([[1,0], [0,1]]) If you want to, you can define the dimensions of your matrix explicitly. This would look like: Matrix(2, 2, [1, 0, 0, 1]) You also can use symbolic variables in your matrices: x = Symbol ('x') y = Symbol('y') A = Matrix([[l,x], [y,1]]) Once a matrix is created, you can operate on it. There are functions to do dot products, cross products or calculate determinants. Vectors are simply matrices made of either one row or one column. Doing all of these calculations is a bit of a waste if you can't print out what you are doing in a form you can use. The most basic output is generated with the print command. If you want to dress it up some, you can use the ppri nt command. This command does some ASCII pretty-printing, using ASCII characters to display things like integral signs. If you want to generate output that you can use in a published article, you can make sympy generate WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 27 [UPFRONT] LaTeX output. This is done with the latex function. Simply using the plain function will generate generic LaTeX output. For example: latex (x**2) x A {2} You can hand in modes, however, for special cases. If you wanted to generate inline LaTeX, you could use: latex(x**2, mode='inline') $x A {2}$ You can generate full LaTeX equation output with: latex(x**2, mode='equation') \begin{equation}x A {2}\end{equation} To end, let's look at some gotchas that may crop up. The first thing to consider is the equal sign. A single equal sign is the assignment operator, while two equal signs are used for equality testing. Equality testing applies only to actual equality, not symbolic. So, testing the following will return false: (x+l)* *2 == x* *2 + 2*x + 1 If you want to test whether two equations are equal, you need to subtract one from the other, and through careful use of expand, simplify and trigs imp, see whether you end up with 0. Sympy doesn't use the default Python int and float, because it provides more control. If you have an expression that contains only numbers, the default Python types are used. If you want to use the sympy data types, you can use the function sympi f y (), or S (). So, using Python data types, you get: 6.2 -> 6.2000000000000002 Whereas the sympy data types give: S (6.2) -> 6.20000000000000 Expressions are immutable in sympy. Any functions applied to them do not change the expressions themselves, but instead return new expressions. This article touched on only the most basic elements of sympy. But, I hope you have seen that it can be very useful in doing scientific calculations. And by using the isympy console, you have the flexibility to do interactive scientific analysis and work. If some functionality isn't there yet, remember that it is under active development, and also remember that you always can chip in and offer to help out. —JOEY BERNARD 28 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM [ UPFRONT] Non-Linux i vo i Usually the Non-Linux FOSS article is dedicated to open-source software for Windows or OS X, but since this month's The Open- Source Classroom column is about surviving in a Windows world as a Linux user, I thought I'd take some liberties here and talk about open-source soda! If you've been to Penguicon (http://www.penguicon.org), it's likely you've been able to taste OpenCola, the fully open-source soda-pop drink designed to take the mystery out of cola. The original formula is available all over the Internet (because it's open source!), but there are some great step-by-step procedures as well. Wiki-How has a great outline here: http://www.wikihow.com/Make-OpenCola. For a more complex recipe and at least one other flavor, be sure to visit http://www.opensoda.org as well. Sharing recipes is nothing new for homebrewers of beer, but it's surprisingly difficult to find soda- pop recipes. Perhaps with the supermarket availability of SodaStream (http://www.sodastream.com), open-source recipes will become more common. In the meantime, if you'd like to try open-source soda, be sure to stop by Penguicon's Consuite next year. —SHAWN POWERS WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 29 [EDITORS' CHOICE] Android Candy: Never Plug In Your Phone Again! Last month (the October 2012 issue of LJ), I showed you an awesome audiobook player app for Android, but I didn't share my frustration in getting the audio files on to my phone. When I plugged my phone in to the computer, I couldn't get the SD card to mount, no matter what settings I changed. It was very frustrating and forced me to come up with a better way. Enter: FolderSync. First off, it's important to note that FolderSync isn't free. There is a free version, but it's limited to a single account and has ads, and although it works, it really isn't the same as the full app. If you're like me, you don't think twice about spending $5 on a fancy cup of coffee, but if you have to pay for an app on your phone, you debate internally for hours. I've never been so happy I spent $2.29 on an app. Basically, FolderSync works on your phone sort of like Dropbox works on your desktop computer. Instead of syncing only your Dropbox files, however, FolderSync supports a wide variety of data sources. At the time of this writing, data can be synchronized with Amazon S3, Google Docs, Google Drive, SkyDrive, Dropbox, SugarSync, Box.net, Ubuntu One, NetDocuments, FTP/FTPES/FTPS, SFTP, WebDAV/WebDAVs and Samba/SMB/CIFS. 30 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM Image from the FolderSync Home Page: http://www.tacit.dk [ EDITORS' CHOICE ] Image from the FolderSync Home Page: http://www.tacit.dk In my case, I keep an Audiobooks folder on a share at home, and every night the files are synced up. With the paid version, synchronizations can be forced as well. Although the Audiobook syncing is the only thing I use FolderSync for, it's the perfect tool to keep music, photos, documents or anything else synchronized on your phone without ever plugging it in to the computer. Because FolderSync supports two-way synchronization, it is possible to delete files from your home server, so be careful! After configuring FolderSync to sync my Audiobooks automatically, but only over Wi-Fi and only while plugged in, I realized it had to be Editors' Choice for this month. That means two Android apps in a row win the coveted title, but once you try it, I suspect you'll agree. Check out FolderSync at the Google Play Store: https://play.google.com/ sto re/a p ps/deta i I s?id=dk. tacit, android.foldersync.full, or if you'd prefer to keep your pumpkin latte money, try out the Lite version: https://play.google.com/store/ apps/details?id=dk.tacit.android. foldersync.lite. —SHAWN POWERS WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 31 COLUMNS AT THE FORGE Chrome Extensions REUVEN M. LERNER Create applications inside the Chrome browser with standard Web technologies: HTML, CSS and JavaScript. Back when Netscape was a rising star in the high-tech world, cofounder Marc Andreessen announced that the browser was a new form of operating system, within which people could create applications. Rather than writing apps for Windows, or the Macintosh, or even Linux (a laughable idea back then), we would write them for the browser. This seemed like a far-fetched idea at the time, but it obviously has become the case. Today, it is the norm to speak of a "Web application", meaning something that is delivered via the browser, but whose code sits on a server. This is what I think of when someone says "Web application", and it has been a while since I really thought seriously about even writing a desktop application. That said, there's certainly an advantage to working with desktop applications. They work more smoothly with other applications; they can interact with the filesystem, and they just have a more natural look and feel. This is changing, especially given the capabilities that HTML5 brings to the table and the ways that browsers are becoming integrated into the overall user experience, rather than being one of many applications running on the computer. What I really like is the relative simplicity of creating a Web application, including using the technologies that are the Web's bread and butter—HTML, CSS and JavaScript—and which I use, at least for client-side development, on a day-to-day basis. Firefox has offered developers the chance to write extensions for a long time. However, I must admit that I wasn't thrilled with the idea of learning an entirely new language and paradigm (Mozilla's XUL). The Greasemonkey extension for Firefox has long been a favorite 32 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM 1 COLUMNS AT THE FORGE of mine, making it possible for me to make client-side changes and customizations to Web sites of all sorts. But, it wasn't completely integrated into the browser, and it required installation and configuration beyond what most people are willing to accept. Extensions in Google Chrome (or the open-source Chromium), by contrast, use Web technologies and are built in to the browser, making it truly possible to extend the browser in a number of different ways by loading packages of HTML, CSS and JavaScript. This month, I look at the different types of extensions you can write with Chrome and consider when it's better to write an extension than a Web application, as well as show how to develop a simple extension of your own. Creating an Extension As I mentioned previously, a Chrome extension is a combination of HTML, CSS and JavaScript. There are different types of extensions; right now, let's concentrate on a browser extension, which puts an icon in the top-right corner of the browser, which produces a pop-up and also can interact with the contents of the browser window. Creating an extension is actually quite simple and can be done from within any directory on your computer. Create a new directory, and in it, create a file called manifest.json. As the file extension indicates, this file (which gives Chrome information about your extension) is written in JSON (JavaScript object notation), which is natural and easy to pick up by anyone familiar with JavaScript. The manifest tells Chrome how to load the extension, what permissions it should have and what elements should be displayed within the browser window. For example, here is a simple extension manifest for the extension I'm building for this article: { "name": "ATF sample extension", "version": "1.0", "manifest_version": 2, "description": "Description of my ATF sample extension", "browser_action": { "default_icon": "atf.png" } } As you can see, manifest.json contains a number of name-value pairs, as you would expect from a JSON or JavaScript object. The names are set by the Chrome extension standard document, and although most of the WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 33 COLUMNS AT THE FORGE Extensions can do any number of things, from providing snapshots of other sites, to interacting with the overall browser environment, to interacting with the page currently being viewed. values are strings, there are cases when they will contain numbers (for example, the mani f est_versi on), objects (for example, the browser_action) or even arrays. According to the standard document (see Resources), the only required fields in manifest.json are "name" (containing the extension name) and "version" (indicating the extension version). However, Google also says that as of Chrome 18, "developers should specify 2" for the version number, and that seems like a reasonable idea to me. Because this extension is a browser action, you need to specify this name-value pair, stating "browser_action" as the name and a JSON object as its value. That value, which can (and will) contain several additional name-value pairs, currently has just one, namely "defaultjcon", which indicates what icon should be displayed in Chrome's toolbar to the right of the address bar. default_icon is a string containing a filename, which should be a PNG graphic of the correct size (19x19) that represents your extension. Once you have created manifest.json, create (or download) a 19x19 PNG icon, and put it inside the extension folder with the filename atf.png. With the extension directory, manifest.json and icon, you're now ready to load the extension into Chrome. Open your browser to chrome://chrome/ extensions/—a special URL for extension management—and make sure the "developer mode" check box is set, so that you can load extensions without Google's permission and from your local disk. Once you have done that, a "Load unpacked extension" button should be available. Click on that, and then use the file-selection dialog to select the extension directory. (Don't select a file within the directory, but rather the directory itself.) Once you have done this, your extension should show up in Chrome with the extension name, description and version number. If and when you update the extension, 34 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM 1 COLUMNS AT THE FORGE you can tell Chrome to reload it by clicking the reload link under the extension name. Make the Extension Useful Now that you've created a basic extension, let's try to make it useful. Extensions can do any number of things, from providing snapshots of other sites, to interacting with the overall browser environment, to interacting with the page currently being viewed. Let's first create a pop-up. The manifest.json file already indicates what popupjcon will be. Let's add to that an HTML file, which then will be displayed when you click on your extension's icon. To do this, just set the "default_popup" value within manifest.json to the name of an HTML file within the extension directory. Then, create an HTML file with that name. For example: { "name": "ATF sample extension", "version": "1.1", "manifest_version": 2, "description": "Description of my ATF sample extension", "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" } } Notice I also have increased the version number of the extension to make sure I can keep track of which version was created when. (If you're using a version-control system, such as Git, you can keep a tag or a commit note indicating when you updated the version number.) Now that I've told Chrome that it should load popup.html whenever I click on the extension's icon, I really should create an HTML file named popup.html. Here's a simple one that you can include:
I am an extension paragraph.
Now, if this file looks simple to you, that's the point. Extensions can become complex, but they don't have to be, particularly if you're doing something simple. Save the new version of manifest.json and popup.html inside the extension directory, reload the extension, and click on the extension icon. You should see the WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 35 COLUMNS AT THE FORGE text pop up, albeit in an ugly black- and-white window. If you want to add some nice styling to popup.html, you can do so with CSS. Create a file, popup.css, and place it (of course) in the extension directory: hi { color: blue; } Now, add a link to that stylesheet inside popup.html:I am an extension paragraph.
Sure enough, the hi headline is now colored blue. JavaScript in Extensions All of this seems pretty straightforward—and it is. But if you're thinking that you can just stick some JavaScript inside the HTML file and have it execute, as would be the case in a normal HTML file... well, that's where things become a bit tricky and different. For security reasons (which I admittedly don't quite understand), JavaScript needs to be in a separate file, referenced from the HTML file. In such a case, the file looks like this:I am an extension paragraph.
What can be in your popup.js? Anything you want, actually. Here's a really simple (and annoying!) one: alert("You have loaded the popup!"); Now clicking on the extension icon will produce a JavaScript alert. Once you have dismissed it by clicking the OK button, you will get your beautifully formatted HTML page, in popup.html. 36 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM COLUMNS AT THE FORGE What can you do within popup.js? Truth be told, you can do just about anything you want—modify text, retrieve content from other sites, calculate things and send information elsewhere. If you can do it in JavaScript, the odds are that you can do it within the browser. You even can use a library, such as jQuery, so long as your copy of jQuery is referenced and loaded from within the extension directory. So, let's try something a bit bolder. Let's retrieve data from a Web site and insert it into the pop-up window, using jQuery. In order to do this, you'll need to modify your popup.html a bit:I am an extension paragraph.
Notice how I've added the line referencing jquery.js in the extension directory. You also can reference one of the copies that Google or another company has put on-line, in order to improve caching and download speeds. I've also given an ID attribute of "paragraph" to the "p" tag in the HTML, which will make it easier to grab the paragraph and do something with it. The biggest difference will be in popup.js. No longer will you just have a call to alertO in there. Instead, you'll actually use jQuery's Ajax facilities to retrieve information from a Web site and stick it into the pop-up window. You're going to do it in an ugly, brute-force way here, in order to see the results more obviously, but you easily can imagine an example that would go through the contents of a Web site more gracefully (or, perhaps, its RSS/Atom feed), picking out information that is of use and then displaying it. For example, you could create a browser extension that displays the current weather. For this example, let's just have the browser go to a Web site, retrieve its contents and stick the raw content into the pop-up's "p" tag. Here's the updated popup.js: alert("Popup -- before"); function showText(data) { $("#paragraph").text(data); WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 37 COLUMNS AT THE FORGE }; $.get('http://lerner.co.il/' , showText); alert("Popup -- after") ; Now, the reason I put in the "before" and "after" alerts is not because I enjoy annoying my users, but because I find it instructive to see when things happen in the asynchronous Ajax world. (Hint: remove the calls to "alert" before you unleash this amazing extension on your users.) You define a function, showText, which adheres to jQuery's definition of what a function should look like, namely that it accepts (at least) one parameter, named "data", which contains the contents of the URL you tried to retrieve. That's all you're going to do here, using the "text" method to stick in the HTML source. That means the end user will see the source; if you want something a bit more aesthetic, you can use the html() method rather than the text() method. But, showText isn't invoked directly. Rather, it's invoked as a callback function, executing when your invocation of $.get(), a function that executes in the background (that is, asynchronously), returns the contents of the Web site. This could take one second or ten, but in most cases, it'll be pretty fast. However, the callback almost certainly will be invoked only after your second call to a I e rt(). That is, you'll see the first alertO call, the second alert() call and then a change in the contents of the paragraph. Such event-based coding is the norm in the JavaScript world, and it can take a little time to get used to it. Notice that the second parameter is showText, the function itself, which then is invoked after a successful Ajax call. If you now reload the browser extension and click on the button, you'll find...that nothing really happens. That is, you get the first and second calls to alert, but the paragraph doesn't change its contents. This is because you haven't told Chrome that it's okay to retrieve data from lerner.co.il, or from any other URL. Because retrieving data from an external URL is a potentially dangerous event, exposing you to things in the outside world, you need to allow its use explicitly. This is done by returning to manifest.json and adding a "permissions" key: { "name": "ATF sample extension", "version": "1.1", "manifest_version": 2, 38 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM 1 COLUMNS AT THE FORGE "description": "Description of my ATF sample extension", "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" }. "permissions": [ "http://lerner.co.il/" ] } The "permissions" key can contain a large variety of items, from URLs (as in this case) to wild-card matches, to keywords that Google has defined. For example, if you'll want your extension to use such FITML5 abilities as geolocation or local storage, you'll need to indicate that here. Now, all of this is nice if you want to modify popup.html, namely the pop-up that you get with your browser extension. What if you actually want to interact with the page itself, either reading from it or writing to it? The answer is that you can do this by writing not a "browser action", as it is known in the Chrome world, but a "content script". Now, a content script requires a different manifest.json, but it also raises questions about how you can interact with a page that itself might have some JavaScript executing. The answer is that Chrome provides an interesting facility known as "isolated worlds", in which two separate JavaScript environments— one on the page and the other in the browser—can operate independently, each with its own JavaScript library (and version of jQuery, if necessary), but interact simultaneously with the DOM and the contents of the page. Such isolation not only means that your content script can play with the contents of the page in a number of ways without worrying LINUX JOURNAL on your Android device www.linuxjournal.com/android WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 39 COLUMNS AT THE FORGE about interfering with existing JavaScript, but also that the page cannot "break out" of its sandbox, infecting or otherwise affecting the browser itself. I should note that although I haven't used them in this article's examples. Chrome provides a wide variety of JavaScript methods and functionality through the "chrome" object, which you can access via the permissions key in manifest.json. Such methods give you access to (for example) the current tabs and windows, really allowing you to control and use the browser as an application platform, rather than just a mechanism for displaying content. Conclusion Chrome was designed to use Web technologies, and nowhere is that more obvious than the extension mechanism, which uses a combination of HTML, CSS and JavaScript to produce new user experiences. Now, browser extensions aren't a panacea; they break the idea that the Web is browser-independent and that everything can be downloaded on demand from a server. But if your entire organization will be using Chrome, or if you're looking for something that interacts with existing pages, or if you want to add capabilities to your browser, Chrome's extension mechanism makes it easy to experiment and try new ideas. ■ Reuven M. Lerner is a longtime Web developer, consultant and trainer. He is also finishing a PhD in learning sciences at Northwestern University. His latest project. SaveMyWebApp.com. went live this spring. Reuven lives with his wife and children in Modi’in. Israel. You can reach him at reuven@lerner.co.il. Resources The home page for Google Chrome is http://google.com/chrome. The home page for Chromium, its open-source counterpart, is at http://chromium.org. Extensive information about writing Chrome extensions, including video tutorials, is available from Google at http://developer.chrome.com/extensions. Specifically, you can read more about the standard for manifest.json and what it can contain at http://developer.chrome.com/extensions/manifest.html. jQuery, which is the 900-pound gorilla of JavaScript libraries, is at http://jquery.org. 40 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM ‘Peratii Pro <*«or AMDtl The control and functionality of a root server with dedicated resources MAXIMUM SECURIT 'Offer valid for a limited time only. Lifetime 50% off applies to base fee and configurations. Base configuration includes 1 processor core, 1 GB RAM, 100 GB storage. This offer applies to new contracts only. 12 month minimum contract term. Other terms and conditions may apply. Visitwww.1and1.com for full promotional offer details. Program and pricing specifications and availability subject to change without notice. 1&1 and the 1&1 logo are trademarks of 1&1 Internet, all other trademarks are the property of their respective owners. © 2012 1&1 Internet. All rights reserved. www.1and1.com for unlimited domains 2000 GB included 11 Parallels | c Plesk Panel Redundant storage and mirrored processing units reliably protect your server against any failure Independently adjust CPU cores, RAM and hard disk space and add up to 99 virtual machines. We offer cost transparency through hourly billing. 1&1 DYNAMIC CLOUD SERVER Our data centers offer top security, Cisco firewall protection and maximum uptime. With more than 20 years experience and an extensive server range, we know what IT professionals need. Get full root access for complete control. We are a strong global company with 3 billion dollars in annual revenue and over 6,000 employees worldwide LIFETIME DISCOUNT 50% OFF INCLUDING CONFIGURATIONS, NO SETUP FEE 1&1 DYNAMIC CLOUD SERVER A fully flexible server for a range of requirements including applications, databases, gaming and much more! ■ Independently configure CPU, RAM, and storage ■ Accurate and fair: Control costs with pay-per-configuration and hourly billing ■ Up to 6 Cores, 24 GB RAM, 800 GB storage ■ 2000 GB of traffic included free ■ Parallels® Plesk Panel 11 for unlimited domains, reseller ready ■ Up to 99 virtual machines with different configurations under one contract ■ No setup fee ■ 24/7 phone and e-mail support per month* a * I' JMBi il COLUMNS WORK THE SHELL SIGALRM Timers and Stdin Analysis DAVE TAYLOR It’s not hard to create functions to ensure that your script doesn’t run forever. But what if you want portions to be timed while others can take as long as they need? Not so fast, Dave explains in his latest Work the Shell. In my last article, I started building out a skeleton script that would have the basic functions needed for any decent shell script you might want to create. I started with command-line argument processing with getopts, then explored syslog and status logging as scripts. Finally, I ended that column by talking about how to capture signals like Ctrl-C and invoke functions that can clean up temp files and so on before actually giving up control of your shell script. This time, I want to explore a different facet of signal management in a shell script: having built-in timers that let you specify an allowable quantum of time for a specific function or command to complete with explicit consequences if it hangs. When does a command hang? Often when you're tapping into a network resource. For example, you might have a script that looks up definitions by handing a query to Google via curl. If everything's running fine, it'll complete in a second or two, and you're on your way. But if the network's off-line or Google's having a problem or any of the million other reasons that a network query can fail, what happens to your script? Does it just hang forever, relying on the curl program to have its own timeout feature? That's not good. Alarm Timers One of the most common alarm timer approaches is to give the entire script a specific amount of time within which it has to finish by spawning 42 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM COLUMNS k WORK THE SHELL But if the network’s off-line or Google’s having a problem or any of the million other reasons that a network query can fail, what happens to your script? a subshell that waits that quantum, then kills its parent. Yeah, kinda Oedipal, but at least we're not poking any eyes out in this script! The additional lines end up looking like this: ( sleep 600 kill -TERM $$ )& There's no "trap" involved—easy enough. Notice especially that the closing parenthesis has a trailing ampersand to ensure that the subshell is pushed into the background and runs without blocking the parent script from proceeding. A smarter, cleaner way to do this would be for the timer child subshell to send the appropriate SIGALRM signal to the parent—a small tweak: ( sleep 600 # if 10 minutes pass kill -ALRM $$ # send it a SIGALRM signal )& If you do that, however, what do you need in the parent script to capture the SIGALRM? Let's add that, and let's set up a few functions along the way to continue the theme of useful generic additions to your scripts: function allow_time { ( echo timer allowing $1 seconds for execution sleep $1 kill -ALRM $$ ) & } This first function lets you easily set a time for subsequent execution, while the second presents your ALRM handler in a bit neater fashion: function timeout_handler { echo allowable time for execution exceeded, exit 1 } Note that both scripts have # if 10 minutes pass # send it a SIGTERM signal WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 43 COLUMNS WORK THE SHELL debugging output that's probably not needed for actual production code. It's easily commented out, but running it as is will help you understand how things interact and work together. How might this be used? Like this: trap timeout_handler SIGALRM allow_time 10 code that has ten seconds to complete That would give the script ten seconds to finish. The problem is, what happens if it finishes up in less time than allotted? The subshell is still out there, waiting, and it pushes out the signal to a nonexistent process, causing the following sloppy error message to show up: sigtest.sh: line 7: kill: (10532) - No such process There are two ways to fix this, either kill the subshell when the parent shell exits or have the subshell test for the existence of the parent shell just before it sends the signal. Let's do the latter. It's easier, and having the subshell float around for a few seconds in a sleep is certainly not going to be a waste of computing resources. The easiest way to test for the existence of a specified process is to use ps and check the return code, like this: ps $$ >/dev/nuLL ; echo $? If the process exists, the return code will be 0. If it's gone, the return code will be nonzero. This suggests a simple test: if [ ! $(ps $$ > /dev/null) ] But, that won't work because it's the return code, not what's handed to the shell. The solution? Simply invoke the ps command, then have the expression test the return code: function allow_time { ( echo timer allowing $1 seconds for execution sleep $1 ps $$ > /dev/null if [ ! $? ] ; then kill -ALRM $$ fi ) & } That solves that problem. But, what if you have sections of code where you want to limit your execution time followed by other sections where you don't care? 44 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM COLUMNS L WORK THE SHELL That's easy if you don't mind leaving some child processes around waiting to shoot a signal at the parent. Just use this: trap 11 SIGALRM when you're done with the timed passage. What happens is that the timer generates a signal, but the parent script ignores it. The limitation on this, of course, is if you have code like this: regular code possible runaway code <-- allocate 100 seconds cancel timer more regular code possible runaway code <-- allocate 100 seconds The situation arises if the second code block is started before the first timer runs out. Imagine that you've allocated 100 seconds for the first timed block and it finishes in 90 seconds. Regular code takes five seconds, then you're in block two, Powerful: Rhino Rhino M4700/M6700 • Dell Precision M4700/M6700 w/ Core i7 Quad (8 core) •15.6"-17.3" FHD LED w/ X@1920xl080 • NVidia Quadro K5000M • 750 GB- 1TB hard drive •Up to 32 GB RAM (1866 MHz) • DVD±RW or Blu-ray • 802.11a/b/g/n •Starts at $1190 • E6230, E6330,E6430,E6530 also available • High performance NVidia 3-D on an FHD RGB/LED • High performance Core i7 Quad CPUs, 32 GB RAM • Ultimate configurability — choose your laptop's features • One year Linux tech support — phone and email • Three year manufacturer's on-site warranty • Choice of pre-installed Linux distribution: © <0 * £ (p 0 * % t \ -Tablet: Raven — ^ i J Raven X230/X230 Tablet • ThinkPad X230/X230 tablet by Lenovo • 12.1" HD LED w/ X@1366x768 • 2.6-2.9 GHz Core i7 •Up to 16 GB RAM • 750 GB hard drive / 180 GB SSD • Pen/finger input to screen, rotation • Starts at $2050 • T430, T530, W530 also available N ✓ V Rugged: Tarantula Tarantula CF-31 • Panasonic Toughbook CF-31 • Fully rugged MIL-SPEC-810G tested: drops, dust, moisture & more • 13.1" XGA TouchScreen • 2.4-2.53 GHz Core i5 •Up to 8 GB RAM • 320-750 GB hard drive / 512 GB SSD • CF-19, CF-52, CF-H2 also available EmperorLinux ...where Linux & laptops converge www.EmperorLinux.com 1 - 888 - 651-6686 0 1 Model specifications and availability may vary. COLUMNS WORK THE SHELL for exactly ten seconds. Then the first ALRM timer triggers, after ten seconds rather than another 100. Not good. This is admittedly a bit of a corner case, but to fix it, let's reverse the decision about having child processes test for the existence of the parent before sending the signal and instead have the parent script kill all child subshells upon completion of the timed portion. It's a bit tricky to build, because it requires the use of ps and picks up more processes than just that subshell, so you not only need to screen out your own process, you also want to get rid of any subshell processes that aren't actually the script itself. I use the following: ps -g $$ | grep $myname | cut -fl -d\ | grep -v $$ This generates a list of process IDs (pids) for all the subshells running, which you then can feed to kill: pids=$(ps -g $$ | grep $myname | cut -fl -d\ | grep -v $$) kill $pids The problem is that not all of those processes are still around by the time they're handed to the kill program. The solution? Ignore any errors generated by PID not found: kill $pids > /dev/null 2>&1 Combined as a function, it'd look like this: function ki1l_chiIdren { myname=$(basename $0) pids=$(ps -g $$ | grep $myname | cut -fl -d\ | grep -v $$) kill $pids > /dev/null 2>&1 } If you're thinking "holy cow, multiple timers in the same script is a bit of a mess", you're right. At the point where you need something of this nature, it's quite possible that a different solution would be a smarter path. Further, I'm sure there are other ways to address this, in which case I'd be most interested in hearing from readers about whether you've encountered a situation where you need to have multiple timed portions of your code, and if so, how you managed it! Send e-mail via http://www.linuxjournal.com/contact. ■ Dave Taylor has been hacking shell scripts for more than 30 years. Really. He’s the author of the popular Wicked Cool Shell Scripts and can be found on Twitter as @DaveTaylor and more generally at http://www.DaveTaylorOnline.com. 46 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM Get the best real-world Android developer training anywhere! Choose from more than Learn from the top Android experts, including speakers straight from Google! Attend sessions that cover app development, deployment, management, design and more Network and connect with hundreds of experienced developers and engineers like yourself Register Early and SAVE BIG! Attend AnDevConltf The Android Developer Conference December 4-7,2012 San Francisco Bay Area “AnDevCon is a fantastic conference! There is no better www.AnDevCon.com place to experience the latest and greatest technologies m Follow us: twitter.com/AnDevCon and techniques in the field of Android development. If you attend one conference this year, this one should be it!” —Jay Dellinger, Senior Software Engineer, Manheim A BZ Media Event AnDevCon™ is a trademark of BZ Media LLC. Android™ is a trademark of Google Inc. Google’s Android Robot is used under terms of the Creative Commons 3.0 Attribution License. COLUMNS What’s Up Dock? Kyle finally has found a replacement for his beloved Nokia N900, but maybe not for the reasons you might suspect. KYLE RANKIN If you have followed my column during the past few years, you'll know that I am a big fan of having a portable Linux environment with me wherever I go. For years, this took the form of small laptops (like the Fujitsu P series) and most recently the Nokia N900, which took the form factor down to pocket size. When I got the N900, I thought technology finally had caught up to a dream of mine: the ability to carry my computer in my pocket and, when I'm out walking around, interface with it via the small keyboard and touchscreen. When I get home, I can dock it, and it will expand to a larger display with a proper keyboard and mouse and become my regular computer. The big advantage of this idea is that I can keep my files and environment with me wherever I go. Where the N900 Fell Short Unfortunately, as much as I loved the N900, it had two major shortcomings that stopped me from realizing this dream: low-resolution composite video output and slightly underpowered hardware. Although the N900 display was 800x480 (not great but large enough for a desktop environment), it could output only standard composite video. When I first got the N900, I thought my presentations looked pretty good on it. I had planned to use it for all my conference presentations going forward, but after seeing the low-res results over composite video, I realized that wasn't going to work. I've tried a number of different techniques to work around this limitation—I could set up a local USB network and then see the display over VNC. In fact, I even wrote a previous column talking about how to do this, but although interesting, it ultimately was not the solution I wanted. The N900 was a reasonably fast 48 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM 1 COLUMNS HACK AND / I want a true Linux distribution in my pocket, not a phone OS where you need a special app to do anything. device at the time it was released with a 600MHz ARM processor, 256MB RAM and 32GB of onboard storage expandable to 64GB with an extra microSD card. Although those specs are fine for a portable device and really worked pretty well for my usage model (mostly terminals and Web browsing), when on a larger screen, it seemed like the computer should perform a bit faster. Basically, the N900 was almost there, but not quite. Since a number of phones released after the N900 not only had better hardware specs but also HDMI output, my plan was to wait for the N900+1, whatever that product ended up being, because I assumed it likely would be faster and have HDMI output. Unfortunately, if you've been following the Nokia story, you know that Nokia abandoned its Linux-based phones shortly after the N900, and the follow-on device never really lived up to expectations—the version that had a hardware keyboard wasn't even available for purchase and still had composite out. With the rise and fall of most of the remaining mobile Linux environments and no real platform to go with, I just kept using the N900 and prayed it wouldn't break before I found a successor. Kyle’s Using Android? I'm about as surprised as anyone that I found the N900's successor in a Droid 4—an Android device. I'll be honest, I don't really like Android. I want a true Linux distribution in my pocket, not a phone OS where you need a special app to do anything. All of the pre-installed junkware you get from your carrier reminds me of Windows desktops. I don't like that you have to sneak around and root the device to use it truly how you want (and to get a halfway usable terminal). I don't like how fragmented Android is and how beholden you are to your carrier to get OS upgrades on your device. I also don't like that all of my favorite Linux apps can't be ported over easily. So when I recently got a Droid 4 from my employer, my plan was just to use it for work e-mail and calendaring and keep the N900 for all my mobile computing. WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 49 COLUMNS Figure 1. Atrix Dock Promotional Picture from Motorola So, what caused me to get past all of my feelings about Android? It came down to a laptop dock. When I first heard about the Motorola Atrix and the fact that it had a laptop dock that essentially turned it into a Netbook running a strange version of Linux (Figure 1), I was intrigued but not enough to run Android and put down a few hundred dollars for a dock. Recently though, I saw an article on-line that described how someone had used a series of USB and HDMI adapters to connect his Raspberry Pi to the Atrix laptop dock. Because the dock is basically just a dumb (but high-res) display with an integrated USB keyboard and touchpad, if you had the right adapters, you could connect just about any computer to it. Plus, because it had a big integrated battery designed to charge the phone over USB, it even could power the Raspberry Pi. The dock never really took off for its intended use, so he was able to find one for around $70 or so, and combined with a Raspberry Pi, it made a nice little portable Linux environment. I had some birthday money burning a hole in my pocket, so I found an Atrix laptop dock for $65 on-line and decided to see if I couldn't get it working with my Raspberry Pi. When the dock arrived, I hadn't yet ordered all of the various adapters to use it with my Raspberry Pi, so I figured in the meantime, I would try to test it with my Droid 4. Although the micro HDMI and USB connectors were the right size and the right distance apart to work in my Droid 4, unfortunately they were both turned 180 degrees the wrong way. I wasn't the first person who tried to do this, however, and I was able to find a solution on-line and turned the connectors around in about ten minutes. Once I docked the Droid 4, a special webtop mode kicked in and basically took my regular phone desktop and expanded it to fill the new larger screen (Figure 2). What used to be the notification area at the top of the screen now filled the bottom of the screen like a panel, and mouse clicks acted like 50 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM 1 COLUMNS HACK AND / What I realized then was that if I could figure out a way to get a real Linux environment on this device, I may be able to get close to the dream of that dockable computer I could take with me everywhere. Figure 2. My Default Docked Desktop touchscreen taps. Many native apps took advantage of the larger space and resized as though I were using a tablet. What I realized then was that if I could figure out a way to get a real Linux environment on this device, I may be able to get close to the dream of that dockable computer I could take with me everywhere. Install Linux on Android Although the laptop dock was interesting as it was, what I wanted was a real Linux environment. I knew that it was possible to install a Linux environment on Android by taking advantage of a chroot environment combined with VNC. While a chrooted environment was not as nice as native WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 51 COLUMNS Linux, all I really needed to be happy was a Linux shell environment with all of my favorite command-line tools plus a browser. It turns out that a number of different apps make it easy to set up a Linux chroot environment (some that even inexplicably charge money just for the instructions), but I was able to find a free app called Complete Linux Installer in the Android Market to make the process relatively simple. The Complete Linux Installer app is from the LinuxonAndroid Project at http://linuxonandroid.org, and it allows you to install a number of Linux distributions including Ubuntu, Debian and Backtrack. The first step is to root your phone, which can vary from phone to phone, so I won't go into that here [see Shawn Powers' article "Pwn Your Phone" in the October 2012 issue for more on that topic]. Then, launch the Complete Linux Installer app and follow the instructions, which involve installing the Terminal Emulator app, the BusyBox app and a VNC client. Once the required software is installed, you then select the distribution you want to install. In my case, I picked Ubuntu 12.04, and I could choose from a small console-only image, a medium-size image with LXDE or a full-size image with a Unity desktop. I went with the medium-size image, and once it downloaded, I was able to ^Applications Menu |:|%> Linux Journal | The Origina.. jw Terminal - greenfly@localhost: - Terminal - greenfly@k>calh.. -I >=i WK\ 07:46 O CLICK TO SUBSCRIBE Subscribe to our Digital Edition today! » Includes access to mobile Apps, Kindle and ePub editions. Visualize your enterprise process with Git Read the white paper now! □ a 1 Figure 3. A Customized Green and Black XFCE Desktop 52 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM 1 COLUMNS HACK AND / After that, you can use the terminal like a standard Ubuntu distribution and apt-get install any software you want. launch it from the app. Linux ends up launching inside a terminal app, and on first boot, it asks a couple basic questions for account setup and whether to enable VNC and SSH at boot time. After that, you can use the terminal like a standard Ubuntu distribution and apt-get install any software you want. If you want to access the desktop environment, just start your VNC client and connect to localhost (Figure 3). Although there's some lag accessing the desktop over VNC, and you have to deal with redrawing windows, it's still usable. For my part though, because I mostly need the desktop for a Web browser, I use the Ubuntu chroot for my console and use a native Android browser for Web browsing. That way, I get a console environment the way I want it with a browser that can play multimedia content. What's nice about this arrangement is I get functionality a lot like a Netbook with a nice screen and an okay keyboard, but after a number of hours when I've used up the laptop dock's battery, I still can undock my now fully charged phone and go on my way. So far. I've tried this with a Droid 4 and a Razr, and both were able to dock and enter webtop mode just fine. Of course, any device with HDMI and USB (like the Raspberry Pi) should be able to use the webtop too, as long as you can track down the right adapters. ■ Kyle Rankin is a Sr. Systems Administrator in the San Francisco Bay Area and the author of a number of books, including The Official Ubuntu Server Book, Knoppix Hacks and Ubuntu Hacks. He is currently the president of the North Bay Linux Users’ Group. Resources How to Make a Raspberry Pi Laptop: http://www.raspberrypi.org/phpBB3/ viewtopic.php?f=63&t=6747 LinuxonAndroid Project Page: http://linuxonandroid.org WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 53 COLUMNS THE OPEN-SOURCE CLASSROOM People in Glass Houses Are Stuck with Windows SHAWN POWERS It’s not easy being stuck with Windows, but here are a few tips and tricks to make life a little more familiar. The universe seems to have a big, cosmic-size sense of humor. I recently switched from a job position that allowed me to use Linux as my day-to-day operating system to a position that forces me to use Windows. Mind you, I've had to use different operating systems in the past, and even managed an entire fleet of OS X workstations, but I've never been stuck using Windows as my day-to- day computer before. If you worry that your faithful editor will turn to the dark side and start touting the benefits of a Microsoft operating system—yeah, not so much. Although I'll admit my computer has locked up only once in the past month (which would mean a serious hardware problem in Linux, but I digress), even a stable Windows system isn't Linux. So in this article, I try to make life a little bit easier for fellow Linux users who can see the light, but are stuck behind the Windows. The Simplest Answer: Virtualization My first day on the job, after I learned to press Ctrl-Alt-Delete to log in (am I the only one who finds that weird?), I downloaded virtualization software. On Windows, there are quite a few options from which to choose, but it turns out that many of them are expensive. Thankfully, VirtualBox is free, and it runs quite nicely under Windows (Figure 1). 54 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM COLUMNS k THE OPEN-SOURCE CLASSROOM Oracle VM VirlualBox Manager 0 £3 I o ■ # <*> New Settings Start Discard Xubuntu 12 [% Saved H General Name: Xubuntu 12,04 OS Type: Ubuntu ■ System Base Memory: 512 MB Boot Order: Floppy, CD/DVD- ROM,. Hard Disk I Details (©3 Snapshots J Figure 1. VirtualBox is really quite nice, and it supports Linux fairly well. I've found my favorite way to use Linux in a virtual machine is to put it full screen on a secondary monitor. If you can use a second monitor at work, it is reminiscent of using Synergy to share the same mouse and keyboard on two computers. It's important to note that although VM technology has come a long way, the performance is not quite the same as a real machine, especially when it comes to video acceleration. For general office work, however, I've found the VM to be quite responsive and usable. If you don't have a second monitor or can't dedicate a full screen to Linux, VirtualBox (and most other virtualization packages) offers a "seamless" mode. This is a nifty mode that puts the menu bar on your screen, but it allows for application windows to work alongside Windows applications. The downside of this, however, is that because they appear as native apps, it would seem that you could drag applications from screen to screen. This is not the case, however, so know your Linux apps will be stuck to a single screen. (This obviously is a moot point if you have only one screen!) WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 55 COLUMNS THE OPEN-SOURCE CLASSROOM Because VirtualBox allows you to set up a virtual NAT network, installing one or more instances of Linux won't necessarily show up on the corporate network. Ideally, your Linux installs will be isolated behind your Windows box, and no one will be the wiser. It's important to see how your network settings are configured, however, because you might not be able to connect at all if your network is set to bridged mode. Yes, Cygwin... Anytime someone talks about Linux users forced to use Windows, the discussion invariably turns to Cygwin—and rightly so. Cygwin is an interesting program that brings a Linux look and feel to Windows. The Cygwin setup utility will install a wide variety of familiar applications that are compiled to run natively under Windows (Figures 2 and 3). Cygwin not only provides ^ 7% - Cygwin Setup Progress This page displays the progress of the download or installation. Downloading... crypt-1.2-1 .tar.bz2from http://cygwin.mirTors.hoobly.eom//relea... 0*4 [Ok/IDkj D.OkB/s Package: Total: Disk: < Back Next > Cancel Figure 2. Cygwin does have a nice installation system. 56 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM COLUMNS THE OPEN-SOURCE CLASSROOM X [This is in Windows, but it "feels- like Linux. :a) Cygwin is interesting, but I prefer to use Putty and SSH into I la real Linux machine... Figure 3. Trusty old vim is running in Windows. Windows-native versions of many Linux applications, it also includes a full X server. This X server runs alongside Windows, and it can run X applications installed locally or tunneled over SSH from a remote server. If it sounds like Cygwin is too good to be true, you're sorta right. Although it includes some amazing features and has a very familiar interface, to me it feels clunky to use. Before I get hate mail, let me be clear. Cygwin is amazing; it's just that I don't like the feel of the interface. It reminds me of using Linux back in the days when TWM was a viable window manager. I recommend giving Cygwin a try to see if it fills your needs. It is completely free, so there's no reason not to install it. What I Actually Use: Putty If you're like me, chances are you have a Linux machine running at home. It's also very likely you have broadband Internet and a dedicated "always on" connection. With a little bit of port forwarding on your router, accessing your computer WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 57 COLUMNS THE OPEN-SOURCE CLASSROOM at home is usually pretty painless. It turns out that a simple terminal window connected to a familiar Linux computer is all I need. When I add Xming (which unfortunately requires a donation to get the latest version), everything I need from home is available on my Windows machine. Here's my basic setup: ■ Putty: if you like the command line and are comfortable with tools like Irssi and B it I Bee (Figure 4), Putty is an open-source SSH client for Windows that supports many features Linux users expect. That said, the configuration for Putty can be confusing. Basically, you need to set up all the options you want for a particular profile, then save the profile. The next time you load that profile, all the settings will return. I've had issues with trying to change settings on an existing profile, but I eventually managed to figure it out. Figures 5 and 6 show a couple important settings in Putty. It's possible to tunnel X traffic to your local Windows machine, and it's also easy to set up port tunneling for multiple ports. ■ Xming: this option, like many things in Windows, isn't really free. It's sort of free, in that you don't actually pay for a license, but if you want to get the f spowers®server: ~ A >3 Welcome to the social channel for Linux Journal - The Original Magazi #linuxjournal [Ping timeout: 263 seconds] 10:52 [~N@CPE536dSf3dda4e-CMG01ila53be53.cpe.net.cable.r cgers. com] has quit from #1inuxjournal [Ping timeout: 244 seconds] 10:54 shawnpOwers® | This screenshot will be in Linux Journal. 10:54 shawnpOwers@ | fci o a ■nl 2d 10:54 snap-1 | Howdv 10:54 snap-1 | Bananafingers 10:54 shawnpOwers® | (OK, my Mom doesn’t actually read Linux Journal... but still) 10:55 pronto | hi! 10:55 pronto | shawnpOwers: will i be in the screenshot? :D 10:55 teratoma t~haidaemon@facepalm.feem.net] has j oined #1inuxj ourna1 [10:55 i] [@shawnpOwer s t^i} 3 [3 : f reenode/#linuxjournal (i-Ccnt} ] [Act: 2 , 4] #1inux j ourna1 1 Figure 4. The #linuxjournal channel in a Putty window, silly as always. 58 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM COLUMNS k THE OPEN-SOURCE CLASSROOM PuTTY Configuration 9 Category: B- Terminal Keyboard Bell Features B- Window Appearance Behaviour Translation Selection Colours B- Connection Data Proxy Telnet Rlogin B-SSH Kex 0 Auth TTY XII Tunnels Bugs Options controlling SSH XII forwarding X11 forwarding J Enable X11 forwarding X display location Remote X11 authentication protocol o MIT-Magic-Cookie-1 XDM-Authorization-1 X authority file for local display Browse. .About Help Open Cancel Figure 5. Putty will forward X11 traffic to your local Windows X server, if you have one. program for Windows, you need to donate to the Xming Project. Basically, Xming is an X11 server that happily sits in the system tray and waits for an X program to access it. If you have X forwarding turned on with Putty (or use the -X flag while SSHing from Cygwin), starting an X Window System application is as easy as typing its name. Figure 7 shows a Putty window WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 59 COLUMNS THE OPEN-SOURCE CLASSROOM in which I've typed gnome-terminal. You can see GNOME Terminal started and appeared on the screen like a regular Windows application. I also put a tiny instance of Notepad in the screenshot to show that it really is Windows! Although the pay wall for Xming is frustrating, I find it's unobtrusive nature to be worth it. HI PuTTY Configuration i v s r Category: B-Terminal Keyboard Bell Features Window Appearance Behaviour Translation Selection Colours B- Connection ■■ Data Proxy Telnet • Rlogin ■■ SSH Kex B 0 Auth TTY XII Tunnels Bugs Options controlling SSH port forwanding Port forwarding □ Local ports accept connections from other hosts O Remote ports do the same {SSH-2 only) Forwarded ports: Remove LBBBE} 192.163.1.123:80 LSljSI 152.1 SB. 1.222: El Add new forwarded port: Source port 2222 Add Destination o Local o Auto 152.1 SB. 1.55:2222 Remote Dynamic IPv4 IPv6 About Help Open Cancel Figure 6. Tunneling traffic to your remote connection can give you access to your entire home network. 60 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM COLUMNS THE OPEN-SOURCE CLASSROOM Figure 7. From the top, clockwise: Putty window, GNOME Terminal, Notepad—all running side by side! ■ Screen: although starting X applications is seamless, it's not something I do very often. Most of the time I just use the command line and the screen command. To be fair, "screen" has nothing to do with Windows, but it makes working from a Windows environment much easier. I simply run Irssi in a screen session, and IRC/IM is available anywhere, anytime (Figure 4). Kyle Rankin has done some great articles on screen in the past, so I won't elaborate much here. Simply put, screen allows applications to stay running even after you disconnect your SSH session. For IRC, this means never logging out, which is very convenient. Going Native If you aren't thrilled with the idea of using SSH or setting up an X server on Windows, it's certainly possible to stick with Windows applications. Granted, that won't be the same as using Linux, but many open-source applications are available for Windows users. The most commonly used are WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 61 COLUMNS THE OPEN-SOURCE CLASSROOM Beware the License Beast Something I’ve noticed about using Windows is that many software packages are “free”, but only for personal use. If you are stuck using Windows at work, be sure to read the fine print. The VirtualBox add-on from Oracle is a perfect example. In order to get USB2 and PXE support, you need to install the add-on, but if you’re not using it for personal use, it’s not free. Firefox and LibreOffice, but if you're committed to using as much open source as possible, there are many, many options. In fact, each month I highlight an open-source application available for non-Linux platforms in our UpFront section. Along with running open-source programs specifically compiled for Windows, don't forget the Web-only applications available, regardless of your underlying operating system. Whether you want to use TweetDeck for sending Tweets or want a simple word processor like WriteBox (which I'm using right now), Web apps are getting more and more powerful every day. I find myself using Pixlr more often than The GIMP or Photoshop, regardless of what platform I'm using. Flip the Problem on Its Head! Do you have control over the computer you're using at work? Maybe suffering through Windows isn't something you need to do at all. If there's anything Linux users are used to, it's surviving in a Windows world. Perhaps you actually could run Linux on your computer, and no one ever has to know! If you have to run only an application or two under Windows, it's possible Wine will work instead of booting into Windows. Sometimes the only reason we need to use Windows is because certain Web sites will function only under Internet Explorer. Using Wine, or Crossover Office, running IE under Linux can be very possible. If Wine won't run the applications you need, or if you need the Windows operating system for other specific purposes, what about running Windows inside a virtual environment? Oddly enough, Windows support as a guest operating system is usually better 62 / NOVEMBER 2012 / WWW.LINUXJOURNAL.COM COLUMNS k THE OPEN-SOURCE CLASSROOM than Linux support, so even things like video acceleration might work. If you can do most of your work in Linux, but need that Windows environment every once in a while, a VM might be the perfect solution. And, of course, if nothing else, Linux will dual-boot quite nicely with Windows. Even if you're stuck using Windows for every aspect of your job, perhaps you can reboot into Linux when you want a familiar environment. (This is especially useful if you have a company laptop you are allowed to take home.) run the programs I talked about today, at least you can browse Linux Journal on your Windows machine. The Web version even works with Internet Explorer!* Shawn Powers is the Associate Editor for Linux Journal. He’s also the Gadget Guy for LinuxJournal.com, and he has an interesting collection of vintage Garfield coffee mugs. Don’t let his silly hairdo fool you. he’s a pretty ordinary guy and can be reached via e-mail at shawn@linuxjournal.com. Or. swing by the #linuxjournal IRC channel on Freenode.net. Resources Cygwin: http://www.cygwin.com Xming: http://www.straightrunning.com/XmingNotes Home Is So Much Sweeter For many readers, installing programs, partitioning hard drives and running virtual environments is just not allowed. All I can offer you is the comfort that at home, you can run whatever you like. If you're stuck running Windows and can't even Putty: http://www.chiark.greenend.org.uk/~sgtatham/putty VirtualBox: http://www.virtualbox.org Irssi: http://www.irssi.org BitIBee: http://www.bitlbee.org TweetDeck: http://www.tweetdeck.com WriteBox: http://is.gd/writebox Pixlr: http://www.pixlr.com LibreOffice: http://www.libreoffice.org Wine: http://www.winehq.org Crossover Office: http://www.codeweavers.com WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 63 NEW PRODUCTS r Cloud Veteran Linux provider SUSE announced the availability of SUSE Cloud, an automated cloud computing platform that enables the rapid deployment and easy ongoing management of an infrastructure-as-a-service (laaS) private cloud. SUSE describes SUSE Cloud as the first enterprise-supported private cloud solution powered by OpenStack, which includes contributions from 3,300+ developers at 180 companies and enjoys broad industry support and a vibrant community. The solution integrates seamlessly with SUSE Studio and SUSE Manager, allowing enterprises to rapidly deploy, adapt and manage applications and workloads across private and public clouds. Additional SUSE Cloud features include the ability to leverage existing infrastructure, optimize licensing costs, improve the speed and accuracy of delivering services to the line of business and take advantage of a high level of support, among others. http://www.suse.com/susecloud Projrct op«nttACk Images M • byacc > byacc analysis 2 > Call Graph Settings | Layout | [ Metrics j_ g Warnings ^ st sign in to save changes to the view. rnings: | active - I o + :: >free_parser: ■ : free_action_row: ■ parse_actions : add_reductions add_reduce ► make parser find final state remove_conflicts get shifts detreds : so | e _reduction total conf licts: i t unused_rules i Function: alio: Path: mair Edges: 31 ii llumher of Warnings: 0 Source line number: 209 MvG: 3 LB: 1 LMCC: 0 LCode: 13 LCodeOnly: 13 TL: 14 LComOnly: 0 VG: 3 LCom: 0 Motes [-] j. Add Note | See Also: source url WWW.LINUXJOURNAL.COM / NOVEMBER 2012 / 65 NEW PRODUCTS Brian Evans’ Practical 3D Printers: The Science and Art of 3D Printing (Apress) Announcing new books like Brian Evans' Practical 3D Printers: The Science and Art of 3D Printing is dangerous. Now we'll never get you out of the basement. Should you decide to sequester yourself in pursuit of 3-D-printing guru-dom, you will find yourself fully armed with everything you need to know. In case you are not yet aware, a 3-D printer is a device you can either buy or (oh so much more fun) build to make parts, toys, art and even 3-D images captured by a sensor or modeled in software. The book takes readers beyond building the printer to calibrating it, customizing it and creating amazing models with it, including 3-D printed text, a warship model, a robot body, windup toys and arcade-inspired alien invaders. Readers also will explore the different types of popular 3-D printer models like the MakerBot, the whiteAnt RepStrap and RepRap printers. Other topics range from finding and creating 3-D models, including using Google Sketchup, creating a 3-D model from a 2-D image, the printer toolchain, creating multipart models and meshes, and upgrading both the mechanical and electronic parts. http://www.apress.com liohuH iitft Risk aiul liinamlintt Ethics of Big Data Kord Davis and Doug Patterson’s Ethics of Big Data (O’Reilly) Kont Davis