Pages

Sunday, January 18, 2009

VirtualBox - No Network Adapter on Windows XP

I had an issue with a guest Windows XP in a virtual machine. It could not recognize neither of the network cards (NICs) offered by VirtualBox.
Here are the links for Intel, and AMD network card drivers. I used the driver (available here) and everything was alright after the driver installation.
Funny the driver was not available on the manufacturer's site for Windows 2008 Server.

Source for the links - here.

JkDefrag

JkDefrag is a great free defragmentation tool for Windows. I'm using it more and more instead of all the other defragmenters out there. It is fast and does a decent job defragmenting.
To complement the functionality of JkDefrag, it is recommended to use

  • Page Defrag (link), for page file defragmentation,
  • NTRegoPT (link), for registry defragmentation

JkDefrag GUI is a nice addition to JkDefrag that makes all the functionality of the underlying program through a graphical user interface. You can schedule defragmentation runs, select options, install screen saver defragmenter, etc.

I'm having issues running the above listed programs on Vista and Windows Server 2008. Not sure if they work correctly or at all.

VMWare Disks

To merge pieces of a virtual disk image that is split into multiple 2GB files back into one file, use

vmware-vdiskmanager.exe

There are some GUI tools that server as wrappers around it, should you need them. Some of these GUI tools are the following:

Disk Manager is also used to increase the size of a virtual hard drive image or perform other maintenance operations.

 

VMware Virtual Disk Manager - build 126130.
Usage: vmware-vdiskmanager.exe OPTIONS <disk-name> | <mount-point>
Offline disk manipulation utility
  Operations, only one may be specified at a time:
     -c                   : create disk.  Additional creation options must be specified.  Only local virtual disks can be created.
     -d                   : defragment the specified virtual disk. Only local virtual disks may be defragmented.
     -k                   : shrink the specified virtual disk. Only local virtual disks may be shrunk.
     -n <source-disk>     : rename the specified virtual disk; need to specify destination disk-name. Only local virtual disks may be renamed.
     -p                   : prepare the mounted virtual disk specified by the drive-letter for shrinking.
     -r <source-disk>     : convert the specified disk; need to specify destination disk-type.  For local destination disks the disk type must be specified.
     -x <new-capacity>    : expand the disk to the specified capacity. Only local virtual disks may be expanded.
     -R                   : check a sparse virtual disk for consistency and attempt to repair any errors.

  Other Options:
     -q                   : do not log messages

  Additional options for create and convert:
     -a <adapter>         : (for use with -c only) adapter type (ide, buslogic or lsilogic)
     -s <size>            : capacity of the virtual disk
     -t <disk-type>       : disk type id

  Options for remote disks:
     -h <hostname>        : hostname of remote server
     -u <username>        : username for remote server
     -f <filename>        : file containing password
     -P <port>            : optional TCP port number (default: 902)
     -S                   : specifies that the source disk is remote, by default  the remote options are assumed to refer to the destination.
  Disk types:
      0                   : single growable virtual disk
      1                   : growable virtual disk split in 2GB files
      2                   : preallocated virtual disk
      3                   : preallocated virtual disk split in 2GB files
      4                   : preallocated ESX-type virtual disk
      5                   : compressed disk optimized for streaming

     The capacity can be specified in sectors, KB, MB or GB.
     The acceptable ranges:
                           ide adapter : [1MB, 950.0GB]
                           scsi adapter: [1MB, 950.0GB]
        ex 1: vmware-vdiskmanager.exe -c -s 850MB -a ide -t 0 myIdeDisk.vmdk
        ex 2: vmware-vdiskmanager.exe -d myDisk.vmdk
        ex 3: vmware-vdiskmanager.exe -r sourceDisk.vmdk -t 0 destinationDisk.vmdk
        ex 4: vmware-vdiskmanager.exe -x 36GB myDisk.vmdk
        ex 5: vmware-vdiskmanager.exe -n sourceName.vmdk destinationName.vmdk
        ex 6: vmware-vdiskmanager.exe -r sourceDisk.vmdk -t 4 -h esx-name.mycomp
any.com \
              -u username -f passwordfile "[storage1]/path/to/targetDisk.vmdk"
        ex 7: vmware-vdiskmanager.exe -k myDisk.vmdk
        ex 8: vmware-vdiskmanager.exe -p <mount-point>
              (A virtual disk first needs to be mounted at <mount-point>)

VirtualBox

From what I read it appears that VirtualBox is a viable alternative to VMWare, not to mention VirtualPC. The installation size is about 30MB, the performance is better to VMWare, the interface is less annoying than VMWare Server, and it supports both .vhd and .vmdk virtual disks! This means you can use both your VirtualPC and VMWare disk images.
The link above contains instructions on how to migrate an existing Windows machine or a virtual machine to a new VirtualBox virtual machine.
 
From my experience, the performance is very good. Software feels very lightweight and is easy to use and configure on the fly.
 

Hard disks

VirtualBox has a concept of storing all the hard drives together. The simple reason for this approach, instead of storing a hard drive together with a virtual machine, is that is supports differencing hard drive images. Some complex setups are available as a result of this.
I hope to test a setup where there is one installation of an operating system (for example, XP) and then there are differencing hard disk images that branch into a development disk and a personal disk.
As I read beforehand, it does read both .vhd and .vmdk drive images. I successfully ran a new virtual machine from both .vhd and .vmdk existing virtual drives.
Immutable images is a feature where a VM starts off an existing hard drive image but all the changes are stored in a separate file - a differencing disk.
It also supports snapshots. While I have had some issues creating snapshots with .vmdk files, the help file states:
"Initial support for VMDK was added with VirtualBox 1.4; since version 2.1, VirtualBox supports VMDK fully, meaning that you can create snapshots and use all the other advanced features described above for VDI images with VMDK also."
 
To convert between different virtual disk image types, use Qemu's img tool. Qemu for Windows available here.
VBoxManage is the command-line tool that is a part of VirtualBox installation and is used for various actions. One of them is conversion of virtual hard drive images.

Seamless Windows

VirtualBox supports Seamless Windows (source). This is a neat feature and means that you can have application windows from the virtual machine being displayed on your real machine!

Shared Folders

Shared folders are supported between the host and the virtual machine.
 
Many advanced tools are available through command line. While that might be a hassle, at least the functionality is there - compact virtual disks, create an immutable disk, etc.
 
To migrate an existing Windows installation or a virtual machine, follow the instructions posted here.

Thursday, January 15, 2009

Optimizing Linq to SQL

Lazy Loading in LINQ: LoadWith And AssociateWith DataLoadOptions - Part 1 - LoadWith - Hilton Giesenow's Jumbled Mind

Above is the link to one of the articles I would recommend if you are using Linq to SQL and are now in a situation to tailor solutions to specific problems and/or optimize the performance of your code that is using Linq to SQL for data manipulation purposes.

One thing that we are going to use in the next project, that is different to default Linq setting, is usage of Stored Procedures with Linq entities.
We are creating partial classes for Linq entities and put the static functions in there that call the stored procedures to return sets of those entities. So, we are mapping result sets from stored procedures to entities. I want to try to get a single result from a call to an SP and map it to multiple entities and see how it works.
Next thing is that we are inheriting the original data context and add the function calls there to provide access to the SPs and return the desired data type instead of automatically generated one.

In the article above there are a few tips to have in mind when playing with Linq. DeferredLoadingEnabled and DataLoadOptions, that provides LoadWith and AssociateWith, are a few interesting points. In the given example, Linq is using a JOIN in SQL statement to fetch the resultset containing multiple entities. Sweet.

Tuesday, January 13, 2009

Mocking Around

While I have been playing with NMock library I found out that NUnit includes mock library as well. Never came across that before. I am ashamed, I admit.
Nonetheless, NMock has better syntax and seems more intuitive to use. The advantage of NUnit mock library is that it is a part of NUnit package. That advantage goes away, though, when unit testing libraries from .Net Framework are used for unit testing.

NUnit's mock library has a weird case when properties are accessed as the syntax for accessing a property has to include "get_" or "set_" before the property name. Also, the accessing of the property itself is a bit counterintuitive. NMock, on the contrary, offers ease of use as the mock object implements the interface and can be used in tests in a very simple and straightforward manner. It is a matter of preference. I prefer it simple.

Another thing I found out about mock objects is that they have to mock Interfaces, not Classes. While this may be obvious to some, I did not realize before what is the issue with creating all the interfaces. Now I have to look at that aspect of TDD. Some redefinition will occour on the way I develop software and hopefully the results will be even easier maintenance of the code I write as well as reusability.
One issue I ran into is that interfaces have to be public. While this may be obvious to some, it is new to me. And it is an issue to have in mind.

Now, having a mock framework in place, the stage is set for some real unit testing. The tests I did before were not pure unit tests because all the units (classes) had their dependencies on other units, or data, or some other external unit. Off to do some real TDD... :)

p.s.

Here is a sample unit test:


[TestMethod]
public void test_nmock()
{
Mockery mock = new Mockery();
ISetting s = null;

try
{
s = mock.NewMock();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}


Stub.On(s)
.GetProperty("SettingValue")
.Will(Return.Value("35"));

Assert.AreEqual("35", s.SettingValue);
}

Enable Windows Search on Windows Server 2008

Bilal Haidar [MVP, MCT] : Install Windows Search Service on Windows Server 2008

I need a desktop search program that will help me find the different texts I have all around my workstation. Windows Search is not automatically enabled on Windows Server 2008 and it is a bit different to enable it than on Vista.
As listed in the post linked at the top of this article, the steps to have the Search service up and running are the following:

  1. Go to the Server Manager
  2. Click on Add Roles
  3. Select File Services
  4. Select Window Search Service
  5. Select the drives you want WSS to have control on
The Search service can be fine-tuned after installation through a plug-in in Control Panel.

NMock

NMock: A Dynamic Mock Object Library for .NET

NMock is a framework for .Net that allows creation of mock and stub objects in Test Driven Development using Microsoft .Net Framework.
The documentation is enhanced and there is a tutorial explaining the concepts. This framework is a necessary addition to Unit Testing framework as it allows to isolate the unit being tested. The external dependencies are replaced with mock/stub objects and provide the expected output. That way, if your class retrieves data from a database and the test depends on it, stub object can be created that fits the expectations. The class will receive what it expects and it will get tested properly. The test will not fail for reasons outside the tested unit, for example - not running database server, or wrong data in the database, etc. This is, of course, unless you are doing some database tests. :)

Quickstart and Tutorial available on the site.

Monday, January 12, 2009

My.Resources in C# and Icons

To embed icons in a windows application, it is very easy to store them in the Resources. A new file can be added or a default one used. It can store strings, images, icons, etc. Once the icons are stored in the Resources.resx they can be accessed through

Properties.Resources

which is the equivalent of My.Resources from Visual Basic.net. The easiest way to use icons is to create and fill an ImageList, which is then utilized by TreeView or ListView.

            tvw.ImageList = new ImageList();
            tvw.ImageList.Images.Add("FolderIcon", Properties.Resources.FolderIcon);
            tvw.ImageList.Images.Add("ItemIcon", Properties.Resources.ItemIcon);


Sunday, January 11, 2009

Unit Test Patterns for .Net

Unit Test Patterns for .NET development - Part I

Here is a series of excellent articles on Unit Testing patterns.