During the development of my Linux From Scratch I was searching for a way to test the boot process using Virtual Box instead of rebooting my development computer again and again. I found some articles on how to achive this using Windows, but I use Linux on my computer. Finally I did it :) using VBoxManage internalcommands. Here comes a short howto:

1. Plug in your usb stick. The usb stick MUST contain a bootable operating system. This can be anything that boots from an usb stick.

2. Open a terminal to find out how the kernel named your usb device. (Or more accurat, its partion(s))

thorsten@home:~$ mount
/dev/sdb2 on / type ext3 (rw,relatime,errors=remount-ro)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
none on /sys/fs/fuse/connections type fusectl (rw)
none on /sys/kernel/debug type debugfs (rw)
none on /sys/kernel/security type securityfs (rw)
udev on /dev type devtmpfs (rw,mode=0755)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755)
none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880)
none on /run/shm type tmpfs (rw,nosuid,nodev)
/dev/sdb5 on /tmp type ext3 (rw,relatime)
/dev/sdb7 on /usr type ext3 (rw,relatime)
/dev/sdb8 on /var type ext3 (rw,relatime)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,noexec,nosuid,nodev)
/dev/md_d0 on /home type ext3 (rw)
gvfs-fuse-daemon on /home/thorsten/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev,user=thorsten)
/dev/sdd1 on /home/thorsten/lfs type ext3 (rw)
thorsten@home:~$

Your output will look different from mine, but you should be able to find the partition(s). In my case it is /dev/sdd1.

3. Unmount all partitions of the usb stick. (This may require root privileges, depending on your system and settings). In my case it is only one partition /dev/sdd1

thorsten@home:~$ umount /dev/sdd1

4. Now comes the interesting part – creating the vmdk file. To achive this I’ll use VBoxManage the console interface provided by VirtualBox. The internalcommands providing a command called createdrawvmdk. The command creates a .vmdk file using a raw disk from the host system. This file can be used later as a *harddisk* for a virtual machine. Yes!, that’s exactly what I want, the raw host disk in this case should be my usb stick.

(!) Note: In this tutorial I’m using the low level (usb)disk device. This is similar to plugin the usb stick into a regular hardware and boot it. So the device of interest is /dev/sdd and not /dev/sdd1.
root@home:/home/thorsten# VBoxManage \
internalcommands createrawvmdk \
-rawdisk /dev/sdd \
-filename my_usb_stick.vmdk
RAW host disk access VMDK file /home/thorsten/Desktop/lfs.vmdk created successfully.
root@home:/home/thorsten#

5. Create a new virtual machine that uses the my_usb_stick.vmdk as harddisk. This can be done on command line too, sure. To not confuse readers I’ll use the more regular graphical interface.

5a. Start Virtualbox

(!) Note: It is important to start VirtualBox as root unless you don’t want to change the permissions of the low level disk file (/dev/sdd, in my case)
root@home:~# virtualbox

5b. Click the ‚New‘ button.

5b. Click ‚Next‘ and choose a name for the virtual machine. I used ‚Boot From Stick‘, as it can be everything that is installed on stick (and this may change).

5c. Click ‚Next‘ and select the memory size for the virtual machine. I selected 1024MB / 1GB in this example.

5d. Click ‚Next‘ and choose ‚Use existing harddisk‘

5e. Click the folder item next to the list box with images. Then select the vmdk file created before.

5f. Click ‚Open‘

5g. Click ‚Create‘ to finish the process.

6. Start the Virtual Machine. It should boot from the usb stick :)

Conclusion: VBoxManage internalcommands createrawvmdk is a nice tool when debugging boot problems or just run your portable usb stick linux in parallel with your home system – maybe be in order to transfer files. In addition createrawvmdk works with regular harddisks or other mediums too. So it might not ever beeing requested to boot from that media but to mount it during runtime – no problem.

You can access the help for this command by typing :

thorsten@computer:~$ VBoxManage internalcommands

You’ll find options to in/exclude partitions from being visible to the Virtual Machine, but you’ll find the alert on end of this output too:

WARNING: This is a development tool and shall only be used to analyse
problems. It is completely unsupported and will change in
incompatible ways without warning.

I hope they will not remove this feature in upcoming versions.

cu.

12 Responses to “Booting your usb stick using Virtual Box on a Linux host”

  1. Smerry Says:

    When I attempt this I get an INT18 BOOT FAILURE. Has anyone else made this work?
    (VBox 4.2.18)

  2. thorsten Says:

    Does VBoxManage outputs this in terminal? Which guest OS do you use?

    Thorsten

  3. Gagandeep Says:

    Thanks a ton.. for nice article..

    @smerry : sudo virtualbox

  4. thorsten Says:

    Nice to see that it was helpful for you :)

    Thorsten

  5. Joerg Says:

    Hi Thorsten,
    Nice piece of work! It looks reasonable to me.

    Unfortunately I hit – maybe – two problems:

    (1) Your output of VBoxManage looks like this
    „RAW host disk access VMDK file /home/thorsten/Desktop/lfs.vmdk created successfully.“
    My output ays
    „RAW host disk access VMDK file my_usb_stick.vmdk created successfully.“
    which is probably fine as well (?).

    (2) But when I assign „my_usb_stick.vmdk“ to my VM it complains about not sufficient permissions.
    „chown“ to myuser:mygroup on my_usb_stick.vmdk does not help. Neither does a „chown“ on /dev/sdg (which is generally a bad thing anyway).

    I’m using VirtualBox 4.3.8 on a Ubuntu host with an USB-Live-System of SliTaz as guest.

    Do you have any clue about what is going wrong?

    Thanks in advance and cheers!
    Joerg

  6. Joerg Says:

    … @Gagandeep: OK. It works if you have sudo permissions. But in my opinion it should be possible as „normal“ vboxuser als well.

    Cheers!
    Joerg

  7. thorsten Says:

    Hi Jörg,

    yeah, this are reasonable concerns. I’ll review this. Many thanks for your feedback!

    Thorsten

  8. james1052 Says:

    This helped a bunch, thanks! What permissions to I need to change in Debian for my sudo user to be able to use the vmdk instead of just root?

  9. Pedro Bezunartea López Says:

    @Joerg:

    I had the same problem, and found the solution at ubuntuforums: your user needs to be member of the groups ‚vboxusers‘ and ‚disk‘. My user was a member of ‚vboxusers‘ but not of ‚disk‘.

    I used ‚vigr‘, and ‚vigr -s‘ to append my username to those groups, but you can also do it using the GUI: from the dash, search ‚Users and Groups‘:

    Select your username, click on ‚Advanced Settings‘.
    On the ‚User Privileges‘ tab, make sure that you have

    Access external storage devices automatically
    Use VirtualBox virtualisation solution

    Log out, log back in and you should not get the error any longer.

    HTH

  10. Luke Says:

    all OK till I try to boot – comes up with

    This kernel requires the following features not present on the CPU:
    pae
    Unable to boot – please use a kernel appropriate for your CPU

    What is a kernel?
    How can I fix this problem?

  11. thorsten Says:

    Hi,

    which operating system do you have installed on your usb stick? What ist your host operating system? (The system where Virtual Box is installed on)

    Thorsten

  12. Wayne Sallee Says:

    I did not want to run Virtualbox as root.
    I did not want my user to be of the disk group, having full permissions to all disks.

    So here is what I got to work:

    Find out what sd the memory stick is. In the following instrucions it’s sdc

    Unmount memory Stick
    su
    chmod 666 /dev/sdc1
    chmod 666 /dev/sdc
    VBoxManage internalcommands createrawvmdk -filename usb.vmdk -rawdisk /dev/sdc
    chmod 777 usb.vmdk
    chmod 666 /dev/sdc
    Attach usb.vmdk virtualbox machine
    chmod 666 /dev/sdc
    Start virtualbox machine
    After done, reset sdc1 permission for security:
    chmod 660 /dev/sdc1

    In the above you can see that I had to reset the permissions for the memory stick a few times, because it would get set back to root each time.

    Wayne Sallee
    Wayne@WayneSallee.com

Leave a Reply