четверг, 18 февраля 2016 г.

Creating VirtualBox machine based on a physical partition using VBoxManage

0. Preface


In this article I am going to show you how to install and setup FreeBSD and start use it as a home OS.

I will use
  • FreeBSD 10.2
  • ZFS (I believe 2GiB of RAM is o.k. for it)
  • LXDE 1.0
  • VBoxManage 4.3
  • SeaMonkey 2.39
  • MPlayer 1.2

1. Virtual Hard Drive using raw partition support


Let me start from creating the virtual hard drive for the machine. I am going to use raw partition support. This feature is a part of the VMDK format.
It allows a guest operating system to access its virtual hard disk without going through the host OS file system

1.1. Create a partition

First of all I create a partition for a virtual hard drive.

1.1.1. Example 1 (physical partition)

# gpart add -t freebsd -s 40G -a 4k ada0
ada0s4 created

1.1.2. Example 2 (logical slice)

# gpart add -t freebsd -s 40G -a 4k ada0s4
ada0s4s1 created
This approach was not applicable for me because of several reasons: 1) too complex partitioning (bsdinstall will divide the partition of partitions into at least three partitions).

The advice: use GPT partitioning scheme and don't forget about aligning.

1.2. Choose the partition for virtual disk and set the correct permissions to it

Secondly, I list partitions for using as a hard drive for the future virtual machine
% VBoxManage internalcommands listpartitions\
   -rawdisk /dev/ada0
It outputs table like these (below).

1.2.1. Example 1 (root scheme)

Number  Type   StartCHS       EndCHS      Size (MiB)  Start (Sect)
1       0xa5  0   /2  /1   1023/255/63        212991          126
2       0xa5  1023/255/63  1023/255/63       1048575    436207716
3       0xa5  1023/255/63  1023/255/63         16383   2583691362
4       0xa5  1023/255/63  1023/255/63         40959   2617245792
The 4th partition was created by the first command (gpart) and I use it as the base for the virtual machine disk. To avoid permissions problem with virtual HD made using raw partition support, issue the next command (as root) before creating a virtual disk with raw partition support:
# chmod g+w /dev/ada0s4
where ada0s4 is the geom for virtual hard drive.

To make this permanent I added the one line to devfs configuration:
# cp /etc/devfs.conf /etc/devfs.conf.old
# echo 'perm  ad6s4  0660' >> /etc/devfs.conf

1.2.2. Example 2 (logical slice)

Number  Type   StartCHS       EndCHS      Size (MiB)  Start (Sect)
1       0xa5  0   /2  /1   1023/255/63         40959          126
# chmod g+w /dev/ada0s4s1
# cp /etc/devfs.conf /etc/devfs.conf.old
# echo 'perm  ad6s4s1  0660' >> /etc/devfs.conf
Remember: this is just example and if you use MBR, your sub-partitions may be erased after reboot (I don't know why). For example /me confronted this problem. But the first example work for me well.

1.3. Create the virtual disk

If you already have got a virtual disk attached to a virtual machine you can detach it using the next command.
% VBoxManage storageattach fbsd\
   --storagectl sata1\
  
--port 1\
  
--type hdd\
  
--medium none
There is an easy way to delete the unusable virtual hard drive: just delete the according VMDK file:
% rm /mnt/barracuda/vm/fbsd.vbox/dsk/ada0s4*.vmdk
After that I can create a VMDK for the VM.
% VBoxManage internalcommands createrawvmdk\
   -filename /mnt/barracuda/vm/fbsd.vbox/dsk/ad6s4.vmdk\
   -rawdisk /dev/ada0\
   -partitions 4\
   -relative
The -relative key prevents access to entire disk. It released me from the access problem and I got the next result
RAW host disk access VMDK file /mnt/barracuda/vm/fbsd.vbox/dsk/ad6s4.vmdk created successfully.

1.3.1. The note about -relative

Actually, you should use the next command
% VBoxManage internalcommands createrawvmdk\
   -filename /mnt/barracuda/vm/fbsd.vbox/dsk/ad6s4.vmdk\
   -rawdisk /dev/ada0\
   -partitions 4
(without the -relative option) as it is more correct. But I faced up with the next useless error:
VBoxManage: error: VMDK: could not open raw partition file '/dev/ad6'
VBoxManage: error: Error code VERR_ACCESS_DENIED at /wrkdirs/usr/ports/emulators/virtualbox-ose/work/VirtualBox-4.3.36/src/VBox/Storage/VMDK.cpp(3533) in function int vmdkCreateRawImage(PVMDKIMAGE, const PVBOXHDDRAW, uint64_t)
VBoxManage: error: Cannot create the raw disk VMDK: VERR_ACCESS_DENIED
VBoxManage: error: The raw disk vmdk file was not created
If you know how to create a VMDK made of a raw partition without the -relative key, please, let me know by e-mail or in comments block below the text.

1.3.2. The additional note concerning permissions

If you still having problems with creating the VMDK, try to issue the next commands:
# chmod a+wr /dev/ada0*
#
chown vblinkov /dev/ada0*

2. Create the VM

I usually create virtual machines (VMs) using the next command.

% VBoxManage createvm\
   --name fbsd\
   --basefolder /mnt/barracuda/vm/fbsd.vbox/machine/\
   --register
The result of the command is listed below
Virtual machine 'fbsd' is created and registered.
UUID: 7f0add90-fbe6-4dba-83b1-b2782290c16a
Settings file: '/mnt/barracuda/vm/fbsd.vbox/machine/fbsd/fbsd.vbox'
To list registered machines use the next command
% VBoxManage list vms
"fbsd" {7f0add90-fbe6-4dba-83b1-b2782290c16a}
Then I look at the memory size:
% VBoxManage showvminfo fbsd | grep Memory
Memory size:     128MB
I usually consume up to 2 GiB of RAM, so I increase memory size using
% VBoxManage modifyvm fbsd --memory 2048
% VBoxManage showvminfo fbsd | grep Memory
Memory size:     2048MB
I enable PAE/NX (required (but does not help to run) for Windows 8+, requires the same option to be enabled on your physical motherboard (look for it in BIOS))
% VBoxManage modifyvm fbsd --pae on
Install sound card:
% VBoxManage modifyvm fbsd\
   --audio oss\
   --audiocontroller hda
 

Now I create a SATA master  controller
% VBoxManage storagectl fbsd\
   --name sata1\
   --add sata\
   --bootable on
After that I attach the created earlier virtual hard disk based on the physical partition.
% VBoxManage storageattach fbsd\
   --storagectl sata1\
   --port 1\
   --type hdd\
   --medium /mnt/barracuda/vm/fbsd.vbox/dsk/ada0s4.vmdk
Look!
% VBoxManage list hdds
UUID:           42db7f73-392e-4af7-9a8a-bc8c461dad29
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       /mnt/barracuda/vm/fbsd.vbox/dsk/ada0s4.vmdk
Storage format: VMDK
Capacity:       1430799 MBytes
My virtual hard drive (HD) is registered in VirtualBox now.

To start the VM I use this command
% VBoxManage startvm fbsd --type sdl

If you do not want to install operating system right now you can halt the machine using the command shown below.
% VBoxManage controlvm fbsd poweroff
I insert a DVD with FreeBSD 10.2 distro typing the next command (from a terminal [emulator])
% VBoxManage storageattach fbsd\
   --storagectl sata1\
   --port 2\
   --type dvddrive\
   --medium\
   /mnt/barracuda/os/freebsd/10.2/FreeBSD-10.2-RELEASE-i386-dvd1.iso
and restart the machine
% VBoxManage controlvm fbsd reset
I issue the next command to make screenshots:
% VBoxManage controlvm fbsd screenshotpng /tmp/02.png

 
This is the FreeBSD 10 boot menu.


3. FreeBSD 10 installation process

Please follow the next link to read my another article concerning FreeBSD 10.2 installation process:
http://freebsdathome.blogspot.ru/2016/03/installing-freebsd-102.html


After installation is complete you might want to eject the distro DVD.
% VBoxManage storageattach fbsd\
  
--storagectl sata1\
  
--port 2\
  
--type dvddrive\
  
--medium emptydrive

4. After installation

You may want to do next things.

4.1. Rename/move a VM

% VBoxManage modifyvm fbsd --name win
% VBoxManage unregistervm win
% cd /mnt/barracuda/vm # location of the VM directory
% mv fbsd.vbox win.vbox # rename (can move) the directory
% # heads up: use the ABSOLUTE path in the next command
% VBoxManage registervm\
   /mnt/barracuda/vm/win.vbox/machine/win/win.vbox
% # detach hard drive (it's old location) from machine:
% VBoxManage closemedium disk\
   /mnt/barracuda/vm/fbsd.vbox/dsk/ad6s4.vmdk
% # attach the disk at its new location
% VBoxManage storageattach win\
   --storagectl sata1\
   --port 1\
   --medium /mnt/barracuda/vm/win.vbox/dsk/ad6s4.vmdk\
   --type hdd

4.2. Install guest additions

Guest additions is a program to add something like cooperation between the guest OS of VM and the host operating system. You can find all about the additions at the official VirtualBox documentation.

Next I will show you how I usually install additions.
% wget http://download.virtualbox.org/virtualbox/\
   4.3.36/VBoxGuestAdditions_4.3.36.iso
% VBoxManage storageattach win\
   --storagectl sata1\
   --port 2\
   --type dvddrive\
   --medium VBoxGuestAdditions_4.3.36.iso

4.3. Setup networking

4.3.1. Connect the VM to the Internet

% VBoxManage controlvm win poweroff
% VBoxManage modifyvm win\
   --nic1 nat\
   --nictype1 82540EM
  

4.3.2. Wire host and guest

% VBoxManage modifyvm win\
   --nic2 hostonly\
   --nictype2 82540EM\
   --hostonlyadapter2 vboxnet0

4.4. Setup share between host and guest

Install additions (see paragraph 4.2) before proceed!
% VBoxManage sharedfolder add win\
   --name share\
   --hostpath /mnt/barracuda/vm/win.vbox/share/
   --automount

4.5. Start VM instead on desktop manager

~/.xinitrc
#!/bin/sh

# login to VM
VirtualBox --fullscreen --startvm fbsd

P.S. Sorry for formatting: Blogger editor sucks :)

Bibliography

  1. Access to individual physical hard disk partitions / VirtualBox manual, /chapter 9.9.1.2 / https://www.virtualbox.org/manual/ch09.html#rawdisk.

Комментариев нет:

Отправить комментарий