Thursday, April 22, 2021

Install Ubuntu OS on the VirtualBox VM Server

Windows 10
Virtual Box
Ubuntu Server 20.04


Goals:
  • Install a Ubuntu OS on a Virtual Machine on another OS, like Windows or MacOS.

Install:
  • Download VirtualBox.
  • Install Virtual Box.
  • Configure a VirtualBox Virtual Machine for Ubuntu:
    • In VirtualBox click New
    • Set a Name for the virtual machine (e.g. Ubuntu 20.04.2)
    • Set the Type as Linux and the Version as Ubuntu (64-bit)
    • Set the VM's Memory size:  (e.g. 4GB)
    • Select Create a virtualized disk now then Create
    • Check the default VDI is selected
    • Select Dynamically allocated or Fixed for the virtual hard disk size and Create (zzz...)
    • Click on the virtual machine created
    • Click on Settings -> System -> Processor
    • Set the number of processors:  (e.g. 3)
    • Click on Display -> Screen -> Video Memory
    • Set the Video Memory: (e.g. 64MB)
    • Click Ok
  • Install the Ubuntu OS:
    • Select the VM created above
    • Click Settings
      • Click Storage
        • Select Controller IDE
        • In the Attributes pane click the disc icon next to IDE Secondary Master
        • Click Choose a disk file and browse for the Ubuntu ISO

        • Click OK to add the ISO then OK to finish
      • Click Network
        •  In the Adapter 1 tab,  on the Attached to combo, select Bridged Adapter and click OK.


  • Install VirtualBox Extension Pack on Windows:
    • Select Tools -> Preferences:
      • In the Preferences window, go to the Extensions section. Click the Add a new package button to add the extension pack.
      • Browse the file you have downloaded
      • Install it
  • Configure Copy and Paste:
    • Select the VM
    • Select General -> Advanced -> Shared Clipboard

  • Boot Ubuntu in the Virtual Machine:
    • Select the VM and click Start.
  • Identifies a folder which VirtualBox will look in to find VirtualBox.xml:
    • Menu -> File -> Preferences
    • Select General -> Default Machine Folder
    • OR:
    • Create an environment variable set (globally, or for the current user of the host) called VBOX_USER_HOME
  • Configure Display Resolution:
    • Open a terminal window:
      • sudo apt-get install virtualbox-guest-dkms
    • Restart Ubuntu:
      • sudo reboot
    • Open Settings -> Displays -> Change Resolution



Extra Tools:
  • How to add an existing VirtualBox VM to the VirtualBox Manager:
    • Click on 'Tools' and then click on 'Add' button
    • Choose the path to the '.vbox' file and click on 'Open' button.

  • How to increase the size of the VirtualBox disk drive:
    • First we have to stop (powered off) the VM.
    • Increase the Size using a Windows CMD terminal:
      • vboxmanage modifyhd "/Users/marcus/VirtualBox VMs/ubuntu2004/ubuntu2004.vdi" --resize <new-size-in-MB>
    • Or, we can use the VirtualBox GUI:
      • menu File -> Virtual Media Manager, then double click a virtual hard disk in the list and use the “Size” slider at the bottom of the window to change its size. Click “Apply” when you’re done.
    • We will still have to enlarge the partition on the disk to take advantage of the additional space. The partition remains the same size even while the disk size increases. We might use the linux  gparted  application to extend the partition size, if it's not a LVM partition.

  • How to convert dynamically sized VirtualBox VDI hard drive to fixed size:
    • Find the location of your actual VDI HD:
    • Open a terminal window and clone your hard drive to a fixed size:
      • vboxmanage clonehd "/Users/marcus/VirtualBox VMs/ubuntu2004/ubuntu2004.vdi" "/Volumes/Seagate Exp/ubuntu2004-fixed.vdi" --variant Fixed
        • 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
        • Clone medium created in format 'VDI'. UUID: ...
    • Open the virtual box manager, go to settings for your machine, remove the old (dynamically sized) drive from the SATA controller for this machine and click Ok.
    • Run the following command in a terminal window:
      • vboxmanage list hdds
      • Make sure to have a backup of your old drive file, just in case. The next step will permanently delete this file.
      • Remove your old drive from the list by running the command:
        • vboxmanage closemedium disk <UUID> --delete
    • Copy your new fixed size VDI HD to the same directory of the old one:
      • cp "/Volumes/Seagate Exp/ubuntu2004-fixed.vdi" "/Users/marcus/VirtualBox VMs/ubuntu2004/ubuntu2004.vdi"
    • Go to the VirtualBox Manager and add the new fixed size VDI HD to the SATA controller for your VM.
    • Start your VM with the new fixed size VDI HD.

  • How to Resize and Extend a LVM Partition:
    • Using a terminal window:
      • sudo pvs
      • df -h
      • sudo lvextend -r -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
      • sudo reboot
      • df -h



  • How to Clone a Snapshot:



    • VirtualBox’s  clonehd  will not let you clone a snapshot other then the base. This is due to the fact that snapshots are simply “difference lists” between the previous snapshot and the current state. Whenever you take a snapshot VirtualBox will “freeze” the current state as a read-only VDI file and create a new VDI where it will save only the blocks that the VM has written too after the snapshot.
    • In order to clone a snapshot VirtualBox will need to first consolidate all the changes between all the previous snapshots.
    • what we can do is to force VirtualBox to consolidate “Current State” back into the base snapshot "Snapshot 1" (“ubuntu2004.vdi” in the above screenshot) by discarding the base snapshot:
      • Open the “Snapshots” tab for the VM whose state you want to clone, select the first snapshot “Snapshot 1” and choose “delete” from the toolbar. What really happens is that VirtualBox pretends to remove “Snapshot 1” but it actually removes “Current State” after copying all the changes in “Current State” back into “Snapshot 1”. You now have a base snapshot which contains the state in “Current State” which you can clone.

  • How to Get rid of cloud-init, when installing Ubuntu Server:
    • echo 'datasource_list: [ None ]' | sudo -s tee /etc/cloud/cloud.cfg.d/90_dpkg.cfg
    • sudo apt-get purge cloud-init
    • sudo rm -rf /etc/cloud/
    • sudo rm -rf /var/lib/cloud/
    • sudo shutdown -r now


References: