This project was forked from evansm7/vftool, which is a simple tool to run virtual machines on macOS using the Virtualization.framework.
Here you can find a written guide to running Linux virtual machine on macOS.
Before You Begin • Getting Started • Troubleshooting • References
Please find the original README from the upstream here. I am using Ubuntu Server in this guide, feel free to use your favourite distro.
-
Build the project.
-
make
-
-
Download the image, initrd (initial ramdisk) and kernel from this site into the
buildfolder. For example,-
# image jammy-server-cloudimg-arm64.tar.gz tar xvfz jammy-server-cloudimg-arm64.tar.gz # initrd ubuntu-22.04-server-cloudimg-arm64-initrd-generic # kernel ubuntu-22.04-server-cloudimg-arm64-vmlinuz-generic
-
-
Unzip the kernel.
-
mv /path/to/kernel /path/to/kernel.gz gunzip /path/to/kernel.gz
-
-
First, start the virtual machine without specifying a root. This allows us to run some basic setup such as creating a root mount point, disabling cloud init, setting a root password and a static IP.
-
./vftool \ -k /path/to/kernel \ -i /path/to/initrd \ -d /path/to/image.img \ -m 8192 \ -a "console=hvc0"
-
-
Look for the indicated terminal input device (e.g.
Waiting for connection to: /dev/ttys001), and connect to it from another terminal session.-
screen /dev/ttys001
-
-
Wait for the virtual machine to finish booting up with
initramfsprompt.-
... ... ... (initramfs)
-
-
Create a root mount point.
-
mkdir /mnt mount /dev/vda /mnt chroot /mnt
-
-
Disable cloud init.
-
touch /etc/cloud/cloud-init.disabled
-
-
Set the root password to
root.-
echo 'root:root' | chpasswd
-
-
Set a static IP.
-
cat <<EOF> /etc/netplan/01-dhcp.yaml network: ethernets: enp0s1: dhcp4: true addresses: [192.168.64.18/20] version: 2 EOF
-
-
Exit and unmount.
-
exit umount /dev/vda
-
-
Manually kill the virtual machine with
Ctrl+Cfrom the original terminal session.
-
Allocate storage space to the virtual machine by resizing the image. In this example, I am allocating 32GB to the virtual machine.
-
dd if=/dev/zero bs=1m count=32000 >> /path/to/image
-
-
Start the virtual machine with a root specified this time.
-
./vftool \ -k /path/to/kernel \ -i /path/to/initrd \ -d /path/to/image.img \ -p 4 \ -m 8192 \ -a "root=/dev/vda rw console=hvc0"
-
-
Repeat Step 2 in Setup the Virtual Machine and login as
root. -
Check if the image resizing worked.
-
df -h | grep vda
-
-
If the size is not what specified in Step 1, check if the
ddcommand worked.-
parted (parted) print devices (parted) quit
-
-
If
/dev/vda/shows the correct size allocated in Step 1, resize the partition to the max.-
resize2fs /dev/vda
-
-
Repeat Step 4, and you should now see the correct allocated size for
/dev/vda.
-
Before we continue any further, instead of using
rootto access the virtual machine, let's create a new user and set a password.-
adduser <YOUR_USERNAME>
-
-
Add this user to the
sudogroup.-
usermod -aG sudo <YOUR_USERNAME>
-
-
Login as the new user.
-
su - <YOUR_USERNAME>
-
My intention here is to use the remote development feature from the IDE.
-
Re-install
openssh-server.-
sudo apt update sudo apt --reinstall install openssh-server
-
-
Disable the
ssh.socket.-
sudo systemctl disable ssh.socket --now
-
-
Enable the
ssh.service.-
sudo systemctl enable ssh.service --now
-
-
Assert that the
ssh.serviceis enabled and running.-
sudo systemctl status sshd
-
-
On your local machine, generate a SSH key and add the public key to the
~/.ssh/authorized_keysfile in the virtual machine. -
Now you should be able to SSH into the virtual machine using the static IP specified in Step 7 of Setup the Virtual Machine.
-
ssh -i /path/to/private-key <YOUR_USERNAME>@192.168.64.18
-
-
Install a few prerequisite packages.
-
sudo apt update sudo apt install apt-transport-https ca-certificates curl software-properties-common
-
-
Add Docker repository to APT sources:
-
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
-
-
Install
docker-ce.-
sudo apt update apt-cache policy docker-ce sudo apt install docker-ce
-
-
Assert that
dockeris enabled and running.-
sudo systemctl status docker
-
-
Add the current user to the
dockergroup, so that Docker can be managed by a non-root user.-
sudo usermod -aG docker $USER newgrp docker
-
-
Assert that images can be downloaded from the Docker hub.
-
docker run --rm hello-world
-
-
A static IP was set in Step 7 of Setup the Virtual Machine.
-
On your local machine, point this IP to your favourite hostname in the
/etc/hostsfile. For example:-
192.168.64.18 your.favourite.hostname
-
-
Now you can access the virtual machine using this hostname.
-
Occasionally, you might run into a situation where
/dev/vdais re-mounted as read-only due to filesystem errors. -
In this case, use
fsckto fix the filesystem errors.-
sudo fsck -y /dev/vda
-
-
Shut down the virtual machine properly and restart it.
-
sudo shutdown now
-