Skip to content

Commit

Permalink
Update shared/ directory with LXD 5.6
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroaki Nakamura <[email protected]>
  • Loading branch information
hnakamur committed Sep 25, 2022
1 parent 4bd2c5b commit 6036c6a
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 227 deletions.
1 change: 1 addition & 0 deletions shared/api/event_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const (
EventLifecycleInstanceMetadataTemplateRetrieved = "instance-metadata-template-retrieved"
EventLifecycleInstanceMetadataUpdated = "instance-metadata-updated"
EventLifecycleInstancePaused = "instance-paused"
EventLifecycleInstanceReady = "instance-ready"
EventLifecycleInstanceRenamed = "instance-renamed"
EventLifecycleInstanceRestarted = "instance-restarted"
EventLifecycleInstanceRestored = "instance-restored"
Expand Down
16 changes: 8 additions & 8 deletions shared/idmap/idmapset_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ func (set *IdmapSet) doUidshiftIntoContainer(dir string, testmode bool, how stri
tmp := filepath.Dir(dir)
tmp, err := filepath.EvalSymlinks(tmp)
if err != nil {
return fmt.Errorf("Expand symlinks: %w", err)
return fmt.Errorf("Failed expanding symlinks of %q: %w", tmp, err)
}

dir = filepath.Join(tmp, filepath.Base(dir))
Expand All @@ -607,7 +607,7 @@ func (set *IdmapSet) doUidshiftIntoContainer(dir string, testmode bool, how stri
return filepath.SkipDir
}

intUid, intGid, _, _, inode, nlink, err := shared.GetFileStat(path)
intUID, intGID, _, _, inode, nlink, err := shared.GetFileStat(path)
if err != nil {
return err
}
Expand All @@ -623,8 +623,8 @@ func (set *IdmapSet) doUidshiftIntoContainer(dir string, testmode bool, how stri
hardLinks = append(hardLinks, inode)
}

uid := int64(intUid)
gid := int64(intGid)
uid := int64(intUID)
gid := int64(intGID)
caps := []byte{}

var newuid, newgid int64
Expand Down Expand Up @@ -661,15 +661,15 @@ func (set *IdmapSet) doUidshiftIntoContainer(dir string, testmode bool, how stri

// Shift capabilities
if len(caps) != 0 {
rootUid := int64(0)
rootUID := int64(0)
if how == "in" {
rootUid, _ = set.ShiftIntoNs(0, 0)
rootUID, _ = set.ShiftIntoNs(0, 0)
}

if how != "in" || atomic.LoadInt32(&VFS3Fscaps) == VFS3FscapsSupported {
err = SetCaps(path, caps, rootUid)
err = SetCaps(path, caps, rootUID)
if err != nil {
logger.Warnf("Unable to set file capabilities on %s", path)
logger.Warnf("Unable to set file capabilities on %q: %v", path, err)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions shared/logger/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import (

// Setup a basic empty logger on init.
func init() {
logger := logrus.StandardLogger()
logger := logrus.New()
logger.SetOutput(ioutil.Discard)

Log = newWrapper(logger)
}

// InitLogger intializes a full logging instance.
func InitLogger(filepath string, syslogName string, verbose bool, debug bool, hook logrus.Hook) error {
logger := logrus.StandardLogger()
logger := logrus.New()
logger.Level = logrus.DebugLevel
logger.SetOutput(io.Discard)

Expand Down
16 changes: 13 additions & 3 deletions shared/osarch/architectures.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ const (
ARCH_64BIT_MIPS = 10
ARCH_32BIT_RISCV_LITTLE_ENDIAN = 11
ARCH_64BIT_RISCV_LITTLE_ENDIAN = 12
ARCH_32BIT_ARMV6_LITTLE_ENDIAN = 13
ARCH_32BIT_ARMV8_LITTLE_ENDIAN = 14
)

var architectureNames = map[int]string{
ARCH_32BIT_INTEL_X86: "i686",
ARCH_64BIT_INTEL_X86: "x86_64",
ARCH_32BIT_ARMV6_LITTLE_ENDIAN: "armv6l",
ARCH_32BIT_ARMV7_LITTLE_ENDIAN: "armv7l",
ARCH_32BIT_ARMV8_LITTLE_ENDIAN: "armv8l",
ARCH_64BIT_ARMV8_LITTLE_ENDIAN: "aarch64",
ARCH_32BIT_POWERPC_BIG_ENDIAN: "ppc",
ARCH_64BIT_POWERPC_BIG_ENDIAN: "ppc64",
Expand All @@ -38,7 +42,9 @@ var architectureNames = map[int]string{
var architectureAliases = map[int][]string{
ARCH_32BIT_INTEL_X86: {"i386", "i586", "386", "x86", "generic_32"},
ARCH_64BIT_INTEL_X86: {"amd64", "generic_64"},
ARCH_32BIT_ARMV7_LITTLE_ENDIAN: {"armel", "armhf", "arm", "armhfp", "armv7a_hardfp", "armv7", "armv7a_vfpv3_hardfp"},
ARCH_32BIT_ARMV6_LITTLE_ENDIAN: {"armel", "arm"},
ARCH_32BIT_ARMV7_LITTLE_ENDIAN: {"armhf", "armhfp", "armv7a_hardfp", "armv7", "armv7a_vfpv3_hardfp"},
ARCH_32BIT_ARMV8_LITTLE_ENDIAN: {},
ARCH_64BIT_ARMV8_LITTLE_ENDIAN: {"arm64", "arm64_generic"},
ARCH_32BIT_POWERPC_BIG_ENDIAN: {"powerpc"},
ARCH_64BIT_POWERPC_BIG_ENDIAN: {"powerpc64", "ppc64"},
Expand All @@ -52,7 +58,9 @@ var architectureAliases = map[int][]string{
var architecturePersonalities = map[int]string{
ARCH_32BIT_INTEL_X86: "linux32",
ARCH_64BIT_INTEL_X86: "linux64",
ARCH_32BIT_ARMV6_LITTLE_ENDIAN: "linux32",
ARCH_32BIT_ARMV7_LITTLE_ENDIAN: "linux32",
ARCH_32BIT_ARMV8_LITTLE_ENDIAN: "linux32",
ARCH_64BIT_ARMV8_LITTLE_ENDIAN: "linux64",
ARCH_32BIT_POWERPC_BIG_ENDIAN: "linux32",
ARCH_64BIT_POWERPC_BIG_ENDIAN: "linux64",
Expand All @@ -67,8 +75,10 @@ var architecturePersonalities = map[int]string{
var architectureSupportedPersonalities = map[int][]int{
ARCH_32BIT_INTEL_X86: {},
ARCH_64BIT_INTEL_X86: {ARCH_32BIT_INTEL_X86},
ARCH_32BIT_ARMV7_LITTLE_ENDIAN: {},
ARCH_64BIT_ARMV8_LITTLE_ENDIAN: {ARCH_32BIT_ARMV7_LITTLE_ENDIAN},
ARCH_32BIT_ARMV6_LITTLE_ENDIAN: {},
ARCH_32BIT_ARMV7_LITTLE_ENDIAN: {ARCH_32BIT_ARMV6_LITTLE_ENDIAN},
ARCH_32BIT_ARMV8_LITTLE_ENDIAN: {ARCH_32BIT_ARMV6_LITTLE_ENDIAN, ARCH_32BIT_ARMV7_LITTLE_ENDIAN},
ARCH_64BIT_ARMV8_LITTLE_ENDIAN: {ARCH_32BIT_ARMV6_LITTLE_ENDIAN, ARCH_32BIT_ARMV7_LITTLE_ENDIAN, ARCH_32BIT_ARMV8_LITTLE_ENDIAN},
ARCH_32BIT_POWERPC_BIG_ENDIAN: {},
ARCH_64BIT_POWERPC_BIG_ENDIAN: {ARCH_32BIT_POWERPC_BIG_ENDIAN},
ARCH_64BIT_POWERPC_LITTLE_ENDIAN: {},
Expand Down
10 changes: 5 additions & 5 deletions shared/subprocess/bgpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestSignalHandling(t *testing.T) {
t.Error("Failed process creation: ", err)
}

err = p.Start()
err = p.Start(context.Background())
if err != nil {
t.Error("Failed to start process ", err)
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestStopRestart(t *testing.T) {
t.Error("Failed process creation: ", err)
}

err = p.Start()
err = p.Start(context.Background())
if err != nil {
t.Error("Failed to start process: ", err)
}
Expand All @@ -110,12 +110,12 @@ func TestStopRestart(t *testing.T) {
t.Error("Failed to import process: ", err)
}

err = p.Start()
err = p.Start(context.Background())
if err != nil {
t.Error("Failed to start process: ", err)
}

err = p.Restart()
err = p.Restart(context.Background())
if err != nil {
t.Error("Failed to restart process: ", err)
}
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestProcessStartWaitExit(t *testing.T) {
t.Error("Failed process creation: ", err)
}

err = p.Start()
err = p.Start(context.Background())
if err != nil {
t.Error("Failed to start process: ", err)
}
Expand Down
10 changes: 3 additions & 7 deletions shared/subprocess/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,14 @@ func NewProcess(name string, args []string, stdoutPath string, stderrPath string
}
}

p, err := NewProcessWithFds(name, args, nil, stdout, stderr)
if err != nil {
return nil, fmt.Errorf("Error when creating process object: %w", err)
}

p := NewProcessWithFds(name, args, nil, stdout, stderr)
p.closeFds = true

return p, nil
}

// NewProcessWithFds is a constructor for a process object. Represents a process with argument config. Returns an address to process.
func NewProcessWithFds(name string, args []string, stdin io.ReadCloser, stdout io.WriteCloser, stderr io.WriteCloser) (*Process, error) {
func NewProcessWithFds(name string, args []string, stdin io.ReadCloser, stdout io.WriteCloser, stderr io.WriteCloser) *Process {
proc := Process{
Name: name,
Args: args,
Expand All @@ -52,7 +48,7 @@ func NewProcessWithFds(name string, args []string, stdin io.ReadCloser, stdout i
Stderr: stderr,
}

return &proc, nil
return &proc
}

// ImportProcess imports a saved process into a subprocess object.
Expand Down
18 changes: 9 additions & 9 deletions shared/subprocess/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,22 @@ func (p *Process) Stop() error {
}

// Start will start the given process object.
func (p *Process) Start() error {
return p.start(nil)
func (p *Process) Start(ctx context.Context) error {
return p.start(ctx, nil)
}

// StartWithFiles will start the given process object with extra file descriptors.
func (p *Process) StartWithFiles(fds []*os.File) error {
return p.start(fds)
func (p *Process) StartWithFiles(ctx context.Context, fds []*os.File) error {
return p.start(ctx, fds)
}

func (p *Process) start(fds []*os.File) error {
func (p *Process) start(ctx context.Context, fds []*os.File) error {
var cmd *exec.Cmd

if p.Apparmor != "" && p.hasApparmor() {
cmd = exec.Command("aa-exec", append([]string{"-p", p.Apparmor, p.Name}, p.Args...)...)
cmd = exec.CommandContext(ctx, "aa-exec", append([]string{"-p", p.Apparmor, p.Name}, p.Args...)...)
} else {
cmd = exec.Command(p.Name, p.Args...)
cmd = exec.CommandContext(ctx, p.Name, p.Args...)
}

cmd.Stdout = p.Stdout
Expand Down Expand Up @@ -189,13 +189,13 @@ func (p *Process) start(fds []*os.File) error {
}

// Restart stop and starts the given process object.
func (p *Process) Restart() error {
func (p *Process) Restart(ctx context.Context) error {
err := p.Stop()
if err != nil {
return fmt.Errorf("Unable to stop process: %w", err)
}

err = p.Start()
err = p.Start(ctx)
if err != nil {
return fmt.Errorf("Unable to start process: %w", err)
}
Expand Down
3 changes: 3 additions & 0 deletions shared/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ const HTTPSDefaultPort = 8443
const HTTPDefaultPort = 8080
const HTTPSMetricsDefaultPort = 9100

// HTTPSStorageBucketsDefaultPort the default port for the storage buckets listener.
const HTTPSStorageBucketsDefaultPort = 9000

// URLEncode encodes a path and query parameters to a URL.
func URLEncode(path string, query map[string]string) (string, error) {
u, err := url.Parse(path)
Expand Down
106 changes: 14 additions & 92 deletions shared/util_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"sync/atomic"
"unsafe"

"github.com/pkg/xattr"
"golang.org/x/sys/unix"

"github.com/lxc/lxd/lxd/revert"
Expand Down Expand Up @@ -68,106 +69,27 @@ func SetSize(fd int, width int, height int) (err error) {
return nil
}

// This uses ssize_t llistxattr(const char *path, char *list, size_t size); to
// handle symbolic links (should it in the future be possible to set extended
// attributed on symlinks): If path is a symbolic link the extended attributes
// associated with the link itself are retrieved.
func llistxattr(path string, list []byte) (sz int, err error) {
var _p0 *byte
_p0, err = unix.BytePtrFromString(path)
// GetAllXattr retrieves all extended attributes associated with a file, directory or symbolic link.
func GetAllXattr(path string) (map[string]string, error) {
xattrNames, err := xattr.LList(path)
if err != nil {
return
}

var _p1 unsafe.Pointer
if len(list) > 0 {
_p1 = unsafe.Pointer(&list[0])
} else {
_p1 = unsafe.Pointer(nil)
}

r0, _, e1 := unix.Syscall(unix.SYS_LLISTXATTR, uintptr(unsafe.Pointer(_p0)), uintptr(_p1), uintptr(len(list)))
sz = int(r0)
if e1 != 0 {
err = e1
}

return
}

// GetAllXattr retrieves all extended attributes associated with a file,
// directory or symbolic link.
func GetAllXattr(path string) (xattrs map[string]string, err error) {
// Call llistxattr() twice: First, to determine the size of the buffer
// we need to allocate to store the extended attributes, second, to
// actually store the extended attributes in the buffer. Also, check if
// the size/number of extended attributes hasn't increased between the
// two calls.
pre, err := llistxattr(path, nil)
if err != nil || pre < 0 {
if err == unix.EOPNOTSUPP {
// Some filesystems don't support llistxattr() for various reasons.
// Interpret this as a set of no xattrs, instead of an error.
if errors.Is(err, unix.EOPNOTSUPP) {
return nil, nil
}

return nil, err
return nil, fmt.Errorf("Failed getting extended attributes from %q: %w", path, err)
}

if pre == 0 {
return nil, nil
}

dest := make([]byte, pre)

post, err := llistxattr(path, dest)
if err != nil || post < 0 {
return nil, err
}

if post > pre {
return nil, fmt.Errorf("Extended attribute list size increased from %d to %d during retrieval", pre, post)
}

split := strings.Split(string(dest), "\x00")
if split == nil {
return nil, fmt.Errorf("No valid extended attribute key found")
}
// *listxattr functions return a list of names as an unordered array
// of null-terminated character strings (attribute names are separated
// by null bytes ('\0')), like this: user.name1\0system.name1\0user.name2\0
// Since we split at the '\0'-byte the last element of the slice will be
// the empty string. We remove it:
if split[len(split)-1] == "" {
split = split[:len(split)-1]
}

xattrs = make(map[string]string, len(split))

for _, x := range split {
xattr := string(x)
// Call Getxattr() twice: First, to determine the size of the
// buffer we need to allocate to store the extended attributes,
// second, to actually store the extended attributes in the
// buffer. Also, check if the size of the extended attribute
// hasn't increased between the two calls.
pre, err = unix.Getxattr(path, xattr, nil)
if err != nil || pre < 0 {
return nil, err
}

dest = make([]byte, pre)
post := 0
if pre > 0 {
post, err = unix.Getxattr(path, xattr, dest)
if err != nil || post < 0 {
return nil, err
}
}

if post > pre {
return nil, fmt.Errorf("Extended attribute '%s' size increased from %d to %d during retrieval", xattr, pre, post)
var xattrs = make(map[string]string, len(xattrNames))
for _, xattrName := range xattrNames {
value, err := xattr.LGet(path, xattrName)
if err != nil {
return nil, fmt.Errorf("Failed getting %q extended attribute from %q: %w", xattrName, path, err)
}

xattrs[xattr] = string(dest)
xattrs[xattrName] = string(value)
}

return xattrs, nil
Expand Down
Loading

0 comments on commit 6036c6a

Please sign in to comment.