Files
gluetun/internal/tun/create.go
Quentin McGaw (desktop) 6a545aa088 Maint: tun package to handle tun device operations
- Moved from openvpn package to tun package
- TUN check verifies Rdev value
- TUN create
- Inject as interface to main function
- Add integration test
- Clearer log message for end users if tun device does not exist
- Remove unix package (unneeded for tests)
- Remove tun file opening at the end of tun file creation
- Do not mock unix.Mkdev (no OS operation)
- Remove Tun operations from OpenVPN configurator
2021-08-18 15:31:08 +00:00

39 lines
629 B
Go

package tun
import (
"errors"
"fmt"
"os"
"path/filepath"
"golang.org/x/sys/unix"
)
type Creator interface {
Create(path string) error
}
var (
ErrMknod = errors.New("cannot create TUN device file node")
)
// Create creates a TUN device at the path specified.
func (t *Tun) Create(path string) error {
parentDir := filepath.Dir(path)
if err := os.MkdirAll(parentDir, 0751); err != nil { //nolint:gomnd
return err
}
const (
major = 10
minor = 200
)
dev := unix.Mkdev(major, minor)
err := t.mknod(path, unix.S_IFCHR, int(dev))
if err != nil {
return fmt.Errorf("%w: %s", ErrMknod, err)
}
return nil
}