diff --git a/libvirtd.tf b/libvirtd.tf deleted file mode 100644 index 8b49834..0000000 --- a/libvirtd.tf +++ /dev/null @@ -1,55 +0,0 @@ -provider "libvirt" { - uri = "qemu+tcp://127.0.0.1/system" -# uri = "qemu+ssh://root@192.168.100.10/system" -} - -resource "libvirt_volume" "centos7-qcow2" { - name = "centos7.qcow2" - pool = "default" - source = "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2" - format = "qcow2" -} - -data "template_file" "user_data" { - template = "${file("${path.module}/cloud_init.cfg")}" -} - -# Use CloudInit to add the instance -resource "libvirt_cloudinit_disk" "commoninit" { - name = "commoninit.iso" - user_data = data.template_file.user_data.rendered -} - -# Define KVM domain to create -resource "libvirt_domain" "db1" { - name = "db1" - memory = "1024" - vcpu = 1 - - network_interface { - network_name = "default" - } - - disk { - volume_id = libvirt_volume.centos7-qcow2.id - } - - cloudinit = libvirt_cloudinit_disk.commoninit.id - - console { - type = "pty" - target_type = "serial" - target_port = "0" - } - - graphics { - type = "spice" - listen_type = "address" - autoport = true - } -} - -# Output Server IP -output "ip" { - value = libvirt_domain.db1.network_interface.0.addresses.0 -} \ No newline at end of file diff --git a/persitent/libvirtd_base.tf b/persitent/libvirtd_base.tf new file mode 100644 index 0000000..fd5517a --- /dev/null +++ b/persitent/libvirtd_base.tf @@ -0,0 +1,38 @@ +# https://github.com/dmacvicar/terraform-provider-libvirt/blob/master/website/docs/r + +terraform { + required_version = ">= 0.12" + + required_providers { + libvirt = ">= 0.0.0" + } + +} + +provider "libvirt" { + uri = "qemu+tcp://127.0.0.1/system" + # uri = "qemu+ssh://root@192.168.100.10/system" +} + +resource "libvirt_pool" "pool_1" { + name = "pool_1" + type = "dir" + path = "/var/lib/libvirt/pool" +} + +# -------- volume disk -------- +# https://github.com/dmacvicar/terraform-provider-libvirt/blob/master/website/docs/r/volume.html.markdown + +resource "libvirt_volume" "centos7_qcow2" { + name = "centos7.qcow2" + pool = "default" + source = "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2" + format = "qcow2" +} + +resource "libvirt_volume" "debian_buster_qcow2" { + name = "debian-buster.qcow2" + pool = "default" + source = "https://cdimage.debian.org/cdimage/openstack/current/debian-10.4.3-20200610-openstack-amd64.qcow2" + format = "qcow2" +} diff --git a/persitent/terraform.tfstate b/persitent/terraform.tfstate new file mode 100644 index 0000000..6822a8f --- /dev/null +++ b/persitent/terraform.tfstate @@ -0,0 +1,79 @@ +{ + "version": 4, + "terraform_version": "0.12.28", + "serial": 203, + "lineage": "e57b8df5-87bd-3954-0ba8-9cbedff9b460", + "outputs": {}, + "resources": [ + { + "mode": "managed", + "type": "libvirt_pool", + "name": "pool_1", + "provider": "provider.libvirt", + "instances": [ + { + "schema_version": 0, + "attributes": { + "allocation": 69478645760, + "available": null, + "capacity": 73445531648, + "id": "3a205457-1e97-455a-8fe6-9ed621f7ba99", + "name": "pool_1", + "path": "/var/lib/libvirt/pool", + "type": "dir", + "xml": [] + }, + "private": "bnVsbA==" + } + ] + }, + { + "mode": "managed", + "type": "libvirt_volume", + "name": "centos7_qcow2", + "provider": "provider.libvirt", + "instances": [ + { + "schema_version": 0, + "attributes": { + "base_volume_id": null, + "base_volume_name": null, + "base_volume_pool": null, + "format": "qcow2", + "id": "/var/lib/libvirt/pool/default/centos7.qcow2", + "name": "centos7.qcow2", + "pool": "default", + "size": 8589934592, + "source": "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2", + "xml": [] + }, + "private": "bnVsbA==" + } + ] + }, + { + "mode": "managed", + "type": "libvirt_volume", + "name": "debian_buster_qcow2", + "provider": "provider.libvirt", + "instances": [ + { + "schema_version": 0, + "attributes": { + "base_volume_id": null, + "base_volume_name": null, + "base_volume_pool": null, + "format": "qcow2", + "id": "/var/lib/libvirt/pool/default/debian-buster.qcow2", + "name": "debian-buster.qcow2", + "pool": "default", + "size": 2147483648, + "source": "https://cdimage.debian.org/cdimage/openstack/current/debian-10.4.3-20200610-openstack-amd64.qcow2", + "xml": [] + }, + "private": "bnVsbA==" + } + ] + } + ] +} diff --git a/readme.md b/readme.md index ad53341..c0cf116 100644 --- a/readme.md +++ b/readme.md @@ -10,6 +10,9 @@ Setup libvirt provider for terraform [https://github.com/dmacvicar/terraform-pro Provision compute into qemu/kvm with terraform and libvirt provider +`export LIBVIRT_DEFAULT_URI="qemu+tcp://127.0.0.1/system"` + `terraform init` `terraform apply` + diff --git a/cloud_init.cfg b/stack/cloud_init.cfg similarity index 92% rename from cloud_init.cfg rename to stack/cloud_init.cfg index 5a537ce..5d7d5a2 100644 --- a/cloud_init.cfg +++ b/stack/cloud_init.cfg @@ -14,6 +14,9 @@ groups: - whell: [antoine] users: + # - name: root + # lock_passwd: false + # password: $1$SaltSalt$.SBI7klTdiInUnDK0/40g. - name: antoine ssh_authorized_keys: - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCuLVDINxqYow17v9oovyqG3Ok5mOa36U3f+D7CtY8cS7jZavx7A6h2DAtlNzzJl3AxRtk3JY1rmV2fhwB+lVA1S8WT61/qHZH+smjbt6YCp6/ICMKPR8tKtbFUnXn+o9tAhqa7cryWs2LerJasXaSUUguIb6Lr0teSM6enzmtNShjiUta+6HlN+M91n/tp4pjn+tvNUSFETg1JVX1DIG/6U43N8V+fu6xsN5ceoPNGpBpikP0ZYmKYdcPMqqcWJkkaLGwh/u4R98BUVSPor/OFaMw3IZ6Nr7yCdWPCe/8SvEkbd2iMQm5dQjqEFEJUyPwoE18lfM2VnVwlaKsFN9Cmu2J5H4w9qlkZCSGkLa6eckJ3dOHDf6PZO409jbU7sV5rEspuo4gWqEzx5l2YKnASxhy+hylCj+sYrgMcEo9XMVSSYgj5ynykr3LyxyaFklZEuVxXJUYrOVcVIRdkJu9cVnhj6LvaUHY1JqdbhOAjPpSIVqKLPUYJIMU5nVKOh53Dd/rXdOx6BQXiA44DGuvYvHMohK7rXA92Sau0Hbb30DDMkPSPDXknIB+anELGCCx6j8HC38FzJyqtvtrDsG6sc/D3UQGApAS0+O6FXeH+v2OiPhJjdQMwNl5YMrJt6AUBVZZAZtFgUlhhZXRbyhuUuhmWYmzsNbWX7N5pzgYPyQ== antoine@archlinux diff --git a/stack/computes.tf b/stack/computes.tf new file mode 100644 index 0000000..49fcaa5 --- /dev/null +++ b/stack/computes.tf @@ -0,0 +1,50 @@ +# https://github.com/dmacvicar/terraform-provider-libvirt/blob/master/website/docs/r/domain.html.markdown + +# Define KVM domain to create + +resource "libvirt_domain" "db1" { + name = "db1" + memory = "1024" + vcpu = 1 + running = "false" + autostart = "true" + + boot_device { + dev = ["hd", "network"] + } + + network_interface { + network_name = "default" + # network_name = libvirt_network.private_network.name + } + + disk { + volume_id = libvirt_volume.debian_buster_qcow2.id + } + + # disk { + # volume_id = libvirt_volume.external_disk_1.id + # } + + cloudinit = libvirt_cloudinit_disk.commoninit.id + + console { + type = "pty" + target_type = "serial" + target_port = "0" + } + + graphics { + type = "vnc" + listen_type = "address" + autoport = "true" + } +} + + +resource "libvirt_volume" "debian_buster_qcow2" { + name = "debian-buster.qcow2" + pool = "default" + source = "https://cdimage.debian.org/cdimage/openstack/current/debian-10.4.3-20200610-openstack-amd64.qcow2" + format = "qcow2" +} diff --git a/stack/libvirtd.tf b/stack/libvirtd.tf new file mode 100644 index 0000000..b3cc696 --- /dev/null +++ b/stack/libvirtd.tf @@ -0,0 +1,43 @@ +# https://github.com/dmacvicar/terraform-provider-libvirt/blob/master/website/docs/r + +terraform { + required_version = ">= 0.12" + + required_providers { + null = "~> 2.1" + template = "~> 2.1" + libvirt = ">= 0.0.0" + } + +} + +provider "libvirt" { + uri = "qemu+tcp://127.0.0.1/system" + # uri = "qemu+ssh://root@192.168.100.10/system" +} + +data "template_file" "user_data" { + template = "${file("${path.module}/cloud_init.cfg")}" +} + +# Use CloudInit to add the instance +resource "libvirt_cloudinit_disk" "commoninit" { + name = "commoninit.iso" + pool = "default" + user_data = data.template_file.user_data.rendered +} + +resource "null_resource" "delay_10s" { + provisioner "local-exec" { + command = "sleep 10" + } + triggers = { + "before" = libvirt_domain.db1.id + } +} + +# Output Server IP +output "ip" { + value = libvirt_domain.db1.network_interface + depends_on = [null_resource.delay_10s] +} diff --git a/stack/networks.tf b/stack/networks.tf new file mode 100644 index 0000000..d72bab1 --- /dev/null +++ b/stack/networks.tf @@ -0,0 +1,12 @@ +# https://github.com/dmacvicar/terraform-provider-libvirt/blob/master/website/docs/r/network.markdown + +resource "libvirt_network" "private_network" { + name = "private" + mode = "nat" + domain = "network.local" + addresses = ["10.17.3.0/24"] + autostart = true + dhcp { + enabled = true + } +} diff --git a/stack/terraform.tfstate b/stack/terraform.tfstate new file mode 100644 index 0000000..5234692 --- /dev/null +++ b/stack/terraform.tfstate @@ -0,0 +1,8 @@ +{ + "version": 4, + "terraform_version": "0.12.28", + "serial": 121, + "lineage": "67fe8f67-1e0d-4c6e-4440-3eac0ca3aadd", + "outputs": {}, + "resources": [] +} diff --git a/stack/volumes.tf b/stack/volumes.tf new file mode 100644 index 0000000..c3a691a --- /dev/null +++ b/stack/volumes.tf @@ -0,0 +1,25 @@ +# https://github.com/dmacvicar/terraform-provider-libvirt/blob/master/website/docs/r/volume.html.markdown + +# -------- root disk use by compute -------- +# ~> Tip: when provisioning multiple domains using the same base image, create a libvirt_volume for the base image and then define +# the domain specific ones as based on it. This way the image will not be modified and no extra disk space is going to be used for the base image. + +resource "libvirt_volume" "my_root_centos" { + name = "my-root-centos" + base_volume_name = "centos7.qcow2" + # base_volume_id = libvirt_volume.centos7_qcow2.id +} + +resource "libvirt_volume" "my_root_debian" { + name = "my-root-debian" + base_volume_name = "debian-buster.qcow2" + # base_volume_id = libvirt_volume.debian_buster_qcow2.id +} + +resource "libvirt_volume" "external_disk_1" { + name = "external-disk-1" + # 10Gb + size = 10737418240 + pool = "pool_1" + # pool = libvirt_pool.pool_1.name +} diff --git a/terraform.tfstate b/terraform.tfstate deleted file mode 100644 index 2a58124..0000000 --- a/terraform.tfstate +++ /dev/null @@ -1,164 +0,0 @@ -{ - "version": 4, - "terraform_version": "0.12.28", - "serial": 40, - "lineage": "e57b8df5-87bd-3954-0ba8-9cbedff9b460", - "outputs": { - "ip": { - "value": "192.168.122.156", - "type": "string" - } - }, - "resources": [ - { - "mode": "data", - "type": "template_file", - "name": "user_data", - "provider": "provider.template", - "instances": [ - { - "schema_version": 0, - "attributes": { - "filename": null, - "id": "924d6b37ed35e7f71aa4793775cf6098a2dd31c5c3cdbfbcbd02cf4893f856cc", - "rendered": "#cloud-config\n# vim: syntax=yaml\n\n# ***********************\n# ---- documentation : \n# ---\u003e https://cloudinit.readthedocs.io/en/latest/topics/examples.html\n# ******************************\n\nssh_pwauth: False\nchpasswd:\n expire: False\n\ngroups:\n - whell: [antoine]\n\nusers:\n - name: antoine\n ssh_authorized_keys:\n - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCuLVDINxqYow17v9oovyqG3Ok5mOa36U3f+D7CtY8cS7jZavx7A6h2DAtlNzzJl3AxRtk3JY1rmV2fhwB+lVA1S8WT61/qHZH+smjbt6YCp6/ICMKPR8tKtbFUnXn+o9tAhqa7cryWs2LerJasXaSUUguIb6Lr0teSM6enzmtNShjiUta+6HlN+M91n/tp4pjn+tvNUSFETg1JVX1DIG/6U43N8V+fu6xsN5ceoPNGpBpikP0ZYmKYdcPMqqcWJkkaLGwh/u4R98BUVSPor/OFaMw3IZ6Nr7yCdWPCe/8SvEkbd2iMQm5dQjqEFEJUyPwoE18lfM2VnVwlaKsFN9Cmu2J5H4w9qlkZCSGkLa6eckJ3dOHDf6PZO409jbU7sV5rEspuo4gWqEzx5l2YKnASxhy+hylCj+sYrgMcEo9XMVSSYgj5ynykr3LyxyaFklZEuVxXJUYrOVcVIRdkJu9cVnhj6LvaUHY1JqdbhOAjPpSIVqKLPUYJIMU5nVKOh53Dd/rXdOx6BQXiA44DGuvYvHMohK7rXA92Sau0Hbb30DDMkPSPDXknIB+anELGCCx6j8HC38FzJyqtvtrDsG6sc/D3UQGApAS0+O6FXeH+v2OiPhJjdQMwNl5YMrJt6AUBVZZAZtFgUlhhZXRbyhuUuhmWYmzsNbWX7N5pzgYPyQ== antoine@archlinux\n sudo: ['ALL=(ALL) NOPASSWD:ALL']\n shell: /bin/bash\n groups: wheel\n", - "template": "#cloud-config\n# vim: syntax=yaml\n\n# ***********************\n# ---- documentation : \n# ---\u003e https://cloudinit.readthedocs.io/en/latest/topics/examples.html\n# ******************************\n\nssh_pwauth: False\nchpasswd:\n expire: False\n\ngroups:\n - whell: [antoine]\n\nusers:\n - name: antoine\n ssh_authorized_keys:\n - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCuLVDINxqYow17v9oovyqG3Ok5mOa36U3f+D7CtY8cS7jZavx7A6h2DAtlNzzJl3AxRtk3JY1rmV2fhwB+lVA1S8WT61/qHZH+smjbt6YCp6/ICMKPR8tKtbFUnXn+o9tAhqa7cryWs2LerJasXaSUUguIb6Lr0teSM6enzmtNShjiUta+6HlN+M91n/tp4pjn+tvNUSFETg1JVX1DIG/6U43N8V+fu6xsN5ceoPNGpBpikP0ZYmKYdcPMqqcWJkkaLGwh/u4R98BUVSPor/OFaMw3IZ6Nr7yCdWPCe/8SvEkbd2iMQm5dQjqEFEJUyPwoE18lfM2VnVwlaKsFN9Cmu2J5H4w9qlkZCSGkLa6eckJ3dOHDf6PZO409jbU7sV5rEspuo4gWqEzx5l2YKnASxhy+hylCj+sYrgMcEo9XMVSSYgj5ynykr3LyxyaFklZEuVxXJUYrOVcVIRdkJu9cVnhj6LvaUHY1JqdbhOAjPpSIVqKLPUYJIMU5nVKOh53Dd/rXdOx6BQXiA44DGuvYvHMohK7rXA92Sau0Hbb30DDMkPSPDXknIB+anELGCCx6j8HC38FzJyqtvtrDsG6sc/D3UQGApAS0+O6FXeH+v2OiPhJjdQMwNl5YMrJt6AUBVZZAZtFgUlhhZXRbyhuUuhmWYmzsNbWX7N5pzgYPyQ== antoine@archlinux\n sudo: ['ALL=(ALL) NOPASSWD:ALL']\n shell: /bin/bash\n groups: wheel\n", - "vars": null - } - } - ] - }, - { - "mode": "managed", - "type": "libvirt_cloudinit_disk", - "name": "commoninit", - "provider": "provider.libvirt", - "instances": [ - { - "schema_version": 0, - "attributes": { - "id": "/var/lib/libvirt/images/commoninit.iso;5f1da8d5-7b33-986f-3888-179eec23b9b3", - "meta_data": "", - "name": "commoninit.iso", - "network_config": "", - "pool": "default", - "user_data": "#cloud-config\n# vim: syntax=yaml\n\n# ***********************\n# ---- documentation : \n# ---\u003e https://cloudinit.readthedocs.io/en/latest/topics/examples.html\n# ******************************\n\nssh_pwauth: False\nchpasswd:\n expire: False\n\ngroups:\n - whell: [antoine]\n\nusers:\n - name: antoine\n ssh_authorized_keys:\n - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCuLVDINxqYow17v9oovyqG3Ok5mOa36U3f+D7CtY8cS7jZavx7A6h2DAtlNzzJl3AxRtk3JY1rmV2fhwB+lVA1S8WT61/qHZH+smjbt6YCp6/ICMKPR8tKtbFUnXn+o9tAhqa7cryWs2LerJasXaSUUguIb6Lr0teSM6enzmtNShjiUta+6HlN+M91n/tp4pjn+tvNUSFETg1JVX1DIG/6U43N8V+fu6xsN5ceoPNGpBpikP0ZYmKYdcPMqqcWJkkaLGwh/u4R98BUVSPor/OFaMw3IZ6Nr7yCdWPCe/8SvEkbd2iMQm5dQjqEFEJUyPwoE18lfM2VnVwlaKsFN9Cmu2J5H4w9qlkZCSGkLa6eckJ3dOHDf6PZO409jbU7sV5rEspuo4gWqEzx5l2YKnASxhy+hylCj+sYrgMcEo9XMVSSYgj5ynykr3LyxyaFklZEuVxXJUYrOVcVIRdkJu9cVnhj6LvaUHY1JqdbhOAjPpSIVqKLPUYJIMU5nVKOh53Dd/rXdOx6BQXiA44DGuvYvHMohK7rXA92Sau0Hbb30DDMkPSPDXknIB+anELGCCx6j8HC38FzJyqtvtrDsG6sc/D3UQGApAS0+O6FXeH+v2OiPhJjdQMwNl5YMrJt6AUBVZZAZtFgUlhhZXRbyhuUuhmWYmzsNbWX7N5pzgYPyQ== antoine@archlinux\n sudo: ['ALL=(ALL) NOPASSWD:ALL']\n shell: /bin/bash\n groups: wheel\n" - }, - "private": "bnVsbA==" - } - ] - }, - { - "mode": "managed", - "type": "libvirt_domain", - "name": "db1", - "provider": "provider.libvirt", - "instances": [ - { - "schema_version": 0, - "attributes": { - "arch": "x86_64", - "autostart": false, - "boot_device": [], - "cloudinit": "/var/lib/libvirt/images/commoninit.iso;5f1da8d5-7b33-986f-3888-179eec23b9b3", - "cmdline": [], - "console": [ - { - "source_host": "127.0.0.1", - "source_path": "", - "source_service": "0", - "target_port": "0", - "target_type": "serial", - "type": "pty" - } - ], - "coreos_ignition": null, - "cpu": null, - "description": "", - "disk": [ - { - "block_device": "", - "file": "", - "scsi": false, - "url": "", - "volume_id": "/var/lib/libvirt/images/centos7.qcow2", - "wwn": "" - } - ], - "emulator": "/usr/bin/qemu-system-x86_64", - "filesystem": [], - "firmware": "", - "fw_cfg_name": "opt/com.coreos/config", - "graphics": [ - { - "autoport": true, - "listen_address": "127.0.0.1", - "listen_type": "address", - "type": "spice" - } - ], - "id": "47a45870-bd85-4705-a835-3985a44e871e", - "initrd": "", - "kernel": "", - "machine": "pc", - "memory": 1024, - "metadata": null, - "name": "db1", - "network_interface": [ - { - "addresses": [ - "192.168.122.156" - ], - "bridge": "", - "hostname": "", - "mac": "52:54:00:4E:19:24", - "macvtap": "", - "network_id": "a27a7ae4-4a14-4ac4-8c52-f7b44bcbb7f3", - "network_name": "default", - "passthrough": "", - "vepa": "", - "wait_for_lease": false - } - ], - "nvram": [], - "qemu_agent": false, - "running": true, - "timeouts": null, - "vcpu": 1, - "video": [], - "xml": [] - }, - "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjozMDAwMDAwMDAwMDB9fQ==", - "dependencies": [ - "libvirt_cloudinit_disk.commoninit", - "libvirt_volume.centos7-qcow2" - ] - } - ] - }, - { - "mode": "managed", - "type": "libvirt_volume", - "name": "centos7-qcow2", - "provider": "provider.libvirt", - "instances": [ - { - "schema_version": 0, - "attributes": { - "base_volume_id": null, - "base_volume_name": null, - "base_volume_pool": null, - "format": "qcow2", - "id": "/var/lib/libvirt/images/centos7.qcow2", - "name": "centos7.qcow2", - "pool": "default", - "size": 8589934592, - "source": "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2", - "xml": [] - }, - "private": "bnVsbA==" - } - ] - } - ] -}