Create a raw disk.img file, wrap it as VMDK, attach it to VirtualBox as sda, and configure SSH port forwarding.
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.
Allocate a 30 GB raw binary file. This file will act as a physical disk โ every byte maps 1:1 to a disk sector.
# Using fsutil to create a 30 GB zero-filled file fsutil file createnew C:\VMs\disk.img 32212254720 # 32212254720 = 30 * 1024 * 1024 * 1024 bytes
mkdir C:\VMs# 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
# 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
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.
VirtualBox needs a .vmdk descriptor file that points to the raw image. Use the VBoxManage command-line tool.
# 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"
VBoxManage internalcommands createrawvmdk requires Administrator privileges and the file path to the raw image must be an absolute path.VBoxManage internalcommands createrawvmdk \ -filename "$HOME/VMs/disk.vmdk" \ -rawdisk "$HOME/VMs/disk.img"
VBoxManage is typically at /usr/local/bin/VBoxManage or inside the VirtualBox.app bundle at /Applications/VirtualBox.app/Contents/MacOS/VBoxManage.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
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.
Add the VMDK as the primary SATA/SCSI disk so it appears as /dev/sda inside the guest.
/dev/sda inside the guest).# 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
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"
lsblk or fdisk -l โ you should see a 30 GB disk at /dev/sda.
VirtualBox NAT networking allows port forwarding rules so you can SSH into the guest from the host.
Port 2222
Connect from here
Port 22
SSH daemon listens here
sudo apt install openssh-server (Debian/Ubuntu).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)
VBoxManage modifyvm "MyVM" \ --natpf1 "ssh,tcp,,2222,,22" # Format: "name,protocol,hostip,hostport,guestip,guestport"
ssh -p 2222 youruser@127.0.0.1 # Or using PuTTY: host=127.0.0.1, port=2222
ssh -p 2222 youruser@127.0.0.1
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
ssh-keygen -R "[127.0.0.1]:2222".