2016-11-14 01:26:59 +00:00
|
|
|
{ runCommand
|
2021-01-24 00:40:18 +00:00
|
|
|
, lib
|
2016-11-14 01:26:59 +00:00
|
|
|
, stdenv
|
|
|
|
, storeDir ? builtins.storeDir
|
|
|
|
, writeScript
|
|
|
|
, singularity
|
|
|
|
, writeReferencesToFile
|
|
|
|
, bash
|
|
|
|
, vmTools
|
|
|
|
, gawk
|
2020-11-24 15:29:28 +00:00
|
|
|
, util-linux
|
2019-02-26 11:45:54 +00:00
|
|
|
, runtimeShell
|
2018-07-21 01:44:44 +01:00
|
|
|
, e2fsprogs }:
|
2016-11-14 01:26:59 +00:00
|
|
|
|
|
|
|
rec {
|
|
|
|
shellScript = name: text:
|
|
|
|
writeScript name ''
|
2019-02-26 11:45:54 +00:00
|
|
|
#!${runtimeShell}
|
2016-11-14 01:26:59 +00:00
|
|
|
set -e
|
|
|
|
${text}
|
|
|
|
'';
|
|
|
|
|
|
|
|
mkLayer = {
|
|
|
|
name,
|
|
|
|
contents ? [],
|
|
|
|
}:
|
|
|
|
runCommand "singularity-layer-${name}" {
|
|
|
|
inherit contents;
|
|
|
|
} ''
|
|
|
|
mkdir $out
|
|
|
|
for f in $contents ; do
|
|
|
|
cp -ra $f $out/
|
|
|
|
done
|
|
|
|
'';
|
|
|
|
|
|
|
|
buildImage = {
|
|
|
|
name,
|
|
|
|
contents ? [],
|
|
|
|
diskSize ? 1024,
|
|
|
|
runScript ? "#!${stdenv.shell}\nexec /bin/sh",
|
2019-06-16 20:59:06 +01:00
|
|
|
runAsRoot ? null
|
2016-11-14 01:26:59 +00:00
|
|
|
}:
|
|
|
|
let layer = mkLayer {
|
|
|
|
inherit name;
|
|
|
|
contents = contents ++ [ bash runScriptFile ];
|
|
|
|
};
|
|
|
|
runAsRootFile = shellScript "run-as-root.sh" runAsRoot;
|
|
|
|
runScriptFile = shellScript "run-script.sh" runScript;
|
|
|
|
result = vmTools.runInLinuxVM (
|
|
|
|
runCommand "singularity-image-${name}.img" {
|
2020-11-24 15:29:28 +00:00
|
|
|
buildInputs = [ singularity e2fsprogs util-linux gawk ];
|
2016-11-14 01:26:59 +00:00
|
|
|
layerClosure = writeReferencesToFile layer;
|
|
|
|
preVM = vmTools.createEmptyImage {
|
|
|
|
size = diskSize;
|
|
|
|
fullName = "singularity-run-disk";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
''
|
|
|
|
rm -rf $out
|
|
|
|
mkdir disk
|
|
|
|
mkfs -t ext3 -b 4096 /dev/${vmTools.hd}
|
|
|
|
mount /dev/${vmTools.hd} disk
|
2020-01-30 23:44:01 +00:00
|
|
|
mkdir -p disk/img
|
|
|
|
cd disk/img
|
2017-11-07 03:47:20 +00:00
|
|
|
mkdir proc sys dev
|
2016-11-14 01:26:59 +00:00
|
|
|
|
|
|
|
# Run root script
|
2021-01-24 00:40:18 +00:00
|
|
|
${lib.optionalString (runAsRoot != null) ''
|
2016-11-14 01:26:59 +00:00
|
|
|
mkdir -p ./${storeDir}
|
|
|
|
mount --rbind ${storeDir} ./${storeDir}
|
|
|
|
unshare -imnpuf --mount-proc chroot ./ ${runAsRootFile}
|
|
|
|
umount -R ./${storeDir}
|
|
|
|
''}
|
|
|
|
|
|
|
|
# Build /bin and copy across closure
|
2021-10-27 07:11:31 +01:00
|
|
|
mkdir -p bin ./${builtins.storeDir}
|
2016-11-14 01:26:59 +00:00
|
|
|
for f in $(cat $layerClosure) ; do
|
|
|
|
cp -ar $f ./$f
|
2017-10-10 19:41:54 +01:00
|
|
|
done
|
|
|
|
|
|
|
|
for c in ${toString contents} ; do
|
|
|
|
for f in $c/bin/* ; do
|
2016-11-14 01:26:59 +00:00
|
|
|
if [ ! -e bin/$(basename $f) ] ; then
|
|
|
|
ln -s $f bin/
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
2020-01-30 23:44:01 +00:00
|
|
|
# Create runScript and link shell
|
2020-07-13 22:20:14 +01:00
|
|
|
if [ ! -e bin/sh ]; then
|
|
|
|
ln -s ${runtimeShell} bin/sh
|
|
|
|
fi
|
2020-01-30 23:44:01 +00:00
|
|
|
mkdir -p .singularity.d
|
|
|
|
ln -s ${runScriptFile} .singularity.d/runscript
|
2016-11-14 01:26:59 +00:00
|
|
|
|
2018-11-01 00:21:02 +00:00
|
|
|
# Fill out .singularity.d
|
|
|
|
mkdir -p .singularity.d/env
|
|
|
|
touch .singularity.d/env/94-appsbase.sh
|
2016-11-14 01:26:59 +00:00
|
|
|
|
2018-11-01 00:21:02 +00:00
|
|
|
cd ..
|
2018-06-18 22:50:06 +01:00
|
|
|
mkdir -p /var/singularity/mnt/{container,final,overlay,session,source}
|
2018-11-01 00:21:02 +00:00
|
|
|
echo "root:x:0:0:System administrator:/root:/bin/sh" > /etc/passwd
|
2020-07-15 13:05:37 +01:00
|
|
|
echo > /etc/resolv.conf
|
2020-01-30 23:44:01 +00:00
|
|
|
TMPDIR=$(pwd -P) singularity build $out ./img
|
2016-11-14 01:26:59 +00:00
|
|
|
'');
|
|
|
|
|
|
|
|
in result;
|
|
|
|
}
|