Wednesday, September 16, 2009

Adding custom console modes to VirtualBox Linux Guest

OK, reading manuals sometime is very tedious. But after some great experiences i have to say that it always a practice that worth. Following the little guide about setting up enriched graphics with GRUB 2, the right compendium is a little refresh about setting console modes on Linux.

To discover which console modes are supported by our Linux Box all we have to do is to issue the command hwinfo --framebuffer, this will present us a list of supported modes, in hexadecimal values, that we can further pass to the kernel via the parameter vga=0x317 (HEX) directly, or the correspondent decimal value , in this case vga=791. I remember that if you are using GRUB 2 bootloader this method has been deprecated (follow the previous post).

When using VirtualBox otherwise, we need non-standard console modes to fit our monitor, for example i suggest a resolution of 1280x800 with a monitor of 1680x1050, cause some 4:3 resolutions may be uncomfortable. To let the resolution of 1280x800 appear on your virtualized Linux all we have to do, is to create a batch file or directly execute a specific action trough the MS-DOS command prompt via the VBoxManage.exe (on Windows) or the same on other platforms (i think is VBoxManage.sh for Linux, correct me). Be sure VBoxManage is in tour path or change to the directory containing it and write down: VBoxManage setextradata "Debian" "CustomVideoMode1" "1280x800x16", as you note Debian is my beloved Virtual Machine, you MUST replace with the correct Virtual Machine name, for example "Ubuntu 9.04 custom x64", everything not just Ubuntu or x64 or custom, it happened.

It's done, there's also a trick to have the same resolution also for the GDM welcome screen (no more dancing resizing). When in GNOME choose Display settings and set manually the resolution to 1280x800, by resizing the guest window, when the applet will tell you i you want to keep the resolution given, just keep, at the next restart you will be into the 1280x800 VirtualBox realm.


Monday, September 14, 2009

GRUB 2, graphical boot tips to set the desired VGA console mode.

Yes, everyone wants graphics, or better, the ability to customize things to suit different situations. I have always suggested programmers regarding interfaces and programs in genre to let the user be comfortable to set and change almost every aspect of program GUI, at least icons, fonts, backgrounds and often ii found some programs being too customizable, on the contrary.

Let's take a look at GRUB 2, also called grub-pc by updates that the Debian distro suggests. GRUB 2 is the successor of the GRUB bootloader, commonly shipped within the majority Linux distributions. Basically i will cover only some graphical aspects that GRUB 2 seems to be strong with: customization of fonts, background, console modes directly set by GRUB 2 itself, no more limitation to the resolution of 640x480 pixels at startup, can be used bigger background pictures also at the resolution of 1650x1050 pixels.

Our favorite kernel parameter vga=791 for example, is now deprecated (if using GRUB 2), and it's a little tricky to set correctly the console mode. The configuration file for GRUB 2 is now called grub.cfg in place of the old menu.lst whose we were addicted, but resides in the same directory /boot/grub. Things are very distribution specific i think, but remain the fact that configuration changes rely on the grub.cfg file that we have to alter in some way. Debian is putting the defaults into /etc/default/grub where you can find and set common used parameter easy to understand.

"For example i use Debian within VirtualBox at a resolution of 1280x800 for the console and also for the Xorg server. If you want you to start with this resolution also for GRUB 2 (remember in the past was very limited this feature, and if you are using VirtualBox or other virtualization programs the annoying result was in resizing the window and than enlarging upon the system goes into graphical mode, for example, with the splash screen: usplash, splashy and others), very nice, no more discrepancies between modes: the line to change into the /etc/default/grub file is GRUB_GFXMODE=1280x800 and then issuing update-grub. The resulting action is to write the line set gfxmode=1280x800 into the grub.cfg file."

We are half a way to our objective, to change also the resolution of our console. We need two conditions, the terminal must be set to insmod vbe line MUST be present in your configuration file (this ensure correct console mode settings, if you note this instruction is almost identical to load a kernel module) and the option gfxpayload=keep MUST be set. This will keep our 1280x800 resolution also for the console, YEAH! no more resizing on VirtualBox! and this is not the end. Someone reported that adding gfxpayload=my_resolution will be able to set a different resolution for the console instead of having the same of the GRUB 2 menu, sincerely i have not tested it. Why i have not tested it? The strong point is that to change grub.cfg requires a little more effort than editing it with a text editor, because all your changes will be discarded when you run the command update-grub.

/boot/grub/grub.cfg excerpt :

set gfxmode=1280x800
set gfxpayload=keep
insmod gfxterm
insmod vbe

update-grub reads the configuration stored in the /etc/default/grub file an the scripts that resides into the /etc/grub.d directory at least on Debian (i suppose is the same for other distributions). To set the option gfxpayload=keep i had to modify one of this file cause i discovered that this line MUST be the next after the gfxmode one, like you can note above.

You have to find the file or script responsible to change the content of your grub.cfg configuration file and append the appropriate line, after the gfxmode sentence, gfxpayload=keep to keep the same resolution of the GRUB 2 menu for the console (wider resolutions result in smaller console fonts). In my case i had to edit the script 00_header stored into the /etc/grub.d directory.

There are other scripts into this directory, responsible to set other parameters, like theme (background picture an font color), distribution and custom OS. To change the background image of your GRUB 2 menu is almost the same process, find the script or configuration file that change the attribute of the background_image instance of the grub.cfg, and change the line pointing to the image (usually a .png or .tga file) with the one you like, i have tested it with a resolution of 1650x1050 pixels modifying the script 05_debian_theme, in my case, stored into /etc/grub.d directory as well.

GNU GRUB
http://www.gnu.org/software/grub/index.html

ADDENDUM
Due to request, i will add the correct edit made to the 00_header script in order to maintain settings, just a clarification:

1. We are on Debian and we are talking about GRUB 2 (NOT GRUB, GRUB 2), on other Linux distributions may vary.

2. Your grub resolution must be set through the variable GRUB_GFXMODE=my_resolution into /etc/default/grub file, to avoid the loss of our settings on every update-grub command. And for the GRUB 2 menu its done. Let proceed for the console VGA console modes resolution, cause someone may be more comfortable with different settings despite the GRUB 2 menu ones, just to understand: if the GRUB 2 menu is set to 640x480 and the console mode is 1024x768 could lead into an aesthetic issue, is not a mistake at all.

3. VGA console modes: At the moment of writing, for the console, we can only keep the resolution we set for the GRUB 2 menu. This is due by the instruction set gfxpayload=keep, that, in order to be inserted into our /boot/grub/grub.cfg configuration file we have to hardcode to the 00_header script, in this way:

/etc/grub.d/00_header excerpt :

set gfxmode=${GRUB_GFXMODE} <-- FIND THIS LINE
(as you note GRUB_GFXMODE is the variable we set before through /etc/default/grub)
set gfxpayload=keep <-- THIS IS FOR THE VGA CONSOLE!
(as you note the statement keep, obviously, keeps, what?, the resolution we set before through GRUB_GFXMODE variable set into /etc/default/grub)
insmod gfxterm
insmod ${GRUB_VIDEO_BACKEND}

4. What I tested and what I not: set gfxpayload=keep MUST be UNDER the set gfxmode=${GRUB_GFXMODE} statement, i tried to put it at the end of the grub.cfg file through other scripts that Debian explicitly define custom, like 40_custom (always in our GRUB 2 scripts dir /etc/grub.d), but nothing worked, so like any well made worker i found my way.

So, I hope to have been more "exhaustive" this time and i excuse me if have missed something, that's it. I salute remembering you that Linux is "you won't find nothing to work if you don't made it to work" (like other few things).

Thanks to the Arch Linux community for the gfxpayload=keep input that has permitted this post. Someone reported that gfxpayload=my_resolution also worked on some configurations. I remember that the vga=console_mode_(HEX_or_DEC) kernel parameter is deprecated (don't know until when), when using GRUB 2.