# Moving Windows to a Virtualbox



## balanga (Feb 16, 2018)

Is anyone aware of a guide for moving Windows to a Virtualbox running under FreeBSD?

I want to do this on a ThinkPad X220 which has 8GB ram and is running Windows 7 on a 256GB mSata disk.

Here are the steps I think I need to perform...

1. Backup the disk using Clonezilla

2. Delete as much as I can from the current Windows installation

3. Backup the disk using Clonezilla again (in case I've deleted too much)

4. Shrink the current Windows partition as much as possible.

5. Create a Windows VHD (or whatever I need to load Windows in a Virtualbox)

6. Install FreeBSD on the mSata disk

7. Install all the required pkgs in FreeBSD

8. Run Virtualbox

9. Setup Vitualbox to run Windows

I'm a bit hazy about the specifics of some of these tasks, but have I missed anything out?

Any comments would be appreciated.


----------



## SirDice (Feb 16, 2018)

There are a few caveats when converting a physical Windows installation to a virtual one. 

https://www.virtualbox.org/wiki/Migrate_Windows


----------



## giahung1997 (Feb 16, 2018)

If I'm right VMWare has a tool to convert a fully functional Windows on physical hardware to virtual machine. You could search and try it.


----------



## balanga (Feb 17, 2018)

giahung1997 said:


> If I'm right VMWare has a tool to convert a fully functional Windows on physical hardware to virtual machine. You could search and try it.




I found this but would the image created work under FreeBSD's Virtualbox?


----------



## giahung1997 (Feb 17, 2018)

balanga said:


> I found this but would the image created work under FreeBSD's Virtualbox?


I think this should be more accurate: https://kb.vmware.com/s/article/1018406
I came across the info vmware offer a tool to convert running system to vm easily but didn't care much so not bookmarked.
BTW, in vmware you convert the vm to .ova and you can import in in virtualbox.


----------



## balanga (Feb 17, 2018)

Can I download a copy of VMware Workstation? And what do I run it on? Is it available for FreeBSD?


----------



## aragats (Feb 17, 2018)

Since you haven't decided yet...
2.5 years ago I stopped using VirtualBox and completely switched to bhyve(8). I'm running a Windows domain workstation (Win7) in a corporate environment 24/7. Everything is fast and smooth. The only thing which is not implemented yet is the USB device pass-through. However, mass storage device can be easily mapped via xfreerdp(1) client.
The migration from physical to virtual was straight forward with sysprep (Windows command), you may not need it (I switched the host hardware). The physical disk image created with dd(1) can be used directly. Of course, you can shrink Windows partition before `dd` using the standard MS Disk Manager.


----------



## balanga (Feb 17, 2018)

I'll have to have a good look at bhyve(8) - it's something I've never investigated. Thanks for suggesting it.

Is there a useful guide for setting it up?


----------



## scottro (Feb 17, 2018)

There's a handbook page, lacks some details in my opinion, but will get you started.  
https://www.freebsd.org/doc/handbook/virtualization-host-bhyve.html

There's also bhyve-vm 
https://github.com/churchers/vm-bhyve/wiki/Quickstart
As I age, I just find it much easier to use VirtualBox, well documented, convenient interface, not a niche program, so if one wants to know how to do something by CLI a quick google will find it.


----------



## SirDice (Feb 18, 2018)

scottro said:


> There's also bhyve-vm
> https://github.com/churchers/vm-bhyve/wiki/Quickstart


+1 on sysutils/vm-bhyve.

Definitely have a read through the handbook to see what's needed to run a VM on bhyve(8). That will give you some insights on how things are linked together. Then quickly forget about all that and use sysutils/vm-bhyve to easily create/delete VMs


----------



## PacketMan (Feb 20, 2018)

SirDice said:


> Then quickly forget about all that and use sysutils/vm-bhyve to easily create/delete VMs



Is that GUI based or CLI based?  I get the impression its CLI based.   Is there a great GUI based bhyve manager?


----------



## SirDice (Feb 20, 2018)

PacketMan said:


> Is that GUI based or CLI based? I get the impression its CLI based.


Yes, your impression is correct. It's a command line tool. Really easy to use though. 


> Is there a great GUI based bhyve manager?


The only one I can think of is sysutils/cbsd. I tried it a long time ago and it was rather tricky to set up. This may have been improved now though, it was a really long time ago. For me a simple command line tool suffices for my purposes.


----------



## balanga (Oct 25, 2019)

Resuming an old thread, since I never got round to getting this working, is it still the case that I should be using sysutils/vm-bhyve rather than virtualbox?


----------



## SirDice (Oct 25, 2019)

It depends, Virtualbox does have a nice GUI and works really well. Bhyve is nice too as it's FreeBSD's own hypervisor. Why don't you try them both? Note however that you cannot use them simultanously.

I'm personally quite happy with bhyve, it's been running on an older dual Xeon Supermicro and I now have a number of VMs running (all FreeBSD) running a variety of services to play with (Gitlab, Zabbix, ElasticSearch/Kibana, Puppet).


----------



## balanga (Oct 25, 2019)

One thing I'm not sure about is whether I should backup the whole Windows disk (ada1) or the two Windows partitions (ada1s1 & ada1s2) ..


```
root@S09:/mnt/ntfs# gpart show ada1
=>       63  500118129  ada1  MBR  (238G)
         63       1985        - free -  (993K)
       2048     204800     1  ntfs  (100M)
     206848  499908608     2  ntfs  [active]  (238G)
  500115456       2736        - free -  (1.3M)
```


----------



## SirDice (Oct 25, 2019)

You should copy the whole disk, the other partition is probably the recovery partition. A VM is, for all intents and purposes, exactly the same as a "real" machine.


----------



## balanga (Oct 26, 2019)

I tried following the guide suggested by scottro 

https://github.com/churchers/vm-bhyve/wiki/Quickstart

I rebooted after step 4 to see if my `/etc/rc.conf` entries were OK but got a boot up error:-



> vmx_init: VMX operation disabled by BIOS
> module_register_init: MOD_LOAD (vmm, 0xffffffff82a51a50, 0) error 6



I'm trying this on a Lenovo ThinkPad T500, so am hoping I recognise the correct options in BIOS. Does anyone know exactly what I should be looking for?


----------



## scottro (Oct 26, 2019)

You definitely need the vmx_init.  That is what is needed for any virtual guest. (that is if it was running Linux and you wanted KVM, or if you use VirtualBox, VMWare or any of the forms of virtualization.

I'm afraid I don't know where it would be on your laptop but there should, be if you go through the menu, a section on virtualization that is usually disabled by default. You have to change its no to yes  or enabled or whatever the menu says. On my yoga2 which *might* be similar, but I really don't know, it's under Configuration=>Intel Virtual Technology. Hit the space bar to focus, hit enter (you  may just need that and not bother with the space bar, and choose enabled.  
Again that's on a yoga2 also made by Lenovo but no idea  how similar or dissimilar the BIOS'S are.


----------



## bjs (Oct 26, 2019)

You may also want to research your CPU as not all are capable of virtualization regardless of the BIOS options, just to be sure.... While I can't remember which CPU/motherboard combo I had at the time, the motherboard BIOS had the options for virtualization, the CPU did not...


----------



## balanga (Oct 26, 2019)

I couldn't find any way to make my ThinkPad T500 work, but eventually found the required option in the BIOS of my X1 Carbon and have now ticked off steps 5 and 6 in this list:-


```
1. pkg install vm-bhyve [grub2-bhyve uefi-edk2-bhyve]
2. zfs create pool/vm
3. sysrc vm_enable="YES"
4. sysrc vm_dir="zfs:pool/vm"
5. vm init
6. cp /usr/local/share/examples/vm-bhyve/* /mountpoint/for/pool/vm/.templates/
7. vm switch create public
8. vm switch add public em0
9. vm iso ftp://ftp.freebsd.org/pub/FreeBSD/releases/ISO-IMAGES/10.3/FreeBSD-10.3-RELEASE-amd64-bootonly.iso
10. vm create myguest
11. vm [-f] install myguest FreeBSD-10.3-RELEASE-amd64-bootonly.iso
12. vm console myguest
```

My ethernet interface is ue1 and I'm hoping to use windows.img as a guest. I have this file on another disk so am not sure how to proceed from here step 6.


----------



## Phishfry (Oct 26, 2019)

PacketMan said:


> Is there a great GUI based bhyve manager?


Why yes there is.
deskutils/virt-manager
The setup method is different as it uses devel/libvirt and its XML format.
So if you have existing VM configs they have to be converted to XML.
If new to bhyve VM's then you simply need to buff up on libvirt.


			https://people.freebsd.org/~rodrigc/libvirt/virt-manager.html
		



			https://people.freebsd.org/~rodrigc/libvirt-bhyve/libvirt-bhyve.html
		



			libvirt: Bhyve driver


----------



## balanga (Oct 26, 2019)

balanga said:


> I couldn't find any way to make my ThinkPad T500 work, but eventually found the required option in the BIOS of my X1 Carbon and have now ticked off steps 5 and 6 in this list:-
> 
> 
> ```
> ...



Well I managed to get to step 12 and have booted up a boot-only installation image of FreeBSD but have no idea what to do with it.  

How do I go about booting from my Windows image file?


----------



## scottro (Oct 27, 2019)

Usually you have to be quick to hit f12 after reboot. It should use the disk you select. For example, the new F12 disk or the old Windows disk.


----------



## balanga (Oct 27, 2019)

scottro said:


> Usually you have to be quick to hit f12 after reboot. It should use the disk you select. For example, the new F12 disk or the old Windows disk.



Not sure I understand... What would I need to do install a Windows image? I'm talking about steps 9, 11 in the instructions you provided.


----------



## scottro (Oct 27, 2019)

I'm a bit confused myself now. When you are booting, if you have an available disk, if you hit F12, said disk should be visible in the menu.

But I don't believe I have ever done what you are doing that is move Windows to VirtualBox. It is possible that you 'd receive better help from the VirtualBox forums, as I haven't done, and don't know how to do what you're trying to accomplish.


----------



## balanga (Nov 20, 2019)

Resuming this topic since not making any progress... I've tried following various guides for creating a Windows image of an existing installation but all the guides lead me down rabbit holes showing options in programs which are no longer pertinent. Does anyone know how I can currently create an image which can be imported into VirtualBox?  With VMWare, I think I need to download the Standalone Converter, but I can't just download it I need apply for an account first.


----------



## balanga (Nov 20, 2019)

Eventually managed to register and download VMware vCenter Converter Standalone which has created two files a *.vmdk and a *.vmx... 

How do I import them into VirtualBox, or do I need to convert them first?


----------



## Phishfry (Nov 21, 2019)

I see you have some sort of converter program. That might work, but you should start with a fresh image just to learn the process.
The vmdk format is a pre-formated VM.








						How to Open a VMDK File in VirtualBox
					

Need to open a VMDK file in VirtualBox? This article will show you how to setup and use a VMDK virtual machine file with VirtualBox. This particular tutorial is demonstrated on a Mac, but using a V…




					osxdaily.com


----------



## Lamia (Nov 21, 2019)

One can import a .vmdk into Virtualbox much like one creates an instance of a virtual machine in it. The instance of the virtual machine takes the size of the vmdk file. No other installation required.


----------



## Phishfry (Nov 21, 2019)

The VMX file describes the hardware that the vm was created for.
It is a plain text file.


----------



## aragats (Nov 21, 2019)

There are two "different" vmdk disk formats: one is for a virtual disk image, another is to describe an actual physical disk to attach, see the corresponding chapter of the VB's manual.


----------



## balanga (Mar 31, 2020)

After another break, time to make some progress. I've downloaded and installed VMware Converter Standalone. The system I'm trying to convert has two installations of Windows and I'm trying to convert the one I haven't booted from but it is on the local machine. But when I select 
'This local machine' I get 'Permission to perform this operation was denied'...

Is it possible to convert the local machine?


----------



## aragats (Apr 1, 2020)

I'm not sure why you may want to use a third-party converter. Just make images of needed partitions using dd(), "construct" a virtual disk (=disk image file) and copy those partitions to it. Perfectly works, I've done it many times. Windows may complain about boot environment, so have a Windows installer handy, use it (if needed) to fix  the current installation when booted from your virtual disk. Also, before `dd`-ing I'd recommend shrinking Windows partition(s) down using Windows Disk Manager to eliminated copying unused blocks. When everything is up and running, you can expand them back.


----------



## balanga (Apr 1, 2020)

aragats said:


> I'm not sure why you may want to use a third-party converter. Just make images of needed partitions using dd(), "construct" a virtual disk (=disk image file) and copy those partitions to it. Perfectly works, I've done it many times. Windows may complain about boot environment, so have a Windows installer handy, use it (if needed) to fix  the current installation when booted from your virtual disk. Also, before `dd`-ing I'd recommend shrinking Windows partition(s) down using Windows Disk Manager to eliminated copying unused blocks. When everything is up and running, you can expand them back.


To be honest I'm using this VMware tool because someone suggested it.

Are you suggesting that I create a virtual disk using dd()? Although if I shrink the partition then presumably *ifs* would be /dev/ada0s1 ?
What about the system partition? Never really known what that was for. Windows is really a big mystery to me and am usually happy for it to stay that way.


----------



## aragats (Apr 1, 2020)

Use gpart() to list the current partitions, save its output in a file. You're right, Windows is tricky, and it doesn't like when you change the partitions' numbers (indices). That's why, if you need e.g Windows installation on partition 4, but there is another one on partition 3, just create a tiny partition 3 on your virtual disk to keep the same indices (I'm not sure about the recent Windows releases, but before they used to re-arrange indices, so just assigning index 4 and skipping 3 didn't help).
If the original disk is GPT, it's easier (IMO): the first partition is EFI, you can just copy files instead of `dd`-ing (it's FAT). If it's MBR, make sure to make the first partition active (check with gpart()'s output first). The "system" partition is the boot partition, I guess. The first one, right? You need it, but you definitely don't need a "recovery" (or whatever it is) partition which is regularly the last one.

If you shrink a partition, its index won't change, you'll have a hole between partitions, you don't need exactly the same hole in the destination disk, just space you'd like to expand that partition later into.


----------



## aragats (Apr 1, 2020)

balanga said:


> Are you suggesting that I create a virtual disk using dd()?


You create a virtual disk using truncate(). Then you create a device node with mdconfig() and use gpart() on that md0 device to create/manipulate partitions.
I suggest using dd() to copy partitions, not the entire disk.


----------



## balanga (Apr 3, 2020)

I've wasted a couple of days trying to recover a Windows system which suddenly wouldn't boot because the BCD file got removed after I booted a different installation of Windows on the same system. Went through dozens of explanations of how to recover the file and eventually found one which worked.

I have now shrunk the Windows partition as much as I can using Windows builtin administrative tools.

So am I at the point where I can create the Windows.VHD by booting up FreeBSD and mounting the Windows partition using ntfs-3g?

Assuming I run 

`ntfs-3g /dev/da0s1 /mnt/ntfs`

I'm not really clear what should be done next. Can't see how truncate() fits into the scheme of things... Obviously missing something. An example of its usage would be useful...


----------



## aragats (Apr 3, 2020)

I'm not sure why do you need to mount a Windows partition?
What I usually do:
	
	



```
# create an empty file:
truncate -s 32g windisk.img

# create a device node:
mdconfig windisk.img

# partition virtual disk:
gpart <command> md0

# copy partitions from physical disk to virtual:
dd if=/dev/ada0pX of=/dev/md0pX bs=1m
```


----------



## balanga (Apr 3, 2020)

I see.... didn't really understand how to do this...

My Windows partition is 171GB. Would this be a problem? I have 200GB spare space on the disk which has Virtual Box installed.

When running `gpart add` what partition type should I use?

One thing I just noticed is that I'm running a 64-bit version of Windows does that mean I won't be able to run this in Virtual Box after all?


----------



## aragats (Apr 3, 2020)

171GB? I can't imagine what really Windows specific can occupy such space. Anyway, you'll still have 29GB remaining.
Have you resized that Windows partition? I found, that ntfsresize() can do it even better than Windows Disk Manager, although, more dangerous. I'd recommend starting Windows after resizing - it will run filesystem check (real Windows, not "virtual").
Regarding the 64bit: VirtualBox executes instructions directly on the CPU, if your CPU is 32bit, it's not going to work. Even if VirtualBox can do full emulation (if - just guessing), the performance will not be acceptable.


----------



## balanga (Apr 3, 2020)

aragats said:


> 171GB? I can't imagine what really Windows specific can occupy such space.



Maybe I'm trying to do something which can't be done... I have a Windows system which has numerous apps installed. Office, Photoshop, Chrome. Currently the partition takes up 171GB after shrinking the partition to the max, although Windows still reports 52GB free. I was hoping to migrate this to Virtual Box, but maybe this isn't viable.

When creating the virtual disk partition what partition type should it be?


----------



## free-and-bsd (Apr 3, 2020)

My goodness, man! A little bit off-topic wisdom, if you can put up with this  I've done this trick so many times before -- I mean copy/restore Windows partition to another disk/computer.
_Theoretically_, it all can be done. I've done that before: converting etc. Migrated the existing installation to VMplayer -- yes, I did it.
But don't you forget that for Windows THIS will be like taking your HDD and inserting into a completely different MACHINE. There are big chances it won't boot at all... So, in the end of it, I felt like the time I spent fiddling with it wasn't worth the result. 

Now won't it be easier to do fresh install and reinstall all your apps, given you have the licenses for all of them safe somewhere...  Really, OS Windows makes it all SO complicated! With Linux/FreeBSD you just put your HDD in there and -- voila! But not in Windows .

On the other hand, with fresh install you can just call the number and re-register your product. I did it recently with Win 10 Pro under bhyve -- no problem at all.


----------



## free-and-bsd (Apr 3, 2020)

balanga said:


> Maybe I'm trying to do something which can't be done... I have a Windows system which has numerous apps installed. Office, Photoshop, Chrome. Currently the partition takes up 171GB after shrinking the partition to the max, although Windows still reports 52GB free. I was hoping to migrate this to Virtual Box, but maybe this isn't viable.


Now that you're mentioning it, you could use the Disk Cleaner tool to clean up updates and other stuff. BEFORE the migration, that is.
Alternatively, you can copy your user data over to some medium and delete your user before migrating. Those user directories are where most junk gets collected. I could bet you would recover some 50G by that.


----------



## Alain De Vos (Apr 3, 2020)

My strategy is to install in Virtualbox and afterwards migrate to bhyve. [convert vdi to raw]


----------



## aragats (Apr 3, 2020)

Alain De Vos said:


> My strategy is to install in Virtualbox and afterwards migrate to bhyve. [convert vdi to raw]


What's wrong with installing directly in bhyve?


----------

