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 (4098B)


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