github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/util/data_image.go (about)

     1  // +build !container-binary
     2  
     3  package util
     4  
     5  import (
     6  	"fmt"
     7  	"io"
     8  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  
    12  	"github.com/sirupsen/logrus"
    13  	"github.com/emc-advanced-dev/pkg/errors"
    14  	unikos "github.com/solo-io/unik/pkg/os"
    15  )
    16  
    17  func BuildRawDataImageWithType(dataTar io.ReadCloser, size unikos.MegaBytes, volType string, usePartitionTables bool) (string, error) {
    18  	buildDir, err := ioutil.TempDir("", ".raw_data_image_folder.")
    19  	if err != nil {
    20  		return "", errors.New("creating tmp build folder", err)
    21  	}
    22  	defer os.RemoveAll(buildDir)
    23  
    24  	dataFolder := filepath.Join(buildDir, "data")
    25  	err = os.Mkdir(dataFolder, 0755)
    26  	if err != nil {
    27  		return "", errors.New("creating tmp data folder", err)
    28  	}
    29  
    30  	if err := unikos.ExtractTar(dataTar, dataFolder); err != nil {
    31  		return "", errors.New("extracting data tar", err)
    32  	}
    33  
    34  	container := NewContainer("image-creator").Privileged(true).WithVolume("/dev/", "/dev/").
    35  		WithVolume(buildDir+"/", "/opt/vol")
    36  
    37  	tmpResultFile, err := ioutil.TempFile(buildDir, "data.image.result.img.")
    38  	if err != nil {
    39  		return "", err
    40  	}
    41  	tmpResultFile.Close()
    42  	args := []string{"-o", filepath.Base(tmpResultFile.Name())}
    43  
    44  	if size > 0 {
    45  		args = append(args, "-p", fmt.Sprintf("%v", usePartitionTables),
    46  			"-v", fmt.Sprintf("%s,%v", filepath.Base(dataFolder), size.ToBytes()))
    47  	} else {
    48  		args = append(args, "-p", fmt.Sprintf("%v", usePartitionTables),
    49  			"-v", filepath.Base(dataFolder),
    50  		)
    51  	}
    52  	args = append(args, "-t", volType)
    53  
    54  	logrus.WithFields(logrus.Fields{
    55  		"command": args,
    56  	}).Debugf("running image-creator container")
    57  
    58  	if err = container.Run(args...); err != nil {
    59  		return "", errors.New("failed running image-creator on "+dataFolder, err)
    60  	}
    61  
    62  	resultFile, err := ioutil.TempFile("", "data-volume-creator-result.img.")
    63  	if err != nil {
    64  		return "", err
    65  	}
    66  	resultFile.Close()
    67  	if err := os.Rename(tmpResultFile.Name(), resultFile.Name()); err != nil {
    68  		return "", errors.New("renaming "+tmpResultFile.Name()+" to "+resultFile.Name(), err)
    69  	}
    70  
    71  	return resultFile.Name(), nil
    72  }
    73  
    74  func BuildRawDataImage(dataTar io.ReadCloser, size unikos.MegaBytes, usePartitionTables bool) (string, error) {
    75  	return BuildRawDataImageWithType(dataTar, size, "ext2", usePartitionTables)
    76  }
    77  func BuildEmptyDataVolumeWithType(size unikos.MegaBytes, volType string) (string, error) {
    78  
    79  	if size < 1 {
    80  		return "", errors.New("must specify size > 0", nil)
    81  	}
    82  	dataFolder, err := ioutil.TempDir("", "empty.data.folder.")
    83  	if err != nil {
    84  		return "", errors.New("creating tmp build folder", err)
    85  	}
    86  	defer os.RemoveAll(dataFolder)
    87  
    88  	buildDir := filepath.Dir(dataFolder)
    89  
    90  	container := NewContainer("image-creator").Privileged(true).WithVolume("/dev/", "/dev/").
    91  		WithVolume(buildDir+"/", "/opt/vol")
    92  
    93  	tmpResultFile, err := ioutil.TempFile(buildDir, "data.image.result.img.")
    94  	if err != nil {
    95  		return "", err
    96  	}
    97  	tmpResultFile.Close()
    98  	args := []string{"-v", fmt.Sprintf("%s,%v", filepath.Base(dataFolder), size.ToBytes()), "-o", filepath.Base(tmpResultFile.Name())}
    99  	args = append(args, "-t", volType)
   100  
   101  	logrus.WithFields(logrus.Fields{
   102  		"command": args,
   103  	}).Debugf("running image-creator container")
   104  	if err := container.Run(args...); err != nil {
   105  		return "", errors.New("failed running image-creator on "+dataFolder, err)
   106  	}
   107  
   108  	resultFile, err := ioutil.TempFile("", "empty-data-volume-creator-result.img.")
   109  	if err != nil {
   110  		return "", err
   111  	}
   112  	resultFile.Close()
   113  	if err := os.Rename(tmpResultFile.Name(), resultFile.Name()); err != nil {
   114  		return "", errors.New("renaming "+tmpResultFile.Name()+" to "+resultFile.Name(), err)
   115  	}
   116  
   117  	return resultFile.Name(), nil
   118  }
   119  
   120  func BuildEmptyDataVolume(size unikos.MegaBytes) (string, error) {
   121  	return BuildEmptyDataVolumeWithType(size, "ext2")
   122  }