fix bug --device enable specifying directory as device

Signed-off-by: Qi Wang <qiwan@redhat.com>
This commit is contained in:
Qi Wang
2019-02-26 11:05:39 -05:00
parent 7418ff988b
commit c90e0ea346
2 changed files with 28 additions and 7 deletions

View File

@ -46,19 +46,32 @@ func devicesFromPath(g *generate.Generator, devicePath string) error {
return errors.Wrapf(err, "cannot stat %s", devicePath)
}
if st.IsDir() {
if len(devs) > 2 {
return errors.Wrapf(unix.EINVAL, "not allowed to specify destination with a directory %s", devicePath)
}
found := false
src := resolvedDevicePath
dest := src
var devmode string
if len(devs) > 1 {
if len(devs[1]) > 0 && devs[1][0] == '/' {
dest = devs[1]
} else {
devmode = devs[1]
}
}
if len(devs) > 2 {
if devmode != "" {
return errors.Wrapf(unix.EINVAL, "invalid device specification %s", devicePath)
}
devmode = devs[2]
}
// mount the internal devices recursively
if err := filepath.Walk(resolvedDevicePath, func(dpath string, f os.FileInfo, e error) error {
if f.Mode()&os.ModeDevice == os.ModeDevice {
found = true
device := dpath
if len(devs) > 1 {
device = fmt.Sprintf("%s:%s", dpath, devs[1])
device := fmt.Sprintf("%s:%s", dpath, filepath.Join(dest, strings.TrimPrefix(dpath, src)))
if devmode != "" {
device = fmt.Sprintf("%s:%s", device, devmode)
}
if err := addDevice(g, device); err != nil {
return errors.Wrapf(err, "failed to add %s device", dpath)