Skip to content

Instantly share code, notes, and snippets.

@rrmistry
Last active November 22, 2024 21:21
Show Gist options
  • Save rrmistry/04d282751122fd41fc5ebb9962e60602 to your computer and use it in GitHub Desktop.
Save rrmistry/04d282751122fd41fc5ebb9962e60602 to your computer and use it in GitHub Desktop.
Run Ubuntu VM using Qemu in Windows User Space (without admin rights)
$ErrorActionPreference = "Stop"
If (-not (Get-Command -Name "scoop" -ErrorAction "SilentlyContinue")) {
# Install Scoop
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
Invoke-RestMethod get.scoop.sh | Invoke-Expression
}
If (-not (Get-Command -Name "qemu-system-x86_64" -ErrorAction "SilentlyContinue")) {
# Install qemu
scoop install qemu
}
# Define the local path where the ISO will be saved
$ubuntuIsoPath = "C:\temp\linux.iso"
$isNewBootDisk = $false
If (-not (Test-Path -Path $ubuntuIsoPath -ErrorAction "SilentlyContinue")) {
# Define the URL for Ubuntu 24.02 LTS ISO
# # # $isoUrl = "https://dl-cdn.alpinelinux.org/alpine/v3.20/releases/x86_64/alpine-standard-3.20.2-x86_64.iso"
# # # $isoUrl = "https://dl-cdn.alpinelinux.org/alpine/v3.20/releases/x86_64/alpine-virt-3.20.2-x86_64.iso"
# # # $isoUrl = "https://releases.ubuntu.com/noble/ubuntu-24.04-live-server-amd64.iso"
$isoUrl = "https://mirror.fsmg.org.nz/ubuntu-releases/24.04/ubuntu-24.04-desktop-amd64.iso"
# Download the ISO file
Invoke-WebRequest -Uri $isoUrl -OutFile $ubuntuIsoPath -Verbose
Write-Host "Download completed! The ISO is saved at: $ubuntuIsoPath" -ForegroundColor "Green"
} Else {
Write-Host "The ISO file already exists at: $ubuntuIsoPath" -ForegroundColor "Green"
}
# Define paths and settings
$qemuPath = "qemu-system-x86_64.exe" # Adjust this path to where your QEMU executable is located
$qemuImgPath = "qemu-img.exe" # Adjust this path to where your QEMU image utility is located
$diskImagePath = "C:\temp\ubuntu.qcow2" # Path where the VM's disk image will be created
$memorySize = "2048M" # Memory size (e.g., 2048M for 2GB)
$cpuCount = 8 # Number of CPUs
If (-not (Test-Path -Path $diskImagePath -ErrorAction SilentlyContinue)) {
& $qemuImgPath create -f "qcow2" $diskImagePath "20G"
$isNewBootDisk = $true
}
# Run QEMU with the specified settings and setup Ubuntu automatically
$argumentList = @(
"-m", $memorySize,
"-smp", $cpuCount,
"-drive", "file=$diskImagePath,format=qcow2,if=virtio",
"-boot", "d",
"-netdev", "user,id=mynet0,hostfwd=tcp::8022-:22,hostfwd=tcp::2375-:2375,hostfwd=tcp::2376-:2376,hostfwd=tcp::5900-:5900,hostfwd=tcp::8000-:8000",
"-device", "virtio-net,netdev=mynet0",
"-nographic",
"-no-reboot"
)
If ($isNewBootDisk) {
$argumentList += "-cdrom"
$argumentList += $ubuntuIsoPath
}
# Run QEMU
Start-Process -FilePath $qemuPath -ArgumentList $argumentList -NoNewWindow -Wait -PassThru
# Install VS Code CLI
apk add gcompat libstdc++ curl
curl -sL "https://code.visualstudio.com/sha/download?build=stable&os=cli-alpine-x64" --output /tmp/vscode-cli.tar.gz \
&& tar -xf /tmp/vscode-cli.tar.gz -C /usr/bin \
&& rm /tmp/vscode-cli.tar.gz
# Setup script for quick access to code server
echo "code serve-web --host 0.0.0.0 --port 8000 --without-connection-token --accept-server-license-terms" > ~/code-server.sh
chmod u+x ~/code-server.sh
# Edit sshd_config
vi /etc/ssh/sshd_config
# # Change this line
# PermitRootLogin yes
# AllowTcpForwarding yes
# Restart sshd service
rc-service sshd restart
# Install docker
apk add docker
apk add docker-cli-compose
# Run docker daemon in the background
addgroup ${USER} docker
rc-update add docker default
service docker start
rc-update add cgroups
# Install Python
apk add python3 py3-pip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment