Thanks to visit codestin.com
Credit goes to github.com

Skip to content

A guide to setup Linux virtual machine using a simple macOS Virtualisation.framework wrapper

License

Notifications You must be signed in to change notification settings

chintiongtan/linux-on-mac

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Linux on Mac

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 BeginGetting StartedTroubleshootingReferences

Before You Begin

Please find the original README from the upstream here. I am using Ubuntu Server in this guide, feel free to use your favourite distro.

Getting Started

Pre-requisite

  1. Build the project.

    • make
  2. Download the image, initrd (initial ramdisk) and kernel from this site into the build folder. 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
  3. Unzip the kernel.

    • mv /path/to/kernel /path/to/kernel.gz
      gunzip /path/to/kernel.gz

Setup the Virtual Machine

  1. 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" 
  2. 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
  3. Wait for the virtual machine to finish booting up with initramfs prompt.

    • ...
      ...
      ...
      (initramfs)
  4. Create a root mount point.

    • mkdir /mnt
      mount /dev/vda /mnt
      chroot /mnt
  5. Disable cloud init.

    • touch /etc/cloud/cloud-init.disabled
  6. Set the root password to root.

    • echo 'root:root' | chpasswd
  7. 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
  8. Exit and unmount.

    • exit
      umount /dev/vda
  9. Manually kill the virtual machine with Ctrl+C from the original terminal session.

Allocating Storage Space

  1. 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
  2. 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" 
  3. Repeat Step 2 in Setup the Virtual Machine and login as root.

  4. Check if the image resizing worked.

    • df -h | grep vda
  5. If the size is not what specified in Step 1, check if the dd command worked.

    • parted
      (parted) print devices
      (parted) quit
  6. If /dev/vda/ shows the correct size allocated in Step 1, resize the partition to the max.

    • resize2fs /dev/vda
  7. Repeat Step 4, and you should now see the correct allocated size for /dev/vda.

Create a New User

  1. Before we continue any further, instead of using root to access the virtual machine, let's create a new user and set a password.

    • adduser <YOUR_USERNAME>
  2. Add this user to the sudo group.

    • usermod -aG sudo <YOUR_USERNAME>
  3. Login as the new user.

    • su - <YOUR_USERNAME>

Setup SSH

My intention here is to use the remote development feature from the IDE.

  1. Re-install openssh-server.

    • sudo apt update
      sudo apt --reinstall install openssh-server
  2. Disable the ssh.socket.

    • sudo systemctl disable ssh.socket --now
  3. Enable the ssh.service.

    • sudo systemctl enable ssh.service --now
  4. Assert that the ssh.service is enabled and running.

    • sudo systemctl status sshd
  5. On your local machine, generate a SSH key and add the public key to the ~/.ssh/authorized_keys file in the virtual machine.

  6. 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

Setup Docker (Optional)

  1. Install a few prerequisite packages.

    • sudo apt update
      sudo apt install apt-transport-https ca-certificates curl software-properties-common
  2. 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
  3. Install docker-ce.

    • sudo apt update
      apt-cache policy docker-ce
      sudo apt install docker-ce
  4. Assert that docker is enabled and running.

    • sudo systemctl status docker
  5. Add the current user to the docker group, so that Docker can be managed by a non-root user.

    • sudo usermod -aG docker $USER
      newgrp docker
  6. Assert that images can be downloaded from the Docker hub.

    • docker run --rm hello-world

Setup Virtual Host (Optional)

  1. A static IP was set in Step 7 of Setup the Virtual Machine.

  2. On your local machine, point this IP to your favourite hostname in the /etc/hosts file. For example:

    • 192.168.64.18  your.favourite.hostname
      
  3. Now you can access the virtual machine using this hostname.

Troubleshooting

  1. Occasionally, you might run into a situation where /dev/vda is re-mounted as read-only due to filesystem errors.

  2. In this case, use fsck to fix the filesystem errors.

    • sudo fsck -y /dev/vda
  3. Shut down the virtual machine properly and restart it.

    • sudo shutdown now

References

About

A guide to setup Linux virtual machine using a simple macOS Virtualisation.framework wrapper

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Objective-C 97.3%
  • Makefile 2.7%