forked from apptainer/singularity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
179 lines (152 loc) · 5.94 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) 2018, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package imgbuild
import (
"fmt"
"net"
"net/rpc"
"os"
"os/exec"
"path/filepath"
"syscall"
"github.com/sylabs/singularity/internal/pkg/buildcfg"
"github.com/sylabs/singularity/internal/pkg/runtime/engines/singularity/rpc/client"
"github.com/sylabs/singularity/internal/pkg/sylog"
)
// CreateContainer creates a container
func (engine *EngineOperations) CreateContainer(pid int, rpcConn net.Conn) error {
if engine.CommonConfig.EngineName != Name {
return fmt.Errorf("engineName configuration doesn't match runtime name")
}
rpcOps := &client.RPC{
Client: rpc.NewClient(rpcConn),
Name: engine.CommonConfig.EngineName,
}
if rpcOps.Client == nil {
return fmt.Errorf("failed to initialiaze RPC client")
}
rootfs := engine.EngineConfig.Rootfs()
st, err := os.Stat(rootfs)
if err != nil {
return fmt.Errorf("stat on %s failed", rootfs)
}
if st.IsDir() == false {
return fmt.Errorf("%s is not a directory", rootfs)
}
sessionPath, err := filepath.EvalSymlinks(buildcfg.SESSIONDIR)
if err != nil {
return fmt.Errorf("failed to resolved session directory %s: %s", buildcfg.SESSIONDIR, err)
}
sylog.Debugf("Mounting image directory %s\n", rootfs)
_, err = rpcOps.Mount(rootfs, sessionPath, "", syscall.MS_BIND|syscall.MS_NOSUID|syscall.MS_NODEV, "errors=remount-ro")
if err != nil {
return fmt.Errorf("failed to mount directory filesystem %s: %s", rootfs, err)
}
sylog.Debugf("Mounting proc at %s\n", filepath.Join(sessionPath, "proc"))
_, err = rpcOps.Mount("/proc", filepath.Join(sessionPath, "proc"), "", syscall.MS_BIND|syscall.MS_NOSUID|syscall.MS_REC, "")
if err != nil {
return fmt.Errorf("mount proc failed: %s", err)
}
sylog.Debugf("Mounting sysfs at %s\n", filepath.Join(sessionPath, "sys"))
_, err = rpcOps.Mount("sysfs", filepath.Join(sessionPath, "sys"), "sysfs", syscall.MS_NOSUID, "")
if err != nil {
return fmt.Errorf("mount sys failed: %s", err)
}
sylog.Debugf("Mounting dev at %s\n", filepath.Join(sessionPath, "dev"))
_, err = rpcOps.Mount("/dev", filepath.Join(sessionPath, "dev"), "", syscall.MS_BIND|syscall.MS_NOSUID|syscall.MS_REC, "")
if err != nil {
return fmt.Errorf("mount /dev failed: %s", err)
}
sylog.Debugf("Mounting tmp at %s\n", filepath.Join(sessionPath, "tmp"))
_, err = rpcOps.Mount("/tmp", filepath.Join(sessionPath, "tmp"), "", syscall.MS_BIND|syscall.MS_NOSUID|syscall.MS_NODEV|syscall.MS_REC, "")
if err != nil {
return fmt.Errorf("mount /tmp failed: %s", err)
}
sylog.Debugf("Mounting var/tmp at %s\n", filepath.Join(sessionPath, "var/tmp"))
_, err = rpcOps.Mount("/var/tmp", filepath.Join(sessionPath, "var/tmp"), "", syscall.MS_BIND|syscall.MS_NOSUID|syscall.MS_NODEV|syscall.MS_REC, "")
if err != nil {
return fmt.Errorf("mount /var/tmp failed: %s", err)
}
sylog.Debugf("Mounting /etc/resolv.conf at %s\n", filepath.Join(sessionPath, "etc/resolv.conf"))
_, err = rpcOps.Mount("/etc/resolv.conf", filepath.Join(sessionPath, "etc/resolv.conf"), "", syscall.MS_BIND|syscall.MS_NOSUID|syscall.MS_REC, "")
if err != nil {
return fmt.Errorf("mount /etc/resolv.conf failed: %s", err)
}
sylog.Debugf("Mounting /etc/hosts at %s\n", filepath.Join(sessionPath, "etc/hosts"))
_, err = rpcOps.Mount("/etc/hosts", filepath.Join(sessionPath, "etc/hosts"), "", syscall.MS_BIND|syscall.MS_NOSUID|syscall.MS_REC, "")
if err != nil {
return fmt.Errorf("mount /etc/hosts failed: %s", err)
}
sylog.Debugf("Set RPC mount propagation flag to SLAVE")
_, err = rpcOps.Mount("", "/", "", syscall.MS_SLAVE|syscall.MS_REC, "")
if err != nil {
return fmt.Errorf("mount /etc/hosts failed: %s", err)
}
if engine.EngineConfig.RunSection("setup") && engine.EngineConfig.Recipe.BuildData.Setup != "" {
// Run %setup script here
setup := exec.Command("/bin/sh", "-cex", engine.EngineConfig.Recipe.BuildData.Setup)
setup.Env = engine.EngineConfig.OciConfig.Process.Env
setup.Stdout = os.Stdout
setup.Stderr = os.Stderr
sylog.Infof("Running setup scriptlet\n")
if err := setup.Start(); err != nil {
sylog.Fatalf("failed to start %%setup proc: %v\n", err)
}
if err := setup.Wait(); err != nil {
sylog.Fatalf("setup proc: %v\n", err)
}
}
if engine.EngineConfig.RunSection("files") {
sylog.Debugf("Copying files from host")
if err := engine.EngineConfig.copyFiles(); err != nil {
return fmt.Errorf("unable to copy files to container fs: %v", err)
}
}
sylog.Debugf("Chdir into %s\n", sessionPath)
err = syscall.Chdir(sessionPath)
if err != nil {
return fmt.Errorf("change directory failed: %s", err)
}
sylog.Debugf("Chroot into %s\n", sessionPath)
_, err = rpcOps.Chroot(sessionPath, true)
if err != nil {
sylog.Debugf("Fallback to move/chroot")
_, err = rpcOps.Chroot(sessionPath, false)
if err != nil {
return fmt.Errorf("chroot failed: %s", err)
}
}
sylog.Debugf("Chdir into / to avoid errors\n")
err = syscall.Chdir("/")
if err != nil {
return fmt.Errorf("change directory failed: %s", err)
}
if err := rpcOps.Client.Close(); err != nil {
return fmt.Errorf("can't close connection with rpc server: %s", err)
}
return nil
}
func (e *EngineConfig) copyFiles() error {
// iterate through filetransfers
for _, transfer := range e.Recipe.BuildData.Files {
// sanity
if transfer.Src == "" {
sylog.Warningf("Attempt to copy file with no name...")
continue
}
// dest = source if not specified
if transfer.Dst == "" {
transfer.Dst = transfer.Src
}
sylog.Infof("Copying %v to %v", transfer.Src, transfer.Dst)
// copy each file into bundle rootfs
transfer.Dst = filepath.Join(e.Rootfs(), transfer.Dst)
copy := exec.Command("/bin/cp", "-fLr", transfer.Src, transfer.Dst)
if err := copy.Run(); err != nil {
return fmt.Errorf("While copying %v to %v: %v", transfer.Src, transfer.Dst, err)
}
}
return nil
}