Ubuntu 20.04 Virtual Machine Building Script

スポンサーリンク
スポンサーリンク

At first

Learn about the script for creating Ubuntu20.04 virtual machines on your Mac.

The script you are using is published as vm-configure on github, so please download it from github or download it from here.

See the link below for the vm-configure folder configuration.

VirtualBox and Vagrant are required to run the script.If it is not installed, please refer to the link below.

Flow of processing

When you run /vm-configure/host-osx/ubuntu-20.04/vm-create.command, you are calling scripts and configuration files in the following order:

  1. Launch Vagrant with vm-create.command
  2. Running /guest-share/ubuntu-20.04/configure.sh with Vagrant provisioning
  3. Running /guest-share/ubuntu-20.04/docker-start.sh with Vagrant provisioning
  4. run com.collbow.vagrant.sh from launchd in vm-create.command

/host-osx/ubuntu-20.04/vm-create.command

This is the main script for building Ubuntu 20.04 virtual machines.

The location of the script file is in the current directory.

SCRIPT_DIR=$(dirname "$0")
cd "$SCRIPT_DIR"

The execution result of the script is set to be output to the file (vm-create.command.log) with .log after the execution script file name, and the execution start date and time are output to the log file.

exec &>>(tee "${0}.log")
date "+%Y/%m/%d %H:%M:%S"

Vagrant’s Box file (the template for virtual terminals for Vagrant) and the directory on which VirtualBox virtual machines are stored VM_BASE_DIR set to default.

# Directory to store vagrant box and virtual machine
VM_BASE_DIR=$(cd "${SCRIPT_DIR}/../" && pwd)

You can change the location of Vagrant’s Box file VAGRANT_HOME environment variables and settings.

The script is writing the .bash_profile to VAGRANT_HOME bash initialization file as an environment variable.

If you want to change the destination, please change the commented out part with # to the state below.

# --------------------------------------------------
# Change the save destination of vagrant box files to the VM_BASE_DIR folder.
# If you don't want to change the save folder, please comment out.
if [ -e "${HOME}/.bash_profile" ]; then
    sed -i _cb.bak -e "/^export VAGRANT_HOME=.*$/d" "${HOME}/.bash_profile"
    rm "${HOME}/.bash_profile_cb.bak"
fi
echo 'export VAGRANT_HOME="'"${VM_BASE_DIR}"'/.vagrant.d"' >>"${HOME}/.bash_profile"
# --------------------------------------------------

Changing the virtual machine location of VirtualBox.

If you want to change the destination, please change the commented out part with # to the state below.

# --------------------------------------------------
# Change the save destination of vagrant box files to the VM_BASE_DIR folder.
# If you don't want to change the save folder, please comment out.
vboxmanage setproperty machinefolder "${VM_BASE_DIR}/VirtualBox VMs"
# --------------------------------------------------

The location of Vagrant’s Box file is determined VAGRANT_HOME environment variables.The environment variables stored in the bash initialization file are re-loaded and activated with echo 'export VAGRANT_HOME="'"${VM_BASE_DIR}"'/.vagrant.d"' >>"${HOME}/.bash_profile".

if [ -e "${HOME}/.bash_profile" ]; then
    . "${HOME}/.bash_profile"
fi

We are initializing Vagrant to run in the current directory in vagrant init. vagrant up is loading the Vagrant file in the current directory and starting the virtual machine.

Vagrant init
Vagrant up

Virtual machines built on macOS can be killed if the host macOS is shut down while the virtual machine is running, and files may be corrupted.

To prevent this, we have registered a script to monitor the macOS status to launch the macOS to launch the virtual machine automatically when macOS starts and the virtual machine also shuts down automatically when macOS shuts down.

For more information, please refer to the link below.

chmod u+x com.collbow.vagrant.sh
sed 's|\[SCRIPT_DIR\]|'"${SCRIPT_DIR}"'|g' "com.collbow.vagrant.plist" > "${HOME}/Library/LaunchAgents/com.collbow.vagrant.plist"
launchctl load "${HOME}/Library/LaunchAgents/com.collbow.vagrant.plist"
launchctl start com.collbow.vagrant

/host-osx/ubuntu-20.04/Vagrantfile

Vagrantfile is a file that describes the configuration required to build a virtual machine in Vagrant.

VAGRANTFILE_API_VERSION api version of the Vagrantfile to be used in the configuration.

VAGRANTFILE_API_VERSION = "2"

You can include definitions of multiple virtual machines between Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| to end.

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  ...
end

Defines the configuration of the virtual machine to build between config.vm.define :vm1 do |vm1| and end.

If you want to define multiple virtual machines, you can configure multiple virtual machines in a single Vagrantfile by repeating the definition with vm1 as vm2, etc.

  config.vm.define :vm1 do |vm1|
    ...
  end

Specify the name of the Box of Vagrant to use.

You can create your own Box files as templates for virtual machines, but you can easily build virtual machines using Box files published to Vagrant Cloud.

This time, Chef Software has specified a Box file for Ubuntu 20.04 published to Vagrant Cloud.

    vm1.vm.box = "bento/ubuntu-20.04"

Specifying the IP address to be set for the virtual machine.

    vm1.vm.network "private_network", ip: "192.168.33.101"

Host OS”./. /.. /guest-share/ubuntu-20.04″Directory is specified to be synchronized to the guest OS’s “/home/vagrant/share” directory.

By synchronizing, you can share and use files on the host OS and guest OS.

    vm1.vm.synced_folder "./../../guest-share/ubuntu-20.04", "/home/vagrant/share"

Provisioning Vagrant specifies that the script should be run when the virtual machine is initialized.

Here we specify to run “configure.sh” to configure Ubuntu in a directory synchronized with the host OS and “docker-start.sh” to configure docker containers.

See the link below for the Docker container build script.

    vm1.vm.provision :shell, inline: "bash \"/home/vagrant/share/os/configure.sh\""
    vm1.vm.provision :shell, inline: "bash \"/home/vagrant/share/docker-start.sh\""

Setting up the virtual machine you want to create in VirtualBox.

ItemContents
nameName of the virtual machine
cpusThe number of CPUs to assign to the virtual machine
memoryThe amount of memory to be allocated to the virtual machine
customizeCustomization settings.
You can set the parameters that can be set in VBoxManage of VirtualBox.
See here for the parameters that can be set.
modifyvmCommand to change the virtual machine
–graphicscontrollerGraphics controller for use with virtual machines
    vm1.vm.provider "virtualbox" do |vb|
      vb.name = "ubuntu-20.04"
      vb.cpus = 2
      vb.memory = "4096"
      vb.customize ['modifyvm', :id, '--graphicscontroller', 'VMSVGA']
    end

Comment

スポンサーリンク