VirtualBox Setup Guide

Raw Disk Image
for VirtualBox

Create a raw disk.img file, wrap it as VMDK, attach it to VirtualBox as sda, and configure SSH port forwarding.

โŠž Windows macOS ๐Ÿง Linux
โš ๏ธ

Why NOT a VDI file?

You cannot use a regular VDI disk for this workflow. VDI is VirtualBox's proprietary container format โ€” it is not a raw byte-for-byte disk image. You cannot mount it directly on the host, partition it with fdisk/parted, loop-mount individual partitions, write a bootloader to a specific offset, or use it with tools like dd, losetup, or kpartx. A raw .img file behaves exactly like a physical disk, which is required for all subsequent operations on the image.

01

Create the 30 GB raw disk image

Allocate a 30 GB raw binary file. This file will act as a physical disk โ€” every byte maps 1:1 to a disk sector.

PowerShell (Run as Administrator)
# Using fsutil to create a 30 GB zero-filled file
fsutil file createnew C:\VMs\disk.img 32212254720

# 32212254720 = 30 * 1024 * 1024 * 1024 bytes
Note: Create the C:\VMs\ directory first if it doesn't exist: mkdir C:\VMs
Terminal
# Create the directory and the raw image
mkdir -p ~/VMs

dd if=/dev/zero of=~/VMs/disk.img bs=1m count=30720

# Alternatively, use truncate (faster โ€” sparse file)
# truncate -s 30G ~/VMs/disk.img
Bash
# Create directory
mkdir -p ~/VMs

# Fast sparse allocation (recommended)
truncate -s 30G ~/VMs/disk.img

# Or full zero-fill with dd (slow but no sparse)
# dd if=/dev/zero of=~/VMs/disk.img bs=1M count=30720 status=progress
๐Ÿ’ก sparse vs. full: truncate creates a sparse file that doesn't immediately consume 30 GB of real disk space โ€” blocks are allocated on write. Use dd if you need a fully pre-allocated image.
02

Create a VMDK wrapper for disk.img

VirtualBox needs a .vmdk descriptor file that points to the raw image. Use the VBoxManage command-line tool.

PowerShell (Run as Administrator โ€” required)
# Add VBoxManage to PATH (adjust version if needed)
$env:Path += ";C:\Program Files\Oracle\VirtualBox"

VBoxManage internalcommands createrawvmdk `
  -filename "C:\VMs\disk.vmdk" `
  -rawdisk  "C:\VMs\disk.img"
Important: On Windows, VBoxManage internalcommands createrawvmdk requires Administrator privileges and the file path to the raw image must be an absolute path.
Terminal
VBoxManage internalcommands createrawvmdk \
  -filename "$HOME/VMs/disk.vmdk" \
  -rawdisk  "$HOME/VMs/disk.img"
macOS path: VBoxManage is typically at /usr/local/bin/VBoxManage or inside the VirtualBox.app bundle at /Applications/VirtualBox.app/Contents/MacOS/VBoxManage.
Bash
VBoxManage internalcommands createrawvmdk \
  -filename "$HOME/VMs/disk.vmdk" \
  -rawdisk  "$HOME/VMs/disk.img"

# If permission denied on the raw file:
sudo chown $USER:$USER ~/VMs/disk.img
๐Ÿ“„ The generated disk.vmdk is just a small text descriptor โ€” it contains no data itself. It tells VirtualBox: "treat disk.img as the raw storage backend." You can open disk.vmdk in a text editor to inspect it.
03

Attach disk.vmdk to the VM as sda

Add the VMDK as the primary SATA/SCSI disk so it appears as /dev/sda inside the guest.

Option A โ€” VirtualBox GUI

Option B โ€” Command line (all platforms)

PowerShell
# Replace "MyVM" with your actual VM name
$VM = "MyVM"
$VMDK = "C:\VMs\disk.vmdk"

# Add SATA controller if it doesn't exist yet
VBoxManage storagectl $VM `
  --name "SATA Controller" `
  --add sata `
  --controller IntelAhci

# Attach VMDK at Port 0 (= /dev/sda)
VBoxManage storageattach $VM `
  --storagectl "SATA Controller" `
  --port 0 `
  --device 0 `
  --type hdd `
  --medium $VMDK
Bash (macOS / Linux)
VM="MyVM"
VMDK="$HOME/VMs/disk.vmdk"

# Add SATA controller (skip if one already exists)
VBoxManage storagectl "$VM" \
  --name "SATA Controller" \
  --add sata \
  --controller IntelAhci

# Attach as Port 0 โ†’ appears as /dev/sda in guest
VBoxManage storageattach "$VM" \
  --storagectl "SATA Controller" \
  --port 0 \
  --device 0 \
  --type hdd \
  --medium "$VMDK"
๐Ÿ”Ž To verify: inside the running guest, run lsblk or fdisk -l โ€” you should see a 30 GB disk at /dev/sda.

04

SSH Port Forwarding โ€” host 2222 โ†’ guest 22

VirtualBox NAT networking allows port forwarding rules so you can SSH into the guest from the host.

opis
opis
opis

Host (your machine)

Port 2222

Connect from here

Guest (VM)

Port 22

SSH daemon listens here

Prerequisites

Option A โ€” GUI

Option B โ€” Command line

PowerShell
VBoxManage modifyvm "MyVM" `
  --natpf1 "ssh,tcp,,2222,,22"

# Format: "name,protocol,hostip,hostport,guestip,guestport"
# Leaving hostip/guestip blank means 0.0.0.0 (all interfaces)
Bash (macOS / Linux)
VBoxManage modifyvm "MyVM" \
  --natpf1 "ssh,tcp,,2222,,22"

# Format: "name,protocol,hostip,hostport,guestip,guestport"

Connecting via SSH

PowerShell / CMD (OpenSSH built-in)
ssh -p 2222 youruser@127.0.0.1

# Or using PuTTY: host=127.0.0.1, port=2222
Terminal
ssh -p 2222 youruser@127.0.0.1
Terminal
ssh -p 2222 youruser@127.0.0.1

# Or add to ~/.ssh/config for convenience:
# Host myvm
#   HostName 127.0.0.1
#   Port 2222
#   User youruser
# Then just: ssh myvm
๐Ÿ”‘ First-time connection will ask you to accept the host key fingerprint โ€” type yes. If you rebuild the guest and get a key mismatch warning, remove the old entry with ssh-keygen -R "[127.0.0.1]:2222".