-
Notifications
You must be signed in to change notification settings - Fork 1
/
bakeform.go
112 lines (92 loc) · 2.64 KB
/
bakeform.go
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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"regexp"
"syscall"
"time"
)
type Bakeform struct {
Name string `json:"name"`
Location string `json:"location"`
mountRoot string
fb fileBackend
bootLocation string
MountedOn []string `json:"-"`
kpartxPath string
}
type BakeformList map[string]*Bakeform
func (b *Bakeform) Delete() error {
//TODO: Check if not in use
err := b.unmount()
if err != nil {
return fmt.Errorf("Unable to unmount image. %v", err)
}
err = os.RemoveAll(b.bootLocation)
if err != nil {
log.Printf("Unable to remove bootlocation.Deleting image anyways.")
}
return os.Remove(b.Location)
}
func (b *Bakeform) mount() error {
if len(b.MountedOn) >= 2 {
return nil
}
//raspbian images have 2 partitions. before mounting we need to map them to devices
out, err := exec.Command(b.kpartxPath, "-av", b.Location).CombinedOutput()
if err != nil {
return err
}
re, err := regexp.Compile("loop\\d+p\\d+")
if err != nil {
return err
}
loops := re.FindAll(out, 2)
if len(loops) < 2 {
return fmt.Errorf("Image could not be mapped")
}
//Go is too fast :). mounting directly after mapping fails. 1s delay fixes it.
time.Sleep(1 * time.Second)
//TODO: check for map device to be available instead of fixed sleep
//Loop through the partitions and mount each one
for i, loop := range loops {
loopDevice := "/dev/mapper/" + string(loop)
mountTarget := fmt.Sprintf("%v/%v-%v", b.mountRoot, b.Name, i)
err = os.Mkdir(mountTarget, 0777) //create the mount point
if err != nil {
exists, _ := regexp.MatchString("file exists$", err.Error())
if !exists {
return err
}
}
log.Printf("Mounting %v on %v\n", loopDevice, mountTarget)
err = syscall.Mount(loopDevice, mountTarget, "vfat", 0, "")
if err != nil && err.Error() != "device or resource busy" { //already mounted if this error occurs. Just continue :){
err = syscall.Mount(loopDevice, mountTarget, "ext4", 0, "")
if err != nil && err.Error() != "device or resource busy" { //already mounted if this error occurs. Just continue :)
return err
}
}
b.MountedOn = append(b.MountedOn, mountTarget) //store the mountpoint
}
return nil
}
func (b *Bakeform) unmount() error {
for _, mountTarget := range b.MountedOn {
log.Println("Unounting: " + mountTarget)
err := syscall.Unmount(mountTarget, 0)
if err != nil {
return err
}
//b.MountedOn = append(b.MountedOn[:i], b.MountedOn[i+1:]...) //delete the mount point from the slice <- goes out of bound
}
b.MountedOn = nil
//unmap devices
_, err := exec.Command(b.kpartxPath, "-d", b.Location).Output()
if err != nil {
return err
}
return nil
}