An example of template file
{
"variables": {
"user": "slapos",
"password": "slapos",
"domain": "",
"disk_size": "100",
"name": "image",
"custom_script": "scripts/empty.sh"
},
"builders":
[
{
"name": "debian10-{{ user `disk_size`}}G-{{ user `name`}}",
"type": "qemu",
"format": "qcow2",
"accelerator": "kvm",
"disk_size": "{{ user `disk_size`}}000",
"iso_url": "https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-10.2.0-amd64-netinst.iso",
"iso_checksum": "36de671429939e90f2a31ce3fbed0aaf",
"iso_checksum_type": "md5",
"http_directory": "http",
"ssh_username": "{{user `user`}}",
"ssh_password": "{{user `password`}}",
"ssh_wait_timeout": "1800s",
"shutdown_command": "echo '{{user `password`}}'|sudo -S shutdown -h now",
"headless": true,
"boot_wait": "2s",
"boot_command": [
"",
"auto preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed-debian10.cfg ",
"debian-installer=en_US.UTF-8 locale=en_US keymap=fr ",
"netcfg/get_hostname={{ .Name }} ",
"netcfg/get_domain={{ user `domain`}} ",
"fb=false debconf/frontend=noninteractive ",
"passwd/user-fullname={{user `user`}} ",
"passwd/user-password={{user `password`}} ",
"passwd/user-password-again={{user `password`}} ",
"passwd/username={{user `user`}} ",
""
]
}
],
"provisioners": [
{
"type": "shell",
"execute_command": "echo '{{user `password`}}' | {{.Vars}} sudo -E -S bash '{{.Path}}'",
"scripts": [
"scripts/update.sh",
"scripts/packages.sh",
"scripts/network-debian.sh",
"scripts/cleanup.sh",
"{{ user `custom_script` }}"
]
}
]
}
Let's look at an example of template file (this file is our debian10.json template).
The "builder
" section is the most important and the only one mandatory. There are also 2 other sections:
- "
variables
" which defines variables we use in other sections. It can be useful to do some minor variations of your images
- "
provisionners
" which is the step executed after the installation itself and let you execute some command inside the newly installed system via ssh. This step will let us customize everything we want inside the image (user, packages, etc.)
Please note that the file preseed.cfg
needs to be located inside a directory named http
. This http directory needs to be alongside the template. For example, look at our http directory at https://lab.nexedi.com/nexedi/slapos.package/tree/master/packer/http. For more information about the preseed file see: https://wiki.debian.org/fr/DebianInstaller/Preseed
When you wrote your template (like the one above), you can make sure it syntaxically correct using the command packer validate debian10.json
Once the file is validated, you can use the command packer build debian10.json to build the image.
Please note that packer supports any operating systems. You just need to look how to automatically install the OS of your choice and then configure a template for it. Of course, there are a lot of available template examples (see "Templates" chapter in https://www.packer.io/community-tools.html).