Skip to content

Commit 3912952

Browse files
Merge pull request #11 from chrodriguez/main
Add support to attach multiple disks
2 parents afbe7f2 + d12388e commit 3912952

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

README.md

+46-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Terraform module for KVM/Libvirt Virtual Machine. This module will create a KVM
1212
- one NIC per domain, connected to the network using the **bridge interface**
1313
- setup network interface using DHCP or static configuration
1414
- cloud_init VM(s) configuration (Ubuntu+Netplan complient)
15+
- optionally add multiple extra disks
1516
- test the ssh connection
1617

1718
## Tested on
@@ -30,6 +31,7 @@ Terraform module for KVM/Libvirt Virtual Machine. This module will create a KVM
3031
|index_start|From where the index start| 1
3132
|vm_hostname_prefix|VM hostname prefix|vm
3233
|memory|RAM in MB|1024
34+
|additional_disk_ids|List of volume ids to be attached to domain| []
3335
|hugepages|Use Hugepages|false
3436
|vcpu|Number of vCPUs|1
3537
|pool|Storage pool name|default
@@ -70,7 +72,7 @@ provider "libvirt" {
7072
7173
module "vm" {
7274
source = "MonolithProjects/vm/libvirt"
73-
version = "1.6.0"
75+
version = "1.8.0"
7476
7577
vm_hostname_prefix = "server"
7678
vm_count = 3
@@ -117,7 +119,7 @@ provider "libvirt" {
117119
118120
module "vm" {
119121
source = "MonolithProjects/vm/libvirt"
120-
version = "1.6.0"
122+
version = "1.8.0"
121123
122124
vm_hostname_prefix = "server"
123125
vm_count = 3
@@ -157,7 +159,48 @@ output "outputs" {
157159
}
158160
```
159161

160-
The shared directory from the example can be mounted inside the VM with command `sudo mount -t 9p -o trans=virtio,version=9p2000.L,rw tmp /host/tmp`
162+
> The shared directory from the example can be mounted inside the VM with command `sudo mount -t 9p -o trans=virtio,version=9p2000.L,rw tmp /host/tmp`
163+
164+
Create a VM with an extra disk
165+
166+
```
167+
# Creates a 50GB extra-data-disk within vms pool
168+
resource "libvirt_volume" "data_volume" {
169+
pool = "vms"
170+
name = "extra-data-disk.qcow2"
171+
format = "qcow2"
172+
size = 1024*1024*1024*50
173+
}
174+
175+
module "vm" {
176+
source = "MonolithProjects/vm/libvirt"
177+
version = "1.8.0"
178+
179+
vm_hostname_prefix = "data-server"
180+
base_volume_name = "debian-11-base.qcow2"
181+
base_pool_name = "linked-images"
182+
vm_count = 1
183+
bridge = "bridge-dmz"
184+
memory = "4096"
185+
hugepages = false
186+
vcpu = 4
187+
pool = "vms"
188+
system_volume = 25
189+
additional_disk_ids = [ libvirt_volume.data_volume.id ]
190+
191+
dhcp = true
192+
193+
ssh_admin = "admin"
194+
ssh_keys = [
195+
chomp(file("~/.ssh/id_rsa.pub"))
196+
]
197+
time_zone = "America/Argentina/Buenos_Aires"
198+
}
199+
200+
output "ip_addresses" {
201+
value = module.vm
202+
}
203+
```
161204

162205
## Module output example
163206

main.tf

+7
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ resource "libvirt_domain" "virt-machine" {
4545
volume_id = element(libvirt_volume.volume-qcow2.*.id, count.index)
4646
}
4747

48+
dynamic "disk" {
49+
for_each = var.additional_disk_ids
50+
content {
51+
volume_id = disk.value
52+
}
53+
}
54+
4855
dynamic "filesystem" {
4956
for_each = var.share_filesystem.source != null ? [ var.share_filesystem.source] : []
5057
content {

variables.tf

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ variable "base_pool_name" {
1313
default = null
1414
}
1515

16+
variable "additional_disk_ids" {
17+
description = "List of volume ids"
18+
default = []
19+
}
20+
1621

1722
variable "autostart" {
1823
description = "Autostart the domain"

0 commit comments

Comments
 (0)