Skip to content
Permalink
Browse files

Merge pull request #8 from ernoaapa/linuxkit-update

Linuxkit update v0.2
  • Loading branch information...
ernoaapa committed Mar 19, 2018
2 parents cb515d9 + 4824194 commit 7b4134cb10356324f03a4cbc6740800a7e6ea412
@@ -40,12 +40,12 @@ func buildImage(sourceDir, filename string) error {
return err
}

if err := addPartitions(filename); err != nil {
if err := addDevMappings(filename); err != nil {
return err
}
defer removePartitions(filename)
defer removeDevMappings(filename)

devices, err := getMappings(filename)
devices, err := getDevMappings(filename)
if err != nil {
return err
}
@@ -5,41 +5,28 @@ import (
"fmt"
"os"
"os/exec"
"sort"
"strconv"
"strings"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

func mapAsDevice(path string) ([]string, error) {
log.Debugf("Map %s as a device", path)
if err := addPartitions(path); err != nil {
return []string{}, errors.Wrapf(err, "Error while adding %s partitions to devmapping", path)
}

mappings, err := getMappings(path)
if err != nil {
return []string{}, errors.Wrapf(err, "Failed to get mappings")
}
return mappings, nil
}

func addPartitions(path string) error {
func addDevMappings(path string) error {
cmd := exec.Command("kpartx", "-s", "-a", path)
cmd.Stdout = os.Stdout
return cmd.Run()
}

func getMappings(path string) ([]string, error) {
func getDevMappings(path string) ([]string, error) {
out, err := exec.Command("kpartx", "-l", path).Output()
if err != nil {
return []string{}, err
}
return mustParseDevMappings(string(out)), nil
}

func removePartitions(path string) error {
func removeDevMappings(path string) error {
log.Debugf("Unmap %s as a device", path)
cmd := exec.Command("kpartx", "-d", path)
cmd.Stdout = os.Stdout
@@ -51,25 +38,41 @@ func removePartitions(path string) error {
func mustParseDevMappings(raw string) []string {
scanner := bufio.NewScanner(strings.NewReader(raw))

partitions := []string{}
lines := []string{}
for scanner.Scan() {
line := scanner.Text()
if line != "" && !strings.Contains(line, "deleted") {
partitions = append(partitions, line)
lines = append(lines, line)
}
}

result := make([]string, len(partitions))
for _, line := range partitions {
partitions := make([]struct {
start int
path string
}, len(lines))

for i, line := range lines {
parts := strings.SplitN(line, ":", 2)
if parts[0] != "" {
info := strings.SplitN(strings.TrimSpace(parts[1]), " ", 4)
index, err := strconv.Atoi(info[0])
size, err := strconv.Atoi(info[3])
if err != nil {
log.Fatalf("Failed to parse kpartx output line: %s", line)
}
result[index] = fmt.Sprintf("/dev/mapper/%s", strings.TrimSpace(parts[0]))
partitions[i] = struct {
start int
path string
}{
start: size,
path: fmt.Sprintf("/dev/mapper/%s", strings.TrimSpace(parts[0])),
}
}
}
sort.Slice(partitions, func(i, j int) bool { return partitions[i].start < partitions[j].start })

result := make([]string, len(partitions))
for i, device := range partitions {
result[i] = device.path
}
return result
}
@@ -14,12 +14,12 @@ loop deleted : /dev/loop0

assert.Equal(t, []string{"/dev/mapper/loop1p1", "/dev/mapper/loop1p2"}, mustParseDevMappings(`
loop1p1 : 0 202752 /dev/loop1 2048
loop1p2 : 1 202752 /dev/loop1 102400
loop1p2 : 0 202752 /dev/loop1 102400
loop deleted : /dev/loop1
`))

assert.Equal(t, []string{"/dev/mapper/loop1p1", "/dev/mapper/loop1p2"}, mustParseDevMappings(`
loop1p2 : 1 202752 /dev/loop1 102400
loop1p2 : 0 202752 /dev/loop1 102400
loop1p1 : 0 202752 /dev/loop1 2048
loop deleted : /dev/loop1
`))
@@ -4,7 +4,7 @@ github.com/gorilla/mux v1.6.0
golang.org/x/net d866cfc389cec985d6fda2859936a575a55a3ab6
golang.org/x/crypto 94eea52f7b742c7cbe0b03b22f0c4c8631ece122
golang.org/x/sys 9418b3fdda89831e55f1d80702d6bf2f06d5438f
github.com/moby/tool d9d2a91780b34b92e669bbfa099f613bd9fad6bb
github.com/moby/tool bd556c86f7b1ea3667990468ab28789e61e6f2f1
github.com/opencontainers/go-digest v1.0.0-rc1
github.com/opencontainers/runtime-spec v1.0.1
github.com/opencontainers/image-spec v1.0.1

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

0 comments on commit 7b4134c

Please sign in to comment.
You can’t perform that action at this time.