Skip to content

Commit

Permalink
Add support for primary disk resizing
Browse files Browse the repository at this point in the history
- Update Vagrantfile to include disks method
- Update vagrant.yaml to include the new disks config
- Update lib/vagrant-host.rb with new disks method
- Update documentation, add disks.md section
- Update ansible roles, add new system/disks role
  • Loading branch information
theodore86 committed Dec 11, 2022
1 parent 49c5973 commit 714acc6
Show file tree
Hide file tree
Showing 17 changed files with 160 additions and 27 deletions.
3 changes: 3 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Vagrant.configure('2') do |config|
VirtualBox.guest_property(vb, HOST)
end

# -- Disks --#
Host.disks(node.vm, HOST)

#-- Networking --#
Host.networking(node.vm, HOST)

Expand Down
2 changes: 1 addition & 1 deletion docs/dev/ansible.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Ansible is an open-source [IaC](https://en.wikipedia.org/wiki/Infrastructure_as_code) tool, provides configuration management and application deployment. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows. Ansible is **agentless**, temporarily connecting remotely via SSH (*distributed mode*) or Windows Remote Management (allowing remote PowerShell execution) to do its tasks on the [managed nodes](https://docs.ansible.com/ansible/latest/network/getting_started/basic_concepts.html).

Despite the default behaviour (connecting remotely), ansible supports an connection type known as **[local](https://docs.ansible.com/ansible/latest/inventory/implicit_localhost.html)** connection (*centralized mode*). In such case Ansible will execute any tasks on the [control node](https://docs.ansible.com/ansible/latest/network/getting_started/basic_concepts.html). Ansibe (control or managed) nodes are organized in an file known as the [inventory]() file. Sometimes called a *hostfile*. Your inventory can specify information like IP address, credentials and variables for each node.
Despite the default behaviour (connecting remotely), ansible supports an connection type known as **[local](https://docs.ansible.com/ansible/latest/inventory/implicit_localhost.html)** connection (*centralized mode*). In such case Ansible will execute any tasks on the [control node](https://docs.ansible.com/ansible/latest/network/getting_started/basic_concepts.html). Ansibe (control or managed) nodes are organized in an file known as the [inventory](https://docs.ansible.com/ansible/latest//user_guide/intro_inventory.html) file. Sometimes called a *hostfile*. Your inventory can specify information like IP address, credentials and variables for each node.

!!! note
Vagrant [ansible_local](../vagrant/provisioners.md) provisioner type uses the *local* connection and not the *remote* (SSH) to execute any guest machine tasks. In such case the guest machine is acting as a *control node*.
Expand Down
79 changes: 79 additions & 0 deletions docs/vagrant/disks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Overview

Vagrant Disks is a feature that allows users to define what mediums should be attached to their guests, as well as allowing users to **resize** their primary disk.

# Supported providers

Currently, only **VirtualBox** is supported. Please refer to the [VirtualBox documentation](https://developer.hashicorp.com/vagrant/docs/disks/virtualbox) for more information on using disks with the VirtualBox provider!

# Customizations

This feature requires the following environmental flag to be used:

```console
export VAGRANT_EXPERIMENTAL="disks"
```

!!! info "Reload Vagrant"
You will need to reload vagrant from the command line for resizing to take affect.

Multiple disks can be added in the ``vagrant.yaml`` configuration file but only the disk with the ``:primary: true``
option is what tells to vagrant to *expand* the guests main drive. Without this option, vagrant will instead attach a *new*
disk to the guest.

```yaml
:host:
:disks:
- :type: 'disk'
:options:
:primary: true
:size: '80GB'
- :type: 'disk'
:options:
:size: '40GB
```
- If Vagrant is provisioned for the first time the [local ansible provisioner](../provisioners/#local-ansible-provisioner) will take care of</br> the logical volume and filesystem resizing.
- If Vagrant is **already provisioned** then you will need to execute:
```console
vagrant provision --provision-with sandbox
```
or
from inside the Vagrant box:
```console
cd /shared/provisioners/ansible
ansible-playbook -i inventory.yml playbook.yml --tags resize_disk
```
Supported options are:
- ``type``: The kind of the disk type, current accepts:
- *disk*
- *dvd*
- *flopppy*
- ``options``:
- ``primary``: Configures disk to be the *primary* disk t o manae on the guest.
- ``size``: The size of the disk to create. For example *10GB*. Not used for type *dvd*.
# Limitations
- Disks can only be **increased** in size. There is no facility to shrink a disk.
- Depending on the guest, you may need to resize the partition and the filesystem</br>from within the guest.
- Vagrant attaches all new disks defined to guests's primary controller. As of Virtualbox *6.1.x*</br>
storage controllers have the following limits to the number of disks that are supported per guest:

- IDE Controllers: 4
- SATA Controllers: 30
- SCSI Controllers: 16

# References

- [vagrant-disks](https://developer.hashicorp.com/vagrant/docs/disks)
- [vagrant-disksize-plugin](https://github.com/sprotheroe/vagrant-disksize)
- [increasing-disk-space-vagrant-box](https://marcbrandner.com/blog/increasing-disk-space-of-a-linux-based-vagrant-box-on-provisioning/)
3 changes: 1 addition & 2 deletions docs/vagrant/provisioners.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ Supported options are:
- [awscli](https://pypi.org/project/awscli) - Universal command line environment for AWS.

- ``:skip_tags:`` Only plays, roles and tasks that *do not match* these values will be executed.
- This parameter can be *string or list* of tags.

- This parameter can be *string or list* of tag
!!! info "More details on local ansible provisioner can be found at:"
- [Ansible on Sandbox](../dev/ansible.md)
- [Local Ansible Options](https://www.vagrantup.com/docs/provisioning/ansible_local)
Expand Down
1 change: 1 addition & 0 deletions docs/vagrant/vagrantfile.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The most important configuration options are listed below:
- ``config.vm.box_url``
- ``config.vm.customize``
- ``config.vm.define``
- ``config.vm.disk``
- ``config.vm.forward_port``
- ``config.vm.guest``
- ``config.vm.hostname``
Expand Down
14 changes: 7 additions & 7 deletions docs/vagrant/workflow.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
**Test and development** workflow using Vagrant includes:

- Workspace - Host OS (*Windows or Linux*)
- A Base Box (*Guest OS*)
- A Git repository (*Vagrantfile, Test Automation framework source code*)
- Git/Git BASH
- [Chocolatey](../../chocolatey/guide/#what-is-chocolatey) package manager (**only for Windows OS**)
- Command Line Emulator such as [Cmder](../../cmder/guide/#what-is-cmder) (**only for Windows OS**)
- IDE (*Sublime, Atom*, etc)
- System Under Test
- The (Ubuntu) Base Box (*Guest OS*)
- Git repository (*Vagrantfile*)
- Git or Git BASH
- [Chocolatey](../../chocolatey/guide/#what-is-chocolatey) package manager (**Windows OS**)
- Command Line Emulator such as [Cmder](../../cmder/guide/#what-is-cmder) (**Windows OS**)
- IDE (*Sublime, Atom, VSCode*, etc)
- System Under Test (*optional*)

Workflows:

Expand Down
16 changes: 16 additions & 0 deletions lib/vagrant-host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ def ansible_provision(vm, host)
vm.provision name, opts
end

def disks(vm, host)
return unless host.key?(:disks)

disks = host[:disks]
return unless disks.is_a?(Array)

disks.each_with_index do |disk, index|
next unless disk.key?(:type)

type = disk[:type].to_s.to_sym
opts = disk.fetch(:options, {})
opts[:name] ||= "storage-#{index}"
vm.disk type, opts
end
end

def post_up_message(vm, host)
return unless host.key?(:name)

Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ nav:
- 'System': 'vagrant/system.md'
- 'Provisioners': 'vagrant/provisioners.md'
- 'Synced Folders': 'vagrant/shared.md'
- 'Disks': 'vagrant/disks.md'
- 'Plugins': 'vagrant/plugins.md'
- 'Debugging': 'vagrant/debugging.md'
- 'Workflow': 'vagrant/workflow.md'
Expand Down
5 changes: 3 additions & 2 deletions provisioners/ansible/playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
name: 'common/system'
tags: 'always'
roles:
- { role: 'system/bash_aliases', tags: 'bash_aliases' }
- { role: 'system/bash_profile', tags: 'bash_profile' }
- { role: 'system/bash/aliases', tags: 'bash_aliases' }
- { role: 'system/bash/profile', tags: 'bash_profile' }
- { role: 'system/disks', tags: 'resize_disk' }
- { role: 'python/ipython', tags: 'ipython' }
- { role: 'python/tox', tags: 'tox' }
- { role: 'docker', tags: 'docker' }
Expand Down
20 changes: 7 additions & 13 deletions provisioners/ansible/roles/repos/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
---
repositories:
- name: 'vagrantenv'
src: 'git@github.com:theodore86/vagrantenv.git'
dest: '{{ ansible_env.HOME }}'
version: 'main'
pull: true
remote: 'origin'
- name: 'sublime'
src: 'git@github.com:theodore86/sublime.git'
dest: '{{ ansible_env.HOME }}'
version: 'master'
pull: true
remote: 'origin'
# - name: 'vagrantenv'
# src: 'git@github.com:theodore86/vagrantenv.git'
# dest: '{{ ansible_env.HOME }}'
# version: 'main'
# pull: true
# remote: 'origin'
repositories: []
...
7 changes: 7 additions & 0 deletions provisioners/ansible/roles/system/disks/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
disks_dependencies:
- 'lvm2'
disks_pvs: '/dev/sda3'
disks_vg_name: 'ubuntu-vg'
disks_lv_name: 'ubuntu-lv'
...
28 changes: 28 additions & 0 deletions provisioners/ansible/roles/system/disks/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
- name: 'Install disks dependencies'
apt:
name: '{{ disks_dependencies }}'
state: 'present'
force_apt_get: 'yes'
install_recommends: 'no'
become: 'yes'

- name: 'Resize volume-group {{ disks_pvs }} to the maximum possible'
lvg:
vg: '{{ disks_vg_name }}'
pvs: '{{ disks_pvs }}'
pvresize: 'yes'
state: 'present'
become: 'yes'

- name: >
Extend the logical volume to take all remaining space
of the PVs and resize the underlying filesystem.
lvol:
vg: '{{ disks_vg_name }}'
lv: '{{ disks_lv_name }}'
size: '100%PVS'
resizefs: 'yes'
state: 'present'
become: 'yes'
...
8 changes: 6 additions & 2 deletions vagrant.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
:memory: null
:boot_timeout: 60
:gui: false
:disks:
- :type: 'disk'
:options:
:primary: true
:size: '80GB'
:customize:
- :command: 'modifyvm'
:settings:
Expand Down Expand Up @@ -161,6 +166,5 @@
:extra_vars:
:gitconfig_user_name: 'Theodoros Georgomanolis'
:gitconfig_user_email: 'tgeorgomanolis@gmail.com'
:skip_tags:
- 'repos'
:skip_tags: []
...

0 comments on commit 714acc6

Please sign in to comment.