File: loopback_linux.go

package info (click to toggle)
continuity 0.4.5~ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 712 kB
  • sloc: makefile: 50
file content (118 lines) | stat: -rw-r--r-- 3,065 bytes parent folder | download
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
//go:build linux
// +build linux

/*
   Copyright The containerd Authors.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package loopback

import (
	"bytes"
	"errors"
	"fmt"
	"os"
	"os/exec"
	"strings"
	"syscall"

	"github.com/containerd/log"
)

// New creates a loopback device
func New(size int64) (*Loopback, error) {
	// create temporary file for the disk image
	file, err := os.CreateTemp("", "containerd-test-loopback")
	if err != nil {
		return nil, fmt.Errorf("could not create temporary file for loopback: %w", err)
	}

	if err := file.Truncate(size); err != nil {
		file.Close()
		os.Remove(file.Name())
		return nil, fmt.Errorf("failed to resize temp file: %w", err)
	}
	file.Close()

	// create device
	losetup := exec.Command("losetup", "--find", "--show", file.Name())
	var stdout, stderr bytes.Buffer
	losetup.Stdout = &stdout
	losetup.Stderr = &stderr
	if err := losetup.Run(); err != nil {
		os.Remove(file.Name())
		return nil, fmt.Errorf("loopback setup failed (%v): stdout=%q, stderr=%q: %w", losetup.Args, stdout.String(), stderr.String(), err)
	}

	deviceName := strings.TrimSpace(stdout.String())
	log.L.Debugf("Created loop device %s (using %s)", deviceName, file.Name())

	cleanup := func() error {
		// detach device
		log.L.Debugf("Removing loop device %s", deviceName)
		losetup := exec.Command("losetup", "--detach", deviceName)
		if out, err := losetup.CombinedOutput(); err != nil {
			return fmt.Errorf("Could not remove loop device %s (%v): %q: %w", deviceName, losetup.Args, string(out), err)
		}

		// remove file
		log.L.Debugf("Removing temporary file %s", file.Name())
		return os.Remove(file.Name())
	}

	l := Loopback{
		File:   file.Name(),
		Device: deviceName,
		close:  cleanup,
	}
	return &l, nil
}

// Loopback device
type Loopback struct {
	// File is the underlying sparse file
	File string
	// Device is /dev/loopX
	Device string
	close  func() error
}

// SoftSize returns st_size
func (l *Loopback) SoftSize() (int64, error) {
	st, err := os.Stat(l.File)
	if err != nil {
		return 0, err
	}
	return st.Size(), nil
}

// HardSize returns st_blocks * 512; see stat(2)
func (l *Loopback) HardSize() (int64, error) {
	st, err := os.Stat(l.File)
	if err != nil {
		return 0, err
	}
	st2, ok := st.Sys().(*syscall.Stat_t)
	if !ok {
		return 0, errors.New("st.Sys() is not a *syscall.Stat_t")
	}
	// NOTE: st_blocks has nothing to do with st_blksize; see stat(2)
	return st2.Blocks * 512, nil
}

// Close detaches the device and removes the underlying file
func (l *Loopback) Close() error {
	return l.close()
}