github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/docs/design-proposals/environment-variables.md (about) 1 # Passing environment variables to VM 2 3 Virtlet uses [cloud-init](../cloud-init-data-generation.md) mechanism to pass 4 the environment variables from pod definition to the VM. 5 6 ## Virtlet-aware images 7 8 Virtlet uses `write_files` cloud-init module (`write_files` key in `user_data`) 9 to write the pod-defined environment variables into `/etc/cloud/environment` 10 using the same `KEY=value` format as used by `/etc/environment`. If the image 11 was prepared specifically for Virtlet it can then utilize this file to load 12 these values. 13 14 ## Simplified access to environment variables 15 16 Most Linux distributions use /etc/environment file to store the system-wide 17 list of environment variables. 18 Using [bootcmd](http://cloudinit.readthedocs.io/en/latest/topics/modules.html#bootcmd) 19 feature Virtlet can provide a script, which then will merge the content 20 of `/etc/environment` adding new lines from `/etc/cloud/environment` and setting 21 existing variables to values from the last file. 22 23 This could be done by extending the following script: 24 ```bash 25 TMPFILE=/tmp/env_$$ 26 cp /etc/cloud/environment ${TMPFILE} 27 cat /etc/environment | while read line ; do 28 VARIABLE="$(echo $line | sed s/=.*//)" 29 if ! egrep -q "^${VARIABLE}=" /etc/cloud/environment ; then 30 echo "$line" >>${TMPFILE} 31 fi 32 done 33 mv ${TMPFILE} /etc/environment 34 ``` 35 36 This way one can combine the content of `/etc/environment` and 37 `/etc/cloud/environment` with values from the latter taking precedence. 38 39 ## Forcing keeping original system environment values 40 41 We may additionally implement a method to pass information, that environment 42 values for variables from `/etc/environment` should be kept even when there 43 are new values for them in pod definition. Assuming that this will be controlled 44 with annotation `VirtletKeepVMEnvVars: true` - merge script should add variables 45 in reverse order - at start it should copy `/etc/environment` into temporary 46 file, then it should add lines from `/etc/cloud/environment` not found 47 in `/etc/environment`.