generate.go 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874
  1. // Package generate implements functions generating container config files.
  2. package generate
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "os"
  8. "strings"
  9. rspec "github.com/opencontainers/runtime-spec/specs-go"
  10. "github.com/opencontainers/runtime-tools/generate/seccomp"
  11. capsCheck "github.com/opencontainers/runtime-tools/validate/capabilities"
  12. "github.com/syndtr/gocapability/capability"
  13. )
  14. var (
  15. // Namespaces include the names of supported namespaces.
  16. Namespaces = []string{"network", "pid", "mount", "ipc", "uts", "user", "cgroup"}
  17. // we don't care about order...and this is way faster...
  18. removeFunc = func(s []string, i int) []string {
  19. s[i] = s[len(s)-1]
  20. return s[:len(s)-1]
  21. }
  22. )
  23. // Generator represents a generator for a container config.
  24. type Generator struct {
  25. Config *rspec.Spec
  26. HostSpecific bool
  27. // This is used to keep a cache of the ENVs added to improve
  28. // performance when adding a huge number of ENV variables
  29. envMap map[string]int
  30. }
  31. // ExportOptions have toggles for exporting only certain parts of the specification
  32. type ExportOptions struct {
  33. Seccomp bool // seccomp toggles if only seccomp should be exported
  34. }
  35. // New creates a configuration Generator with the default
  36. // configuration for the target operating system.
  37. func New(os string) (generator Generator, err error) {
  38. if os != "linux" && os != "solaris" && os != "windows" && os != "freebsd" {
  39. return generator, fmt.Errorf("no defaults configured for %s", os)
  40. }
  41. config := rspec.Spec{
  42. Version: rspec.Version,
  43. Hostname: "mrsdalloway",
  44. }
  45. if os == "windows" {
  46. config.Process = &rspec.Process{
  47. Args: []string{
  48. "cmd",
  49. },
  50. Cwd: `C:\`,
  51. }
  52. config.Windows = &rspec.Windows{}
  53. } else {
  54. config.Root = &rspec.Root{
  55. Path: "rootfs",
  56. Readonly: false,
  57. }
  58. config.Process = &rspec.Process{
  59. Terminal: false,
  60. Args: []string{
  61. "sh",
  62. },
  63. }
  64. }
  65. if os == "linux" || os == "solaris" || os == "freebsd" {
  66. config.Process.User = rspec.User{}
  67. config.Process.Env = []string{
  68. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  69. "TERM=xterm",
  70. }
  71. config.Process.Cwd = "/"
  72. config.Process.Rlimits = []rspec.POSIXRlimit{
  73. {
  74. Type: "RLIMIT_NOFILE",
  75. Hard: uint64(1024),
  76. Soft: uint64(1024),
  77. },
  78. }
  79. }
  80. if os == "linux" {
  81. config.Process.Capabilities = &rspec.LinuxCapabilities{
  82. Bounding: []string{
  83. "CAP_CHOWN",
  84. "CAP_DAC_OVERRIDE",
  85. "CAP_FSETID",
  86. "CAP_FOWNER",
  87. "CAP_MKNOD",
  88. "CAP_NET_RAW",
  89. "CAP_SETGID",
  90. "CAP_SETUID",
  91. "CAP_SETFCAP",
  92. "CAP_SETPCAP",
  93. "CAP_NET_BIND_SERVICE",
  94. "CAP_SYS_CHROOT",
  95. "CAP_KILL",
  96. "CAP_AUDIT_WRITE",
  97. },
  98. Permitted: []string{
  99. "CAP_CHOWN",
  100. "CAP_DAC_OVERRIDE",
  101. "CAP_FSETID",
  102. "CAP_FOWNER",
  103. "CAP_MKNOD",
  104. "CAP_NET_RAW",
  105. "CAP_SETGID",
  106. "CAP_SETUID",
  107. "CAP_SETFCAP",
  108. "CAP_SETPCAP",
  109. "CAP_NET_BIND_SERVICE",
  110. "CAP_SYS_CHROOT",
  111. "CAP_KILL",
  112. "CAP_AUDIT_WRITE",
  113. },
  114. Inheritable: []string{
  115. "CAP_CHOWN",
  116. "CAP_DAC_OVERRIDE",
  117. "CAP_FSETID",
  118. "CAP_FOWNER",
  119. "CAP_MKNOD",
  120. "CAP_NET_RAW",
  121. "CAP_SETGID",
  122. "CAP_SETUID",
  123. "CAP_SETFCAP",
  124. "CAP_SETPCAP",
  125. "CAP_NET_BIND_SERVICE",
  126. "CAP_SYS_CHROOT",
  127. "CAP_KILL",
  128. "CAP_AUDIT_WRITE",
  129. },
  130. Effective: []string{
  131. "CAP_CHOWN",
  132. "CAP_DAC_OVERRIDE",
  133. "CAP_FSETID",
  134. "CAP_FOWNER",
  135. "CAP_MKNOD",
  136. "CAP_NET_RAW",
  137. "CAP_SETGID",
  138. "CAP_SETUID",
  139. "CAP_SETFCAP",
  140. "CAP_SETPCAP",
  141. "CAP_NET_BIND_SERVICE",
  142. "CAP_SYS_CHROOT",
  143. "CAP_KILL",
  144. "CAP_AUDIT_WRITE",
  145. },
  146. Ambient: []string{
  147. "CAP_CHOWN",
  148. "CAP_DAC_OVERRIDE",
  149. "CAP_FSETID",
  150. "CAP_FOWNER",
  151. "CAP_MKNOD",
  152. "CAP_NET_RAW",
  153. "CAP_SETGID",
  154. "CAP_SETUID",
  155. "CAP_SETFCAP",
  156. "CAP_SETPCAP",
  157. "CAP_NET_BIND_SERVICE",
  158. "CAP_SYS_CHROOT",
  159. "CAP_KILL",
  160. "CAP_AUDIT_WRITE",
  161. },
  162. }
  163. config.Mounts = []rspec.Mount{
  164. {
  165. Destination: "/proc",
  166. Type: "proc",
  167. Source: "proc",
  168. Options: []string{"nosuid", "noexec", "nodev"},
  169. },
  170. {
  171. Destination: "/dev",
  172. Type: "tmpfs",
  173. Source: "tmpfs",
  174. Options: []string{"nosuid", "noexec", "strictatime", "mode=755", "size=65536k"},
  175. },
  176. {
  177. Destination: "/dev/pts",
  178. Type: "devpts",
  179. Source: "devpts",
  180. Options: []string{"nosuid", "noexec", "newinstance", "ptmxmode=0666", "mode=0620", "gid=5"},
  181. },
  182. {
  183. Destination: "/dev/shm",
  184. Type: "tmpfs",
  185. Source: "shm",
  186. Options: []string{"nosuid", "noexec", "nodev", "mode=1777", "size=65536k"},
  187. },
  188. {
  189. Destination: "/dev/mqueue",
  190. Type: "mqueue",
  191. Source: "mqueue",
  192. Options: []string{"nosuid", "noexec", "nodev"},
  193. },
  194. {
  195. Destination: "/sys",
  196. Type: "sysfs",
  197. Source: "sysfs",
  198. Options: []string{"nosuid", "noexec", "nodev", "ro"},
  199. },
  200. }
  201. config.Linux = &rspec.Linux{
  202. Resources: &rspec.LinuxResources{
  203. Devices: []rspec.LinuxDeviceCgroup{
  204. {
  205. Allow: false,
  206. Access: "rwm",
  207. },
  208. },
  209. },
  210. Namespaces: []rspec.LinuxNamespace{
  211. {
  212. Type: "pid",
  213. },
  214. {
  215. Type: "network",
  216. },
  217. {
  218. Type: "ipc",
  219. },
  220. {
  221. Type: "uts",
  222. },
  223. {
  224. Type: "mount",
  225. },
  226. },
  227. Seccomp: seccomp.DefaultProfile(&config),
  228. }
  229. } else if os == "freebsd" {
  230. config.Mounts = []rspec.Mount{
  231. {
  232. Destination: "/dev",
  233. Type: "devfs",
  234. Source: "devfs",
  235. Options: []string{"ruleset=4"},
  236. },
  237. {
  238. Destination: "/dev/fd",
  239. Type: "fdescfs",
  240. Source: "fdesc",
  241. Options: []string{},
  242. },
  243. }
  244. }
  245. envCache := map[string]int{}
  246. if config.Process != nil {
  247. envCache = createEnvCacheMap(config.Process.Env)
  248. }
  249. return Generator{Config: &config, envMap: envCache}, nil
  250. }
  251. // NewFromSpec creates a configuration Generator from a given
  252. // configuration.
  253. func NewFromSpec(config *rspec.Spec) Generator {
  254. envCache := map[string]int{}
  255. if config != nil && config.Process != nil {
  256. envCache = createEnvCacheMap(config.Process.Env)
  257. }
  258. return Generator{
  259. Config: config,
  260. envMap: envCache,
  261. }
  262. }
  263. // NewFromFile loads the template specified in a file into a
  264. // configuration Generator.
  265. func NewFromFile(path string) (Generator, error) {
  266. cf, err := os.Open(path)
  267. if err != nil {
  268. if os.IsNotExist(err) {
  269. return Generator{}, fmt.Errorf("template configuration at %s not found", path)
  270. }
  271. return Generator{}, err
  272. }
  273. defer cf.Close()
  274. return NewFromTemplate(cf)
  275. }
  276. // NewFromTemplate loads the template from io.Reader into a
  277. // configuration Generator.
  278. func NewFromTemplate(r io.Reader) (Generator, error) {
  279. var config rspec.Spec
  280. if err := json.NewDecoder(r).Decode(&config); err != nil {
  281. return Generator{}, err
  282. }
  283. envCache := map[string]int{}
  284. if config.Process != nil {
  285. envCache = createEnvCacheMap(config.Process.Env)
  286. }
  287. return Generator{
  288. Config: &config,
  289. envMap: envCache,
  290. }, nil
  291. }
  292. // createEnvCacheMap creates a hash map with the ENV variables given by the config
  293. func createEnvCacheMap(env []string) map[string]int {
  294. envMap := make(map[string]int, len(env))
  295. for i, val := range env {
  296. envMap[val] = i
  297. }
  298. return envMap
  299. }
  300. // SetSpec sets the configuration in the Generator g.
  301. //
  302. // Deprecated: Replace with:
  303. //
  304. // Use generator.Config = config
  305. func (g *Generator) SetSpec(config *rspec.Spec) {
  306. g.Config = config
  307. }
  308. // Spec gets the configuration from the Generator g.
  309. //
  310. // Deprecated: Replace with generator.Config.
  311. func (g *Generator) Spec() *rspec.Spec {
  312. return g.Config
  313. }
  314. // Save writes the configuration into w.
  315. func (g *Generator) Save(w io.Writer, exportOpts ExportOptions) (err error) {
  316. var data []byte
  317. if g.Config.Linux != nil {
  318. buf, err := json.Marshal(g.Config.Linux)
  319. if err != nil {
  320. return err
  321. }
  322. if string(buf) == "{}" {
  323. g.Config.Linux = nil
  324. }
  325. }
  326. if exportOpts.Seccomp {
  327. data, err = json.MarshalIndent(g.Config.Linux.Seccomp, "", "\t")
  328. } else {
  329. data, err = json.MarshalIndent(g.Config, "", "\t")
  330. }
  331. if err != nil {
  332. return err
  333. }
  334. _, err = w.Write(data)
  335. if err != nil {
  336. return err
  337. }
  338. return nil
  339. }
  340. // SaveToFile writes the configuration into a file.
  341. func (g *Generator) SaveToFile(path string, exportOpts ExportOptions) error {
  342. f, err := os.Create(path)
  343. if err != nil {
  344. return err
  345. }
  346. defer f.Close()
  347. return g.Save(f, exportOpts)
  348. }
  349. // SetVersion sets g.Config.Version.
  350. func (g *Generator) SetVersion(version string) {
  351. g.initConfig()
  352. g.Config.Version = version
  353. }
  354. // SetRootPath sets g.Config.Root.Path.
  355. func (g *Generator) SetRootPath(path string) {
  356. g.initConfigRoot()
  357. g.Config.Root.Path = path
  358. }
  359. // SetRootReadonly sets g.Config.Root.Readonly.
  360. func (g *Generator) SetRootReadonly(b bool) {
  361. g.initConfigRoot()
  362. g.Config.Root.Readonly = b
  363. }
  364. // SetHostname sets g.Config.Hostname.
  365. func (g *Generator) SetHostname(s string) {
  366. g.initConfig()
  367. g.Config.Hostname = s
  368. }
  369. // SetOCIVersion sets g.Config.Version.
  370. func (g *Generator) SetOCIVersion(s string) {
  371. g.initConfig()
  372. g.Config.Version = s
  373. }
  374. // ClearAnnotations clears g.Config.Annotations.
  375. func (g *Generator) ClearAnnotations() {
  376. if g.Config == nil {
  377. return
  378. }
  379. g.Config.Annotations = make(map[string]string)
  380. }
  381. // AddAnnotation adds an annotation into g.Config.Annotations.
  382. func (g *Generator) AddAnnotation(key, value string) {
  383. g.initConfigAnnotations()
  384. g.Config.Annotations[key] = value
  385. }
  386. // RemoveAnnotation remove an annotation from g.Config.Annotations.
  387. func (g *Generator) RemoveAnnotation(key string) {
  388. if g.Config == nil || g.Config.Annotations == nil {
  389. return
  390. }
  391. delete(g.Config.Annotations, key)
  392. }
  393. // RemoveHostname removes g.Config.Hostname, setting it to an empty string.
  394. func (g *Generator) RemoveHostname() {
  395. if g.Config == nil {
  396. return
  397. }
  398. g.Config.Hostname = ""
  399. }
  400. // SetProcessConsoleSize sets g.Config.Process.ConsoleSize.
  401. func (g *Generator) SetProcessConsoleSize(width, height uint) {
  402. g.initConfigProcessConsoleSize()
  403. g.Config.Process.ConsoleSize.Width = width
  404. g.Config.Process.ConsoleSize.Height = height
  405. }
  406. // SetProcessUID sets g.Config.Process.User.UID.
  407. func (g *Generator) SetProcessUID(uid uint32) {
  408. g.initConfigProcess()
  409. g.Config.Process.User.UID = uid
  410. }
  411. // SetProcessUsername sets g.Config.Process.User.Username.
  412. func (g *Generator) SetProcessUsername(username string) {
  413. g.initConfigProcess()
  414. g.Config.Process.User.Username = username
  415. }
  416. // SetProcessUmask sets g.Config.Process.User.Umask.
  417. func (g *Generator) SetProcessUmask(umask uint32) {
  418. g.initConfigProcess()
  419. u := umask
  420. g.Config.Process.User.Umask = &u
  421. }
  422. // SetProcessGID sets g.Config.Process.User.GID.
  423. func (g *Generator) SetProcessGID(gid uint32) {
  424. g.initConfigProcess()
  425. g.Config.Process.User.GID = gid
  426. }
  427. // SetProcessCwd sets g.Config.Process.Cwd.
  428. func (g *Generator) SetProcessCwd(cwd string) {
  429. g.initConfigProcess()
  430. g.Config.Process.Cwd = cwd
  431. }
  432. // SetProcessNoNewPrivileges sets g.Config.Process.NoNewPrivileges.
  433. func (g *Generator) SetProcessNoNewPrivileges(b bool) {
  434. g.initConfigProcess()
  435. g.Config.Process.NoNewPrivileges = b
  436. }
  437. // SetProcessTerminal sets g.Config.Process.Terminal.
  438. func (g *Generator) SetProcessTerminal(b bool) {
  439. g.initConfigProcess()
  440. g.Config.Process.Terminal = b
  441. }
  442. // SetProcessApparmorProfile sets g.Config.Process.ApparmorProfile.
  443. func (g *Generator) SetProcessApparmorProfile(prof string) {
  444. g.initConfigProcess()
  445. g.Config.Process.ApparmorProfile = prof
  446. }
  447. // SetProcessArgs sets g.Config.Process.Args.
  448. func (g *Generator) SetProcessArgs(args []string) {
  449. g.initConfigProcess()
  450. g.Config.Process.Args = args
  451. }
  452. // ClearProcessEnv clears g.Config.Process.Env.
  453. func (g *Generator) ClearProcessEnv() {
  454. if g.Config == nil || g.Config.Process == nil {
  455. return
  456. }
  457. g.Config.Process.Env = []string{}
  458. // Clear out the env cache map as well
  459. g.envMap = map[string]int{}
  460. }
  461. // AddProcessEnv adds name=value into g.Config.Process.Env, or replaces an
  462. // existing entry with the given name.
  463. func (g *Generator) AddProcessEnv(name, value string) {
  464. if name == "" {
  465. return
  466. }
  467. g.initConfigProcess()
  468. g.addEnv(fmt.Sprintf("%s=%s", name, value), name)
  469. }
  470. // AddMultipleProcessEnv adds multiple name=value into g.Config.Process.Env, or replaces
  471. // existing entries with the given name.
  472. func (g *Generator) AddMultipleProcessEnv(envs []string) {
  473. g.initConfigProcess()
  474. for _, val := range envs {
  475. split := strings.SplitN(val, "=", 2)
  476. g.addEnv(val, split[0])
  477. }
  478. }
  479. // addEnv looks through adds ENV to the Process and checks envMap for
  480. // any duplicates
  481. // This is called by both AddMultipleProcessEnv and AddProcessEnv
  482. func (g *Generator) addEnv(env, key string) {
  483. if idx, ok := g.envMap[key]; ok {
  484. // The ENV exists in the cache, so change its value in g.Config.Process.Env
  485. g.Config.Process.Env[idx] = env
  486. } else {
  487. // else the env doesn't exist, so add it and add it's index to g.envMap
  488. g.Config.Process.Env = append(g.Config.Process.Env, env)
  489. g.envMap[key] = len(g.Config.Process.Env) - 1
  490. }
  491. }
  492. // AddProcessRlimits adds rlimit into g.Config.Process.Rlimits.
  493. func (g *Generator) AddProcessRlimits(rType string, rHard uint64, rSoft uint64) {
  494. g.initConfigProcess()
  495. for i, rlimit := range g.Config.Process.Rlimits {
  496. if rlimit.Type == rType {
  497. g.Config.Process.Rlimits[i].Hard = rHard
  498. g.Config.Process.Rlimits[i].Soft = rSoft
  499. return
  500. }
  501. }
  502. newRlimit := rspec.POSIXRlimit{
  503. Type: rType,
  504. Hard: rHard,
  505. Soft: rSoft,
  506. }
  507. g.Config.Process.Rlimits = append(g.Config.Process.Rlimits, newRlimit)
  508. }
  509. // RemoveProcessRlimits removes a rlimit from g.Config.Process.Rlimits.
  510. func (g *Generator) RemoveProcessRlimits(rType string) {
  511. if g.Config == nil || g.Config.Process == nil {
  512. return
  513. }
  514. for i, rlimit := range g.Config.Process.Rlimits {
  515. if rlimit.Type == rType {
  516. g.Config.Process.Rlimits = append(g.Config.Process.Rlimits[:i], g.Config.Process.Rlimits[i+1:]...)
  517. return
  518. }
  519. }
  520. }
  521. // ClearProcessRlimits clear g.Config.Process.Rlimits.
  522. func (g *Generator) ClearProcessRlimits() {
  523. if g.Config == nil || g.Config.Process == nil {
  524. return
  525. }
  526. g.Config.Process.Rlimits = []rspec.POSIXRlimit{}
  527. }
  528. // ClearProcessAdditionalGids clear g.Config.Process.AdditionalGids.
  529. func (g *Generator) ClearProcessAdditionalGids() {
  530. if g.Config == nil || g.Config.Process == nil {
  531. return
  532. }
  533. g.Config.Process.User.AdditionalGids = []uint32{}
  534. }
  535. // AddProcessAdditionalGid adds an additional gid into g.Config.Process.AdditionalGids.
  536. func (g *Generator) AddProcessAdditionalGid(gid uint32) {
  537. g.initConfigProcess()
  538. for _, group := range g.Config.Process.User.AdditionalGids {
  539. if group == gid {
  540. return
  541. }
  542. }
  543. g.Config.Process.User.AdditionalGids = append(g.Config.Process.User.AdditionalGids, gid)
  544. }
  545. // SetProcessSelinuxLabel sets g.Config.Process.SelinuxLabel.
  546. func (g *Generator) SetProcessSelinuxLabel(label string) {
  547. g.initConfigProcess()
  548. g.Config.Process.SelinuxLabel = label
  549. }
  550. // SetLinuxCgroupsPath sets g.Config.Linux.CgroupsPath.
  551. func (g *Generator) SetLinuxCgroupsPath(path string) {
  552. g.initConfigLinux()
  553. g.Config.Linux.CgroupsPath = path
  554. }
  555. // SetLinuxIntelRdtClosID sets g.Config.Linux.IntelRdt.ClosID
  556. func (g *Generator) SetLinuxIntelRdtClosID(clos string) {
  557. g.initConfigLinuxIntelRdt()
  558. g.Config.Linux.IntelRdt.ClosID = clos
  559. }
  560. // SetLinuxIntelRdtL3CacheSchema sets g.Config.Linux.IntelRdt.L3CacheSchema
  561. func (g *Generator) SetLinuxIntelRdtL3CacheSchema(schema string) {
  562. g.initConfigLinuxIntelRdt()
  563. g.Config.Linux.IntelRdt.L3CacheSchema = schema
  564. }
  565. // SetLinuxMountLabel sets g.Config.Linux.MountLabel.
  566. func (g *Generator) SetLinuxMountLabel(label string) {
  567. g.initConfigLinux()
  568. g.Config.Linux.MountLabel = label
  569. }
  570. // SetProcessOOMScoreAdj sets g.Config.Process.OOMScoreAdj.
  571. func (g *Generator) SetProcessOOMScoreAdj(adj int) {
  572. g.initConfigProcess()
  573. g.Config.Process.OOMScoreAdj = &adj
  574. }
  575. // SetLinuxResourcesBlockIOLeafWeight sets g.Config.Linux.Resources.BlockIO.LeafWeight.
  576. func (g *Generator) SetLinuxResourcesBlockIOLeafWeight(weight uint16) {
  577. g.initConfigLinuxResourcesBlockIO()
  578. g.Config.Linux.Resources.BlockIO.LeafWeight = &weight
  579. }
  580. // AddLinuxResourcesBlockIOLeafWeightDevice adds or sets g.Config.Linux.Resources.BlockIO.WeightDevice.LeafWeight.
  581. func (g *Generator) AddLinuxResourcesBlockIOLeafWeightDevice(major int64, minor int64, weight uint16) {
  582. g.initConfigLinuxResourcesBlockIO()
  583. for i, weightDevice := range g.Config.Linux.Resources.BlockIO.WeightDevice {
  584. if weightDevice.Major == major && weightDevice.Minor == minor {
  585. g.Config.Linux.Resources.BlockIO.WeightDevice[i].LeafWeight = &weight
  586. return
  587. }
  588. }
  589. weightDevice := new(rspec.LinuxWeightDevice)
  590. weightDevice.Major = major
  591. weightDevice.Minor = minor
  592. weightDevice.LeafWeight = &weight
  593. g.Config.Linux.Resources.BlockIO.WeightDevice = append(g.Config.Linux.Resources.BlockIO.WeightDevice, *weightDevice)
  594. }
  595. // DropLinuxResourcesBlockIOLeafWeightDevice drops a item form g.Config.Linux.Resources.BlockIO.WeightDevice.LeafWeight
  596. func (g *Generator) DropLinuxResourcesBlockIOLeafWeightDevice(major int64, minor int64) {
  597. if g.Config == nil || g.Config.Linux == nil || g.Config.Linux.Resources == nil || g.Config.Linux.Resources.BlockIO == nil {
  598. return
  599. }
  600. for i, weightDevice := range g.Config.Linux.Resources.BlockIO.WeightDevice {
  601. if weightDevice.Major == major && weightDevice.Minor == minor {
  602. if weightDevice.Weight != nil {
  603. newWeightDevice := new(rspec.LinuxWeightDevice)
  604. newWeightDevice.Major = major
  605. newWeightDevice.Minor = minor
  606. newWeightDevice.Weight = weightDevice.Weight
  607. g.Config.Linux.Resources.BlockIO.WeightDevice[i] = *newWeightDevice
  608. } else {
  609. g.Config.Linux.Resources.BlockIO.WeightDevice = append(g.Config.Linux.Resources.BlockIO.WeightDevice[:i], g.Config.Linux.Resources.BlockIO.WeightDevice[i+1:]...)
  610. }
  611. return
  612. }
  613. }
  614. }
  615. // SetLinuxResourcesBlockIOWeight sets g.Config.Linux.Resources.BlockIO.Weight.
  616. func (g *Generator) SetLinuxResourcesBlockIOWeight(weight uint16) {
  617. g.initConfigLinuxResourcesBlockIO()
  618. g.Config.Linux.Resources.BlockIO.Weight = &weight
  619. }
  620. // AddLinuxResourcesBlockIOWeightDevice adds or sets g.Config.Linux.Resources.BlockIO.WeightDevice.Weight.
  621. func (g *Generator) AddLinuxResourcesBlockIOWeightDevice(major int64, minor int64, weight uint16) {
  622. g.initConfigLinuxResourcesBlockIO()
  623. for i, weightDevice := range g.Config.Linux.Resources.BlockIO.WeightDevice {
  624. if weightDevice.Major == major && weightDevice.Minor == minor {
  625. g.Config.Linux.Resources.BlockIO.WeightDevice[i].Weight = &weight
  626. return
  627. }
  628. }
  629. weightDevice := new(rspec.LinuxWeightDevice)
  630. weightDevice.Major = major
  631. weightDevice.Minor = minor
  632. weightDevice.Weight = &weight
  633. g.Config.Linux.Resources.BlockIO.WeightDevice = append(g.Config.Linux.Resources.BlockIO.WeightDevice, *weightDevice)
  634. }
  635. // DropLinuxResourcesBlockIOWeightDevice drops a item form g.Config.Linux.Resources.BlockIO.WeightDevice.Weight
  636. func (g *Generator) DropLinuxResourcesBlockIOWeightDevice(major int64, minor int64) {
  637. if g.Config == nil || g.Config.Linux == nil || g.Config.Linux.Resources == nil || g.Config.Linux.Resources.BlockIO == nil {
  638. return
  639. }
  640. for i, weightDevice := range g.Config.Linux.Resources.BlockIO.WeightDevice {
  641. if weightDevice.Major == major && weightDevice.Minor == minor {
  642. if weightDevice.LeafWeight != nil {
  643. newWeightDevice := new(rspec.LinuxWeightDevice)
  644. newWeightDevice.Major = major
  645. newWeightDevice.Minor = minor
  646. newWeightDevice.LeafWeight = weightDevice.LeafWeight
  647. g.Config.Linux.Resources.BlockIO.WeightDevice[i] = *newWeightDevice
  648. } else {
  649. g.Config.Linux.Resources.BlockIO.WeightDevice = append(g.Config.Linux.Resources.BlockIO.WeightDevice[:i], g.Config.Linux.Resources.BlockIO.WeightDevice[i+1:]...)
  650. }
  651. return
  652. }
  653. }
  654. }
  655. // AddLinuxResourcesBlockIOThrottleReadBpsDevice adds or sets g.Config.Linux.Resources.BlockIO.ThrottleReadBpsDevice.
  656. func (g *Generator) AddLinuxResourcesBlockIOThrottleReadBpsDevice(major int64, minor int64, rate uint64) {
  657. g.initConfigLinuxResourcesBlockIO()
  658. throttleDevices := addOrReplaceBlockIOThrottleDevice(g.Config.Linux.Resources.BlockIO.ThrottleReadBpsDevice, major, minor, rate)
  659. g.Config.Linux.Resources.BlockIO.ThrottleReadBpsDevice = throttleDevices
  660. }
  661. // DropLinuxResourcesBlockIOThrottleReadBpsDevice drops a item from g.Config.Linux.Resources.BlockIO.ThrottleReadBpsDevice.
  662. func (g *Generator) DropLinuxResourcesBlockIOThrottleReadBpsDevice(major int64, minor int64) {
  663. if g.Config == nil || g.Config.Linux == nil || g.Config.Linux.Resources == nil || g.Config.Linux.Resources.BlockIO == nil {
  664. return
  665. }
  666. throttleDevices := dropBlockIOThrottleDevice(g.Config.Linux.Resources.BlockIO.ThrottleReadBpsDevice, major, minor)
  667. g.Config.Linux.Resources.BlockIO.ThrottleReadBpsDevice = throttleDevices
  668. }
  669. // AddLinuxResourcesBlockIOThrottleReadIOPSDevice adds or sets g.Config.Linux.Resources.BlockIO.ThrottleReadIOPSDevice.
  670. func (g *Generator) AddLinuxResourcesBlockIOThrottleReadIOPSDevice(major int64, minor int64, rate uint64) {
  671. g.initConfigLinuxResourcesBlockIO()
  672. throttleDevices := addOrReplaceBlockIOThrottleDevice(g.Config.Linux.Resources.BlockIO.ThrottleReadIOPSDevice, major, minor, rate)
  673. g.Config.Linux.Resources.BlockIO.ThrottleReadIOPSDevice = throttleDevices
  674. }
  675. // DropLinuxResourcesBlockIOThrottleReadIOPSDevice drops a item from g.Config.Linux.Resources.BlockIO.ThrottleReadIOPSDevice.
  676. func (g *Generator) DropLinuxResourcesBlockIOThrottleReadIOPSDevice(major int64, minor int64) {
  677. if g.Config == nil || g.Config.Linux == nil || g.Config.Linux.Resources == nil || g.Config.Linux.Resources.BlockIO == nil {
  678. return
  679. }
  680. throttleDevices := dropBlockIOThrottleDevice(g.Config.Linux.Resources.BlockIO.ThrottleReadIOPSDevice, major, minor)
  681. g.Config.Linux.Resources.BlockIO.ThrottleReadIOPSDevice = throttleDevices
  682. }
  683. // AddLinuxResourcesBlockIOThrottleWriteBpsDevice adds or sets g.Config.Linux.Resources.BlockIO.ThrottleWriteBpsDevice.
  684. func (g *Generator) AddLinuxResourcesBlockIOThrottleWriteBpsDevice(major int64, minor int64, rate uint64) {
  685. g.initConfigLinuxResourcesBlockIO()
  686. throttleDevices := addOrReplaceBlockIOThrottleDevice(g.Config.Linux.Resources.BlockIO.ThrottleWriteBpsDevice, major, minor, rate)
  687. g.Config.Linux.Resources.BlockIO.ThrottleWriteBpsDevice = throttleDevices
  688. }
  689. // DropLinuxResourcesBlockIOThrottleWriteBpsDevice drops a item from g.Config.Linux.Resources.BlockIO.ThrottleWriteBpsDevice.
  690. func (g *Generator) DropLinuxResourcesBlockIOThrottleWriteBpsDevice(major int64, minor int64) {
  691. if g.Config == nil || g.Config.Linux == nil || g.Config.Linux.Resources == nil || g.Config.Linux.Resources.BlockIO == nil {
  692. return
  693. }
  694. throttleDevices := dropBlockIOThrottleDevice(g.Config.Linux.Resources.BlockIO.ThrottleWriteBpsDevice, major, minor)
  695. g.Config.Linux.Resources.BlockIO.ThrottleWriteBpsDevice = throttleDevices
  696. }
  697. // AddLinuxResourcesBlockIOThrottleWriteIOPSDevice adds or sets g.Config.Linux.Resources.BlockIO.ThrottleWriteIOPSDevice.
  698. func (g *Generator) AddLinuxResourcesBlockIOThrottleWriteIOPSDevice(major int64, minor int64, rate uint64) {
  699. g.initConfigLinuxResourcesBlockIO()
  700. throttleDevices := addOrReplaceBlockIOThrottleDevice(g.Config.Linux.Resources.BlockIO.ThrottleWriteIOPSDevice, major, minor, rate)
  701. g.Config.Linux.Resources.BlockIO.ThrottleWriteIOPSDevice = throttleDevices
  702. }
  703. // DropLinuxResourcesBlockIOThrottleWriteIOPSDevice drops a item from g.Config.Linux.Resources.BlockIO.ThrottleWriteIOPSDevice.
  704. func (g *Generator) DropLinuxResourcesBlockIOThrottleWriteIOPSDevice(major int64, minor int64) {
  705. if g.Config == nil || g.Config.Linux == nil || g.Config.Linux.Resources == nil || g.Config.Linux.Resources.BlockIO == nil {
  706. return
  707. }
  708. throttleDevices := dropBlockIOThrottleDevice(g.Config.Linux.Resources.BlockIO.ThrottleWriteIOPSDevice, major, minor)
  709. g.Config.Linux.Resources.BlockIO.ThrottleWriteIOPSDevice = throttleDevices
  710. }
  711. // SetLinuxResourcesCPUShares sets g.Config.Linux.Resources.CPU.Shares.
  712. func (g *Generator) SetLinuxResourcesCPUShares(shares uint64) {
  713. g.InitConfigLinuxResourcesCPU()
  714. g.Config.Linux.Resources.CPU.Shares = &shares
  715. }
  716. // SetLinuxResourcesCPUQuota sets g.Config.Linux.Resources.CPU.Quota.
  717. func (g *Generator) SetLinuxResourcesCPUQuota(quota int64) {
  718. g.InitConfigLinuxResourcesCPU()
  719. g.Config.Linux.Resources.CPU.Quota = &quota
  720. }
  721. // SetLinuxResourcesCPUPeriod sets g.Config.Linux.Resources.CPU.Period.
  722. func (g *Generator) SetLinuxResourcesCPUPeriod(period uint64) {
  723. g.InitConfigLinuxResourcesCPU()
  724. g.Config.Linux.Resources.CPU.Period = &period
  725. }
  726. // SetLinuxResourcesCPURealtimeRuntime sets g.Config.Linux.Resources.CPU.RealtimeRuntime.
  727. func (g *Generator) SetLinuxResourcesCPURealtimeRuntime(time int64) {
  728. g.InitConfigLinuxResourcesCPU()
  729. g.Config.Linux.Resources.CPU.RealtimeRuntime = &time
  730. }
  731. // SetLinuxResourcesCPURealtimePeriod sets g.Config.Linux.Resources.CPU.RealtimePeriod.
  732. func (g *Generator) SetLinuxResourcesCPURealtimePeriod(period uint64) {
  733. g.InitConfigLinuxResourcesCPU()
  734. g.Config.Linux.Resources.CPU.RealtimePeriod = &period
  735. }
  736. // SetLinuxResourcesCPUCpus sets g.Config.Linux.Resources.CPU.Cpus.
  737. func (g *Generator) SetLinuxResourcesCPUCpus(cpus string) {
  738. g.InitConfigLinuxResourcesCPU()
  739. g.Config.Linux.Resources.CPU.Cpus = cpus
  740. }
  741. // SetLinuxResourcesCPUMems sets g.Config.Linux.Resources.CPU.Mems.
  742. func (g *Generator) SetLinuxResourcesCPUMems(mems string) {
  743. g.InitConfigLinuxResourcesCPU()
  744. g.Config.Linux.Resources.CPU.Mems = mems
  745. }
  746. // AddLinuxResourcesHugepageLimit adds or sets g.Config.Linux.Resources.HugepageLimits.
  747. func (g *Generator) AddLinuxResourcesHugepageLimit(pageSize string, limit uint64) {
  748. hugepageLimit := rspec.LinuxHugepageLimit{
  749. Pagesize: pageSize,
  750. Limit: limit,
  751. }
  752. g.initConfigLinuxResources()
  753. for i, pageLimit := range g.Config.Linux.Resources.HugepageLimits {
  754. if pageLimit.Pagesize == pageSize {
  755. g.Config.Linux.Resources.HugepageLimits[i].Limit = limit
  756. return
  757. }
  758. }
  759. g.Config.Linux.Resources.HugepageLimits = append(g.Config.Linux.Resources.HugepageLimits, hugepageLimit)
  760. }
  761. // DropLinuxResourcesHugepageLimit drops a hugepage limit from g.Config.Linux.Resources.HugepageLimits.
  762. func (g *Generator) DropLinuxResourcesHugepageLimit(pageSize string) {
  763. if g.Config == nil || g.Config.Linux == nil || g.Config.Linux.Resources == nil {
  764. return
  765. }
  766. for i, pageLimit := range g.Config.Linux.Resources.HugepageLimits {
  767. if pageLimit.Pagesize == pageSize {
  768. g.Config.Linux.Resources.HugepageLimits = append(g.Config.Linux.Resources.HugepageLimits[:i], g.Config.Linux.Resources.HugepageLimits[i+1:]...)
  769. return
  770. }
  771. }
  772. }
  773. // AddLinuxResourcesUnified sets the g.Config.Linux.Resources.Unified
  774. func (g *Generator) SetLinuxResourcesUnified(unified map[string]string) {
  775. g.initConfigLinuxResourcesUnified()
  776. for k, v := range unified {
  777. g.Config.Linux.Resources.Unified[k] = v
  778. }
  779. }
  780. // AddLinuxResourcesUnified adds or updates the key-value pair from g.Config.Linux.Resources.Unified
  781. func (g *Generator) AddLinuxResourcesUnified(key, val string) {
  782. g.initConfigLinuxResourcesUnified()
  783. g.Config.Linux.Resources.Unified[key] = val
  784. }
  785. // DropLinuxResourcesUnified drops a key-value pair from g.Config.Linux.Resources.Unified
  786. func (g *Generator) DropLinuxResourcesUnified(key string) {
  787. if g.Config == nil || g.Config.Linux == nil || g.Config.Linux.Resources == nil || g.Config.Linux.Resources.Unified == nil {
  788. return
  789. }
  790. delete(g.Config.Linux.Resources.Unified, key)
  791. }
  792. // SetLinuxResourcesMemoryLimit sets g.Config.Linux.Resources.Memory.Limit.
  793. func (g *Generator) SetLinuxResourcesMemoryLimit(limit int64) {
  794. g.initConfigLinuxResourcesMemory()
  795. g.Config.Linux.Resources.Memory.Limit = &limit
  796. }
  797. // SetLinuxResourcesMemoryReservation sets g.Config.Linux.Resources.Memory.Reservation.
  798. func (g *Generator) SetLinuxResourcesMemoryReservation(reservation int64) {
  799. g.initConfigLinuxResourcesMemory()
  800. g.Config.Linux.Resources.Memory.Reservation = &reservation
  801. }
  802. // SetLinuxResourcesMemorySwap sets g.Config.Linux.Resources.Memory.Swap.
  803. func (g *Generator) SetLinuxResourcesMemorySwap(swap int64) {
  804. g.initConfigLinuxResourcesMemory()
  805. g.Config.Linux.Resources.Memory.Swap = &swap
  806. }
  807. // SetLinuxResourcesMemoryKernel sets g.Config.Linux.Resources.Memory.Kernel.
  808. func (g *Generator) SetLinuxResourcesMemoryKernel(kernel int64) {
  809. g.initConfigLinuxResourcesMemory()
  810. g.Config.Linux.Resources.Memory.Kernel = &kernel
  811. }
  812. // SetLinuxResourcesMemoryKernelTCP sets g.Config.Linux.Resources.Memory.KernelTCP.
  813. func (g *Generator) SetLinuxResourcesMemoryKernelTCP(kernelTCP int64) {
  814. g.initConfigLinuxResourcesMemory()
  815. g.Config.Linux.Resources.Memory.KernelTCP = &kernelTCP
  816. }
  817. // SetLinuxResourcesMemorySwappiness sets g.Config.Linux.Resources.Memory.Swappiness.
  818. func (g *Generator) SetLinuxResourcesMemorySwappiness(swappiness uint64) {
  819. g.initConfigLinuxResourcesMemory()
  820. g.Config.Linux.Resources.Memory.Swappiness = &swappiness
  821. }
  822. // SetLinuxResourcesMemoryDisableOOMKiller sets g.Config.Linux.Resources.Memory.DisableOOMKiller.
  823. func (g *Generator) SetLinuxResourcesMemoryDisableOOMKiller(disable bool) {
  824. g.initConfigLinuxResourcesMemory()
  825. g.Config.Linux.Resources.Memory.DisableOOMKiller = &disable
  826. }
  827. // SetLinuxResourcesNetworkClassID sets g.Config.Linux.Resources.Network.ClassID.
  828. func (g *Generator) SetLinuxResourcesNetworkClassID(classid uint32) {
  829. g.initConfigLinuxResourcesNetwork()
  830. g.Config.Linux.Resources.Network.ClassID = &classid
  831. }
  832. // AddLinuxResourcesNetworkPriorities adds or sets g.Config.Linux.Resources.Network.Priorities.
  833. func (g *Generator) AddLinuxResourcesNetworkPriorities(name string, prio uint32) {
  834. g.initConfigLinuxResourcesNetwork()
  835. for i, netPriority := range g.Config.Linux.Resources.Network.Priorities {
  836. if netPriority.Name == name {
  837. g.Config.Linux.Resources.Network.Priorities[i].Priority = prio
  838. return
  839. }
  840. }
  841. interfacePrio := new(rspec.LinuxInterfacePriority)
  842. interfacePrio.Name = name
  843. interfacePrio.Priority = prio
  844. g.Config.Linux.Resources.Network.Priorities = append(g.Config.Linux.Resources.Network.Priorities, *interfacePrio)
  845. }
  846. // DropLinuxResourcesNetworkPriorities drops one item from g.Config.Linux.Resources.Network.Priorities.
  847. func (g *Generator) DropLinuxResourcesNetworkPriorities(name string) {
  848. if g.Config == nil || g.Config.Linux == nil || g.Config.Linux.Resources == nil || g.Config.Linux.Resources.Network == nil {
  849. return
  850. }
  851. for i, netPriority := range g.Config.Linux.Resources.Network.Priorities {
  852. if netPriority.Name == name {
  853. g.Config.Linux.Resources.Network.Priorities = append(g.Config.Linux.Resources.Network.Priorities[:i], g.Config.Linux.Resources.Network.Priorities[i+1:]...)
  854. return
  855. }
  856. }
  857. }
  858. // SetLinuxResourcesPidsLimit sets g.Config.Linux.Resources.Pids.Limit.
  859. func (g *Generator) SetLinuxResourcesPidsLimit(limit int64) {
  860. g.initConfigLinuxResourcesPids()
  861. g.Config.Linux.Resources.Pids.Limit = limit
  862. }
  863. // ClearLinuxSysctl clears g.Config.Linux.Sysctl.
  864. func (g *Generator) ClearLinuxSysctl() {
  865. if g.Config == nil || g.Config.Linux == nil {
  866. return
  867. }
  868. g.Config.Linux.Sysctl = make(map[string]string)
  869. }
  870. // AddLinuxSysctl adds a new sysctl config into g.Config.Linux.Sysctl.
  871. func (g *Generator) AddLinuxSysctl(key, value string) {
  872. g.initConfigLinuxSysctl()
  873. g.Config.Linux.Sysctl[key] = value
  874. }
  875. // RemoveLinuxSysctl removes a sysctl config from g.Config.Linux.Sysctl.
  876. func (g *Generator) RemoveLinuxSysctl(key string) {
  877. if g.Config == nil || g.Config.Linux == nil || g.Config.Linux.Sysctl == nil {
  878. return
  879. }
  880. delete(g.Config.Linux.Sysctl, key)
  881. }
  882. // ClearLinuxUIDMappings clear g.Config.Linux.UIDMappings.
  883. func (g *Generator) ClearLinuxUIDMappings() {
  884. if g.Config == nil || g.Config.Linux == nil {
  885. return
  886. }
  887. g.Config.Linux.UIDMappings = []rspec.LinuxIDMapping{}
  888. }
  889. // AddLinuxUIDMapping adds uidMap into g.Config.Linux.UIDMappings.
  890. func (g *Generator) AddLinuxUIDMapping(hid, cid, size uint32) {
  891. idMapping := rspec.LinuxIDMapping{
  892. HostID: hid,
  893. ContainerID: cid,
  894. Size: size,
  895. }
  896. g.initConfigLinux()
  897. g.Config.Linux.UIDMappings = append(g.Config.Linux.UIDMappings, idMapping)
  898. }
  899. // ClearLinuxGIDMappings clear g.Config.Linux.GIDMappings.
  900. func (g *Generator) ClearLinuxGIDMappings() {
  901. if g.Config == nil || g.Config.Linux == nil {
  902. return
  903. }
  904. g.Config.Linux.GIDMappings = []rspec.LinuxIDMapping{}
  905. }
  906. // AddLinuxGIDMapping adds gidMap into g.Config.Linux.GIDMappings.
  907. func (g *Generator) AddLinuxGIDMapping(hid, cid, size uint32) {
  908. idMapping := rspec.LinuxIDMapping{
  909. HostID: hid,
  910. ContainerID: cid,
  911. Size: size,
  912. }
  913. g.initConfigLinux()
  914. g.Config.Linux.GIDMappings = append(g.Config.Linux.GIDMappings, idMapping)
  915. }
  916. // SetLinuxRootPropagation sets g.Config.Linux.RootfsPropagation.
  917. func (g *Generator) SetLinuxRootPropagation(rp string) error {
  918. switch rp {
  919. case "":
  920. case "private":
  921. case "rprivate":
  922. case "slave":
  923. case "rslave":
  924. case "shared":
  925. case "rshared":
  926. case "unbindable":
  927. case "runbindable":
  928. default:
  929. return fmt.Errorf("rootfs-propagation %q must be empty or one of (r)private|(r)slave|(r)shared|(r)unbindable", rp)
  930. }
  931. g.initConfigLinux()
  932. g.Config.Linux.RootfsPropagation = rp
  933. return nil
  934. }
  935. // ClearPreStartHooks clear g.Config.Hooks.Prestart.
  936. func (g *Generator) ClearPreStartHooks() {
  937. if g.Config == nil || g.Config.Hooks == nil {
  938. return
  939. }
  940. g.Config.Hooks.Prestart = []rspec.Hook{}
  941. }
  942. // AddPreStartHook add a prestart hook into g.Config.Hooks.Prestart.
  943. func (g *Generator) AddPreStartHook(preStartHook rspec.Hook) {
  944. g.initConfigHooks()
  945. g.Config.Hooks.Prestart = append(g.Config.Hooks.Prestart, preStartHook)
  946. }
  947. // ClearPostStopHooks clear g.Config.Hooks.Poststop.
  948. func (g *Generator) ClearPostStopHooks() {
  949. if g.Config == nil || g.Config.Hooks == nil {
  950. return
  951. }
  952. g.Config.Hooks.Poststop = []rspec.Hook{}
  953. }
  954. // AddPostStopHook adds a poststop hook into g.Config.Hooks.Poststop.
  955. func (g *Generator) AddPostStopHook(postStopHook rspec.Hook) {
  956. g.initConfigHooks()
  957. g.Config.Hooks.Poststop = append(g.Config.Hooks.Poststop, postStopHook)
  958. }
  959. // ClearPostStartHooks clear g.Config.Hooks.Poststart.
  960. func (g *Generator) ClearPostStartHooks() {
  961. if g.Config == nil || g.Config.Hooks == nil {
  962. return
  963. }
  964. g.Config.Hooks.Poststart = []rspec.Hook{}
  965. }
  966. // AddPostStartHook adds a poststart hook into g.Config.Hooks.Poststart.
  967. func (g *Generator) AddPostStartHook(postStartHook rspec.Hook) {
  968. g.initConfigHooks()
  969. g.Config.Hooks.Poststart = append(g.Config.Hooks.Poststart, postStartHook)
  970. }
  971. // AddMount adds a mount into g.Config.Mounts.
  972. func (g *Generator) AddMount(mnt rspec.Mount) {
  973. g.initConfig()
  974. g.Config.Mounts = append(g.Config.Mounts, mnt)
  975. }
  976. // RemoveMount removes a mount point on the dest directory
  977. func (g *Generator) RemoveMount(dest string) {
  978. g.initConfig()
  979. for index, mount := range g.Config.Mounts {
  980. if mount.Destination == dest {
  981. g.Config.Mounts = append(g.Config.Mounts[:index], g.Config.Mounts[index+1:]...)
  982. return
  983. }
  984. }
  985. }
  986. // Mounts returns the list of mounts
  987. func (g *Generator) Mounts() []rspec.Mount {
  988. g.initConfig()
  989. return g.Config.Mounts
  990. }
  991. // ClearMounts clear g.Config.Mounts
  992. func (g *Generator) ClearMounts() {
  993. if g.Config == nil {
  994. return
  995. }
  996. g.Config.Mounts = []rspec.Mount{}
  997. }
  998. // SetupPrivileged sets up the privilege-related fields inside g.Config.
  999. func (g *Generator) SetupPrivileged(privileged bool) {
  1000. if privileged { // Add all capabilities in privileged mode.
  1001. var finalCapList []string
  1002. for _, cap := range capability.List() {
  1003. if g.HostSpecific && cap > capsCheck.LastCap() {
  1004. continue
  1005. }
  1006. finalCapList = append(finalCapList, fmt.Sprintf("CAP_%s", strings.ToUpper(cap.String())))
  1007. }
  1008. g.initConfigLinux()
  1009. g.initConfigProcessCapabilities()
  1010. g.ClearProcessCapabilities()
  1011. g.Config.Process.Capabilities.Bounding = append(g.Config.Process.Capabilities.Bounding, finalCapList...)
  1012. g.Config.Process.Capabilities.Effective = append(g.Config.Process.Capabilities.Effective, finalCapList...)
  1013. g.Config.Process.Capabilities.Inheritable = append(g.Config.Process.Capabilities.Inheritable, finalCapList...)
  1014. g.Config.Process.Capabilities.Permitted = append(g.Config.Process.Capabilities.Permitted, finalCapList...)
  1015. g.Config.Process.Capabilities.Ambient = append(g.Config.Process.Capabilities.Ambient, finalCapList...)
  1016. g.Config.Process.SelinuxLabel = ""
  1017. g.Config.Process.ApparmorProfile = ""
  1018. g.Config.Linux.Seccomp = nil
  1019. }
  1020. }
  1021. // ClearProcessCapabilities clear g.Config.Process.Capabilities.
  1022. func (g *Generator) ClearProcessCapabilities() {
  1023. if g.Config == nil || g.Config.Process == nil || g.Config.Process.Capabilities == nil {
  1024. return
  1025. }
  1026. g.Config.Process.Capabilities.Bounding = []string{}
  1027. g.Config.Process.Capabilities.Effective = []string{}
  1028. g.Config.Process.Capabilities.Inheritable = []string{}
  1029. g.Config.Process.Capabilities.Permitted = []string{}
  1030. g.Config.Process.Capabilities.Ambient = []string{}
  1031. }
  1032. // AddProcessCapability adds a process capability into all 5 capability sets.
  1033. func (g *Generator) AddProcessCapability(c string) error {
  1034. cp := strings.ToUpper(c)
  1035. if err := capsCheck.CapValid(cp, g.HostSpecific); err != nil {
  1036. return err
  1037. }
  1038. g.initConfigProcessCapabilities()
  1039. var foundAmbient, foundBounding, foundEffective, foundInheritable, foundPermitted bool
  1040. for _, cap := range g.Config.Process.Capabilities.Ambient {
  1041. if strings.ToUpper(cap) == cp {
  1042. foundAmbient = true
  1043. break
  1044. }
  1045. }
  1046. if !foundAmbient {
  1047. g.Config.Process.Capabilities.Ambient = append(g.Config.Process.Capabilities.Ambient, cp)
  1048. }
  1049. for _, cap := range g.Config.Process.Capabilities.Bounding {
  1050. if strings.ToUpper(cap) == cp {
  1051. foundBounding = true
  1052. break
  1053. }
  1054. }
  1055. if !foundBounding {
  1056. g.Config.Process.Capabilities.Bounding = append(g.Config.Process.Capabilities.Bounding, cp)
  1057. }
  1058. for _, cap := range g.Config.Process.Capabilities.Effective {
  1059. if strings.ToUpper(cap) == cp {
  1060. foundEffective = true
  1061. break
  1062. }
  1063. }
  1064. if !foundEffective {
  1065. g.Config.Process.Capabilities.Effective = append(g.Config.Process.Capabilities.Effective, cp)
  1066. }
  1067. for _, cap := range g.Config.Process.Capabilities.Inheritable {
  1068. if strings.ToUpper(cap) == cp {
  1069. foundInheritable = true
  1070. break
  1071. }
  1072. }
  1073. if !foundInheritable {
  1074. g.Config.Process.Capabilities.Inheritable = append(g.Config.Process.Capabilities.Inheritable, cp)
  1075. }
  1076. for _, cap := range g.Config.Process.Capabilities.Permitted {
  1077. if strings.ToUpper(cap) == cp {
  1078. foundPermitted = true
  1079. break
  1080. }
  1081. }
  1082. if !foundPermitted {
  1083. g.Config.Process.Capabilities.Permitted = append(g.Config.Process.Capabilities.Permitted, cp)
  1084. }
  1085. return nil
  1086. }
  1087. // AddProcessCapabilityAmbient adds a process capability into g.Config.Process.Capabilities.Ambient.
  1088. func (g *Generator) AddProcessCapabilityAmbient(c string) error {
  1089. cp := strings.ToUpper(c)
  1090. if err := capsCheck.CapValid(cp, g.HostSpecific); err != nil {
  1091. return err
  1092. }
  1093. g.initConfigProcessCapabilities()
  1094. var foundAmbient bool
  1095. for _, cap := range g.Config.Process.Capabilities.Ambient {
  1096. if strings.ToUpper(cap) == cp {
  1097. foundAmbient = true
  1098. break
  1099. }
  1100. }
  1101. if !foundAmbient {
  1102. g.Config.Process.Capabilities.Ambient = append(g.Config.Process.Capabilities.Ambient, cp)
  1103. }
  1104. return nil
  1105. }
  1106. // AddProcessCapabilityBounding adds a process capability into g.Config.Process.Capabilities.Bounding.
  1107. func (g *Generator) AddProcessCapabilityBounding(c string) error {
  1108. cp := strings.ToUpper(c)
  1109. if err := capsCheck.CapValid(cp, g.HostSpecific); err != nil {
  1110. return err
  1111. }
  1112. g.initConfigProcessCapabilities()
  1113. var foundBounding bool
  1114. for _, cap := range g.Config.Process.Capabilities.Bounding {
  1115. if strings.ToUpper(cap) == cp {
  1116. foundBounding = true
  1117. break
  1118. }
  1119. }
  1120. if !foundBounding {
  1121. g.Config.Process.Capabilities.Bounding = append(g.Config.Process.Capabilities.Bounding, cp)
  1122. }
  1123. return nil
  1124. }
  1125. // AddProcessCapabilityEffective adds a process capability into g.Config.Process.Capabilities.Effective.
  1126. func (g *Generator) AddProcessCapabilityEffective(c string) error {
  1127. cp := strings.ToUpper(c)
  1128. if err := capsCheck.CapValid(cp, g.HostSpecific); err != nil {
  1129. return err
  1130. }
  1131. g.initConfigProcessCapabilities()
  1132. var foundEffective bool
  1133. for _, cap := range g.Config.Process.Capabilities.Effective {
  1134. if strings.ToUpper(cap) == cp {
  1135. foundEffective = true
  1136. break
  1137. }
  1138. }
  1139. if !foundEffective {
  1140. g.Config.Process.Capabilities.Effective = append(g.Config.Process.Capabilities.Effective, cp)
  1141. }
  1142. return nil
  1143. }
  1144. // AddProcessCapabilityInheritable adds a process capability into g.Config.Process.Capabilities.Inheritable.
  1145. func (g *Generator) AddProcessCapabilityInheritable(c string) error {
  1146. cp := strings.ToUpper(c)
  1147. if err := capsCheck.CapValid(cp, g.HostSpecific); err != nil {
  1148. return err
  1149. }
  1150. g.initConfigProcessCapabilities()
  1151. var foundInheritable bool
  1152. for _, cap := range g.Config.Process.Capabilities.Inheritable {
  1153. if strings.ToUpper(cap) == cp {
  1154. foundInheritable = true
  1155. break
  1156. }
  1157. }
  1158. if !foundInheritable {
  1159. g.Config.Process.Capabilities.Inheritable = append(g.Config.Process.Capabilities.Inheritable, cp)
  1160. }
  1161. return nil
  1162. }
  1163. // AddProcessCapabilityPermitted adds a process capability into g.Config.Process.Capabilities.Permitted.
  1164. func (g *Generator) AddProcessCapabilityPermitted(c string) error {
  1165. cp := strings.ToUpper(c)
  1166. if err := capsCheck.CapValid(cp, g.HostSpecific); err != nil {
  1167. return err
  1168. }
  1169. g.initConfigProcessCapabilities()
  1170. var foundPermitted bool
  1171. for _, cap := range g.Config.Process.Capabilities.Permitted {
  1172. if strings.ToUpper(cap) == cp {
  1173. foundPermitted = true
  1174. break
  1175. }
  1176. }
  1177. if !foundPermitted {
  1178. g.Config.Process.Capabilities.Permitted = append(g.Config.Process.Capabilities.Permitted, cp)
  1179. }
  1180. return nil
  1181. }
  1182. // DropProcessCapability drops a process capability from all 5 capability sets.
  1183. func (g *Generator) DropProcessCapability(c string) error {
  1184. if g.Config == nil || g.Config.Process == nil || g.Config.Process.Capabilities == nil {
  1185. return nil
  1186. }
  1187. cp := strings.ToUpper(c)
  1188. for i, cap := range g.Config.Process.Capabilities.Ambient {
  1189. if strings.ToUpper(cap) == cp {
  1190. g.Config.Process.Capabilities.Ambient = removeFunc(g.Config.Process.Capabilities.Ambient, i)
  1191. }
  1192. }
  1193. for i, cap := range g.Config.Process.Capabilities.Bounding {
  1194. if strings.ToUpper(cap) == cp {
  1195. g.Config.Process.Capabilities.Bounding = removeFunc(g.Config.Process.Capabilities.Bounding, i)
  1196. }
  1197. }
  1198. for i, cap := range g.Config.Process.Capabilities.Effective {
  1199. if strings.ToUpper(cap) == cp {
  1200. g.Config.Process.Capabilities.Effective = removeFunc(g.Config.Process.Capabilities.Effective, i)
  1201. }
  1202. }
  1203. for i, cap := range g.Config.Process.Capabilities.Inheritable {
  1204. if strings.ToUpper(cap) == cp {
  1205. g.Config.Process.Capabilities.Inheritable = removeFunc(g.Config.Process.Capabilities.Inheritable, i)
  1206. }
  1207. }
  1208. for i, cap := range g.Config.Process.Capabilities.Permitted {
  1209. if strings.ToUpper(cap) == cp {
  1210. g.Config.Process.Capabilities.Permitted = removeFunc(g.Config.Process.Capabilities.Permitted, i)
  1211. }
  1212. }
  1213. return capsCheck.CapValid(cp, false)
  1214. }
  1215. // DropProcessCapabilityAmbient drops a process capability from g.Config.Process.Capabilities.Ambient.
  1216. func (g *Generator) DropProcessCapabilityAmbient(c string) error {
  1217. if g.Config == nil || g.Config.Process == nil || g.Config.Process.Capabilities == nil {
  1218. return nil
  1219. }
  1220. cp := strings.ToUpper(c)
  1221. for i, cap := range g.Config.Process.Capabilities.Ambient {
  1222. if strings.ToUpper(cap) == cp {
  1223. g.Config.Process.Capabilities.Ambient = removeFunc(g.Config.Process.Capabilities.Ambient, i)
  1224. }
  1225. }
  1226. return capsCheck.CapValid(cp, false)
  1227. }
  1228. // DropProcessCapabilityBounding drops a process capability from g.Config.Process.Capabilities.Bounding.
  1229. func (g *Generator) DropProcessCapabilityBounding(c string) error {
  1230. if g.Config == nil || g.Config.Process == nil || g.Config.Process.Capabilities == nil {
  1231. return nil
  1232. }
  1233. cp := strings.ToUpper(c)
  1234. for i, cap := range g.Config.Process.Capabilities.Bounding {
  1235. if strings.ToUpper(cap) == cp {
  1236. g.Config.Process.Capabilities.Bounding = removeFunc(g.Config.Process.Capabilities.Bounding, i)
  1237. }
  1238. }
  1239. return capsCheck.CapValid(cp, false)
  1240. }
  1241. // DropProcessCapabilityEffective drops a process capability from g.Config.Process.Capabilities.Effective.
  1242. func (g *Generator) DropProcessCapabilityEffective(c string) error {
  1243. if g.Config == nil || g.Config.Process == nil || g.Config.Process.Capabilities == nil {
  1244. return nil
  1245. }
  1246. cp := strings.ToUpper(c)
  1247. for i, cap := range g.Config.Process.Capabilities.Effective {
  1248. if strings.ToUpper(cap) == cp {
  1249. g.Config.Process.Capabilities.Effective = removeFunc(g.Config.Process.Capabilities.Effective, i)
  1250. }
  1251. }
  1252. return capsCheck.CapValid(cp, false)
  1253. }
  1254. // DropProcessCapabilityInheritable drops a process capability from g.Config.Process.Capabilities.Inheritable.
  1255. func (g *Generator) DropProcessCapabilityInheritable(c string) error {
  1256. if g.Config == nil || g.Config.Process == nil || g.Config.Process.Capabilities == nil {
  1257. return nil
  1258. }
  1259. cp := strings.ToUpper(c)
  1260. for i, cap := range g.Config.Process.Capabilities.Inheritable {
  1261. if strings.ToUpper(cap) == cp {
  1262. g.Config.Process.Capabilities.Inheritable = removeFunc(g.Config.Process.Capabilities.Inheritable, i)
  1263. }
  1264. }
  1265. return capsCheck.CapValid(cp, false)
  1266. }
  1267. // DropProcessCapabilityPermitted drops a process capability from g.Config.Process.Capabilities.Permitted.
  1268. func (g *Generator) DropProcessCapabilityPermitted(c string) error {
  1269. if g.Config == nil || g.Config.Process == nil || g.Config.Process.Capabilities == nil {
  1270. return nil
  1271. }
  1272. cp := strings.ToUpper(c)
  1273. for i, cap := range g.Config.Process.Capabilities.Permitted {
  1274. if strings.ToUpper(cap) == cp {
  1275. g.Config.Process.Capabilities.Permitted = removeFunc(g.Config.Process.Capabilities.Permitted, i)
  1276. }
  1277. }
  1278. return capsCheck.CapValid(cp, false)
  1279. }
  1280. func mapStrToNamespace(ns string, path string) (rspec.LinuxNamespace, error) {
  1281. switch ns {
  1282. case "network":
  1283. return rspec.LinuxNamespace{Type: rspec.NetworkNamespace, Path: path}, nil
  1284. case "pid":
  1285. return rspec.LinuxNamespace{Type: rspec.PIDNamespace, Path: path}, nil
  1286. case "mount":
  1287. return rspec.LinuxNamespace{Type: rspec.MountNamespace, Path: path}, nil
  1288. case "ipc":
  1289. return rspec.LinuxNamespace{Type: rspec.IPCNamespace, Path: path}, nil
  1290. case "uts":
  1291. return rspec.LinuxNamespace{Type: rspec.UTSNamespace, Path: path}, nil
  1292. case "user":
  1293. return rspec.LinuxNamespace{Type: rspec.UserNamespace, Path: path}, nil
  1294. case "cgroup":
  1295. return rspec.LinuxNamespace{Type: rspec.CgroupNamespace, Path: path}, nil
  1296. default:
  1297. return rspec.LinuxNamespace{}, fmt.Errorf("unrecognized namespace %q", ns)
  1298. }
  1299. }
  1300. // ClearLinuxNamespaces clear g.Config.Linux.Namespaces.
  1301. func (g *Generator) ClearLinuxNamespaces() {
  1302. if g.Config == nil || g.Config.Linux == nil {
  1303. return
  1304. }
  1305. g.Config.Linux.Namespaces = []rspec.LinuxNamespace{}
  1306. }
  1307. // AddOrReplaceLinuxNamespace adds or replaces a namespace inside
  1308. // g.Config.Linux.Namespaces.
  1309. func (g *Generator) AddOrReplaceLinuxNamespace(ns string, path string) error {
  1310. namespace, err := mapStrToNamespace(ns, path)
  1311. if err != nil {
  1312. return err
  1313. }
  1314. g.initConfigLinux()
  1315. for i, ns := range g.Config.Linux.Namespaces {
  1316. if ns.Type == namespace.Type {
  1317. g.Config.Linux.Namespaces[i] = namespace
  1318. return nil
  1319. }
  1320. }
  1321. g.Config.Linux.Namespaces = append(g.Config.Linux.Namespaces, namespace)
  1322. return nil
  1323. }
  1324. // RemoveLinuxNamespace removes a namespace from g.Config.Linux.Namespaces.
  1325. func (g *Generator) RemoveLinuxNamespace(ns string) error {
  1326. namespace, err := mapStrToNamespace(ns, "")
  1327. if err != nil {
  1328. return err
  1329. }
  1330. if g.Config == nil || g.Config.Linux == nil {
  1331. return nil
  1332. }
  1333. for i, ns := range g.Config.Linux.Namespaces {
  1334. if ns.Type == namespace.Type {
  1335. g.Config.Linux.Namespaces = append(g.Config.Linux.Namespaces[:i], g.Config.Linux.Namespaces[i+1:]...)
  1336. return nil
  1337. }
  1338. }
  1339. return nil
  1340. }
  1341. // AddDevice - add a device into g.Config.Linux.Devices
  1342. func (g *Generator) AddDevice(device rspec.LinuxDevice) {
  1343. g.initConfigLinux()
  1344. for i, dev := range g.Config.Linux.Devices {
  1345. if dev.Path == device.Path {
  1346. g.Config.Linux.Devices[i] = device
  1347. return
  1348. }
  1349. }
  1350. g.Config.Linux.Devices = append(g.Config.Linux.Devices, device)
  1351. }
  1352. // RemoveDevice remove a device from g.Config.Linux.Devices
  1353. func (g *Generator) RemoveDevice(path string) {
  1354. if g.Config == nil || g.Config.Linux == nil || g.Config.Linux.Devices == nil {
  1355. return
  1356. }
  1357. for i, device := range g.Config.Linux.Devices {
  1358. if device.Path == path {
  1359. g.Config.Linux.Devices = append(g.Config.Linux.Devices[:i], g.Config.Linux.Devices[i+1:]...)
  1360. return
  1361. }
  1362. }
  1363. }
  1364. // ClearLinuxDevices clears g.Config.Linux.Devices
  1365. func (g *Generator) ClearLinuxDevices() {
  1366. if g.Config == nil || g.Config.Linux == nil || g.Config.Linux.Devices == nil {
  1367. return
  1368. }
  1369. g.Config.Linux.Devices = []rspec.LinuxDevice{}
  1370. }
  1371. // AddLinuxResourcesDevice - add a device into g.Config.Linux.Resources.Devices
  1372. func (g *Generator) AddLinuxResourcesDevice(allow bool, devType string, major, minor *int64, access string) {
  1373. g.initConfigLinuxResources()
  1374. device := rspec.LinuxDeviceCgroup{
  1375. Allow: allow,
  1376. Type: devType,
  1377. Access: access,
  1378. Major: major,
  1379. Minor: minor,
  1380. }
  1381. g.Config.Linux.Resources.Devices = append(g.Config.Linux.Resources.Devices, device)
  1382. }
  1383. // RemoveLinuxResourcesDevice - remove a device from g.Config.Linux.Resources.Devices
  1384. func (g *Generator) RemoveLinuxResourcesDevice(allow bool, devType string, major, minor *int64, access string) {
  1385. if g.Config == nil || g.Config.Linux == nil || g.Config.Linux.Resources == nil {
  1386. return
  1387. }
  1388. for i, device := range g.Config.Linux.Resources.Devices {
  1389. if device.Allow == allow &&
  1390. (devType == device.Type || (devType != "" && device.Type != "" && devType == device.Type)) &&
  1391. (access == device.Access || (access != "" && device.Access != "" && access == device.Access)) &&
  1392. (major == device.Major || (major != nil && device.Major != nil && *major == *device.Major)) &&
  1393. (minor == device.Minor || (minor != nil && device.Minor != nil && *minor == *device.Minor)) {
  1394. g.Config.Linux.Resources.Devices = append(g.Config.Linux.Resources.Devices[:i], g.Config.Linux.Resources.Devices[i+1:]...)
  1395. return
  1396. }
  1397. }
  1398. }
  1399. // SetSyscallAction adds rules for syscalls with the specified action
  1400. func (g *Generator) SetSyscallAction(arguments seccomp.SyscallOpts) error {
  1401. g.initConfigLinuxSeccomp()
  1402. return seccomp.ParseSyscallFlag(arguments, g.Config.Linux.Seccomp)
  1403. }
  1404. // SetDefaultSeccompAction sets the default action for all syscalls not defined
  1405. // and then removes any syscall rules with this action already specified.
  1406. func (g *Generator) SetDefaultSeccompAction(action string) error {
  1407. g.initConfigLinuxSeccomp()
  1408. return seccomp.ParseDefaultAction(action, g.Config.Linux.Seccomp)
  1409. }
  1410. // SetDefaultSeccompActionForce only sets the default action for all syscalls not defined
  1411. func (g *Generator) SetDefaultSeccompActionForce(action string) error {
  1412. g.initConfigLinuxSeccomp()
  1413. return seccomp.ParseDefaultActionForce(action, g.Config.Linux.Seccomp)
  1414. }
  1415. // SetDomainName sets g.Config.Domainname
  1416. func (g *Generator) SetDomainName(domain string) {
  1417. g.initConfig()
  1418. g.Config.Domainname = domain
  1419. }
  1420. // SetSeccompArchitecture sets the supported seccomp architectures
  1421. func (g *Generator) SetSeccompArchitecture(architecture string) error {
  1422. g.initConfigLinuxSeccomp()
  1423. return seccomp.ParseArchitectureFlag(architecture, g.Config.Linux.Seccomp)
  1424. }
  1425. // RemoveSeccompRule removes rules for any specified syscalls
  1426. func (g *Generator) RemoveSeccompRule(arguments string) error {
  1427. g.initConfigLinuxSeccomp()
  1428. return seccomp.RemoveAction(arguments, g.Config.Linux.Seccomp)
  1429. }
  1430. // RemoveAllSeccompRules removes all syscall rules
  1431. func (g *Generator) RemoveAllSeccompRules() error {
  1432. g.initConfigLinuxSeccomp()
  1433. return seccomp.RemoveAllSeccompRules(g.Config.Linux.Seccomp)
  1434. }
  1435. // AddLinuxMaskedPaths adds masked paths into g.Config.Linux.MaskedPaths.
  1436. func (g *Generator) AddLinuxMaskedPaths(path string) {
  1437. g.initConfigLinux()
  1438. g.Config.Linux.MaskedPaths = append(g.Config.Linux.MaskedPaths, path)
  1439. }
  1440. // AddLinuxReadonlyPaths adds readonly paths into g.Config.Linux.MaskedPaths.
  1441. func (g *Generator) AddLinuxReadonlyPaths(path string) {
  1442. g.initConfigLinux()
  1443. g.Config.Linux.ReadonlyPaths = append(g.Config.Linux.ReadonlyPaths, path)
  1444. }
  1445. func addOrReplaceBlockIOThrottleDevice(tmpList []rspec.LinuxThrottleDevice, major int64, minor int64, rate uint64) []rspec.LinuxThrottleDevice {
  1446. throttleDevices := tmpList
  1447. for i, throttleDevice := range throttleDevices {
  1448. if throttleDevice.Major == major && throttleDevice.Minor == minor {
  1449. throttleDevices[i].Rate = rate
  1450. return throttleDevices
  1451. }
  1452. }
  1453. throttleDevice := new(rspec.LinuxThrottleDevice)
  1454. throttleDevice.Major = major
  1455. throttleDevice.Minor = minor
  1456. throttleDevice.Rate = rate
  1457. throttleDevices = append(throttleDevices, *throttleDevice)
  1458. return throttleDevices
  1459. }
  1460. func dropBlockIOThrottleDevice(tmpList []rspec.LinuxThrottleDevice, major int64, minor int64) []rspec.LinuxThrottleDevice {
  1461. throttleDevices := tmpList
  1462. for i, throttleDevice := range throttleDevices {
  1463. if throttleDevice.Major == major && throttleDevice.Minor == minor {
  1464. throttleDevices = append(throttleDevices[:i], throttleDevices[i+1:]...)
  1465. return throttleDevices
  1466. }
  1467. }
  1468. return throttleDevices
  1469. }
  1470. // AddSolarisAnet adds network into g.Config.Solaris.Anet
  1471. func (g *Generator) AddSolarisAnet(anet rspec.SolarisAnet) {
  1472. g.initConfigSolaris()
  1473. g.Config.Solaris.Anet = append(g.Config.Solaris.Anet, anet)
  1474. }
  1475. // SetSolarisCappedCPUNcpus sets g.Config.Solaris.CappedCPU.Ncpus
  1476. func (g *Generator) SetSolarisCappedCPUNcpus(ncpus string) {
  1477. g.initConfigSolarisCappedCPU()
  1478. g.Config.Solaris.CappedCPU.Ncpus = ncpus
  1479. }
  1480. // SetSolarisCappedMemoryPhysical sets g.Config.Solaris.CappedMemory.Physical
  1481. func (g *Generator) SetSolarisCappedMemoryPhysical(physical string) {
  1482. g.initConfigSolarisCappedMemory()
  1483. g.Config.Solaris.CappedMemory.Physical = physical
  1484. }
  1485. // SetSolarisCappedMemorySwap sets g.Config.Solaris.CappedMemory.Swap
  1486. func (g *Generator) SetSolarisCappedMemorySwap(swap string) {
  1487. g.initConfigSolarisCappedMemory()
  1488. g.Config.Solaris.CappedMemory.Swap = swap
  1489. }
  1490. // SetSolarisLimitPriv sets g.Config.Solaris.LimitPriv
  1491. func (g *Generator) SetSolarisLimitPriv(limitPriv string) {
  1492. g.initConfigSolaris()
  1493. g.Config.Solaris.LimitPriv = limitPriv
  1494. }
  1495. // SetSolarisMaxShmMemory sets g.Config.Solaris.MaxShmMemory
  1496. func (g *Generator) SetSolarisMaxShmMemory(memory string) {
  1497. g.initConfigSolaris()
  1498. g.Config.Solaris.MaxShmMemory = memory
  1499. }
  1500. // SetSolarisMilestone sets g.Config.Solaris.Milestone
  1501. func (g *Generator) SetSolarisMilestone(milestone string) {
  1502. g.initConfigSolaris()
  1503. g.Config.Solaris.Milestone = milestone
  1504. }
  1505. // SetVMHypervisorPath sets g.Config.VM.Hypervisor.Path
  1506. func (g *Generator) SetVMHypervisorPath(path string) error {
  1507. if !strings.HasPrefix(path, "/") {
  1508. return fmt.Errorf("hypervisorPath %v is not an absolute path", path)
  1509. }
  1510. g.initConfigVM()
  1511. g.Config.VM.Hypervisor.Path = path
  1512. return nil
  1513. }
  1514. // SetVMHypervisorParameters sets g.Config.VM.Hypervisor.Parameters
  1515. func (g *Generator) SetVMHypervisorParameters(parameters []string) {
  1516. g.initConfigVM()
  1517. g.Config.VM.Hypervisor.Parameters = parameters
  1518. }
  1519. // SetVMKernelPath sets g.Config.VM.Kernel.Path
  1520. func (g *Generator) SetVMKernelPath(path string) error {
  1521. if !strings.HasPrefix(path, "/") {
  1522. return fmt.Errorf("kernelPath %v is not an absolute path", path)
  1523. }
  1524. g.initConfigVM()
  1525. g.Config.VM.Kernel.Path = path
  1526. return nil
  1527. }
  1528. // SetVMKernelParameters sets g.Config.VM.Kernel.Parameters
  1529. func (g *Generator) SetVMKernelParameters(parameters []string) {
  1530. g.initConfigVM()
  1531. g.Config.VM.Kernel.Parameters = parameters
  1532. }
  1533. // SetVMKernelInitRD sets g.Config.VM.Kernel.InitRD
  1534. func (g *Generator) SetVMKernelInitRD(initrd string) error {
  1535. if !strings.HasPrefix(initrd, "/") {
  1536. return fmt.Errorf("kernelInitrd %v is not an absolute path", initrd)
  1537. }
  1538. g.initConfigVM()
  1539. g.Config.VM.Kernel.InitRD = initrd
  1540. return nil
  1541. }
  1542. // SetVMImagePath sets g.Config.VM.Image.Path
  1543. func (g *Generator) SetVMImagePath(path string) error {
  1544. if !strings.HasPrefix(path, "/") {
  1545. return fmt.Errorf("imagePath %v is not an absolute path", path)
  1546. }
  1547. g.initConfigVM()
  1548. g.Config.VM.Image.Path = path
  1549. return nil
  1550. }
  1551. // SetVMImageFormat sets g.Config.VM.Image.Format
  1552. func (g *Generator) SetVMImageFormat(format string) error {
  1553. switch format {
  1554. case "raw":
  1555. case "qcow2":
  1556. case "vdi":
  1557. case "vmdk":
  1558. case "vhd":
  1559. default:
  1560. return fmt.Errorf("Commonly supported formats are: raw, qcow2, vdi, vmdk, vhd")
  1561. }
  1562. g.initConfigVM()
  1563. g.Config.VM.Image.Format = format
  1564. return nil
  1565. }
  1566. // SetWindowsHypervUntilityVMPath sets g.Config.Windows.HyperV.UtilityVMPath.
  1567. func (g *Generator) SetWindowsHypervUntilityVMPath(path string) {
  1568. g.initConfigWindowsHyperV()
  1569. g.Config.Windows.HyperV.UtilityVMPath = path
  1570. }
  1571. // SetWindowsIgnoreFlushesDuringBoot sets g.Config.Windows.IgnoreFlushesDuringBoot.
  1572. func (g *Generator) SetWindowsIgnoreFlushesDuringBoot(ignore bool) {
  1573. g.initConfigWindows()
  1574. g.Config.Windows.IgnoreFlushesDuringBoot = ignore
  1575. }
  1576. // AddWindowsLayerFolders adds layer folders into g.Config.Windows.LayerFolders.
  1577. func (g *Generator) AddWindowsLayerFolders(folder string) {
  1578. g.initConfigWindows()
  1579. g.Config.Windows.LayerFolders = append(g.Config.Windows.LayerFolders, folder)
  1580. }
  1581. // AddWindowsDevices adds or sets g.Config.Windwos.Devices
  1582. func (g *Generator) AddWindowsDevices(id, idType string) error {
  1583. if idType != "class" {
  1584. return fmt.Errorf("Invalid idType value: %s. Windows only supports a value of class", idType)
  1585. }
  1586. device := rspec.WindowsDevice{
  1587. ID: id,
  1588. IDType: idType,
  1589. }
  1590. g.initConfigWindows()
  1591. for i, device := range g.Config.Windows.Devices {
  1592. if device.ID == id {
  1593. g.Config.Windows.Devices[i].IDType = idType
  1594. return nil
  1595. }
  1596. }
  1597. g.Config.Windows.Devices = append(g.Config.Windows.Devices, device)
  1598. return nil
  1599. }
  1600. // SetWindowsNetwork sets g.Config.Windows.Network.
  1601. func (g *Generator) SetWindowsNetwork(network rspec.WindowsNetwork) {
  1602. g.initConfigWindows()
  1603. g.Config.Windows.Network = &network
  1604. }
  1605. // SetWindowsNetworkAllowUnqualifiedDNSQuery sets g.Config.Windows.Network.AllowUnqualifiedDNSQuery
  1606. func (g *Generator) SetWindowsNetworkAllowUnqualifiedDNSQuery(setting bool) {
  1607. g.initConfigWindowsNetwork()
  1608. g.Config.Windows.Network.AllowUnqualifiedDNSQuery = setting
  1609. }
  1610. // SetWindowsNetworkNamespace sets g.Config.Windows.Network.NetworkNamespace
  1611. func (g *Generator) SetWindowsNetworkNamespace(path string) {
  1612. g.initConfigWindowsNetwork()
  1613. g.Config.Windows.Network.NetworkNamespace = path
  1614. }
  1615. // SetWindowsResourcesCPU sets g.Config.Windows.Resources.CPU.
  1616. func (g *Generator) SetWindowsResourcesCPU(cpu rspec.WindowsCPUResources) {
  1617. g.initConfigWindowsResources()
  1618. g.Config.Windows.Resources.CPU = &cpu
  1619. }
  1620. // SetWindowsResourcesMemoryLimit sets g.Config.Windows.Resources.Memory.Limit.
  1621. func (g *Generator) SetWindowsResourcesMemoryLimit(limit uint64) {
  1622. g.initConfigWindowsResourcesMemory()
  1623. g.Config.Windows.Resources.Memory.Limit = &limit
  1624. }
  1625. // SetWindowsResourcesStorage sets g.Config.Windows.Resources.Storage.
  1626. func (g *Generator) SetWindowsResourcesStorage(storage rspec.WindowsStorageResources) {
  1627. g.initConfigWindowsResources()
  1628. g.Config.Windows.Resources.Storage = &storage
  1629. }
  1630. // SetWindowsServicing sets g.Config.Windows.Servicing.
  1631. func (g *Generator) SetWindowsServicing(servicing bool) {
  1632. g.initConfigWindows()
  1633. g.Config.Windows.Servicing = servicing
  1634. }