Skip to content

Commit 26cfdc4

Browse files
author
Ma Shimiao
committed
validate: add mount type check
Signed-off-by: Ma Shimiao <mashimiao.fnst@cn.fujitsu.com>
1 parent ee24bb5 commit 26cfdc4

3 files changed

Lines changed: 54 additions & 3 deletions

File tree

completions/bash/ocitools

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ _ocitools_validate() {
253253

254254
case "$cur" in
255255
-*)
256-
COMPREPLY=( $( compgen -W "--hooks --path --help" -- "$cur" ) )
256+
COMPREPLY=( $( compgen -W "--hooks --host-specific --path --help" -- "$cur" ) )
257257
;;
258258
esac
259259

man/ocitools-validate.1.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Validate an OCI bundle
2121
**--hooks**
2222
Check specified hooks exist and are executable on the host.
2323

24+
**--host-specific**
25+
Check host specified configs.
26+
2427
# SEE ALSO
2528
**ocitools**(1)
2629

validate.go

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package main
22

33
import (
4+
"bufio"
45
"encoding/json"
56
"fmt"
67
"io/ioutil"
78
"os"
89
"path"
910
"path/filepath"
1011
"reflect"
12+
"runtime"
1113
"strings"
1214
"unicode"
1315
"unicode/utf8"
@@ -21,6 +23,7 @@ import (
2123
var bundleValidateFlags = []cli.Flag{
2224
cli.StringFlag{Name: "path", Value: ".", Usage: "path to a bundle"},
2325
cli.BoolFlag{Name: "hooks", Usage: "Check specified hooks exist and are executable on the host."},
26+
cli.BoolFlag{Name: "hook-specific", Usage: "Check host specified configs."},
2427
}
2528

2629
var (
@@ -80,17 +83,21 @@ var bundleValidateCommand = cli.Command{
8083
}
8184

8285
hooksCheck := context.Bool("hooks")
83-
bundleValidate(spec, rootfsPath, hooksCheck)
86+
hostCheck := context.Bool("host-specific")
87+
bundleValidate(spec, rootfsPath, hooksCheck, hostCheck)
8488
logrus.Infof("Bundle validation succeeded.")
8589
return nil
8690
},
8791
}
8892

89-
func bundleValidate(spec rspec.Spec, rootfs string, hooksCheck bool) {
93+
func bundleValidate(spec rspec.Spec, rootfs string, hooksCheck bool, hostCheck bool) {
9094
checkMandatoryField(spec)
9195
checkSemVer(spec.Version)
9296
checkPlatform(spec.Platform)
9397
checkProcess(spec.Process, rootfs)
98+
if hostCheck && spec.Platform.OS == "linux" && runtime.GOOS == "linux" {
99+
checkMounts(spec.Mounts)
100+
}
94101
checkLinux(spec)
95102
checkHooks(spec.Hooks, hooksCheck)
96103
}
@@ -192,6 +199,47 @@ func checkProcess(process rspec.Process, rootfs string) {
192199
}
193200
}
194201

202+
func supportedMountTypes() (map[string]bool, error) {
203+
supportedTypes := make(map[string]bool)
204+
f, err := os.Open("/proc/filesystems")
205+
if err != nil {
206+
return supportedTypes, err
207+
}
208+
defer f.Close()
209+
210+
s := bufio.NewScanner(f)
211+
for s.Scan() {
212+
if err := s.Err(); err != nil {
213+
return supportedTypes, err
214+
}
215+
216+
text := s.Text()
217+
parts := strings.Split(text, "\t")
218+
if len(parts) > 1 {
219+
supportedTypes[parts[1]] = true
220+
} else {
221+
supportedTypes[parts[0]] = true
222+
}
223+
}
224+
225+
supportedTypes["bind"] = true
226+
227+
return supportedTypes, nil
228+
}
229+
230+
func checkMounts(mounts []rspec.Mount) {
231+
supportedTypes, err := supportedMountTypes()
232+
if err != nil {
233+
logrus.Fatal(err)
234+
}
235+
236+
for _, mount := range mounts {
237+
if !supportedTypes[mount.Type] {
238+
logrus.Fatalf("Unsupported mount type %q", mount.Type)
239+
}
240+
}
241+
}
242+
195243
//Linux only
196244
func checkLinux(spec rspec.Spec) {
197245
utsExists := false

0 commit comments

Comments
 (0)