Tuesday, July 5, 2011

Accessing your Physical Hard Drives/Disks from a Virtual Machine (VirtualBox)

I recently found that my computer running Ubuntu 64-bit had problems with the nForce motherboard SATA drivers and I kept getting errors. However, having used Linux long enough, I realized that I had a good alternative, which is to use Windows with a Virtual Machine running Ubuntu Server, easy enough to do.

But there was something that I forgot, which was that my hard drives were in ext4 format, but instead of taking the easy way (moving around data in a Live USB and reformatting the drives), I decided to try to access the data from the Ubuntu virtual machine, because I knew it was possible (without any proof).

Accessing the physical drives from VirtualBox allows for full support of the filesystems supported in the guest and (I'm pretty sure) at full speed. Combined with shared folders, this is very useful.

Any way, here is the end result done on VirtualBox (because it's the best and free):

First, you need to create a VMDK file describing the physical drive so that you can access it through VirtualBox. To do this you will first need to know the location of the drive, which looks like "\\.\PhysicalDriveX" where "X" is a number for Windows users, and /dev/sda (\\.\PhysicalDrive0), /dev/sdb (\\.\PhysicalDrive1), and so on for Linux users.

On Linux you can easily list the drives with the command (as root):
# fdisk -l
On Windows, you can find the drive number in many ways.
The easiest way is to open the Disk Manager by right clicking on "My Computer" or "Computer", clicking on "Manage" to open "Computer Management", and then clicking on "Disk Management". The bottom pane lists the disks with the correct disk numbers.

Sunday, May 8, 2011

C++0x and Variadic Templates

I recently looked into C++0x's variadic templates and thought that I should put up a couple of tips so that someone can get started with them.

The first thing is that with variadic templates, the only way to use them is in a recursive structure.
Now for an example:

void output() {
  std::cout << "\n";
}

template<class T, class... Args>
void output(T value, Args... args) {
  std::cout << value << "; ";
  output(args...);
}


There are a few things about this example you should notice. The declaration of the class template has the period of ellipses before the name of the parameter pack type, but the use of parameter pack as an argument always has the periods after the pack name (args...).

Then there is what I like to call the "Falling Tower Structure," which describes the functions which you have to define. You should define the maximum number of parameters you would need for the function with the variadic template, then you need to define the functions incrementally starting from the base case up to that function with an increasing number of parameters. I recommend using this structure when the number of parameters you need can vary because while there is probably more code, it is much cleaner than any other method I came up with.
Example time:

void test() { /*...*/ }

template<class T, class U>
void test(T t, U u) { /*...*/ }

template<class T, class U, class V>
void test(T t, U u, V v) { /*...*/ }

// Takes care of test with 3 or more parameters (args... can be empty)
template<class T, class U, class V, class... Args>
void test(T t, U u, V v, Args... args) { /*...*/ }

Here's another thing you might want to know if you want to use variadic templates as well. Say you have a situation where you are parsing through a list of arguments and if you get an indicator, you want to interpret an argument as a specific type. Here's an example of a rudimentary printf with support for specifying width argument:

#include <iostream>
#include <iomanip>
#include <string.h>

template<class T>
inline bitcopy(void *p, T* t) {
  memcpy(p, t, sizeof(*t));
}

void test(const char* s) {
  while (*s) {
    if (*s == '%' && *(++s) != '%') {
      throw std::runtime_error("Not enough parameters!");
    }
    std::cout << *s++;
  }
}

template<class T>
void test(const char* s, T t) {
  while (*s) {
    if (*s == '%' && *(++s) != '%') {
      if (*s == '*') {
        throw std::runtime_error("Not enough parameters!");
      } else {
        std::cout << t;
        test(++s);
      }
      return;
    }
    std::cout << *s++;
  }
  throw std::logic_error("Too many arguments!");
}

template<class T, class U, class... Args>
void test(const char* s, T t, U u, Args... args) {
  while (*s) {
    if (*s == '%' && *(++s) != '%') {
      if (*s == '*') {
        int width = 0;
//      width = t;                // Uncommenting this would cause a failure if the parameters aren't all int's.
        bitcopy(&width, &t);
        std::cout << std::setw(width) << u;
        test(s+2, args...);
      } else {
        std::cout << t;
        test(++s, u, args...);
      }
      return;
    }
    std::cout << *s++;
  }
  throw std::logic_error("Too many arguments!");
}

int main(int argc, char** argv) {
  test("Hello my name is %*s and I am %s.\n", 15, "Ashkan", 10);
  return 0;
}

Now here's something I made up so that I could work with a parameter by it's index. It might not be useful and it's really simple, but it should get you started on writing any utility functions you might need.

void getn(void* p, int n) {
}

template<class T, class... Args>
void getn(void* p, int n, T t, Args... args) {
  if (n == 0)
    bitcopy(p, &t);
  else
    getn(p, n-1, args...);
}

Last note: You can get the size of the parameter pack list by using sizeof.
Example:

sizeof...(args...)

Saturday, October 23, 2010

CCV on 64 bit Linux (Ubuntu 10.04 LTS)

While a 64 bit Linux binary is available on the CCV website, there are multiple disadvantages to using that binary. This is especially true if you are using the PS3 Eye Toy, which requires a modified unicap library to work; however, it is also an older version while the subversion repository provides the most recent version.

Now when I'm doing anything like installing someone else's program, I immediately start making a script so that I don't have to remember how to do things a second time. So here are the scripts I used to create three separate things: the CCV program from the svn repository; the modified unicap library; and the gspca driver for the PS3 Eye Toy.

ps3-unicap.sh is the script you can use to compile and install the patched unicap library.
ps3-eye.sh is the script you use to compile and install the gspca ov534 module for the kernel (PS3 Eye Toy driver).
ucview.sh is the script you use to compile and install ucview, not patched, not really required, just here because I wrote one.
ps3_settings.sh is a file that lets you set the gspca module settings through arguments, if no arguments are specified, it prints out the options.


Here are the steps I would take:
-Run ps3-eye.sh
-Run ps3-unicap.sh
-Run (and install VLC because it is awesome if not already installed) VLC, then click Media > Open Capture Device. You should see /dev/video0 or /dev/video1 (if you already have a video capture device).
-As far as I remember, compiling CCV is fairly straight forward. I recommend mt-dev CCV tutorial for now until I find my script and edit this post, or just reading the readMe.txt.

Sorry for anyone who came here to find an incomplete page, I'll be updating this page more in the coming days.
Please feel free to post any comments or complaints or criticisms of me, anything that doesn't kill me can only make things better.

PS3 Eye on Linux Made Easy