Skip to content

Commit fe17440

Browse files
authored
Merge pull request rancher#2545 from bernard-wagner/lvm-crypt
Added LVM and LUKS
2 parents ff15abd + 900c57b commit fe17440

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

cmd/control/bootstrap.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package control
22

33
import (
4+
"fmt"
45
"io/ioutil"
56
"os"
67
"os/exec"
8+
"path/filepath"
79
"strings"
810
"time"
911

@@ -37,6 +39,20 @@ func BootstrapMain() {
3739
}
3840
}
3941

42+
log.Debugf("bootstrapAction: cryptsetup(%v)", cfg.Rancher.State.Cryptsetup)
43+
if cfg.Rancher.State.Cryptsetup {
44+
if err := cryptsetup(); err != nil {
45+
log.Errorf("Failed to run cryptsetup: %v", err)
46+
}
47+
}
48+
49+
log.Debugf("bootstrapAction: LvmScan(%v)", cfg.Rancher.State.LvmScan)
50+
if cfg.Rancher.State.LvmScan {
51+
if err := vgchange(); err != nil {
52+
log.Errorf("Failed to run vgchange: %v", err)
53+
}
54+
}
55+
4056
stateScript := cfg.Rancher.State.Script
4157
log.Debugf("bootstrapAction: stateScript(%v)", stateScript)
4258
if stateScript != "" {
@@ -75,6 +91,45 @@ func mdadmScan() error {
7591
return cmd.Run()
7692
}
7793

94+
func vgchange() error {
95+
cmd := exec.Command("vgchange", "--activate", "ay")
96+
cmd.Stdout = os.Stdout
97+
cmd.Stderr = os.Stderr
98+
return cmd.Run()
99+
}
100+
101+
func cryptsetup() error {
102+
devices, err := util.BlkidType("crypto_LUKS")
103+
if err != nil {
104+
return err
105+
}
106+
107+
for _, cryptdevice := range devices {
108+
fdRead, err := os.Open("/dev/console")
109+
if err != nil {
110+
return err
111+
}
112+
defer fdRead.Close()
113+
114+
fdWrite, err := os.OpenFile("/dev/console", os.O_WRONLY|os.O_APPEND, 0)
115+
if err != nil {
116+
return err
117+
}
118+
defer fdWrite.Close()
119+
120+
cmd := exec.Command("cryptsetup", "luksOpen", cryptdevice, fmt.Sprintf("luks-%s", filepath.Base(cryptdevice)))
121+
cmd.Stdout = fdWrite
122+
cmd.Stderr = fdWrite
123+
cmd.Stdin = fdRead
124+
125+
if err := cmd.Run(); err != nil {
126+
log.Errorf("Failed to run cryptsetup for %s: %v", cryptdevice, err)
127+
}
128+
}
129+
130+
return nil
131+
}
132+
78133
func runRngd() error {
79134
// use /dev/urandom as random number input for rngd
80135
// this is a really bad idea

config/schema.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ var schema = `{
170170
"required": {"type": "boolean"},
171171
"autoformat": {"$ref": "#/definitions/list_of_strings"},
172172
"mdadm_scan": {"type": "boolean"},
173+
"cryptsetup": {"type": "boolean"},
174+
"lvm_scan": {"type": "boolean"},
173175
"rngd": {"type": "boolean"},
174176
"script": {"type": "string"},
175177
"oem_fstype": {"type": "string"},

config/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ type StateConfig struct {
205205
Required bool `yaml:"required,omitempty"`
206206
Autoformat []string `yaml:"autoformat,omitempty"`
207207
MdadmScan bool `yaml:"mdadm_scan,omitempty"`
208+
LvmScan bool `yaml:"lvm_scan,omitempty"`
209+
Cryptsetup bool `yaml:"cryptsetup,omitempty"`
208210
Rngd bool `yaml:"rngd,omitempty"`
209211
Script string `yaml:"script,omitempty"`
210212
OemFsType string `yaml:"oem_fstype,omitempty"`

pkg/util/util_linux.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,28 @@ func Blkid(label string) (deviceName, deviceType string, err error) {
119119
return
120120
}
121121

122+
func BlkidType(deviceType string) (deviceNames []string, err error) {
123+
// Not all blkid's have `blkid -L label (see busybox/alpine)
124+
cmd := exec.Command("blkid")
125+
cmd.Stderr = os.Stderr
126+
out, err := cmd.Output()
127+
if err != nil {
128+
return nil, err
129+
}
130+
r := bytes.NewReader(out)
131+
s := bufio.NewScanner(r)
132+
for s.Scan() {
133+
line := s.Text()
134+
if !strings.Contains(line, `TYPE="`+deviceType+`"`) {
135+
continue
136+
}
137+
d := strings.Split(line, ":")
138+
deviceName := d[0]
139+
deviceNames = append(deviceNames, deviceName)
140+
}
141+
return deviceNames, nil
142+
}
143+
122144
// GetHypervisor tries to detect if we're running in a VM, and returns a string for its type
123145
func GetHypervisor() string {
124146
return cpuid.CPU.HypervisorName

scripts/schema.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@
164164
"required": {"type": "boolean"},
165165
"autoformat": {"$ref": "#/definitions/list_of_strings"},
166166
"mdadm_scan": {"type": "boolean"},
167+
"cryptsetup": {"type": "boolean"},
168+
"lvm_scan": {"type": "boolean"},
167169
"script": {"type": "string"},
168170
"oem_fstype": {"type": "string"},
169171
"oem_dev": {"type": "string"}

0 commit comments

Comments
 (0)