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.

01 May 2011

Install Ubuntu 11.04 and migrate

This is my story of installing Ubuntu 11.04 Natty Narwall on an SSD partition and migrating my existing Ubuntu 10.10 to the new system for the applications I use.

I use my machine primarily for web development and the majority of my tools are web-based. With that in mind, my migration was easier than what it could have been.

Installation


There are many guides out there that will lead you step by step through the installation process. For the most part it's a breeze.

After the installation, I ran into one major hurdle - the UI wouldn't boot properly. After looking through the log files, I identified the problem to be related to the nvidia drivers. After some experimentation, I ended up uninstalling the driver that came with the distribution and installing the latest nvidia drivers from the repository and that fixed my problem.


sudo apt-get remove nvidia-173
sudo apt-get install nvidia-current
reboot


SSD Related tweaks


I made a few basic changes to optimize the performance of the file system for SSD drive.

First, change disk scheduling to noop.

sudo apt-get install sysfs

Then add

block/sda/queue/scheduler = noop

to /etc/sysfs.conf

Next, add the noatime option to the SSD drive in /etc/fstab
Finish off by mapping the /tmp folder into RAM. That way a lot of the random, not important writes will go directly to RAM instead of wearing the disk. This also has the side effect of clearing the /tmp folder on reboot. Add the following line to the /etc/fstab file.

tmpfs /tmp tmpfs nodev,nosuid,noexec,mode=1777 0 0


Migration


This is what it took to migrate my applications and settings I needed to resume work.

SSH keys


A lot of systems I login to authenticate me via my SSH key. To carry that forward, I copied the ~/.ssh folder over to the current home directory.

Sudo


I like sudo to do its thing without pestering me for a password all the time. This is how I do this:

Replace

%admin ALL=(ALL) ALL

with

%admin ALL=NOPASSWD: ALL


NFS


I have a few NFS shares mapped to my machine. I had to install NFS and copy the shares information from the old system.

sudo apt-get install nfs-client

copy remote systems from /etc/fstab

Compiz Settings


Unity is a compiz plugin, but even though the system runs compiz, the compiz manager was not installed by default.

sudo apt-get install compizconfig-settings-manager

Once I had that installed, I searched for compiz and found the config manager.
One of the features I was missing was the enhanced zoom shortcut. To enable that, in enhanced zoom, enable shortcuts for zoom in and zoom out.

Another point of interest in the compiz configuration is the Unity section, where you can adjust the Ubuntu Unity Plugin setting.

Applications


I am used to vim and have grown to expect it when I type the vi command. To fix this, just install vim.
sudo apt-get install vim

Browser


I use google chrome as my main browser. Download from google.com/chrome (get the 64-bit .deb package)
After it's installed, enable account sync. With google chrome and the account sync, all my bookmarks, saved passwords and other browser settings were migrated automatically.

FTP Client


My FTP client of choice is Filezilla for features, speed and supporting SFTP.

sudo apt-get install filezilla

copy ~/.filezilla folder over to preserve all saved FTP accounts

Code Editor / IDE



sudo apt-get install geany

I also install a few geany plugins (geany-plugin-* - codenav, prj, webhelper, treebrowser)

Source Control



sudo apt-get install git


Remote Desktop


tsclient is installed by default. To copy your Remote Desktop Server configurations,
copy ~/.tsclient to your home directory

Terminal


I find the drop down consoles quite handly, so I use guake (a quake like console)

sudo apt-get install guake

It is advised to setup guake to start on login. In unity, search for "Startup Applications" and select "Guake Terminal".

29 January 2009

System Administration with Webmin

Whether you have a linux server somewhere you would like to keep an eye on or you are doing shared hosting for other people, webmin is a great tool to let you do a lot of the common administration tasks via an easy to use web application.

To install, get the latest version from here http://www.webmin.com/download.html (or install from your distro's repository, but it might not be up to date)

Then, depending on the OS you have, you may have to run '/usr/libexec/webmin/setup.sh'

After that, you are ready to go. To access webmin, headoff to http://yourserver:10000/ (default configuration)

You can install a number of additional modules available here: http://www.webmin.com/third.html
Some that I found useful:
System Information - http://swelltech.com/projects/webmin/modules/sysinfo-1.170.wbm
Custom Link - http://www.webmin.com/download/modules/link.wbm
Stats - http://downloads.sourceforge.net/webminstats/sysstats-1.2.tgz

One nice feature is that webmin allows you to install module directly from the net, just by pasting the link above.

If you are running a shared hosting environment, the usermin module is a must. You might also want to rely on virtualmin for setting up all the virtual domains, but if you go that route, you have to set it up and use it from the start.

28 January 2009

Monitoring (and Conky)

First, you should install lm-sensors

Then, to make it detect what it can, run the following command 'sensors-detect', which will determine what kernel modules should be loaded for lm-sensors to do its job.

Then you install conky

here is a sample config file to get you started:

# fork to background ? ################################################
background no

# font settings #######################################################
use_xft yes
#font monospace-8
xftfont Acknowledge TT BRK:size=8
#xftfont Edit Undo BRK:size=8
#xftfont Visitor TT2 BRK:size=10
uppercase no
override_utf8_local yes

# update every 1 secs #################################################
update_interval 5
# stay running forever ################################################
total_run_times 0

# draw to root window #################################################
own_window no

# avoid flickering ####################################################
double_buffer yes

# size ################################################################
minimum_size 1280 800
maximum_width 1280

# position ############################################################
alignment top_left
#alignment top_middle
#alignment top_right
#alignment bottom_left
#alignment bottom_middle
#alignment bottom_right
#alignment middle_left
#alignment middle_right
gap_x 0
gap_y 0

# colors ##############################################################
default_color white
default_shade_color white
default_outline_color black
# custom colors #######################################################
color0 FFFFFF
color1 F5F5F5
color2 A2AEC6
color3 696969
color4 D3D3D3
color5 6495ED
color6 87CEFA
color7 5F9EA0
color8 BBBBBB
color9 262729


# borders #############################################################
draw_borders no
draw_graph_borders yes
stippled_borders 8
border_margin 4
border_width 1

# shades ##############################################################
draw_shades no

# outline #############################################################
draw_outline no

# spacer ##############################################################
use_spacer no

# buffers (Substract (file system) buffers from used memory?)##########
no_buffers yes
# sampling ############################################################
cpu_avg_samples 2
net_avg_samples 2

# configuration #######################################################
TEXT
${voffset 20}${offset 10}$color8 /$color3 System$color8 /
${offset 10}$color2 $sysname$color9 -$color2 $machine
${offset 10}$color2 $kernel $color9 on$color2 $nodename
${offset 10}$color6 ${time %A %d/%m/%y %kh%M}
${offset 10}$color2 Uptime :$color6 $uptime

${offset 10}$color8 /$color3 Mails$color8 /
${offset 15}$color2 mail : $color4${imap_unseen imap.server.com "user" ""}

${voffset -100}${offset 980}$color8 /$color3 Ubuntu Planet RSS$color8 /
${offset 985}$color2 >$color4${rss http://planet.ubuntu-fr.org/feed/tag/Accueil/rss2 60 item_title 0}

${voffset 50}${offset 10}$color8 /$color3 CPU #01$color8 /
${offset 10}$color2 Temperature :$color4 ${execi 10 sensors | grep -A 2 '^coretemp-isa-0000' | cut -c15-18 | grep '°'}C - $color2 Frequency : $color4${freq_dyn cpu1}$color2 MHz
${offset 10}$color2 Load: $color9 ${cpubar cpu1 6,220} $color4${cpu cpu1}$color2 %
${offset 10}$color4 ${cpugraph cpu1 15,255 262729 87CEFA}
${offset 10}$color8 /$color3 CPU #02$color8 /
${offset 10}$color2 Temperature :$color4 ${execi 10 sensors | grep -A 2 '^coretemp-isa-0001' | cut -c15-18 | grep '°'}C - $color2 Frequency : $color4${freq_dyn cpu2}$color2 MHz
${offset 10}$color2 Load: $color9 ${cpubar cpu2 6,220} $color4${cpu cpu2}$color2 %
${offset 10}$color4 ${cpugraph cpu2 15,255 262729 87CEFA}

${offset 10}$color8 /$color3 Entropy $color8 /
${offset 10}$color4 ${entropy_bar 15,255 262729 87CEFA}

${voffset 10}${offset 20}$color8 /$color3 Lan (eth0)$color8 /
${offset 35}$color2 IPv4 : $color4${addr eth0}$color2
${offset 30}$color2 Down : $color4 ${downspeed eth0}$color2 kb/s
${offset 30}$color4 ${downspeedgraph eth0 15,250 262729 87CEFA}
${offset 30}$color2 Up : ${offset 6} $color4 ${upspeed eth0}$color2 kb/s
${offset 30}$color4 ${upspeedgraph eth0 15,250 262729 87CEFA}

${voffset 10}${offset 20}$color8 /$color3 Wireless (wlan0)$color8 /
${offset 35}$color2 essid : $color4${wireless_essid wlan0}$color2 - MAC : $color4${wireless_ap wlan0}
${offset 35}$color2 Wifi Level : $color4${wireless_link_qual wlan0}$color2 /$color4 ${wireless_link_qual_max wlan0}$color2 - Quality: $color4 ${wireless_link_qual_perc wlan0}
${offset 35}$color2 IPv4 : $color4${addr wlan0}$color2
${offset 30}$color2 Down : $color4 ${downspeed wlan0}$color2 kb/s
${offset 30}$color4 ${downspeedgraph wlan0 15,250 262729 87CEFA}
${offset 30}$color2 Up : ${offset 6} $color4 ${upspeed wlan0}$color2 kb/s
${offset 30}$color4 ${upspeedgraph wlan0 15,250 262729 87CEFA}

${voffset -45}${offset 1060}$color8 /$color3 Power$color8 /
${offset 1055}$color2 $acpiacadapter$color4 $battery
${offset 1050}$color2 Time Left :$color4 $battery_time
${offset 1045}$color9 ${battery_bar 6,160}

${voffset 10}${offset 20}$color8 /$color3 Volumes$color8 /
${offset 35}$color2 / : $color4 ${fs_used_perc /}% (${fs_free /} free)
${offset 35}$color9 ${fs_bar 6,320 /}
${offset 50}$color2 /home : $color4 ${fs_used_perc /home}% (${fs_used /home} free)
${offset 50}$color9 ${fs_bar 6,320 /home}

${voffset -90}${offset 600}$color8 /$color3 Memory$color8 /
${offset 605}$color2 RAM: ${offset 0} $color9 ${membar 6,200}$color4 $mem ($memperc%)
${offset 610}$color2 Swap: $color9 ${swapbar 6,200}$color4 $swap ($swapperc%)

${voffset -500}${offset 1110}$color8 /$color3 Activity$color8 /
${offset 1105}$color2 Hdd Temp:$color4 ${hddtemp /dev/sda}
${offset 1100}$color2 Read/Write: $color4 $diskio
${offset 1090}$color2 Process :$color4 $running_processes$color2 /$color4 $processes
${offset 1090}$color6 ${top name 1}$alignr$color4 ${top cpu 1}%
${offset 1090}$color5 ${top name 2}$alignr$color4 ${top cpu 2}%
${offset 1090}$color5 ${top name 3}$alignr$color4 ${top cpu 3}%
${offset 1090}$color5 ${top name 4}$alignr$color4 ${top cpu 4}%
${offset 1090}$color5 ${top name 5}$alignr$color4 ${top cpu 5}%
${offset 1090}$color2 Memory :
${offset 1090}$color6 ${top_mem name 1}$alignr$color4 ${top_mem mem 1}%
${offset 1090}$color5 ${top_mem name 2}$alignr$color4 ${top_mem mem 2}%
${offset 1090}$color5 ${top_mem name 3}$alignr$color4 ${top_mem mem 3}%
${offset 1090}$color5 ${top_mem name 4}$alignr$color4 ${top_mem mem 4}%
${offset 1090}$color5 ${top_mem name 5}$alignr$color4 ${top_mem mem 5}%

${offset 1090} ${diskiograph 15,180 262729 87CEFA}



save this as ~/.conkyrc

This setup is good for black background (plain or with an element in the middle), wide screen at least 1280 pixels wide.

Documentation on conky variables can be found here: http://conky.sourceforge.net/variables.html