-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathlinux.rs
More file actions
2165 lines (1869 loc) · 67.2 KB
/
linux.rs
File metadata and controls
2165 lines (1869 loc) · 67.2 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use crate::error::{oci_error, OciSpecError};
use crate::is_none_or_empty;
use derive_builder::Builder;
use getset::{CopyGetters, Getters, MutGetters, Setters};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt::Display, path::PathBuf, vec};
use strum_macros::{Display as StrumDisplay, EnumString};
#[derive(
Builder, Clone, Debug, Deserialize, Eq, Getters, MutGetters, Setters, PartialEq, Serialize,
)]
#[serde(rename_all = "camelCase")]
#[builder(
default,
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
/// Linux contains platform-specific configuration for Linux based
/// containers.
pub struct Linux {
#[serde(default, skip_serializing_if = "Option::is_none")]
/// NetDevices are key-value pairs, keyed by network device name on the host, moved to the container's network namespace.
net_devices: Option<HashMap<String, LinuxNetDevice>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// UIDMappings specifies user mappings for supporting user namespaces.
uid_mappings: Option<Vec<LinuxIdMapping>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// GIDMappings specifies group mappings for supporting user namespaces.
gid_mappings: Option<Vec<LinuxIdMapping>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// Sysctl are a set of key value pairs that are set for the container
/// on start.
sysctl: Option<HashMap<String, String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// Resources contain cgroup information for handling resource
/// constraints for the container.
resources: Option<LinuxResources>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// CgroupsPath specifies the path to cgroups that are created and/or
/// joined by the container. The path is expected to be relative
/// to the cgroups mountpoint. If resources are specified,
/// the cgroups at CgroupsPath will be updated based on resources.
cgroups_path: Option<PathBuf>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// Namespaces contains the namespaces that are created and/or joined by
/// the container.
namespaces: Option<Vec<LinuxNamespace>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// Devices are a list of device nodes that are created for the
/// container.
devices: Option<Vec<LinuxDevice>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// Seccomp specifies the seccomp security settings for the container.
seccomp: Option<LinuxSeccomp>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// RootfsPropagation is the rootfs mount propagation mode for the
/// container.
rootfs_propagation: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// MaskedPaths masks over the provided paths inside the container.
masked_paths: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// ReadonlyPaths sets the provided paths as RO inside the container.
readonly_paths: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// MountLabel specifies the selinux context for the mounts in the
/// container.
mount_label: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// IntelRdt contains Intel Resource Director Technology (RDT)
/// information for handling resource constraints and monitoring metrics
/// (e.g., L3 cache, memory bandwidth) for the container.
intel_rdt: Option<LinuxIntelRdt>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// MemoryPolicy contains NUMA memory policy for the container.
memory_policy: Option<LinuxMemoryPolicy>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// Personality contains configuration for the Linux personality
/// syscall.
personality: Option<LinuxPersonality>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// TimeOffsets specifies the offset for supporting time namespaces.
time_offsets: Option<HashMap<String, LinuxTimeOffset>>,
}
// Default impl for Linux (see functions for more info)
impl Default for Linux {
fn default() -> Self {
Linux {
// Creates empty Hashmap
net_devices: None,
// Creates empty Vec
uid_mappings: Default::default(),
// Creates empty Vec
gid_mappings: Default::default(),
// Empty sysctl Hashmap
sysctl: Default::default(),
resources: Some(LinuxResources {
devices: vec![].into(),
memory: Default::default(),
cpu: Default::default(),
pids: Default::default(),
block_io: Default::default(),
hugepage_limits: Default::default(),
network: Default::default(),
rdma: Default::default(),
unified: Default::default(),
}),
// Defaults to None
cgroups_path: Default::default(),
namespaces: get_default_namespaces().into(),
// Empty Vec
devices: Default::default(),
// Empty String
rootfs_propagation: Default::default(),
masked_paths: get_default_maskedpaths().into(),
readonly_paths: get_default_readonly_paths().into(),
// Empty String
mount_label: Default::default(),
seccomp: None,
intel_rdt: None,
memory_policy: None,
personality: None,
time_offsets: None,
}
}
}
impl Linux {
/// Return rootless Linux configuration.
pub fn rootless(uid: u32, gid: u32) -> Self {
let mut namespaces = get_default_namespaces();
namespaces.retain(|ns| ns.typ != LinuxNamespaceType::Network);
namespaces.push(LinuxNamespace {
typ: LinuxNamespaceType::User,
..Default::default()
});
Self {
resources: None,
uid_mappings: Some(vec![LinuxIdMapping {
container_id: 0,
host_id: uid,
size: 1,
}]),
gid_mappings: Some(vec![LinuxIdMapping {
container_id: 0,
host_id: gid,
size: 1,
}]),
namespaces: Some(namespaces),
..Default::default()
}
}
}
#[derive(
Builder, Clone, Copy, CopyGetters, Debug, Default, Deserialize, Eq, PartialEq, Serialize,
)]
#[serde(rename_all = "camelCase")]
#[builder(
default,
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
#[getset(get_copy = "pub", set = "pub")]
/// LinuxIDMapping specifies UID/GID mappings.
pub struct LinuxIdMapping {
#[serde(default, rename = "hostID")]
/// HostID is the starting UID/GID on the host to be mapped to
/// `container_id`.
host_id: u32,
#[serde(default, rename = "containerID")]
/// ContainerID is the starting UID/GID in the container.
container_id: u32,
#[serde(default)]
/// Size is the number of IDs to be mapped.
size: u32,
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize, EnumString)]
#[strum(serialize_all = "lowercase")]
#[serde(rename_all = "lowercase")]
/// Device types
pub enum LinuxDeviceType {
/// All
A,
/// block (buffered)
B,
/// character (unbuffered)
C,
/// character (unbufferd)
U,
/// FIFO
P,
}
#[allow(clippy::derivable_impls)] // because making it clear that All is the default
impl Default for LinuxDeviceType {
fn default() -> LinuxDeviceType {
LinuxDeviceType::A
}
}
impl LinuxDeviceType {
/// Retrieve a string reference for the device type.
pub fn as_str(&self) -> &str {
match self {
Self::A => "a",
Self::B => "b",
Self::C => "c",
Self::U => "u",
Self::P => "p",
}
}
}
#[derive(
Builder,
Clone,
Debug,
Default,
Deserialize,
Eq,
Getters,
MutGetters,
Setters,
PartialEq,
Serialize,
)]
#[builder(
default,
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
/// LinuxNetDevice represents a single network device to be added to the container's network namespace
pub struct LinuxNetDevice {
#[serde(default)]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
/// Name of the device in the container namespace
name: Option<String>,
}
#[derive(
Builder,
Clone,
CopyGetters,
Debug,
Default,
Deserialize,
Eq,
Getters,
MutGetters,
Setters,
PartialEq,
Serialize,
)]
#[builder(
default,
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
/// Represents a device rule for the devices specified to the device
/// controller
pub struct LinuxDeviceCgroup {
#[serde(default)]
#[getset(get_mut = "pub", get_copy = "pub", set = "pub")]
/// Allow or deny
allow: bool,
#[serde(default, rename = "type", skip_serializing_if = "Option::is_none")]
#[getset(get_mut = "pub", get_copy = "pub", set = "pub")]
/// Device type, block, char, etc.
typ: Option<LinuxDeviceType>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get_mut = "pub", get_copy = "pub", set = "pub")]
/// Device's major number
major: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get_mut = "pub", get_copy = "pub", set = "pub")]
/// Device's minor number
minor: Option<i64>,
/// Cgroup access permissions format, rwm.
#[serde(default)]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
access: Option<String>,
}
/// This ToString trait is automatically implemented for any type which implements the Display trait.
/// As such, ToString shouldn’t be implemented directly: Display should be implemented instead,
/// and you get the ToString implementation for free.
impl Display for LinuxDeviceCgroup {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let major = self
.major
.map(|mj| mj.to_string())
.unwrap_or_else(|| "*".to_string());
let minor = self
.minor
.map(|mi| mi.to_string())
.unwrap_or_else(|| "*".to_string());
let access = self.access.as_deref().unwrap_or("");
write!(
f,
"{} {}:{} {}",
self.typ.unwrap_or_default().as_str(),
major,
minor,
access
)
}
}
#[derive(
Builder,
Clone,
Copy,
CopyGetters,
Debug,
Default,
Deserialize,
Eq,
PartialEq,
Serialize,
Setters,
)]
#[serde(rename_all = "camelCase")]
#[builder(
default,
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
#[getset(get_copy = "pub", set = "pub")]
/// LinuxMemory for Linux cgroup 'memory' resource management.
pub struct LinuxMemory {
#[serde(skip_serializing_if = "Option::is_none")]
#[getset(get_copy = "pub", set = "pub")]
/// Memory limit (in bytes).
limit: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
#[getset(get_copy = "pub", set = "pub")]
/// Memory reservation or soft_limit (in bytes).
reservation: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
#[getset(get_copy = "pub", set = "pub")]
/// Total memory limit (memory + swap).
swap: Option<i64>,
#[deprecated(
note = "kernel-memory limits are not supported in cgroups v2, and were obsoleted in kernel v5.4"
)]
#[serde(skip_serializing_if = "Option::is_none")]
#[getset(get_copy = "pub", set = "pub")]
/// Kernel memory limit (in bytes).
///
/// # Deprecated
///
/// kernel-memory limits are not supported in cgroups v2,
/// and were obsoleted in kernel v5.4.
kernel: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none", rename = "kernelTCP")]
#[getset(get_copy = "pub", set = "pub")]
/// Kernel memory limit for tcp (in bytes).
kernel_tcp: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
#[getset(get_copy = "pub", set = "pub")]
/// How aggressive the kernel will swap memory pages.
swappiness: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none", rename = "disableOOMKiller")]
#[getset(get_copy = "pub", set = "pub")]
/// DisableOOMKiller disables the OOM killer for out of memory
/// conditions.
disable_oom_killer: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
#[getset(get_copy = "pub", set = "pub")]
/// Enables hierarchical memory accounting
use_hierarchy: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
#[getset(get_copy = "pub", set = "pub")]
/// Enables checking if a new memory limit is lower
check_before_update: Option<bool>,
}
#[derive(
Builder,
Clone,
CopyGetters,
Debug,
Default,
Deserialize,
Eq,
Getters,
Setters,
PartialEq,
Serialize,
)]
#[serde(rename_all = "camelCase")]
#[builder(
default,
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
/// LinuxCPU for Linux cgroup 'cpu' resource management.
pub struct LinuxCpu {
#[serde(skip_serializing_if = "Option::is_none")]
#[getset(get_copy = "pub", set = "pub")]
/// CPU shares (relative weight (ratio) vs. other cgroups with cpu
/// shares).
shares: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
#[getset(get_copy = "pub", set = "pub")]
/// CPU hardcap limit (in usecs). Allowed cpu time in a given period.
quota: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
#[getset(get_copy = "pub", set = "pub")]
/// Cgroups are configured with minimum weight, 0: default behavior, 1: SCHED_IDLE.
idle: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
#[getset(get_copy = "pub", set = "pub")]
/// Maximum amount of accumulated time in microseconds for which tasks
/// in a cgroup can run additionally for burst during one period
burst: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
#[getset(get_copy = "pub", set = "pub")]
/// CPU period to be used for hardcapping (in usecs).
period: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
#[getset(get_copy = "pub", set = "pub")]
/// How much time realtime scheduling may use (in usecs).
realtime_runtime: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
#[getset(get_copy = "pub", set = "pub")]
/// CPU period to be used for realtime scheduling (in usecs).
realtime_period: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get = "pub", set = "pub")]
/// CPUs to use within the cpuset. Default is to use any CPU available.
cpus: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get = "pub", set = "pub")]
/// List of memory nodes in the cpuset. Default is to use any available
/// memory node.
mems: Option<String>,
}
#[derive(
Builder,
Clone,
Copy,
Debug,
Default,
Deserialize,
Eq,
CopyGetters,
Setters,
PartialEq,
Serialize,
)]
#[builder(
default,
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
#[getset(get_copy = "pub", set = "pub")]
/// LinuxPids for Linux cgroup 'pids' resource management (Linux 4.3).
pub struct LinuxPids {
#[serde(default)]
/// Maximum number of PIDs. Default is "no limit".
limit: i64,
}
#[derive(
Builder, Clone, Copy, CopyGetters, Debug, Default, Deserialize, Eq, PartialEq, Serialize,
)]
#[serde(rename_all = "camelCase")]
#[builder(
default,
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
#[getset(get_copy = "pub", set = "pub")]
/// LinuxWeightDevice struct holds a `major:minor weight` pair for
/// weightDevice.
pub struct LinuxWeightDevice {
#[serde(default)]
/// Major is the device's major number.
major: i64,
#[serde(default)]
/// Minor is the device's minor number.
minor: i64,
#[serde(skip_serializing_if = "Option::is_none")]
/// Weight is the bandwidth rate for the device.
weight: Option<u16>,
#[serde(skip_serializing_if = "Option::is_none")]
/// LeafWeight is the bandwidth rate for the device while competing with
/// the cgroup's child cgroups, CFQ scheduler only.
leaf_weight: Option<u16>,
}
#[derive(
Builder, Clone, Copy, CopyGetters, Debug, Default, Deserialize, Eq, PartialEq, Serialize,
)]
#[builder(
default,
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
#[getset(get_copy = "pub", set = "pub")]
/// LinuxThrottleDevice struct holds a `major:minor rate_per_second` pair.
pub struct LinuxThrottleDevice {
#[serde(default)]
/// Major is the device's major number.
major: i64,
#[serde(default)]
/// Minor is the device's minor number.
minor: i64,
#[serde(default)]
/// Rate is the IO rate limit per cgroup per device.
rate: u64,
}
#[derive(
Builder,
Clone,
CopyGetters,
Debug,
Default,
Deserialize,
Eq,
Getters,
Setters,
PartialEq,
Serialize,
)]
#[serde(rename_all = "camelCase")]
#[builder(
default,
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
/// LinuxBlockIO for Linux cgroup 'blkio' resource management.
pub struct LinuxBlockIo {
#[serde(skip_serializing_if = "Option::is_none")]
#[getset(get_copy = "pub", set = "pub")]
/// Specifies per cgroup weight.
weight: Option<u16>,
#[serde(skip_serializing_if = "Option::is_none")]
#[getset(get_copy = "pub", set = "pub")]
/// Specifies tasks' weight in the given cgroup while competing with the
/// cgroup's child cgroups, CFQ scheduler only.
leaf_weight: Option<u16>,
#[serde(skip_serializing_if = "Option::is_none")]
#[getset(get = "pub", set = "pub")]
/// Weight per cgroup per device, can override BlkioWeight.
weight_device: Option<Vec<LinuxWeightDevice>>,
#[serde(skip_serializing_if = "Option::is_none")]
#[getset(get = "pub", set = "pub")]
/// IO read rate limit per cgroup per device, bytes per second.
throttle_read_bps_device: Option<Vec<LinuxThrottleDevice>>,
#[serde(skip_serializing_if = "Option::is_none")]
#[getset(get = "pub", set = "pub")]
/// IO write rate limit per cgroup per device, bytes per second.
throttle_write_bps_device: Option<Vec<LinuxThrottleDevice>>,
#[serde(
skip_serializing_if = "Option::is_none",
rename = "throttleReadIOPSDevice"
)]
#[getset(get = "pub", set = "pub")]
/// IO read rate limit per cgroup per device, IO per second.
throttle_read_iops_device: Option<Vec<LinuxThrottleDevice>>,
#[serde(
skip_serializing_if = "Option::is_none",
rename = "throttleWriteIOPSDevice"
)]
#[getset(get = "pub", set = "pub")]
/// IO write rate limit per cgroup per device, IO per second.
throttle_write_iops_device: Option<Vec<LinuxThrottleDevice>>,
}
#[derive(
Builder,
Clone,
CopyGetters,
Debug,
Default,
Deserialize,
Eq,
Getters,
Setters,
PartialEq,
Serialize,
)]
#[serde(rename_all = "camelCase")]
#[builder(
default,
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
/// LinuxHugepageLimit structure corresponds to limiting kernel hugepages.
/// Default to reservation limits if supported. Otherwise fallback to page fault limits.
pub struct LinuxHugepageLimit {
#[serde(default)]
#[getset(get = "pub", set = "pub")]
/// Pagesize is the hugepage size.
/// Format: "<size><unit-prefix>B' (e.g. 64KB, 2MB, 1GB, etc.)
page_size: String,
#[serde(default)]
#[getset(get_copy = "pub", set = "pub")]
/// Limit is the limit of "hugepagesize" hugetlb reservations (if supported) or usage.
limit: i64,
}
#[derive(
Builder,
Clone,
CopyGetters,
Debug,
Default,
Deserialize,
Eq,
Getters,
Setters,
PartialEq,
Serialize,
)]
#[builder(
default,
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
/// LinuxInterfacePriority for network interfaces.
pub struct LinuxInterfacePriority {
#[serde(default)]
#[getset(get = "pub", set = "pub")]
/// Name is the name of the network interface.
name: String,
#[serde(default)]
#[getset(get_copy = "pub", set = "pub")]
/// Priority for the interface.
priority: u32,
}
/// This ToString trait is automatically implemented for any type which implements the Display trait.
/// As such, ToString shouldn’t be implemented directly: Display should be implemented instead,
/// and you get the ToString implementation for free.
impl Display for LinuxInterfacePriority {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// Serde serialization never fails since this is
// a combination of String and enums.
writeln!(f, "{} {}", self.name, self.priority)
}
}
#[derive(
Builder,
Clone,
CopyGetters,
Debug,
Default,
Deserialize,
Eq,
Getters,
Setters,
PartialEq,
Serialize,
)]
#[builder(
default,
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
/// LinuxNetwork identification and priority configuration.
pub struct LinuxNetwork {
#[serde(skip_serializing_if = "Option::is_none", rename = "classID")]
#[getset(get_copy = "pub", set = "pub")]
/// Set class identifier for container's network packets
class_id: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get = "pub", set = "pub")]
/// Set priority of network traffic for container.
priorities: Option<Vec<LinuxInterfacePriority>>,
}
#[derive(
Builder,
Clone,
CopyGetters,
Debug,
Default,
Deserialize,
Eq,
Getters,
MutGetters,
Setters,
PartialEq,
Serialize,
)]
#[serde(rename_all = "camelCase")]
#[builder(
default,
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
/// Resource constraints for container
pub struct LinuxResources {
#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
/// Devices configures the device allowlist.
devices: Option<Vec<LinuxDeviceCgroup>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
/// Memory restriction configuration.
memory: Option<LinuxMemory>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
/// CPU resource restriction configuration.
cpu: Option<LinuxCpu>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
/// Task resource restrictions
pids: Option<LinuxPids>,
#[serde(default, skip_serializing_if = "Option::is_none", rename = "blockIO")]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
/// BlockIO restriction configuration.
block_io: Option<LinuxBlockIo>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
/// Hugetlb limit (in bytes).
hugepage_limits: Option<Vec<LinuxHugepageLimit>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
/// Network restriction configuration.
network: Option<LinuxNetwork>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
/// Rdma resource restriction configuration. Limits are a set of key
/// value pairs that define RDMA resource limits, where the key
/// is device name and value is resource limits.
rdma: Option<HashMap<String, LinuxRdma>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
/// Unified resources.
unified: Option<HashMap<String, String>>,
}
#[derive(
Builder,
Clone,
Copy,
CopyGetters,
Debug,
Default,
Deserialize,
Eq,
MutGetters,
PartialEq,
Serialize,
)]
#[serde(rename_all = "camelCase")]
#[builder(
default,
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
#[getset(get_mut = "pub", get_copy = "pub", set = "pub")]
/// LinuxRdma for Linux cgroup 'rdma' resource management (Linux 4.11).
pub struct LinuxRdma {
#[serde(skip_serializing_if = "Option::is_none")]
/// Maximum number of HCA handles that can be opened. Default is "no
/// limit".
hca_handles: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
/// Maximum number of HCA objects that can be created. Default is "no
/// limit".
hca_objects: Option<u32>,
}
#[derive(
Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize, Hash, StrumDisplay,
)]
#[strum(serialize_all = "lowercase")]
#[serde(rename_all = "snake_case")]
/// Available Linux namespaces.
pub enum LinuxNamespaceType {
#[strum(to_string = "mnt")]
/// Mount Namespace for isolating mount points
Mount = 0x00020000,
/// Cgroup Namespace for isolating cgroup hierarchies
Cgroup = 0x02000000,
/// Uts Namespace for isolating hostname and NIS domain name
Uts = 0x04000000,
/// Ipc Namespace for isolating System V, IPC, POSIX message queues
Ipc = 0x08000000,
/// User Namespace for isolating user and group ids
User = 0x10000000,
/// PID Namespace for isolating process ids
#[default]
Pid = 0x20000000,
#[strum(to_string = "net")]
/// Network Namespace for isolating network devices, ports, stacks etc.
Network = 0x40000000,
/// Time Namespace for isolating the clocks
Time = 0x00000080,
}
impl TryFrom<&str> for LinuxNamespaceType {
type Error = OciSpecError;
fn try_from(namespace: &str) -> Result<Self, Self::Error> {
match namespace {
"mnt" | "mount" => Ok(LinuxNamespaceType::Mount),
"cgroup" => Ok(LinuxNamespaceType::Cgroup),
"uts" => Ok(LinuxNamespaceType::Uts),
"ipc" => Ok(LinuxNamespaceType::Ipc),
"user" => Ok(LinuxNamespaceType::User),
"pid" => Ok(LinuxNamespaceType::Pid),
"net" | "network" => Ok(LinuxNamespaceType::Network),
"time" => Ok(LinuxNamespaceType::Time),
_ => Err(oci_error(format!(
"unknown namespace {namespace}, could not convert"
))),
}
}
}
#[derive(
Builder,
Clone,
CopyGetters,
Debug,
Default,
Deserialize,
Eq,
Getters,
Setters,
PartialEq,
Serialize,
)]
#[builder(
default,
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
/// LinuxNamespace is the configuration for a Linux namespace.
pub struct LinuxNamespace {
#[serde(rename = "type")]
#[getset(get_copy = "pub", set = "pub")]
/// Type is the type of namespace.
typ: LinuxNamespaceType,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get = "pub", set = "pub")]
/// Path is a path to an existing namespace persisted on disk that can
/// be joined and is of the same type
path: Option<PathBuf>,
}
/// Utility function to get default namespaces.
pub fn get_default_namespaces() -> Vec<LinuxNamespace> {
vec![
LinuxNamespace {
typ: LinuxNamespaceType::Pid,
path: Default::default(),
},
LinuxNamespace {
typ: LinuxNamespaceType::Network,
path: Default::default(),
},
LinuxNamespace {
typ: LinuxNamespaceType::Ipc,
path: Default::default(),
},
LinuxNamespace {
typ: LinuxNamespaceType::Uts,
path: Default::default(),
},
LinuxNamespace {
typ: LinuxNamespaceType::Mount,
path: Default::default(),
},
LinuxNamespace {
typ: LinuxNamespaceType::Cgroup,
path: Default::default(),
},
]
}
#[derive(
Builder,
Clone,
CopyGetters,
Debug,
Default,
Deserialize,
Eq,
Getters,
MutGetters,
Setters,
PartialEq,
Serialize,
)]
#[serde(rename_all = "camelCase")]
#[builder(
default,
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
/// LinuxDevice represents the mknod information for a Linux special device
/// file.
pub struct LinuxDevice {
#[serde(default)]
#[getset(get_mut = "pub", get = "pub", set = "pub")]