packer-builds

Packer Builds for LXC and Libvirt
git clone https://git.in0rdr.ch/packer-builds.git
Log | Files | Refs | Pull requests |Archive | README

README.md (4061B)


      1 # packer-builds
      2 
      3 This repo contains some snippets to build containers with Packer.
      4 
      5 The notes in this Readme do not follow a particular structure, but should help using the scripts.
      6 
      7 ## Build Templates
      8 
      9 List templates (from `man lxc-create`):
     10 
     11 ```
     12 /usr/share/lxc/templates/lxc-download -l
     13 ```
     14 
     15 ## Build Instructions
     16 
     17 For instance, to build the MariaDB image with debug logs:
     18 ```bash
     19 $ PACKER_LOG=1 packer build mariadb-lxc.json
     20 ```
     21 
     22 For some newer, hcl-based Packer configurations, it is necessary to supply
     23 extra variables through var files or the `-var` input flag:
     24 
     25 ```bash
     26 packer build \
     27  -var "gatus_tls_nfs_server=server:/share" \
     28  -var-file="variables.auto.pkrvars.hcl" \
     29  gatus-lxc.pkr.hcl
     30 ```
     31 
     32 The `-var-file` ideally contains all sensitive variables.
     33 
     34 ### Suggested Build Order
     35 
     36 The following sequence of builds allows you to spin up a small Nextcloud installation.
     37 
     38 **1 Build Mariadb**
     39 
     40 ```
     41 PACKER_LOG=1 packer build \
     42  -var 'mysql_root_password=123' \
     43  -var 'mysql_additional_passwords="456"' \
     44  mariadb-nextcloud.json
     45 
     46 ./deploy-output.sh output-packer-mariadb-nextcloud mariadb
     47 ```
     48 
     49 **2 Build Haproxy**
     50 
     51 ```
     52 PACKER_LOG=1 packer build haproxy-lxc.json
     53 ./deploy-output.sh output-packer-haproxy haproxy
     54 ```
     55 
     56 **3 Nextcloud**
     57 
     58 First, start the previously built components:
     59 ```bash
     60 lxc-start haproxy
     61 lxc-start mariadb
     62 ```
     63 
     64 Afterwards, build the Nextcloud container:
     65 ```bash
     66 # install nextcloud with the correct database password from step (1)
     67 PACKER_LOG=1 packer build \
     68  -var 'nextcloud_admin_user=admin' \
     69  -var 'nextcloud_admin_pass=abc' \
     70  -var 'nextcloud_database_pass=456' \
     71  -var 'certbot_mail=root@dev.mail' \
     72  -var 'overwrite_cli_url=nextcloud.com' \
     73  nextcloud-lxc.json
     74 
     75 ./deploy-output.sh output-packer-nextcloud nextcloud
     76 ```
     77 
     78 ## Manual Container Install to LXC Directory
     79 
     80 Prepare container name and target config file variables:
     81 ```bash
     82 name="container_name"
     83 target_config="/srv/lxc/$name/config"
     84 mkdir "/srv/lxc/$name"
     85 ```
     86 
     87 Extract the rootfs tarball and add the config:
     88 ```bash
     89 # exctract root fs
     90 tar -xvf output-lxc/rootfs.tar.gz -C "/srv/lxc/$name"
     91 
     92 # copy config
     93 cp output-lxc/lxc-config $target_config
     94 
     95 # adjust rootfs path
     96 echo -e "\nlxc.rootfs.path = dir:/srv/lxc/$name/rootfs" >> $target_config
     97 ```
     98 
     99 The script `deploy-output.sh` automates the above steps. Usage:
    100 ```
    101 $ ./deploy-output.sh
    102   This script deploys templates built with the lxc builder for Packer:
    103   https://www.packer.io/docs/builders/lxc.html
    104   
    105   No arguments supplied, required args:
    106    PACKER_OUTPUT_DIR: Packer lxc output directory with rootfs and config template
    107    CONTAINER_NAME: Name of the new container
    108   
    109   Usage: deploy-output.sh PACKER_OUTPUT_DIR CONTAINER_NAME
    110   
    111   Copy lxc template from PACKER_OUTPUT_DIR to /var/lib/lxc/CONTAINER_NAME
    112 ```
    113 
    114 ## Haproxy stats
    115 ```bash
    116 echo "show stat" | socat stdio /run/haproxy/admin.sock
    117 ```
    118 
    119 ## Build armv7l Minio
    120 On amd64, cross compile for arm:
    121 ```
    122 git clone --depth=1 https://github.com/minio/minio.git minio.git
    123 cd minio.git
    124 # https://github.com/golang/go/issues/65568
    125 sed -i 's/go 1.22/go 1.22.0/' go.mod
    126 
    127 GOARCH=arm CGO_ENABLED=0 go build -o minio github.com/minio/minio
    128 ```
    129 
    130 Copy this binary to the arm host.
    131 
    132 ## Isssues
    133 ### Issue: Missing LXC Library Dir
    134 
    135 ```
    136 Build 'lxc' errored: Error creating container: Command error: touch: /var/lib/lxc/packer-lxc/rootfs/tmp/.tmpfs: No such file or directory
    137 ```
    138 
    139 For lxc on Turris, create a symbolic link:
    140 ```bash
    141 ln -s /srv/lxc/ /var/lib/lxc
    142 ```
    143 (yep, it's [hardcoded](https://github.com/hashicorp/packer-plugin-lxc/blob/main/builder/lxc/step_lxc_create.go#L23))
    144 
    145 
    146 ### Issue: Script not Found
    147 ```bash
    148 chmod: cannot access '/tmp/script_9801.sh': No such file or directory
    149 /bin/sh: 1: /tmp/script_9801.sh: not found         
    150 ```
    151 
    152 Fix: Retry, most likely a timing bug on repeated builds
    153 
    154 
    155 ### Issue: Duplicate Nexctloud User
    156 ```
    157 Username is invalid because files already exist for this user
    158 ```
    159 
    160 Fix: Choose another Nextcloud admin user name
    161 ```bash
    162 $ PACKER_LOG=1 packer build -var 'nextcloud_admin_user=admin2' -var 'nextcloud_admin_pass=admin2' nextcloud-lxc.json 
    163 ```