 a8c5ff57e5
			
		
	
	a8c5ff57e5
	
	
	
		
			
			Cleans up loop devices if part of the test or mount process fails. Also increases btrfs default file size to 650MB to accommodate minimum btrfs size on ppc64le and s390x Signed-off-by: Christopher Jones <tophj@linux.vnet.ibm.com>
		
			
				
	
	
		
			57 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			57 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // +build linux
 | |
| 
 | |
| package testutil
 | |
| 
 | |
| import (
 | |
| 	"io/ioutil"
 | |
| 	"os"
 | |
| 	"os/exec"
 | |
| 	"strings"
 | |
| 
 | |
| 	"github.com/pkg/errors"
 | |
| 	"github.com/sirupsen/logrus"
 | |
| )
 | |
| 
 | |
| // NewLoopback creates a loopback device, and returns its device name (/dev/loopX), and its clean-up function.
 | |
| func NewLoopback(size int64) (string, func() error, error) {
 | |
| 	// create temporary file for the disk image
 | |
| 	file, err := ioutil.TempFile("", "containerd-test-loopback")
 | |
| 	if err != nil {
 | |
| 		return "", nil, errors.Wrap(err, "could not create temporary file for loopback")
 | |
| 	}
 | |
| 
 | |
| 	if err := file.Truncate(size); err != nil {
 | |
| 		file.Close()
 | |
| 		os.Remove(file.Name())
 | |
| 		return "", nil, errors.Wrap(err, "failed to resize temp file")
 | |
| 	}
 | |
| 	file.Close()
 | |
| 
 | |
| 	// create device
 | |
| 	losetup := exec.Command("losetup", "--find", "--show", file.Name())
 | |
| 	p, err := losetup.Output()
 | |
| 	if err != nil {
 | |
| 		os.Remove(file.Name())
 | |
| 		return "", nil, errors.Wrap(err, "loopback setup failed")
 | |
| 	}
 | |
| 
 | |
| 	deviceName := strings.TrimSpace(string(p))
 | |
| 	logrus.Debugf("Created loop device %s (using %s)", deviceName, file.Name())
 | |
| 
 | |
| 	cleanup := func() error {
 | |
| 		// detach device
 | |
| 		logrus.Debugf("Removing loop device %s", deviceName)
 | |
| 		losetup := exec.Command("losetup", "--detach", deviceName)
 | |
| 		err := losetup.Run()
 | |
| 		if err != nil {
 | |
| 			return errors.Wrapf(err, "Could not remove loop device %s", deviceName)
 | |
| 		}
 | |
| 
 | |
| 		// remove file
 | |
| 		logrus.Debugf("Removing temporary file %s", file.Name())
 | |
| 		return os.Remove(file.Name())
 | |
| 	}
 | |
| 
 | |
| 	return deviceName, cleanup, nil
 | |
| }
 |