<commit_before>package devicemapper

import (
	"archive/tar"
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"
	"sync"
	"syscall"
	"time"

	"github.com/control-center/serviced/utils"
	"github.com/control-center/serviced/volume"
	"github.com/docker/docker/daemon/graphdriver/devmapper"
	"github.com/docker/docker/pkg/devicemapper"
	"github.com/docker/docker/pkg/units"
	"github.com/zenoss/glog"
)

var (
	ErrNoShrinkage = errors.New("you can't shrink a device")
)

func init() {
	volume.Register(volume.DriverTypeDeviceMapper, Init)
}

type DeviceMapperDriver struct {
	root         string
	options      []string
	DeviceSet    *devmapper.DeviceSet
	DevicePrefix string
}

type DeviceMapperVolume struct {
	name     string
	path     string
	tenant   string
	driver   *DeviceMapperDriver
	Metadata *SnapshotMetadata
	sync.Mutex
}

// Init initializes the devicemapper driver
func Init(root string, options []string) (volume.Driver, error) {
	driver := &DeviceMapperDriver{
		root:    root,
		options: options,
	}
	if err := driver.ensureInitialized(); err != nil {
		return nil, err
	}
	return driver, nil
}

// Root implements volume.Driver.Root
func (d *DeviceMapperDriver) Root() string {
	return d.root
}

// DriverType implements volume.Driver.DriverType
func (d *DeviceMapperDriver) DriverType() volume.DriverType {
	return volume.DriverTypeDeviceMapper
}

func getTenant(from string) string {
	parts := strings.Split(from, "_")
	return parts[0]
}

// Create implements volume.Driver.Create
func (d *DeviceMapperDriver) Create(volumeName string) (volume.Volume, error) {
	glog.V(2).Infof("Create() (%s) START", volumeName)
	defer glog.V(2).Infof("Create() (%s) END", volumeName)
	if d.Exists(volumeName) {
		return nil, volume.ErrVolumeExists
	}
	// Create a new device
	volumeDevice, err := utils.NewUUID62()
	if err != nil {
		return nil, err
	}
	if err := d.DeviceSet.AddDevice(volumeDevice, ""); err != nil {
		return nil, err
	}
	glog.V(1).Infof("Allocated new device %s", volumeDevice)
	// Create the mount target directory if it doesn't exist
	mountpoint := d.volumeDir(volumeName)
	if err := os.MkdirAll(mountpoint, 0755); err != nil && !os.IsExist(err) {
		return nil, err
	}
	glog.V(1).Infof("Ensured existence of mount point %s", mountpoint)
	md := d.MetadataDir()
	if err := os.MkdirAll(md, 0755); err != nil && !os.IsExist(err) {
		return nil, err
	}
	glog.V(1).Infof("Ensured existence of metadata dir %s", md)
	vol, err := d.newVolume(volumeName)
	if err != nil {
		return nil, err
	}
	if err := vol.Metadata.SetCurrentDevice(volumeDevice); err != nil {
		return nil, err
	}
	glog.V(1).Infof("Set current device to %s", volumeDevice)
	if err := d.DeviceSet.MountDevice(volumeDevice, mountpoint, volumeName); err != nil {
		return nil, err
	}
	glog.V(1).Infof("Mounted device %s to %s", volumeDevice, mountpoint)
	return vol, nil
}

func (d *DeviceMapperDriver) newVolume(volumeName string) (*DeviceMapperVolume, error) {
	tenant := getTenant(volumeName)
	metadata, err := NewMetadata(d.MetadataPath(tenant))
	if err != nil {
		return nil, err
	}
	vol := &DeviceMapperVolume{
		name:     volumeName,
		path:     d.volumeDir(volumeName),
		tenant:   tenant,
		driver:   d,
		Metadata: metadata,
	}
	return vol, nil
}

// GetTenant implements volume.Driver.GetTenant
func (d *DeviceMapperDriver) GetTenant(volumeName string) (volume.Volume, error) {
	if !d.Exists(volumeName) {
		return nil, volume.ErrVolumeNotExists
	}
	return d.Get(getTenant(volumeName))
}

// Resize implements volume.Driver.Resize.
func (d *DeviceMapperDriver) Resize(volumeName string, size uint64) error {
	vol, err := d.newVolume(volumeName)
	if err != nil {
		return err
	}

	dev := vol.Metadata.CurrentDevice()
	devicename := fmt.Sprintf("%s-%s", d.DevicePrefix, dev)

	// Get the active table for the device
	start, oldSectors, targetType, params, err := devicemapper.GetTable(devicename)
	if err != nil {
		return err
	}

	// Figure out how many sectors we need
	newSectors := size / 512

	// If the new table size isn't larger than the old, it's invalid
	if newSectors <= oldSectors {
		return ErrNoShrinkage
	}

	// Create the new table description using the sectors computed
	oldTable := fmt.Sprintf("%d %d %s %s", start, oldSectors, targetType, params)
	newTable := fmt.Sprintf("%d %d %s %s", start, newSectors, targetType, params)
	glog.V(2).Infof("Replacing old table (%s) with new table (%s)", oldTable, newTable)

	// Would love to do this with libdevmapper, but DM_TABLE_LOAD isn't
	// exposed, so we'll shell out rather than muck with ioctl
	dmsetupLoad := exec.Command("dmsetup", "load", devicename)
	dmsetupLoad.Stdin = strings.NewReader(newTable)
	if output, err := dmsetupLoad.CombinedOutput(); err != nil {
		glog.Errorf("Unable to load new table (%s)", string(output))
		return err
	}
	glog.V(2).Infof("Inactive table slot updated with new size")

	// "Resume" the device to load the inactive table into the active slot
	if err := devicemapper.ResumeDevice(devicename); err != nil {
		return err
	}
	glog.V(2).Infof("Loaded inactive table into the active slot")

	// Resize the filesystem to use the new space
	dmDevice := fmt.Sprintf("/dev/mapper/%s", devicename)
	resize2fs := exec.Command("resize2fs", dmDevice)
	if output, err := resize2fs.CombinedOutput(); err != nil {
		glog.Errorf("Unable to resize filesystem (%s)", string(output))
		return err
	}
	newSize := volume.FilesystemBytesSize(vol.Path())
	human := units.HumanSize(float64(newSize))
	glog.Infof("Resized filesystem. New size: %s", human)
	return nil<commit_msg>Use BytesSize instead of HumanSize, obviously<commit_after>package devicemapper

import (
	"archive/tar"
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"
	"sync"
	"syscall"
	"time"

	"github.com/control-center/serviced/utils"
	"github.com/control-center/serviced/volume"
	"github.com/docker/docker/daemon/graphdriver/devmapper"
	"github.com/docker/docker/pkg/devicemapper"
	"github.com/docker/docker/pkg/units"
	"github.com/zenoss/glog"
)

var (
	ErrNoShrinkage = errors.New("you can't shrink a device")
)

func init() {
	volume.Register(volume.DriverTypeDeviceMapper, Init)
}

type DeviceMapperDriver struct {
	root         string
	options      []string
	DeviceSet    *devmapper.DeviceSet
	DevicePrefix string
}

type DeviceMapperVolume struct {
	name     string
	path     string
	tenant   string
	driver   *DeviceMapperDriver
	Metadata *SnapshotMetadata
	sync.Mutex
}

// Init initializes the devicemapper driver
func Init(root string, options []string) (volume.Driver, error) {
	driver := &DeviceMapperDriver{
		root:    root,
		options: options,
	}
	if err := driver.ensureInitialized(); err != nil {
		return nil, err
	}
	return driver, nil
}

// Root implements volume.Driver.Root
func (d *DeviceMapperDriver) Root() string {
	return d.root
}

// DriverType implements volume.Driver.DriverType
func (d *DeviceMapperDriver) DriverType() volume.DriverType {
	return volume.DriverTypeDeviceMapper
}

func getTenant(from string) string {
	parts := strings.Split(from, "_")
	return parts[0]
}

// Create implements volume.Driver.Create
func (d *DeviceMapperDriver) Create(volumeName string) (volume.Volume, error) {
	glog.V(2).Infof("Create() (%s) START", volumeName)
	defer glog.V(2).Infof("Create() (%s) END", volumeName)
	if d.Exists(volumeName) {
		return nil, volume.ErrVolumeExists
	}
	// Create a new device
	volumeDevice, err := utils.NewUUID62()
	if err != nil {
		return nil, err
	}
	if err := d.DeviceSet.AddDevice(volumeDevice, ""); err != nil {
		return nil, err
	}
	glog.V(1).Infof("Allocated new device %s", volumeDevice)
	// Create the mount target directory if it doesn't exist
	mountpoint := d.volumeDir(volumeName)
	if err := os.MkdirAll(mountpoint, 0755); err != nil && !os.IsExist(err) {
		return nil, err
	}
	glog.V(1).Infof("Ensured existence of mount point %s", mountpoint)
	md := d.MetadataDir()
	if err := os.MkdirAll(md, 0755); err != nil && !os.IsExist(err) {
		return nil, err
	}
	glog.V(1).Infof("Ensured existence of metadata dir %s", md)
	vol, err := d.newVolume(volumeName)
	if err != nil {
		return nil, err
	}
	if err := vol.Metadata.SetCurrentDevice(volumeDevice); err != nil {
		return nil, err
	}
	glog.V(1).Infof("Set current device to %s", volumeDevice)
	if err := d.DeviceSet.MountDevice(volumeDevice, mountpoint, volumeName); err != nil {
		return nil, err
	}
	glog.V(1).Infof("Mounted device %s to %s", volumeDevice, mountpoint)
	return vol, nil
}

func (d *DeviceMapperDriver) newVolume(volumeName string) (*DeviceMapperVolume, error) {
	tenant := getTenant(volumeName)
	metadata, err := NewMetadata(d.MetadataPath(tenant))
	if err != nil {
		return nil, err
	}
	vol := &DeviceMapperVolume{
		name:     volumeName,
		path:     d.volumeDir(volumeName),
		tenant:   tenant,
		driver:   d,
		Metadata: metadata,
	}
	return vol, nil
}

// GetTenant implements volume.Driver.GetTenant
func (d *DeviceMapperDriver) GetTenant(volumeName string) (volume.Volume, error) {
	if !d.Exists(volumeName) {
		return nil, volume.ErrVolumeNotExists
	}
	return d.Get(getTenant(volumeName))
}

// Resize implements volume.Driver.Resize.
func (d *DeviceMapperDriver) Resize(volumeName string, size uint64) error {
	vol, err := d.newVolume(volumeName)
	if err != nil {
		return err
	}

	dev := vol.Metadata.CurrentDevice()
	devicename := fmt.Sprintf("%s-%s", d.DevicePrefix, dev)

	// Get the active table for the device
	start, oldSectors, targetType, params, err := devicemapper.GetTable(devicename)
	if err != nil {
		return err
	}

	// Figure out how many sectors we need
	newSectors := size / 512

	// If the new table size isn't larger than the old, it's invalid
	if newSectors <= oldSectors {
		return ErrNoShrinkage
	}

	// Create the new table description using the sectors computed
	oldTable := fmt.Sprintf("%d %d %s %s", start, oldSectors, targetType, params)
	newTable := fmt.Sprintf("%d %d %s %s", start, newSectors, targetType, params)
	glog.V(2).Infof("Replacing old table (%s) with new table (%s)", oldTable, newTable)

	// Would love to do this with libdevmapper, but DM_TABLE_LOAD isn't
	// exposed, so we'll shell out rather than muck with ioctl
	dmsetupLoad := exec.Command("dmsetup", "load", devicename)
	dmsetupLoad.Stdin = strings.NewReader(newTable)
	if output, err := dmsetupLoad.CombinedOutput(); err != nil {
		glog.Errorf("Unable to load new table (%s)", string(output))
		return err
	}
	glog.V(2).Infof("Inactive table slot updated with new size")

	// "Resume" the device to load the inactive table into the active slot
	if err := devicemapper.ResumeDevice(devicename); err != nil {
		return err
	}
	glog.V(2).Infof("Loaded inactive table into the active slot")

	// Resize the filesystem to use the new space
	dmDevice := fmt.Sprintf("/dev/mapper/%s", devicename)
	resize2fs := exec.Command("resize2fs", dmDevice)
	if output, err := resize2fs.CombinedOutput(); err != nil {
		glog.Errorf("Unable to resize filesystem (%s)", string(output))
		return err
	}
	newSize := volume.FilesystemBytesSize(vol.Path())
	human := units.BytesSize(float64(newSize))
	glog.Infof("Resized filesystem. New size: %s", human)
	return nil
{'fraction_non_alphanumeric': 0.1311344238975818, 'fraction_numerical': 0.005512091038406828, 'mean_word_length': 4.33127962085308, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5919268', 'n_tokens_mistral': 3991, 'n_tokens_neox': 3871, 'n_words': 1060}