Scripting in Google ChromeOS

I recently got my hands on one of those Google ChromeBooks (Cr-48). Interesting to have an operating system that is just a web browser. But, as anyone likely reading this article already knows, the graphical interface is the web browser and the operating system is still Linux. But what version? Well, let’s go on a journey together. First, you need ChromeOS. If you’ve got a ChromeBook this is a pretty easy thing to get. If not, check for a USB or optical download that can be run live (or even in a virtual machine). Or, if you know that you’re going to be using a virtual machine, consider a pre-built system from hexxeh at I have found the VMware builds to be a bit persnickety about the wireless on a Mac, whereas the VirtualBox builds ran perfectly. I split my time between the two anyway, so I’ve just (for now) been rocking VirtualBox for ChromeOS. When you load it for the first time it asks for a Google account. Provide that, select your network adapter, choose from one of the semi-lame account images ( for the record, I like the mad scientist one) and you’re off to the races. Next, we need a shell. When you first log in, you see a web page that shows you all of the Chromium apps you have installed. By default, you’ll see File manager and Web Store. If you’ve used the OS X App Store then the Chrome Web Store is going to look pretty darn familiar. My favorite for now is Chrome Sniffer. But all of these kinda’ get away from where we’re trying to go: get a scripting environment for Chrome OS. Chrome comes with 2 types of shell environments. The first is crosh. To bring up a crosh environment, use Control-Alt-t. This keystroke invokes the crosh shell. Here, type help to see a list of the commands available. Notice that cd, chmod, etc don’t work. Instead, there are a bunch of commands that a basic user environment might need for troubleshooting primarily network connections. “But this is Linux” you ask? Yup. At the help output you’ll notice shell. Type shell and then hit enter. The prompt will change from crosh> to chronos@localhost. Now you can cd and perform other basic commands to your hearts delight. But you’re probably going to need to elevate privileges for the remainder of this exersize. So let’s type sudo bash and just get there for now. If you’re using a ChromeBook, the root password might be root, or if you’re using a downloaded vm from hexxeh then it might be facepunch (great password, btw). Provided the password worked, the prompt should turn red. Now, if you’re using a hexxeh build then the file system is going to be read-only. You won’t be able to change the root password nor build scripts. But otherwise, you should be able to use passwd to change the password: passwd chronos Once you’ve got slightly more secure shell environment (by virtue of not using the default root password), it is time to do a little exploring. Notice that in /bin, you see sh, bash, rbash and the standard fare of Linux commands (chmod, chown, cp, attr, etc. Notice that you don’t see tcsh, csh or ksh. So bash commands from other platforms can come in, but YMMV with tcsh, etc. Running ps will give you some idea of what’s going on process-wise under the hood: ps aux From encrypts to crypto to the wpa supplicant, there’s plenty to get lost in exploring here, but as the title of the article suggests, we’re here to write a script. And where better to start than hello world. So let’s mkdir a /scripts directory: mkdir /scripts Then let’s touch a script in there called touch /scripts/ Then let’s give it the classic echo by opening it in a text editor (use vi as nano and pico aren’t there) and typing: echo "Hello Cruel World" Now close, save and then run it: /scripts/ And you’ve done it. Use the exit command twice to get back to crosh and another time to close the command line screen. You now have a script running on ChromeOS. Next up, it’s time to start looking at deployment. This starts with knowing what you’re looking at. To see the kernel version: uname -r Or better: cat /proc/version Google has been kind enough to build in similar sandboxing to that in Mac OS X, but the concept that you can’t run local applications is a bit mistaken. Sure, the user interface is a web browser, but under the hood you can still do much of what most deployment engineers will need to do. If these devices are to be deployed en masse at companies and schools, scripts that setup users, bind to LDAP (GCC isn’t built-in, so it might be a bit of a pain to get there), join networks and the such will need to be forthcoming. These don’t often come from the vendor of an operating system, but from the community that ends up supporting and owning the support. While the LDAP functionality could come from Google Apps accounts that are integrated with LDAP, the ability to have a “One touch deploy” is a necessity for any OS at scale, and until I start digging around for a few specific commands/frameworks and doing some deployment scripts to use them, right now I’m at about a 6 touch deploy… But all in good time!

The Basics of pico (or nano)

At your Unix shell prompt, type: pico filename Replace filename with the name of the file you want to create or edit. For example, to create a file and name it indiana.txt, type: pico charles.txt If the file already exits, Pico opens it for you to edit. If it doesn’t exist yet, Pico creates it and places you in an editing buffer. Pico displays a menu bar of commonly-used commands at the bottom of the screen. Pico accepts commands from your keyboard but not from your mouse. To insert text into your Pico editing screen at the cursor, just begin typing. Pico inserts the text to the left of the cursor, moving any existing text along to the right. Each time the cursor reaches the end of a line, Pico’s word wrap feature automatically moves it to the beginning of the next line. (Also see “Justify.”) To move the cursor, use the arrow keys or use CTRL/f (forward), CTRL/b (back), CTRL/n (next line), CTRL/p (previous line). See the “Command overview” for more cursor movement commands. To delete the character to the left of the cursor, press BACKSPACE, DELETE, or CTRL/h. To delete the character highlighted by the cursor, press CTRL/d. To delete the current line, press CTRL/k. To save your edited file to disk, press CTRL/o. Pico displays the current filename. (To save the file under a different name, delete the filename that Pico displays and type a new one.) Press RETURN. To exit Pico, press CTRL/x. If you have made any changes since the last save, Pico asks whether to save them. Type y (yes) or n (no). If you type y, Pico displays the filename. (To save the edited file under a different name, delete the filename and type a new one.) Press ENTER. Pico lets you search forward from the current cursor position for any text string you specify. Press CTRL/w (for whereis) to invoke the search. Pico prompts you for a search term. To begin searching, type the text you’re looking for and press RETURN. Pico moves the cursor to the first instance of the text string you entered. You can find additional occurrences by pressing CTRL/w again. As you type, Pico’s word wrap automatically begins a new line when needed. However, when you edit existing text, you may create text lines that are either too short or too long. To re-wrap or “justify” a paragraph, move the cursor to that paragraph and press CTRL/j. To undo this action and restore the paragraph to its original condition, press CTRL/u. You can cut and paste text lines with Pico. Place the cursor on the first text line you wish to cut and press CTRL/k to remove it. (To cut and paste two or more consecutive text lines, press CTRL/k until all the text lines are removed.) You can now move the cursor to the location where you want to paste the text. Press CTRL/u. Pico pastes the text back into the file at the new cursor position. You can also cut and paste text blocks:    1. Move the cursor over the first character of the text you want to remove.    2. Press CTRL/^ to “set the mark.”    3. Use the arrow keys to highlight the text you wish to cut.    4. Press CTRL/k to cut the text. (Be sure you got all of the text you wanted, including the last character.)    5. Move the cursor to the place where you want to insert the text.    6. Press CTRL/u to paste the text into the new position. To insert the contents of an existing file at the cursor location, press Ctrl/r. Pico prompts you for a filename. You can either type the filename and press Return, or press Ctrl/t to select from a list of available files. Pico enters the File Browser, which displays a list of the files in your current working directory. Use the arrow keys to highlight the file you wish to insert and press Return. Select the parent directory (..) to move “up” the directory tree. Select a subdirectory to move “down” the directory tree. Besides inserting text, you can use the File Browser to rename, delete, or copy any file, even a file in another directory. To exit the File Browser, press e. To use the spell checker, press CTRL/t. When Pico discovers a word it does not recognize, it highlights the word and prompts you to enter a replacement. You can either type a replacement or press RETURN to keep the original word. Pico then continues to the next misspelled word. When Pico has checked your entire document, it returns the cursor to its original position. If your Pico session crashes, Pico attempts to save a copy of the file you were working on. Look in your working directory for a filename with the extension .save.   Some commands inside pico include the following keystrokes:     * CTRL/a Move to the beginning of the current line.     * CTRL/e Move to the end of the current line.     * CTRL/v Move forward one page.     * CTRL/y Move backward one page.     * CTRL/w Search for text (whereis).     * CTRL/L Redraw a garbled screen.     * CTRL/d Delete the current character.     * CTRL/^ Begin selecting text.     * CTRL/k Remove (cut) current line or selected text.     * CTRL/u Paste (uncut) last cut text at the cursor position.     * CTRL/j Format (justify) the current paragraph.     * CTRL/t Spell check the text.     * CTRL/r Insert (read in) a file into this file.     * CTRL/o Save (output) the file.     * CTRL/g View Pico’s online help.     * CTRL/x Exit Pico, saving the file.