forked from go-debos/debos
/
filesystem_deploy_action.go
109 lines (88 loc) · 2.49 KB
/
filesystem_deploy_action.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
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"strings"
)
type FilesystemDeployAction struct {
BaseAction `yaml:",inline"`
SetupFSTab bool `yaml:setup-fstab`
SetupKernelCmdline bool `yaml:setup-kernel-cmdline`
}
func newFilesystemDeployAction() *FilesystemDeployAction {
fd := &FilesystemDeployAction{SetupFSTab: true, SetupKernelCmdline: true}
fd.Description = "Deploying filesystem"
return fd
}
func (fd *FilesystemDeployAction) setupFSTab(context *DebosContext) error {
if context.imageFSTab.Len() == 0 {
return errors.New("Fstab not generated, missing image-partition action?")
}
log.Print("Setting up fstab")
err := os.MkdirAll(path.Join(context.rootdir, "etc"), 0755)
if err != nil {
return fmt.Errorf("Couldn't create etc in image: %v", err)
}
fstab := path.Join(context.rootdir, "etc/fstab")
f, err := os.OpenFile(fstab, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return fmt.Errorf("Couldn't open fstab: %v", err)
}
_, err = io.Copy(f, &context.imageFSTab)
if err != nil {
return fmt.Errorf("Couldn't write fstab: %v", err)
}
f.Close()
return nil
}
func (fd *FilesystemDeployAction) setupKernelCmdline(context *DebosContext) error {
log.Print("Setting up /etc/kernel/cmdline")
err := os.MkdirAll(path.Join(context.rootdir, "etc", "kernel"), 0755)
if err != nil {
return fmt.Errorf("Couldn't create etc/kernel in image: %v", err)
}
path := path.Join(context.rootdir, "etc/kernel/cmdline")
current, _ := ioutil.ReadFile(path)
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
log.Fatalf("Couldn't open kernel cmdline: %v", err)
}
cmdline := fmt.Sprintf("%s %s\n",
strings.TrimSpace(string(current)),
context.imageKernelRoot)
_, err = f.WriteString(cmdline)
if err != nil {
return fmt.Errorf("Couldn't write kernel/cmdline: %v", err)
}
f.Close()
return nil
}
func (fd *FilesystemDeployAction) Run(context *DebosContext) error {
fd.LogStart()
/* Copying files is actually silly hafd, one has to keep permissions, ACL's
* extended attribute, misc, other. Leave it to cp...
*/
err := Command{}.Run("Deploy to image", "cp", "-a", context.rootdir+"/.", context.imageMntDir)
if err != nil {
return fmt.Errorf("rootfs deploy failed: %v", err)
}
context.rootdir = context.imageMntDir
if fd.SetupFSTab {
err = fd.setupFSTab(context)
if err != nil {
return err
}
}
if fd.SetupKernelCmdline {
err = fd.setupKernelCmdline(context)
if err != nil {
return err
}
}
return nil
}