Skip to content

Commit 0b399ae

Browse files
authored
Merge pull request #55 from SLH335/filesystems
feat: choose filesystem type when creating volume
2 parents 8b71a84 + 1429657 commit 0b399ae

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

plugin.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"os/exec"
99
"path/filepath"
10+
"slices"
1011
"strconv"
1112
"strings"
1213
"sync"
@@ -89,6 +90,7 @@ func (d plugin) Create(r *volume.CreateRequest) error {
8990

9091
// DEFAULT SIZE IN GB
9192
var size = 10
93+
var filesystem = "ext4"
9294
var err error
9395

9496
if s, ok := r.Options["size"]; ok {
@@ -99,6 +101,15 @@ func (d plugin) Create(r *volume.CreateRequest) error {
99101
}
100102
}
101103

104+
if f, ok := r.Options["type"]; ok {
105+
f = strings.ToLower(f)
106+
if !slices.Contains(filesystems, f) {
107+
logger.WithError(err).Error("Error parsing filesystem type option")
108+
return fmt.Errorf("Invalid filesystem option: %s", f)
109+
}
110+
filesystem = f
111+
}
112+
102113
vol, err := volumes.Create(ctx, d.blockClient, volumes.CreateOpts{
103114
Size: size,
104115
Name: r.Name,
@@ -109,6 +120,35 @@ func (d plugin) Create(r *volume.CreateRequest) error {
109120
return err
110121
}
111122

123+
//
124+
// Attaching block volume to compute instance
125+
126+
opts := volumeattach.CreateOpts{VolumeID: vol.ID}
127+
_, err = volumeattach.Create(ctx, d.computeClient, d.config.MachineID, opts).Extract()
128+
129+
if err != nil {
130+
logger.WithError(err).Errorf("Error attaching volume: %s", err.Error())
131+
return err
132+
}
133+
134+
//
135+
// Waiting for device appearance
136+
137+
dev, err := findDeviceWithTimeout(vol.ID)
138+
139+
if err != nil {
140+
logger.WithError(err).Error("Block device not found")
141+
return err
142+
}
143+
144+
logger.WithField("dev", dev).Debug("Found device")
145+
146+
logger.Debug("Volume is empty, formatting")
147+
if err := formatFilesystem(dev, r.Name, filesystem); err != nil {
148+
logger.WithError(err).Error("Formatting failed")
149+
return err
150+
}
151+
112152
logger.WithField("id", vol.ID).Debug("Volume created")
113153

114154
return nil
@@ -253,7 +293,7 @@ func (d plugin) Mount(r *volume.MountRequest) (*volume.MountResponse, error) {
253293

254294
if fsType == "" {
255295
logger.Debug("Volume is empty, formatting")
256-
if err := formatFilesystem(dev, r.Name); err != nil {
296+
if err := formatFilesystem(dev, r.Name, "ext4"); err != nil {
257297
logger.WithError(err).Error("Formatting failed")
258298
return nil, err
259299
}

util.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,21 @@ import (
55
"fmt"
66
"os"
77
"os/exec"
8+
"slices"
89
"time"
910
)
1011

12+
var filesystems []string = []string{
13+
"btrfs",
14+
"ext2",
15+
"ext3",
16+
"ext4",
17+
"fat",
18+
"fat32",
19+
"ntfs",
20+
"xfs",
21+
}
22+
1123
func getFilesystemType(dev string) (string, error) {
1224
out, err := exec.Command("blkid", "-s", "TYPE", "-o", "value", dev).CombinedOutput()
1325

@@ -22,9 +34,13 @@ func getFilesystemType(dev string) (string, error) {
2234
return string(out), nil
2335
}
2436

25-
func formatFilesystem(dev string, label string) error {
26-
out, err := exec.Command("mkfs.ext4", "-L", label, dev).CombinedOutput()
37+
func formatFilesystem(dev, label, filesystem string) error {
38+
_, err := exec.LookPath("mkfs." + filesystem)
39+
if err != nil || !slices.Contains(filesystems, filesystem) {
40+
return errors.New(fmt.Sprintf("filesystem '%s' does not exist", filesystem))
41+
}
2742

43+
out, err := exec.Command(fmt.Sprintf("mkfs.%s", filesystem), "-L", label, dev).CombinedOutput()
2844
if err != nil {
2945
return errors.New(string(out))
3046
}

0 commit comments

Comments
 (0)