github.com/apptainer/singularity@v3.1.1+incompatible/internal/pkg/build/sources/base_environment.go (about) 1 // Copyright (c) 2018, Sylabs Inc. All rights reserved. 2 // This software is licensed under a 3-clause BSD license. Please consult the 3 // LICENSE.md file distributed with the sources of this project regarding your 4 // rights to use or distribute this software. 5 6 package sources 7 8 import ( 9 "fmt" 10 "os" 11 "path/filepath" 12 ) 13 14 const ( 15 // Contents of /.singularity.d/actions/exec 16 execFileContent = `#!/bin/sh 17 18 for script in /.singularity.d/env/*.sh; do 19 if [ -f "$script" ]; then 20 . "$script" 21 fi 22 done 23 24 exec "$@" 25 ` 26 // Contents of /.singularity.d/actions/run 27 runFileContent = `#!/bin/sh 28 29 for script in /.singularity.d/env/*.sh; do 30 if [ -f "$script" ]; then 31 . "$script" 32 fi 33 done 34 35 if test -n "${SINGULARITY_APPNAME:-}"; then 36 37 if test -x "/scif/apps/${SINGULARITY_APPNAME:-}/scif/runscript"; then 38 exec "/scif/apps/${SINGULARITY_APPNAME:-}/scif/runscript" "$@" 39 else 40 echo "No Singularity runscript for contained app: ${SINGULARITY_APPNAME:-}" 41 exit 1 42 fi 43 44 elif test -x "/.singularity.d/runscript"; then 45 exec "/.singularity.d/runscript" "$@" 46 else 47 echo "No Singularity runscript found, executing /bin/sh" 48 exec /bin/sh "$@" 49 fi 50 ` 51 // Contents of /.singularity.d/actions/shell 52 shellFileContent = `#!/bin/sh 53 54 for script in /.singularity.d/env/*.sh; do 55 if [ -f "$script" ]; then 56 . "$script" 57 fi 58 done 59 60 if test -n "$SINGULARITY_SHELL" -a -x "$SINGULARITY_SHELL"; then 61 exec $SINGULARITY_SHELL "$@" 62 63 echo "ERROR: Failed running shell as defined by '\$SINGULARITY_SHELL'" 1>&2 64 exit 1 65 66 elif test -x /bin/bash; then 67 SHELL=/bin/bash 68 PS1="Singularity $SINGULARITY_NAME:\\w> " 69 export SHELL PS1 70 exec /bin/bash --norc "$@" 71 elif test -x /bin/sh; then 72 SHELL=/bin/sh 73 export SHELL 74 exec /bin/sh "$@" 75 else 76 echo "ERROR: /bin/sh does not exist in container" 1>&2 77 fi 78 exit 1 79 ` 80 // Contents of /.singularity.d/actions/start 81 startFileContent = `#!/bin/sh 82 83 # if we are here start notify PID 1 to continue 84 # DON'T REMOVE 85 kill -CONT 1 86 87 for script in /.singularity.d/env/*.sh; do 88 if [ -f "$script" ]; then 89 . "$script" 90 fi 91 done 92 93 if test -x "/.singularity.d/startscript"; then 94 exec "/.singularity.d/startscript" 95 fi 96 ` 97 // Contents of /.singularity.d/actions/test 98 testFileContent = `#!/bin/sh 99 100 for script in /.singularity.d/env/*.sh; do 101 if [ -f "$script" ]; then 102 . "$script" 103 fi 104 done 105 106 107 if test -n "${SINGULARITY_APPNAME:-}"; then 108 109 if test -x "/scif/apps/${SINGULARITY_APPNAME:-}/scif/test"; then 110 exec "/scif/apps/${SINGULARITY_APPNAME:-}/scif/test" "$@" 111 else 112 echo "No Singularity tests for contained app: ${SINGULARITY_APPNAME:-}" 113 exit 1 114 fi 115 elif test -x "/.singularity.d/test"; then 116 exec "/.singularity.d/test" "$@" 117 else 118 echo "No Singularity container test found, executing /bin/sh" 119 exec /bin/sh "$@" 120 fi 121 ` 122 // Contents of /.singularity.d/env/01-base.sh 123 baseShFileContent = `#!/bin/sh 124 # 125 # Copyright (c) 2017, SingularityWare, LLC. All rights reserved. 126 # 127 # Copyright (c) 2015-2017, Gregory M. Kurtzer. All rights reserved. 128 # 129 # Copyright (c) 2016-2017, The Regents of the University of California, 130 # through Lawrence Berkeley National Laboratory (subject to receipt of any 131 # required approvals from the U.S. Dept. of Energy). All rights reserved. 132 # 133 # This software is licensed under a customized 3-clause BSD license. Please 134 # consult LICENSE.md file distributed with the sources of this project regarding 135 # your rights to use or distribute this software. 136 # 137 # NOTICE. This Software was developed under funding from the U.S. Department of 138 # Energy and the U.S. Government consequently retains certain rights. As such, 139 # the U.S. Government has been granted for itself and others acting on its 140 # behalf a paid-up, nonexclusive, irrevocable, worldwide license in the Software 141 # to reproduce, distribute copies to the public, prepare derivative works, and 142 # perform publicly and display publicly, and to permit other to do so. 143 # 144 # 145 146 147 ` 148 // Contents of /.singularity.d/env/90-environment.sh and /.singularity.d/env/91-environment.sh 149 environmentShFileContent = `#!/bin/sh 150 #Custom environment shell code should follow 151 152 ` 153 // Contents of /.singularity.d/env/95-apps.sh 154 appsShFileContent = `#!/bin/sh 155 # 156 # Copyright (c) 2017, SingularityWare, LLC. All rights reserved. 157 # 158 # See the COPYRIGHT.md file at the top-level directory of this distribution and at 159 # https://github.com/sylabs/singularity/blob/master/COPYRIGHT.md. 160 # 161 # This file is part of the Singularity Linux container project. It is subject to the license 162 # terms in the LICENSE.md file found in the top-level directory of this distribution and 163 # at https://github.com/sylabs/singularity/blob/master/LICENSE.md. No part 164 # of Singularity, including this file, may be copied, modified, propagated, or distributed 165 # except according to the terms contained in the LICENSE.md file. 166 167 168 if test -n "${SINGULARITY_APPNAME:-}"; then 169 170 # The active app should be exported 171 export SINGULARITY_APPNAME 172 173 if test -d "/scif/apps/${SINGULARITY_APPNAME:-}/"; then 174 SCIF_APPS="/scif/apps" 175 SCIF_APPROOT="/scif/apps/${SINGULARITY_APPNAME:-}" 176 export SCIF_APPROOT SCIF_APPS 177 PATH="/scif/apps/${SINGULARITY_APPNAME:-}:$PATH" 178 179 # Automatically add application bin to path 180 if test -d "/scif/apps/${SINGULARITY_APPNAME:-}/bin"; then 181 PATH="/scif/apps/${SINGULARITY_APPNAME:-}/bin:$PATH" 182 fi 183 184 # Automatically add application lib to LD_LIBRARY_PATH 185 if test -d "/scif/apps/${SINGULARITY_APPNAME:-}/lib"; then 186 LD_LIBRARY_PATH="/scif/apps/${SINGULARITY_APPNAME:-}/lib:$LD_LIBRARY_PATH" 187 export LD_LIBRARY_PATH 188 fi 189 190 # Automatically source environment 191 if [ -f "/scif/apps/${SINGULARITY_APPNAME:-}/scif/env/01-base.sh" ]; then 192 . "/scif/apps/${SINGULARITY_APPNAME:-}/scif/env/01-base.sh" 193 fi 194 if [ -f "/scif/apps/${SINGULARITY_APPNAME:-}/scif/env/90-environment.sh" ]; then 195 . "/scif/apps/${SINGULARITY_APPNAME:-}/scif/env/90-environment.sh" 196 fi 197 198 export PATH 199 else 200 echo "Could not locate the container application: ${SINGULARITY_APPNAME}" 201 exit 1 202 fi 203 fi 204 205 ` 206 // Contents of /.singularity.d/env/99-base.sh 207 base99ShFileContent = `#!/bin/sh 208 # 209 # Copyright (c) 2017, SingularityWare, LLC. All rights reserved. 210 # 211 # Copyright (c) 2015-2017, Gregory M. Kurtzer. All rights reserved. 212 # 213 # Copyright (c) 2016-2017, The Regents of the University of California, 214 # through Lawrence Berkeley National Laboratory (subject to receipt of any 215 # required approvals from the U.S. Dept. of Energy). All rights reserved. 216 # 217 # This software is licensed under a customized 3-clause BSD license. Please 218 # consult LICENSE.md file distributed with the sources of this project regarding 219 # your rights to use or distribute this software. 220 # 221 # NOTICE. This Software was developed under funding from the U.S. Department of 222 # Energy and the U.S. Government consequently retains certain rights. As such, 223 # the U.S. Government has been granted for itself and others acting on its 224 # behalf a paid-up, nonexclusive, irrevocable, worldwide license in the Software 225 # to reproduce, distribute copies to the public, prepare derivative works, and 226 # perform publicly and display publicly, and to permit other to do so. 227 # 228 # 229 230 231 if [ -z "$LD_LIBRARY_PATH" ]; then 232 LD_LIBRARY_PATH="/.singularity.d/libs" 233 else 234 LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/.singularity.d/libs" 235 fi 236 237 PS1="Singularity> " 238 export LD_LIBRARY_PATH PS1 239 ` 240 241 // Contents of /.singularity.d/env/99-runtimevars.sh 242 base99runtimevarsShFileContent = `#!/bin/sh 243 # Copyright (c) 2017-2018, SyLabs, Inc. All rights reserved. 244 # 245 # This software is licensed under a customized 3-clause BSD license. Please 246 # consult LICENSE.md file distributed with the sources of this project regarding 247 # your rights to use or distribute this software. 248 # 249 # 250 251 if [ -n "${SING_USER_DEFINED_PREPEND_PATH:-}" ]; then 252 PATH="${SING_USER_DEFINED_PREPEND_PATH}:${PATH}" 253 fi 254 255 if [ -n "${SING_USER_DEFINED_APPEND_PATH:-}" ]; then 256 PATH="${PATH}:${SING_USER_DEFINED_APPEND_PATH}" 257 fi 258 259 if [ -n "${SING_USER_DEFINED_PATH:-}" ]; then 260 PATH="${SING_USER_DEFINED_PATH}" 261 fi 262 263 unset SING_USER_DEFINED_PREPEND_PATH \ 264 SING_USER_DEFINED_APPEND_PATH \ 265 SING_USER_DEFINED_PATH 266 267 export PATH 268 ` 269 270 // Contents of /.singularity.d/runscript 271 runscriptFileContent = `#!/bin/sh 272 273 echo "There is no runscript defined for this container\n"; 274 ` 275 // Contents of /.singularity.d/startscript 276 startscriptFileContent = `#!/bin/sh 277 ` 278 ) 279 280 func makeDirs(rootPath string) (err error) { 281 if err = os.MkdirAll(filepath.Join(rootPath, ".singularity.d", "libs"), 0755); err != nil { 282 return 283 } 284 if err = os.MkdirAll(filepath.Join(rootPath, ".singularity.d", "actions"), 0755); err != nil { 285 return 286 } 287 if err = os.MkdirAll(filepath.Join(rootPath, ".singularity.d", "env"), 0755); err != nil { 288 return 289 } 290 if err = os.MkdirAll(filepath.Join(rootPath, "dev"), 0755); err != nil { 291 return 292 } 293 if err = os.MkdirAll(filepath.Join(rootPath, "proc"), 0755); err != nil { 294 return 295 } 296 if err = os.MkdirAll(filepath.Join(rootPath, "root"), 0755); err != nil { 297 return 298 } 299 if err = os.MkdirAll(filepath.Join(rootPath, "var", "tmp"), 0755); err != nil { 300 return 301 } 302 if err = os.MkdirAll(filepath.Join(rootPath, "tmp"), 0755); err != nil { 303 return 304 } 305 if err = os.MkdirAll(filepath.Join(rootPath, "etc"), 0755); err != nil { 306 return 307 } 308 if err = os.MkdirAll(filepath.Join(rootPath, "sys"), 0755); err != nil { 309 return 310 } 311 if err = os.MkdirAll(filepath.Join(rootPath, "home"), 0755); err != nil { 312 return 313 } 314 return 315 } 316 317 func makeSymlinks(rootPath string) (err error) { 318 if _, err := os.Stat(filepath.Join(rootPath, "singularity")); err != nil { 319 if err = os.Symlink(".singularity.d/runscript", filepath.Join(rootPath, "singularity")); err != nil { 320 return err 321 } 322 } 323 if _, err := os.Stat(filepath.Join(rootPath, ".run")); err != nil { 324 if err = os.Symlink(".singularity.d/actions/run", filepath.Join(rootPath, ".run")); err != nil { 325 return err 326 } 327 } 328 if _, err := os.Stat(filepath.Join(rootPath, ".exec")); err != nil { 329 if err = os.Symlink(".singularity.d/actions/exec", filepath.Join(rootPath, ".exec")); err != nil { 330 return err 331 } 332 } 333 if _, err := os.Stat(filepath.Join(rootPath, ".test")); err != nil { 334 if err = os.Symlink(".singularity.d/actions/test", filepath.Join(rootPath, ".test")); err != nil { 335 return err 336 } 337 } 338 if _, err := os.Stat(filepath.Join(rootPath, ".shell")); err != nil { 339 if err = os.Symlink(".singularity.d/actions/shell", filepath.Join(rootPath, ".shell")); err != nil { 340 return err 341 } 342 } 343 if _, err := os.Stat(filepath.Join(rootPath, "environment")); err != nil { 344 if err = os.Symlink(".singularity.d/env/90-environment.sh", filepath.Join(rootPath, "environment")); err != nil { 345 return err 346 } 347 } 348 return 349 } 350 351 func makeFile(name string, perm os.FileMode, s string) (err error) { 352 f, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) 353 if err != nil { 354 return 355 } 356 defer f.Close() 357 358 _, err = f.WriteString(s) 359 return 360 } 361 362 func makeFiles(rootPath string) (err error) { 363 if err = makeFile(filepath.Join(rootPath, "etc", "hosts"), 0644, ""); err != nil { 364 return 365 } 366 if err = makeFile(filepath.Join(rootPath, "etc", "resolv.conf"), 0644, ""); err != nil { 367 return 368 } 369 if err = makeFile(filepath.Join(rootPath, ".singularity.d", "actions", "exec"), 0755, execFileContent); err != nil { 370 return 371 } 372 if err = makeFile(filepath.Join(rootPath, ".singularity.d", "actions", "run"), 0755, runFileContent); err != nil { 373 return 374 } 375 if err = makeFile(filepath.Join(rootPath, ".singularity.d", "actions", "shell"), 0755, shellFileContent); err != nil { 376 return 377 } 378 if err = makeFile(filepath.Join(rootPath, ".singularity.d", "actions", "start"), 0755, startFileContent); err != nil { 379 return 380 } 381 if err = makeFile(filepath.Join(rootPath, ".singularity.d", "actions", "test"), 0755, testFileContent); err != nil { 382 return 383 } 384 if err = makeFile(filepath.Join(rootPath, ".singularity.d", "env", "01-base.sh"), 0755, baseShFileContent); err != nil { 385 return 386 } 387 if err = makeFile(filepath.Join(rootPath, ".singularity.d", "env", "90-environment.sh"), 0755, environmentShFileContent); err != nil { 388 return 389 } 390 if err = makeFile(filepath.Join(rootPath, ".singularity.d", "env", "95-apps.sh"), 0755, appsShFileContent); err != nil { 391 return 392 } 393 if err = makeFile(filepath.Join(rootPath, ".singularity.d", "env", "99-base.sh"), 0755, base99ShFileContent); err != nil { 394 return 395 } 396 if err = makeFile(filepath.Join(rootPath, ".singularity.d", "env", "99-runtimevars.sh"), 0755, base99runtimevarsShFileContent); err != nil { 397 return 398 } 399 if err = makeFile(filepath.Join(rootPath, ".singularity.d", "runscript"), 0755, runscriptFileContent); err != nil { 400 return 401 } 402 if err = makeFile(filepath.Join(rootPath, ".singularity.d", "startscript"), 0755, startscriptFileContent); err != nil { 403 return 404 } 405 return 406 } 407 408 func makeBaseEnv(rootPath string) (err error) { 409 if err = makeDirs(rootPath); err != nil { 410 err = fmt.Errorf("build: failed to make environment dirs: %v", err) 411 return 412 } 413 if err = makeSymlinks(rootPath); err != nil { 414 err = fmt.Errorf("build: failed to make environment symlinks: %v", err) 415 return 416 } 417 if err = makeFiles(rootPath); err != nil { 418 err = fmt.Errorf("build: failed to make environment files: %v", err) 419 return 420 } 421 422 return 423 }