Compiling C++ with gcc in Kubernetes container

Note: Truenas 24.10 dropped Kubernetes for Docker. While this is still interesting if you use Kubernetes, it no longer applies to Truenas.

I recently purchased a Terramaster F4-424 Pro NAS, and quickly discovered that its temperature management was not so great. My hard disks would often hit 40°C and above and, while still well within the recommended maximum operating temperature, I like them cooler.

I found some code that was written for the F4-220. It reads the disk temperatures and adjusts the fan speed by interfacing with the IT8772E chipset. The problem is that the F4-424 has a different chipset, the IT8613E, so the code didn’t work. Because the code didn’t seem to be actively maintained, I decided to fork it and implement the changes.

As NAS operating system I chose Truenas Scale. In order to compile the C++ code, I needed gcc. The problem is that Truenas Scale does not allow you to install gcc with apt:

Truenas Scale however can run Kubernetes containers. I found a way to compile my code using a gcc container:

  1. In the same folder as the code you want to compile, create kubernetes-compile.yaml and adapt it for your use case.
apiVersion: batch/v1                                                                                                                                                                                                      
kind: Job                                                                                                                                                                                                                 
metadata:                                                                                                                                                                                                                 
  name: compile-job                                                                                                                                                                                                       
spec:
  ttlSecondsAfterFinished: 100                                                                                                                                                                                                                     
  template:                                                                                                                                                                                                               
    spec:                                                                                                                                                                                                                 
      containers:                                                                                                                                                                                                         
      - name: gcc-container                                                                                                                                                                                               
        image: gcc                                                                                                                                                                                                        
        command: ["sh", "-c", "gcc -o fancontrol fancontrol.cpp"]                                                                                                                                                         
        volumeMounts:                                                                                                                                                                                                     
        - name: myapp-volume                                                                                                                                                                                              
          mountPath: /usr/src/myapp                                                                                                                                                                                       
        workingDir: /usr/src/myapp                                                                                                                                                                                        
      restartPolicy: Never                                                                                                                                                                                                
      volumes:                                                                                                                                                                                                            
      - name: myapp-volume                                                                                                                                                                                                
        hostPath:                                                                                                                                                                                                         
          path: "/home/admin/terramaster-fancontrol"                                                                                                                                                                      
  backoffLimit: 0 
  1. Create the compile job:
    sudo k3s kubectl create -f kubernetes-compile.yaml
  2. Monitor the job:
    sudo k3s kubectl describe job $JOB
  3. When it’s finished, the job will be deleted after 100 seconds, or delete it manually:
    sudo k3s kubectl delete job compile-job

Now you should have your compiled program in the same folder as the code.

Niek

I'm a pharmacist who likes to travel, designing electrical stuff, HAM radio and sailing...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.