forked from kubernetes/kops
/
mount_disk.go
136 lines (110 loc) · 3.67 KB
/
mount_disk.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package nodetasks
import (
"fmt"
"github.com/golang/glog"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/nodeup/cloudinit"
"k8s.io/kops/upup/pkg/fi/nodeup/local"
"k8s.io/kops/upup/pkg/fi/utils"
"k8s.io/kubernetes/pkg/util/exec"
"k8s.io/kubernetes/pkg/util/mount"
"os"
"path/filepath"
"time"
)
// MountDiskTask is responsible for mounting a device on a mountpoint
// It will wait for the device to show up, safe_format_and_mount it,
// and then mount it.
type MountDiskTask struct {
Name string
Device string `json:"device"`
Mountpoint string `json:"mountpoint"`
}
var _ fi.Task = &MountDiskTask{}
func (s *MountDiskTask) String() string {
return fmt.Sprintf("Disk: %s", s.Name)
}
func NewMountDiskTask(name string, contents string, meta string) (fi.Task, error) {
s := &MountDiskTask{Name: name}
err := utils.YamlUnmarshal([]byte(contents), s)
if err != nil {
return nil, fmt.Errorf("error parsing json for disk %q: %v", name, err)
}
return s, nil
}
func (e *MountDiskTask) Find(c *fi.Context) (*MountDiskTask, error) {
mounter := mount.New()
mps, err := mounter.List()
if err != nil {
return nil, fmt.Errorf("error finding existing mounts: %v", err)
}
// If device is a symlink, it will show up by its final name
targetDevice, err := filepath.EvalSymlinks(e.Device)
if err != nil {
return nil, fmt.Errorf("error resolving device symlinks for %q: %v", e.Device, err)
}
for i := range mps {
mp := &mps[i]
if mp.Device == targetDevice {
actual := &MountDiskTask{
Name: e.Name,
Mountpoint: mp.Path,
Device: e.Device, // Use our alias, to keep change detection happy
}
return actual, nil
}
}
return nil, nil
}
func (e *MountDiskTask) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
func (s *MountDiskTask) CheckChanges(a, e, changes *MountDiskTask) error {
return nil
}
func (_ *MountDiskTask) RenderLocal(t *local.LocalTarget, a, e, changes *MountDiskTask) error {
dirMode := os.FileMode(0755)
// Create the mountpoint
err := os.MkdirAll(e.Mountpoint, dirMode)
if err != nil {
return fmt.Errorf("error creating mountpoint %q: %v", e.Mountpoint, err)
}
// Wait for the device to show up
for {
_, err := os.Stat(e.Device)
if err == nil {
break
}
if !os.IsNotExist(err) {
return fmt.Errorf("error checking for device %q: %v", e.Device, err)
}
glog.Infof("Waiting for device %q to be attached", e.Device)
time.Sleep(1 * time.Second)
}
glog.Infof("Found device %q", e.Device)
// Mount the device
if changes.Mountpoint != "" {
glog.Infof("Mounting device %q on %q", e.Device, e.Mountpoint)
mounter := &mount.SafeFormatAndMount{Interface: mount.New(), Runner: exec.New()}
fstype := ""
options := []string{}
err := mounter.FormatAndMount(e.Device, e.Mountpoint, fstype, options)
if err != nil {
return fmt.Errorf("error formatting and mounting disk %q on %q: %v", e.Device, e.Mountpoint, err)
}
}
// TODO: Should we add to /etc/fstab?
// Mount the master PD as early as possible
// echo "/dev/xvdb /mnt/master-pd ext4 noatime 0 0" >> /etc/fstab
return nil
}
func (_ *MountDiskTask) RenderCloudInit(t *cloudinit.CloudInitTarget, a, e, changes *MountDiskTask) error {
// TODO: Run safe_format_and_mount
// Download on aws (or bake into image)
// # TODO: Where to get safe_format_and_mount?
//mkdir -p /usr/share/google
//cd /usr/share/google
//download-or-bust "dc96f40fdc9a0815f099a51738587ef5a976f1da" https://raw.githubusercontent.com/GoogleCloudPlatform/compute-image-packages/82b75f314528b90485d5239ab5d5495cc22d775f/google-startup-scripts/usr/share/google/safe_format_and_mount
//chmod +x safe_format_and_mount
return fmt.Errorf("Disk::RenderCloudInit not implemented")
}