summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Berto <463349+robertoberto@users.noreply.github.com>2024-05-15 03:31:01 +0000
committerRoberto Berto <463349+robertoberto@users.noreply.github.com>2024-05-15 03:31:01 +0000
commitb1b9151865275b52647e784c9a8f201e3f090a30 (patch)
tree3561c1523eb6dec0b2a6104a66451adec0a1fc8c
parent769d3a05284392068a3ae56542af64e786fda74e (diff)
downloadpacker-vyos-b1b9151865275b52647e784c9a8f201e3f090a30.tar.gz
packer-vyos-b1b9151865275b52647e784c9a8f201e3f090a30.zip
Improvements for v0.1
- Makefile now clone build1 to build2 to stop break build1 image if build2 fail - Fix README.md github md syntax - grub-serial.sh: bool should be integer and fixed grub default sed - Improved build workflow between build1 to build2 - qmeuargs not create sparse files +detect-zeroes=on -discard=ignore
-rw-r--r--.gitignore3
-rw-r--r--Makefile18
-rw-r--r--README.md27
-rw-r--r--scripts/vyos/cleanup.sh2
-rw-r--r--scripts/vyos/configure.sh2
-rw-r--r--scripts/vyos/grub-serial.sh12
-rw-r--r--vyos-image1.pkr.hcl47
-rw-r--r--vyos-image2.pkr.hcl31
-rw-r--r--vyos.pkrvars.hcl6
9 files changed, 102 insertions, 46 deletions
diff --git a/.gitignore b/.gitignore
index 7cd3bea..aa49f16 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,6 @@ iso/*
*.code-workspace
dev/
*.checksum
+.vm_name
+iso
+output
diff --git a/Makefile b/Makefile
index 905d398..8bcb8b3 100644
--- a/Makefile
+++ b/Makefile
@@ -11,6 +11,14 @@ DISPLAY=:99
# export all
export
+VM_NAME_FILE := .vm_name
+VM_NAME := $(shell cat $(VM_NAME_FILE))
+SRC_QCOW2 := iso/$(VM_NAME)-build1.qcow2
+DST_QCOW2 := iso/$(VM_NAME)-build2.qcow2
+SRC_CHECKSUM := iso/$(VM_NAME)-build1.qcow2.checksum
+DST_CHECKSUM := iso/$(VM_NAME)-build2.qcow2.checksum
+
+
.PHONY: help
help:
@echo "make working:"
@@ -25,6 +33,9 @@ help:
@echo " make x11server - start Xvfb X11 server on DISPLAY=:99. Require apt install xvfb"
+
+
+
# ifneq ("$(wildcard .env)","")
# include .env
# export
@@ -46,8 +57,15 @@ else
vyos-image1.pkr.hcl
endif
+
.PHONY: build2
build2:
+ # create a copy of qcow2 - if build2 fail you can run again
+ cp -f $(SRC_QCOW2) $(DST_QCOW2)
+ cp -f $(SRC_CHECKSUM) $(DST_CHECKSUM)
+ sed -i 's/$(VM_NAME)-build1.qcow2/$(VM_NAME)-build2.qcow2/' $(DST_CHECKSUM)
+ cat iso/*.checksum > iso/SHA256SUM
+
# if exist local.pkrvars.hcl load it
ifneq ($(wildcard local.pkrvars.hcl),)
packer build \
diff --git a/README.md b/README.md
index 2cc55a4..0263737 100644
--- a/README.md
+++ b/README.md
@@ -86,14 +86,13 @@ For headless=false follow development instructions bellow.
* in headless/remote ssh, before make build you need to start Xvfb. ```make x11server``` start X11 server, but you can put on init with something like https://gist.github.com/jterrace/2911875
* for SSH access put in .env SLEEP_BEFORE_SHUTDOWN=600 to keep SSH on for 10 minutes after scripts run. Also put HOST_PORT_FIXED=2222 for open SSH in VM 127.0.0.1 in port 2222. ```ssh vyos@127.0.0.1 -p 2222``` default password is vyos.
-# Install
+# Usage
-## Usage
+## Edit Variables
* local.pkrvars.hcl if exists or will use default vars vyos.pkrvars.hcl if local not exists
* if .env exists will load
* example.env is provided in git repo as base of .env. .env file has building vars, which control building process
-
## Initialize packer
Packer need to load plugins first.
@@ -101,5 +100,23 @@ Use:
* ```make init```, for first time init
* ```make upgrade```, when want to upgrade plugins
-## Build
-* ```make build```, for build images
+## Build images
+* ```make build1```
+ * build first stage image
+ * create a new qcow2 image
+ * mount iso as cdrom
+ * use vyos installer
+ * ```install image```
+ * files generated:
+ * source: iso/vyos-1.3.6.iso
+ * output: iso/vyos-1.3.6.qcow2
+ * checksum: iso/SHA256SUM
+* ```make build2```
+ * build second stage image
+ * use same qcow2 image from first stage
+ * customize image
+ * files generated:
+ * source: iso/vyos-1.3.6.qcow2
+ * output: iso/vyos-1.3.6.qcow2
+ * checksum: iso/SHA256SUM
+ \ No newline at end of file
diff --git a/scripts/vyos/cleanup.sh b/scripts/vyos/cleanup.sh
index 6ccf753..f1586f7 100644
--- a/scripts/vyos/cleanup.sh
+++ b/scripts/vyos/cleanup.sh
@@ -13,7 +13,7 @@ cat <<EOF > /home/vyos/cleanup-vyos.sh
#!/bin/vbash
source /opt/vyatta/etc/functions/script-template
configure
-set system host-name 'test'
+set system host-name 'vyosbuild'
commit
save
exit
diff --git a/scripts/vyos/configure.sh b/scripts/vyos/configure.sh
index 2628959..8d328ee 100644
--- a/scripts/vyos/configure.sh
+++ b/scripts/vyos/configure.sh
@@ -13,7 +13,7 @@ cat <<EOF > /home/vyos/configure-vyos.sh
#!/bin/vbash
source /opt/vyatta/etc/functions/script-template
configure
-set system host-name 'test'
+set system host-name 'vyoshost'
commit
save
exit
diff --git a/scripts/vyos/grub-serial.sh b/scripts/vyos/grub-serial.sh
index b72b1a8..5beb889 100644
--- a/scripts/vyos/grub-serial.sh
+++ b/scripts/vyos/grub-serial.sh
@@ -3,6 +3,7 @@
set -e
set -x
+#GRUB_SERIAL=1
if [[ "${GRUB_SERIAL}" -ne 1 ]]; then
echo "$0 - info: grub will keep default=0 (kvm). to use serial add to .env: GRUB_SERIAL=1"
exit 0
@@ -12,4 +13,13 @@ GRUB_CFG="/boot/grub/grub.cfg"
GRUB_DEFAULT="/etc/default/grub"
sed -i 's/^set default=.*/set default=1/' $GRUB_CFG
-sed -i 's/^set default=.*/set default=1/' $GRUB_DEFAULT
+
+if grep -q "^GRUB_DEFAULT=" $GRUB_DEFAULT; then
+ sed -i 's/^GRUB_DEFAULT=.*/GRUB_DEFAULT=1/' $GRUB_DEFAULT
+else
+ echo "GRUB_DEFAULT=1" >> $GRUB_DEFAULT
+fi
+
+# update-grub
+
+
diff --git a/vyos-image1.pkr.hcl b/vyos-image1.pkr.hcl
index 98c93fb..fc45a66 100644
--- a/vyos-image1.pkr.hcl
+++ b/vyos-image1.pkr.hcl
@@ -93,14 +93,13 @@ variable "sleep_after_grub" {
# set grub_serial=1 to turn grub default=1, ie: use serial console. it is need to adjust on hypervisor
variable "grub_serial" {
- type = bool
- default = true
+ type = string
+ default = 1
}
locals {
iso_path = "iso/${var.vm_name}.iso"
output_dir = "output/vyos-image1/${regex_replace(timestamp(), "[: ]", "-")}"
- #output_dir = "build/image-install"
}
source "qemu" "vyos" {
@@ -115,29 +114,23 @@ source "qemu" "vyos" {
"set service ssh port '22'<enter><wait>",
"commit<enter><wait>",
"save<enter><wait>",
- # "exit<enter><wait>",
"exit<enter><wait>",
"install image<enter><wait3s>",
- "Yes<enter><wait>",
- "Auto<enter><wait>",
- "<enter><wait>", # vda
+ "Yes<enter><wait3s>",
+ "Auto<enter><wait3s>",
+ "<enter><wait3s>", # vda
"Yes<enter><wait5s>",
"<enter><wait15s>", #disk size
"${var.vm_name}<enter><wait10s>",
"<enter><wait2s>",
"${var.ssh_password}<enter><wait>",
"${var.ssh_password}<enter><wait>",
- "<enter><wait10s>", #vda
- #"shutdown -h now<enter>"
- #"reboot now<enter><wait60s>",
- #"${var.ssh_username}<enter><wait>",
- #"${var.ssh_password}<enter><wait>",
+ "<enter><wait5s>",
]
accelerator = "kvm"
iso_checksum = var.iso_checksum
- #iso_url = fileexists(local.iso_path) ? local.iso_path : var.iso_url
iso_url = local.iso_path
boot_wait = var.boot_wait
@@ -190,24 +183,36 @@ build {
provisioner "shell-local" {
inline = [
- #"rm -rf ${local.output_dir}",
+ "mkdir -p iso/",
"mkdir -p ${local.output_dir}"
]
}
- post-processor "checksum" {
- checksum_types = ["sha256"]
- keep_input_artifact = true
+ # checksum
+ post-processors {
+ post-processor "checksum" {
+ checksum_types = ["sha256"]
+ keep_input_artifact = true
+ }
+
+ post-processor "shell-local" {
+ inline = [
+ "mv packer_vyos_qemu_sha256.checksum iso/${var.vm_name}-build1.qcow2.checksum.tmp",
+ "awk '{print $1, \" ${var.vm_name}-build1.qcow2\"}' iso/${var.vm_name}-build1.qcow2.checksum.tmp > iso/${var.vm_name}-build1.qcow2.checksum",
+ "rm -f iso/${var.vm_name}-build1.qcow2.checksum.tmp",
+ "cat iso/*.checksum > iso/SHA256SUM",
+ "echo '${var.vm_name}' > .vm_name"
+ ]
+ }
}
+ # copy from output to iso/ for vyos-image2.pkr.hcl customize
post-processors {
post-processor "shell-local" {
inline = [
- "cp '${local.output_dir}/${var.vm_name}-${source.name}.qcow2' iso/${var.vm_name}.qcow2",
- "qemu-img convert -O vpc iso/${var.vm_name}.qcow2 iso/${var.vm_name}.vhd",
- "cd iso && sha256sum * > SHA256SUM"
+ "cp '${local.output_dir}/${var.vm_name}-${source.name}.qcow2' iso/${var.vm_name}-build1.qcow2",
+ "rm -rf '${local.output_dir}'"
]
- #only = ["vyos_qemu_qcow2"]
}
}
} \ No newline at end of file
diff --git a/vyos-image2.pkr.hcl b/vyos-image2.pkr.hcl
index 539c518..a3b0c95 100644
--- a/vyos-image2.pkr.hcl
+++ b/vyos-image2.pkr.hcl
@@ -22,7 +22,7 @@ variable "memsize" {
}
variable "disk_size" {
- default = "10240"
+ default = "1024"
}
variable "iso_checksum" {
@@ -94,8 +94,8 @@ variable "sleep_after_grub" {
# set grub_serial=1 to turn grub default=1, ie: use serial console. it is need to adjust on hypervisor
variable "grub_serial" {
- type = bool
- default = true
+ type = string
+ default = 1
}
# which kind of datasource should be used
@@ -106,14 +106,13 @@ variable "cloud_init_datasource" {
}
locals {
- iso_path = "iso/${var.vm_name}.vhd"
- #iso_path = "iso/vyos-1.3.6.qcow2"
+ iso_path = "iso/${var.vm_name}-build2.qcow2" # not used at all since qemuargs -drive override it
output_dir = "output/vyos-image2/${regex_replace(timestamp(), "[: ]", "-")}"
}
source "qemu" "vyos" {
boot_command = [
- "<enter>",
+ "<wait2s><enter>",
"<wait${var.sleep_after_grub}s>",
"${var.ssh_username}<enter><wait>",
"${var.ssh_password}<enter><wait>",
@@ -123,14 +122,13 @@ source "qemu" "vyos" {
"set service ssh port '22'<enter><wait>",
"commit<enter><wait>",
"save<enter><wait>",
- "exit<enter><wait>",
+ "exit<enter><wait10s>",
]
accelerator = "kvm"
iso_checksum = var.iso_checksum
- #iso_url = fileexists(local.iso_path) ? local.iso_path : var.iso_url
- iso_url = local.iso_path
+ iso_url = local.iso_path # not used at all since qemuargs -drive override it
boot_wait = var.boot_wait
@@ -168,7 +166,9 @@ source "qemu" "vyos" {
["-cpu", "host"],
["-netdev", "user,id=user.0,", "hostfwd=tcp::{{ .SSHHostPort }}-:22"],
["-device", "virtio-net,netdev=user.0"],
- ["-drive", "file=iso/vyos-1.3.6.qcow2,if=virtio,cache=writeback,discard=ignore,format=qcow2"]
+ #["-drive", "file=iso/${var.vm_name}.qcow2,if=virtio,cache=writeback,discard=ignore,format=qcow2"]
+ #["-drive", "file=iso/${var.vm_name}.qcow2,if=none,id=drive-virtio0,format=qcow2,cache=writeback,aio=io_uring,detect-zeroes=on"]
+ ["-drive", "file=iso/${var.vm_name}-build2.qcow2,if=virtio,cache=writeback,format=qcow2,aio=io_uring,detect-zeroes=on"]
]
}
@@ -231,7 +231,7 @@ build {
]
}
- # if grub_serial=1 will install change grub to serial
+ # if grub_serial=1 change grub default to serial
provisioner "shell" {
execute_command = "VYOS_RELEASE='${var.vyos_release}' GRUB_SERIAL='${var.grub_serial}' {{ .Vars }} sudo -E bash '{{ .Path }}'"
scripts = [
@@ -239,7 +239,7 @@ build {
]
}
- # cleanup
+ # image cleanup
provisioner "shell" {
execute_command = "VYOS_RELEASE='${var.vyos_release}' {{ .Vars }} sudo -E bash '{{ .Path }}'"
scripts = [
@@ -247,11 +247,14 @@ build {
]
}
+ # copy qcow2 to final destination
post-processors {
post-processor "shell-local" {
inline = [
- #"cp '${local.output_dir}/${var.vm_name}-${source.name}.qcow2' /mnt/pve/svm_privateos_ic1a_main/template/iso/"
- "cp 'iso/vyos-1.3.6.qcow2' /mnt/pve/svm_privateos_ic1a_main/template/iso/"
+ "cp 'iso/${var.vm_name}-build2.qcow2' iso/${var.vm_name}.img",
+ "cd iso/ && sha256sum ${var.vm_name}.img > ${var.vm_name}.img.checksum && cd ../" ,
+ "cat iso/*.checksum > iso/SHA256SUM",
+ "rm -rf '${local.output_dir}'"
]
}
}
diff --git a/vyos.pkrvars.hcl b/vyos.pkrvars.hcl
index f08d109..91bdb2a 100644
--- a/vyos.pkrvars.hcl
+++ b/vyos.pkrvars.hcl
@@ -30,7 +30,7 @@ cloud_init_datasource = "nocloud_configdrive"
#
# for proxmox:
# qm set 9000 --serial0 socket --vga serial0
-grub_serial = true
+grub_serial = 1
# equuleus: debian 11 (branch 1.3.*)
# sagitta: debian 12 (branch 1.4.*)
@@ -41,8 +41,8 @@ vyos_release = "equuleus"
# false will start vnc for console
headless = false
-# in MB (10GB x 1024 = 10240)
-disk_size = 10240
+# in MB (10GB x 1024 = 10240, minimum 2048)
+disk_size = 2048