| 
									
										
										
										
											2019-04-26 03:39:49 +08:00
										 |  |  | // +build linux darwin
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package parse | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2019-10-22 09:59:15 +08:00
										 |  |  | 	"os" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2019-07-18 16:42:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-06 19:40:34 +08:00
										 |  |  | 	"github.com/containers/common/pkg/unshare" | 
					
						
							| 
									
										
										
										
											2019-09-13 05:44:50 +08:00
										 |  |  | 	"github.com/opencontainers/runc/libcontainer/configs" | 
					
						
							|  |  |  | 	"github.com/opencontainers/runc/libcontainer/devices" | 
					
						
							|  |  |  | 	"github.com/pkg/errors" | 
					
						
							| 
									
										
										
										
											2019-04-26 03:39:49 +08:00
										 |  |  | 	"golang.org/x/sys/unix" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func getDefaultProcessLimits() []string { | 
					
						
							|  |  |  | 	rlim := unix.Rlimit{Cur: 1048576, Max: 1048576} | 
					
						
							|  |  |  | 	defaultLimits := []string{} | 
					
						
							|  |  |  | 	if err := unix.Setrlimit(unix.RLIMIT_NOFILE, &rlim); err == nil { | 
					
						
							|  |  |  | 		defaultLimits = append(defaultLimits, fmt.Sprintf("nofile=%d:%d", rlim.Cur, rlim.Max)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if err := unix.Setrlimit(unix.RLIMIT_NPROC, &rlim); err == nil { | 
					
						
							|  |  |  | 		defaultLimits = append(defaultLimits, fmt.Sprintf("nproc=%d:%d", rlim.Cur, rlim.Max)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return defaultLimits | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-09-13 05:44:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-22 09:59:15 +08:00
										 |  |  | func DeviceFromPath(device string) ([]configs.Device, error) { | 
					
						
							|  |  |  | 	var devs []configs.Device | 
					
						
							| 
									
										
										
										
											2019-09-13 05:44:50 +08:00
										 |  |  | 	src, dst, permissions, err := Device(device) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2019-10-22 09:59:15 +08:00
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2019-09-13 05:44:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if unshare.IsRootless() { | 
					
						
							| 
									
										
										
										
											2019-10-22 09:59:15 +08:00
										 |  |  | 		return nil, errors.Errorf("Renaming device %s to %s is not a supported in rootless containers", src, dst) | 
					
						
							| 
									
										
										
										
											2019-09-13 05:44:50 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-10-22 09:59:15 +08:00
										 |  |  | 	srcInfo, err := os.Stat(src) | 
					
						
							| 
									
										
										
										
											2019-09-13 05:44:50 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2019-10-22 09:59:15 +08:00
										 |  |  | 		return nil, errors.Wrapf(err, "error getting info of source device %s", src) | 
					
						
							| 
									
										
										
										
											2019-09-13 05:44:50 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-10-22 09:59:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if !srcInfo.IsDir() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		dev, err := devices.DeviceFromPath(src, permissions) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, errors.Wrapf(err, "%s is not a valid device", src) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		dev.Path = dst | 
					
						
							|  |  |  | 		devs = append(devs, *dev) | 
					
						
							|  |  |  | 		return devs, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// If source device is a directory
 | 
					
						
							|  |  |  | 	srcDevices, err := devices.GetDevices(src) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, errors.Wrapf(err, "error getting source devices from directory %s", src) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for _, d := range srcDevices { | 
					
						
							|  |  |  | 		d.Path = filepath.Join(dst, filepath.Base(d.Path)) | 
					
						
							|  |  |  | 		d.Permissions = permissions | 
					
						
							|  |  |  | 		devs = append(devs, *d) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return devs, nil | 
					
						
							| 
									
										
										
										
											2019-09-13 05:44:50 +08:00
										 |  |  | } |