How to Automate VM Building on Windows Machine — Part 2

Sathiyaraj Sridhar
2 min readApr 21, 2024

--

This article guides you through configuring a VM, including tasks like increasing CPU and memory and setting a private static IP address, etc.

This continues from the previous article on automating VM building (Part 1). In this section, we’ll explore configuring the VM, covering adjustments to CPU, memory, and hostname, as well as setting a static private IP address.

Configuring VM

As you may remember, we constructed the VM using the fedora/39-cloud-base box. If you’ve created a VM using a different Vagrant box, take a moment to comprehend how I tailored the Fedora 39 Cloud Base VM. Then, determine if similar adjustments are required.

Let’s move forward with the customization

To create a backup of the current Vagrantfile generated when you execute the vagrant init box-name command, run the following command:

cd project-dir
cp Vagrantfile Vagrantfile.orig

Please run the following command, and it will modify the existing Vagrantfile with the specified VM specifications.

cat <<EOF | tee Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

config.vm.box = "fedora/39-cloud-base"
config.vm.box_version = "39.20231031.1"
config.vm.hostname = "fedora.vm.compute.internal"
config.vm.network "private_network", ip: "10.1.1.5", auto_config: false
config.vm.synced_folder ".", "/vagrant", disabled: true
config.vm.provider "virtualbox" do |vb|
vb.name = "fedora.vm.compute.internal"
vb.cpus = 2
vb.memory = "2048"
vb.check_guest_additions = false
end

end
EOF

From the above command content, you could observe how I configure CPU, memory, hostname, private IP address, and more. Feel free to customize it according to your needs.

Feel free to explore additional settings available in Vagrant’s official documentation.

Configuring Static Private IP Address

As you observed in the private network configuration outlined above, I specified auto_config: false. Typically, I opt for auto_config: true, which automatically assigns the private IP address. However, in this VM instance, the private IP address remains unset. Upon debugging, I identified alterations in the network connection profile format.

Before proceeding with this setup, you can verify whether a static IP is assigned or not by running the ifconfig command inside the VM.

Vagrant includes a connection profile in ifcfg format within the /etc/sysconfig/network-scripts/ path. However, this format is deprecated. NetworkManager now stores new network profiles in keyfile format within the /etc/NetworkManager/system-connections/ directory.

Let’s proceed to create a network profile for our private static IP in keyfile format.

SSH into the VM and execute the following command:

cat <<EOF | sudo tee /etc/NetworkManager/system-connections/eth1.ini
[connection]
id=eth1_connection
uuid=727cb087-42ce-3ffa-aea3-83bf907feb21
type=802-3-ethernet
interface-name=eth1
autoconnect=true

[ethernet]

[ipv4]
address1=10.1.1.5/24
method=manual

[ipv6]
addr-gen-mode=default
method=auto

EOF

In this configuration, I’ve enabled IPv4 and disabled IPv6 address.

Now, modify the permissions of the /etc/NetworkManager/system-connections/eth1.ini file.

sudo chmod 600 /etc/NetworkManager/system-connections/eth1.ini

Finally, exit from the VM and reload it.

exit
vagrant reload

Now, you could see that the 10.1.1.5 IPv4 address is assigned to the eth1 network interface.

I hope it helps you. Thank you.

--

--