Windows


Reclaim empty space from VirtualBox VDI disk images by shrinking

Recently, we did some cleanup in certain GNU/Linux virtual machines, where we hoped that VirtualBox would release the disk space that is not used and shrink the size of the VDI files.
Unfortunately, that did not happen even after freeing more than 100GB of space from the guest machine.

We did manage though to reclaim the empty space manually, using the zerofree and VBoxManage utilities.

Following is the guide we followed to do so:

Part 1: Clean-up the guest machine using zerofree

We needed to find the unallocated, blocks with non-zero value content in the ext2, ext3 or ext4 filesystem (e.g. /dev/sda1) and fill them with zeroes.
Since the filesystem has to be unmounted or mounted as read-only for zerofree to work, we decided to use a Live CD to complete this task as it would be the simplest solution to follow.

Step 1: Download SystemRescueCd Live Image

We downloaded the SystemRescueCd Live Image and inserted it to the guest machine as a Live CD.

Step 2: Identify partitions and disks using fdisk -l

We booted the virtual machine from the Live CD, when the terminal appeared, we typed fdisk -l to see all available disks and partitions.

Depending on your configuration you will see the following

If you performed your installation using standard partition

Disk /dev/sda: 200 GiB, 214748364800 bytes, 419430400 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x64af3527

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048 411043839 411041792  196G 83 Linux
/dev/sda2       411045886 419428351   8382466    4G  5 Extended
/dev/sda5       411045888 419428351   8382464    4G 82 Linux swap / Solaris

or if you performed your installation using LVM

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000523ce

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048   419430399   209202176   8e  Linux LVM

Disk /dev/mapper/centos-root: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 4160 MB, 4160749568 bytes, 8126464 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-home: 156.3 GB, 156304932864 bytes, 305283072 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

From the above results, we keep the paths to the real partitions and the paths to the LVM partitions, e.g.
/dev/sda1 which is 196GB, /dev/mapper/centos-home and /dev/mapper/centos-root

Step 3: Perform the cleanup

To perform the clean up, execute for each of the above paths the command zerofree, e.g.

zerofree /dev/sda1;
zerofree /dev/mapper/centos-home;
zerofree /dev/mapper/centos-root;

Give it some time to complete the task, the larger the partition, the more the time it will take.

Part 2: Shrink the guest machine disk images using VBoxManage

We used VBoxManage with the parameter --compact, which it is used to compact disk images, i.e. remove blocks that only contains zeroes. It shrinks dynamically allocated images by reducing the physical size of the image without affecting the logical size of the virtual disk. Compaction works both for base images and for diff images created as part of a snapshot. For this operation to be effective, it is required that free space in the guest system first be zeroed out and that is why we had to perform Step 1 using zerofree before.
Please note that compacting is currently only available for VDI images.

To use, just issue the command "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyhd --compact <DISK_PATH> pointing to the disk you just cleaned up using zerofree
Please note that the virtual machine should be stopped before starting this operation.

Press the key combination Win + R to pop up the Run prompt.
Type cmd in the input box and hit the Enter key.

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyhd --compact "F:\Ubuntu\Ubuntu.vdi"
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyhd --compact "C:\Users\bytefreaks\VirtualBox VMs\CentOS\CentOS.vdi"


VirtualBox: Failed to attach the USB device to the virtual machine 29

Recently we were using a Windows 10 64bit machine which had Oracle VirtualBox installed.
At some point all USB devices stopped mounting on the guest systems.
We would get errors similar to the following:

Failed to attach the USB device OnePlus A0001 [0232] to the virtual machine Ubuntu.

USB device 'OnePlus A0001' with UUID {544e5582-9e77-4301-a538-5326cf2250c0} is busy with a previous request. Please try again later.

Result Code: E_INVALIDARG (0x80070057)
Component: HostUSBDeviceWrap
Interface: IHostUSBDevice {c19073dd-cc7b-431b-98b2-951fda8eab89}

Callee: IConsole {872da645-4a9b-1727-bee2-5585105b9eed}

USB device  with UUID  is busy with a previous request. Please try again later.

After a couple of restarts of both the guest and the host machines we realized that this time, a restart was not enough to fix the error.
Right before this error occurred, we had installed Wireshark with USBPcap support.
Apparently this was the root of our problem.

Resolution:

Following are the steps we followed to solve this issue:

Step A: Delete problematic system configuration.

Press the key combination Win + R to pop up the Run prompt.
Type regedit in the input box and hit the Enter key.

regedit

On the left side of the new window, navigate to the following location:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Class\{36FC9E60-C465-11CF-8056-444553540000}

upperfilters

In the right part, select the UpperFilters entry, right click it and select Delete.

delete-upperfilters
When a prompt window appear asks you to confirm that you want to delete the value, click Yes.

confirm-delete-upperfilters

Step B: Manually re-install VirtualBox USB drivers (Optional)

Just in case there is an issue with the VirtualBox USB drivers, you can re-install them to be sure everything is OK.
To do that, you can either re-install the whole VirtualBox using their installer or manually re-install the driver itself.

To re-install the VirtualBox USB driver manually, using Windows Explore navigate to this folder

C:\Program Files\Oracle\VirtualBox\drivers\USB\filter

Right click the file VBoxUSBMon.inf and select Install.

install-virtualbox-usb-driver
You will get a confirmation once the installation is complete.
Restart your machine, so that new changes will get applied.
Your USB devices should work as expected.


The disk is offline because it has a signature collision with another disk that is online 1

Recently, we cloned a hard disk using dd.
When we booted into Windows, the new drive was not visible.
After checking with the disk utilities, we got the following informative message:

The disk is offline because it has a signature collision with another disk that is online

To resolve the issue, we used diskpart.
To start diskpart, press the key combination Win + R which will pop up the Run prompt.
Type diskpart in the input box and hit the Enter key.

A new terminal window will appear.
Using that we identified the two disks and changed the label for the second one.

First step:

We issued list disk to get the list of disks.

DISKPART

Microsoft DiskPart version 10.0.14393.0
Copyright (C) 1999-2013 Microsoft Corporation.
On computer: BYTEFREAKS-NET

DISKPART> list disk

  Disk ###  Status         Size     Free     Dyn  Gpt
  --------  -------------  -------  -------  ---  ---
  Disk 0    Online          931 GB      0 B
  Disk 1    Offline         465 GB      0 B

Second step:

We issued select disk 1 so that we could process the disk 1 that was offline and using uniqueid disk we got the signature of the disk.

DISKPART> select disk 1
Disk 1 is now the selected disk.

DISKPART> uniqueid disk
Disk ID: 09FC13CB

Third step:

Set the signature of the disk to a random value other than the one that it already had using the command uniqueid disk ID=FFAABBCCDD

DISKPART> uniqueid disk ID=FFAABBCCDD

DISKPART> uniqueid disk
Disk ID: FFAABBCCDD

The random value must be 8 characters long and each character must be a value between 0-9 or A-F.

Finally:

Restart the machine to get both disks running.


Activate a wireless hotspot on Windows 10 1

Requirements

  • You must be an administrator of the machine to complete this guide.
  • You need to have at least two network devices.
  • One of them needs to have access to the internet and the other one needs to be a WiFi adapter which has Hosted Network support.

To check if your wireless adapter supports the functionality for Hosted Network, open a Command Prompt and type NETSH WLAN show drivers.

To open the Command Prompt, press the buttons Windows+R on your keyboard.
A new run command prompt will appear.
Type in the input box cmd and hit the Enter button.

In the new Command Prompt type NETSH WLAN show drivers, the results should be similar to below.

C:\Users\bytefreaks>NETSH WLAN show drivers
 
Interface name: Wi-Fi
 
    Driver                    : Realtek RTL8188CU Wireless LAN 802.11n USB 2.0 Network Adapter
    Vendor                    : Realtek Semiconductor Corp.
    Provider                  : Realtek Semiconductor Corp.
    Date                      : 3/4/2016
    Version                   : 1027.4.630.2015
    INF file                  : ????
    Type                      : Native Wi-Fi Driver
    Radio types supported     : 802.11n 802.11b 802.11g
    FIPS 140-2 mode supported : Yes
    802.11w Management Frame Protection supported : Yes
    Hosted network supported  : Yes
    Authentication and cipher supported in infrastructure mode:
                                Open            None
                                WPA2-Personal   CCMP
                                Open            WEP-40bit
                                Open            WEP-104bit
                                Open            WEP
                                WPA-Enterprise  TKIP
                                WPA-Personal    TKIP
                                WPA2-Enterprise TKIP
                                WPA2-Personal   TKIP
                                WPA-Enterprise  CCMP
                                WPA-Personal    CCMP
                                WPA2-Enterprise CCMP
                                Vendor defined  TKIP
                                Vendor defined  CCMP
                                Vendor defined  Vendor defined
                                Vendor defined  Vendor defined
                                WPA2-Enterprise Vendor defined
                                WPA2-Enterprise Vendor defined
                                Vendor defined  Vendor defined
                                Vendor defined  Vendor defined
    Authentication and cipher supported in ad-hoc mode:
                                Open            None
                                Open            WEP-40bit
                                Open            WEP-104bit
                                Open            WEP
                                WPA2-Personal   CCMP
    Wireless Display Supported: Yes (Graphics Driver: Yes, Wi-Fi Driver: Yes)

In the results you need to find the line Hosted network supported and verify that the value is set to Yes, if it is not, then you cannot proceed with this wireless network adapter.

In case you got a permission error on the above command, try to open a new Command Prompt with admin rights.

Press the keys Windows+X, in the pop-up menu select Command Prompt (Admin). If your account has enough access rights, a new run command prompt will appear. In the new Command Prompt (Admin) type NETSH WLAN show drivers, the results should be similar to above.

If this failed as well, you cannot proceed with the current account, you either need to sign in with another account or ask your system administrator to perform this task for you.

How to setup the hotspot

In the Command Prompt enter the following command:

NETSH WLAN set hostednetwork mode=allow ssid=BYTEFREAKS key=0123456789

Update ssid=BYTEFREAKS with the name that you want to give your network. e.g. ssid=MY_NETWORK.

Update key=0123456789 with the password that you want to give your network. e.g. ssid=y0m2ZSQ3ng.

The new network will use WPA/WPA2 PSK security policy so your password needs to be at least 8 characters long.

The results will be similar to the following block.

C:\Users\bytefreaks>NETSH WLAN set hostednetwork mode=allow ssid=HIDDEN007 key=0123456789
The hosted network mode has been set to allow.
The SSID of the hosted network has been successfully changed.
The user key passphrase of the hosted network has been successfully changed.

How to activate the hotspot

Once the Hosted Network is created, enter the following command as is to activate it

NETSH WLAN start hostednetwork

C:\Users\bytefreaks>NETSH WLAN start hostednetwork
The hosted network started.

After this step, your network will be visible to connect to but it will not provide its users with internet access.

Please note that due to the lack of commands in the documentation (https://msdn.microsoft.com/en-us/library/windows/desktop/dd815243(v=vs.85).aspx) it is not possible to prevent your hotspot from broadcasting its SSID to everyone. In other words, it is not possible to hide your network from other users, so use a strong password!

hotspot-creation-commands

How to share internet connection with the hotspot

Press on the keyboard Windows+X to open the Power User menu, and select Network Connections.

power-user-menu

You will notice that a there is a new device in this list. The name of this device will be something line Local Area Connection* 12. That device is the new virtual device you created in the previous step to create the hotspot.

network-configuration-before-sharing

Right-click the other network adapter, that has an active internet connection and select Properties.

network-configuration-right-click-device

Click on the Sharing tab.

network-configuration-share-settings

Enable the Allow other network users to connect through this computer's Internet connection option.

From the Home networking connection drop-down menu select the virtual device that we created.

Click OK to close the configuration and apply the changes.

By completing this step, all devices connected to your Hotspot will have access to the internet via the connection of the second network device.