12 September 2012

Virtual Box Console Commands

These are some notes about commands I had to dig out to operate my VirtualBox image through the command line (since the GUI for it segfaults on start).
For the purpose of this article, I am using [machine_name] as the name you have given during the creation of the virtual machine. Also, this article does not describe the actual creation of the virtualbox machine and image for attached storage, as those can vary quite a bit, depending on your needs and situation. This just gives some tips about how to operate your machines when you need to work with them.

Before we start, all the virtualbox modules should be loaded in. The following command makes sure that is done:
for m in vbox{drv,netadp,netflt}; do modprobe $m; done

Create Virtual Machine

VBoxManage createvm -name "[machine_name]" -register --ostype Windows7 --basefolder /mnt/disks

Adjust settings

VBoxManage modifyvm [machine_name] --memory 2048

Set network in bridged mode

VBoxManage modifyvm [machine_name] --nic1 bridged --bridgeadapter1 eth0
or for running OS:
VBoxManage controlvm [machine_name] nic1 bridged eth0

Attach Storage controller

VBoxManage storagectl [machine_name] --name "SATA Controller" --add sata --controller IntelAHCI --bootable on

Create and attach disk

VBoxManage createhd --filename /mnt/disks/[machine_name]/root.vdi --format VDI --size 20480
VBoxManage storageattach [machine_name] --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium /mnt/disks/[machine_name]/root.vdi



Attach usb devices

VBoxManage list usbhost
VBoxManage controlvm [machine_name] usbattach [UUID]



Attach usb devices permanently

VBoxManage list usbfilters
VBoxManage usbfilter add 1 --target [machine_name] --name Mouse --vendorid [linux vendor id] --productid [linux product id]
VBoxManage controlvm [machine_name] usbattach [UUID]




To start the virtual machine (in headless mode), you can use this command:

VBoxManage startvm [machine_name] --type headless

If you want to access a GUI during the booting process, you can enable the buildin RDP with:
VBoxManage controlvm [machine_name] vrde on

And then connect with any Remote Desktop application to it on the server (at default port 3389).

When installing/modifying OS, it is often handy to change the booting device.

This will let you set the DVD to an ISO image of your choice (the name of my storagectl is "SATA Controller", but that is something you could have set to anything):
VBoxManage storageattach [machine_name] --storagectl "SATA Controller" --port 1 --device 0 --type dvddrive --medium /mnt/video/disks/VBoxGuestAdditions_4.1.20.iso

To mount the system's CD-ROM:
VBoxManage storageattach [machine_name] --storagectl "SATA Controller" --port 1 --device 0 --type dvddrive --medium Host:/dev/cdrom

To "eject" the ISO:
VBoxManage storageattach [machine_name] --storagectl "SATA Controller" --port 1 --device 0 --type dvddrive --medium emptydrive

This commands help set the DVD as a booting drive:
VBoxManage modifyvm [machine_name] --boot1 dvd

And change it back to HDD:
VBoxManage modifyvm [machine_name] --boot1 disk


modifyvm is also the command to change a lot of the virtual machine's settings, like how much RAM is allocated, what OS is going to be installed on it and other similar hardware and other settings. The virtual machine has to be power off for most of those changes to be made.


To shutdown the machine, best option would be to emulate the pressing of the power button, which should trigger the OS shutdown procedures:
VBoxManage controlvm [machine_name] acpipowerbutton

If that doesn't do the job, you can do a hard plug with:
VBoxManage controlvm [machine_name] poweroff
To see the settings of a machine:
VBoxManage showvminfo [machine_name]

Show running virtual machines
VBoxManage list runningvms

To monitor the operation of a virtual machine, you can enable metrics:
VBoxManage metrics setup --period [refresh seconds] --samples [samples to keep] [machine_name]
as
VBoxManage metrics setup --period 1 --samples 1 [machine_name]

Show all statistics:
VBoxManage metrics query [machine_name]
Look CPU stats:
VBoxManage metrics query [machine_name] CPU/Load/User
Check RAM stats:
VBoxManage metrics query [machine_name] RAM/Usage/Used


This is all I've needed so far to operate virtual machines through the command line for all intents and purposes.

11 September 2012

Handling disconnected NFS mounts

If you use NFS, when the network/server hosting the NFS gets disconnected, it leaves the mount unresponsive and impossible to unmount. Some programs (including KDE/Gnome) sometimes freeze, trying to access the drive.

To fix it, mount the nfs with the "soft" option (as in -o soft parameter for mount or in fstab)

enable url rewriting on default linux setup

A few distributions don't have URL rewriting in apache enabled by default. Here is how to turn it on in Ubuntu:

make a symbolic link for the module

cd /etc/apache2/mods-enabled
ln -s ../mods-available/rewrite.load


Enable .htaccess to overwrite server set behavior - edit /etc/apache2/sites-enabled/000-default and change

...
AllowOverride None


to


<Directory /var/www>
...
AllowOverride All


or instead of all, you can allow only whatever you need.

Restart Apache

/etc/init.d/apache2 restart


edit your .htaccess in the folder you are serving you want to enable url rewriting:

RewriteCond on

RewriteCond %{REQUEST_FILENAME} ^.*\.(gif|jpg|png|css|js|ico)$
RewriteRule ^(.*)$ $1 [L]

RewriteRule ^.*$ index.php [L]

This setup forwards any request that is not a resource to a central index.php page.

That's it folks.

09 September 2012

Converting archives

How To Batch Convert Files From ACE Compression to ZIP

So recently I realized I had a bunch of ACE compressed files that I couldn't open in a lot of other computers without having to install additional software. That over time was becoming less and less ideal so in the end I gave up and decided to recompress all these files to something a little more generic, like .zip

As background history, I must say the reason I had so many .ace files was because a few years ago (after some intensive testing) I determined that the ACE format was giving me the best compression results. Props go to WinACE for consistently beating all other windows based compression algorithms I could get my hands on at the time. I don't know if that is still true as the 7zip and a few others have progressed by leaps and bounds since then, but it seems like winace is still a decent choice as far as file size is concerned.
Unfortunately, it seems like the format never got a huge public support and didn't spread to become ubiquitous enough to be easy to use and share files with. Thus, my decision to recompress files into something more accessible.

After I started converting a few files, I decided it was taking too long to do them all one by one, so I wrote the following script to get a directory full with *.ace files and recompress them all one by one to their .zip equivalents.


#!/bin/bash
mkdir tmp
for i in *.ace
do
        echo "Recompressing $i"
        mv "$i" "tmp/$i"
        cd tmp
        unace x "$i"
        rm "$i"
        zip -mr "$i.zip" *
        mv "$i.zip" ../
        cd ..
        echo "Done $i"
done
rm -r tmp



Rehash of what it is doing.

  1. Create a temporary directory
  2. For each file with .ace extension, do the following
  3. Move the .ace file to the temporary directory
  4. Extract the files
  5. Delete the .ace file
  6. Compress the files to .zip (and delete them)
  7. Move the ready .zip file to the original directory
  8. Continue with the next .ace file
  9. When done, delete the temporary directory.
Please note that with a small change to this script, this can be modified to batch change files from any supported format to any other supported format (change the unace and zip lines with their equivalents).

Also note, I'm using the unace program, which typically does not come pre-installed with distributions, but most distros would have it in their repositories.