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 }
0 commit comments