@@ -10,6 +10,7 @@ import (
1010 "context"
1111 "fmt"
1212 "os"
13+ "os/exec"
1314 "path/filepath"
1415 "runtime"
1516 "strings"
@@ -25,19 +26,20 @@ import (
2526var _ multistep.Step = & StepMountDevice {}
2627
2728type StepMountDevice struct {
29+ Command string
2830 MountOptions []string
2931 MountPartition string
3032 MountPath string
3133
32- mountPath string
34+ mountPath string
35+ isManualMount bool
3336}
3437
3538func (s * StepMountDevice ) Run (ctx context.Context , state multistep.StateBag ) multistep.StepAction {
3639 ui := state .Get ("ui" ).(packersdk.Ui )
3740 device := state .Get ("device" ).(string )
3841 config := state .Get ("config" ).(* Config )
39- wrappedCommand := state .Get ("wrappedCommand" ).(common.CommandWrapper )
40-
42+ isManualMount := s .Command != ""
4143 ictx := config .ctx
4244
4345 ictx .Data = & struct { Device string }{Device : filepath .Base (device )}
@@ -59,12 +61,13 @@ func (s *StepMountDevice) Run(ctx context.Context, state multistep.StateBag) mul
5961 }
6062
6163 log .Printf ("Mount path: %s" , mountPath )
62-
63- if err := os .MkdirAll (mountPath , 0755 ); err != nil {
64- err := fmt .Errorf ("error creating mount directory: %s" , err )
65- state .Put ("error" , err )
66- ui .Error (err .Error ())
67- return multistep .ActionHalt
64+ if ! isManualMount {
65+ if err := os .MkdirAll (mountPath , 0755 ); err != nil {
66+ err := fmt .Errorf ("error creating mount directory: %s" , err )
67+ state .Put ("error" , err )
68+ ui .Error (err .Error ())
69+ return multistep .ActionHalt
70+ }
6871 }
6972
7073 var deviceMount string
@@ -79,23 +82,31 @@ func (s *StepMountDevice) Run(ctx context.Context, state multistep.StateBag) mul
7982
8083 ui .Say ("Mounting the root device..." )
8184 stderr := new (bytes.Buffer )
82-
83- // build mount options from mount_options config, useful for nouuid options
84- // or other specific device type settings for mount
85- opts := ""
86- if len (s .MountOptions ) > 0 {
87- opts = "-o " + strings .Join (s .MountOptions , " -o " )
88- }
89- mountCommand , err := wrappedCommand (
90- fmt .Sprintf ("mount %s %s %s" , opts , deviceMount , mountPath ))
91- if err != nil {
92- err := fmt .Errorf ("error creating mount command: %s" , err )
93- state .Put ("error" , err )
94- ui .Error (err .Error ())
95- return multistep .ActionHalt
85+ var cmd * exec.Cmd
86+ if ! isManualMount {
87+ // build mount options from mount_options config, useful for nouuid options
88+ // or other specific device type settings for mount
89+ opts := ""
90+ if len (s .MountOptions ) > 0 {
91+ opts = "-o " + strings .Join (s .MountOptions , " -o " )
92+ }
93+ wrappedCommand := state .Get ("wrappedCommand" ).(common.CommandWrapper )
94+ mountCommand , err := wrappedCommand (
95+ fmt .Sprintf ("mount %s %s %s" , opts , deviceMount , mountPath ))
96+ if err != nil {
97+ err := fmt .Errorf ("error creating mount command: %s" , err )
98+ state .Put ("error" , err )
99+ ui .Error (err .Error ())
100+ return multistep .ActionHalt
101+ }
102+ log .Printf ("[DEBUG] (step mount) mount command is %s" , mountCommand )
103+ cmd = common .ShellCommand (mountCommand )
104+
105+ } else {
106+ log .Printf ("[DEBUG] (step mount) mount command is %s" , s .Command )
107+ cmd = common .ShellCommand (fmt .Sprintf ("%s %s" , s .Command , mountPath ))
96108 }
97- log .Printf ("[DEBUG] (step mount) mount command is %s" , mountCommand )
98- cmd := common .ShellCommand (mountCommand )
109+
99110 cmd .Stderr = stderr
100111 if err := cmd .Run (); err != nil {
101112 err := fmt .Errorf (
@@ -107,6 +118,7 @@ func (s *StepMountDevice) Run(ctx context.Context, state multistep.StateBag) mul
107118
108119 // Set the mount path so we remember to unmount it later
109120 s .mountPath = mountPath
121+ s .isManualMount = isManualMount
110122 state .Put ("mount_path" , s .mountPath )
111123 state .Put ("mount_device_cleanup" , s )
112124
@@ -126,19 +138,22 @@ func (s *StepMountDevice) CleanupFunc(state multistep.StateBag) error {
126138 }
127139
128140 ui := state .Get ("ui" ).(packersdk.Ui )
129- wrappedCommand := state .Get ("wrappedCommand" ).(common.CommandWrapper )
130-
131- ui .Say ("Unmounting the root device..." )
132- unmountCommand , err := wrappedCommand (fmt .Sprintf ("umount -R %s" , s .mountPath ))
133- if err != nil {
134- return fmt .Errorf ("error creating unmount command: %s" , err )
141+ if ! s .isManualMount {
142+ wrappedCommand := state .Get ("wrappedCommand" ).(common.CommandWrapper )
143+
144+ ui .Say ("Unmounting the root device..." )
145+ unmountCommand , err := wrappedCommand (fmt .Sprintf ("umount -R %s" , s .mountPath ))
146+ if err != nil {
147+ return fmt .Errorf ("error creating unmount command: %s" , err )
148+ }
149+
150+ cmd := common .ShellCommand (unmountCommand )
151+ if err := cmd .Run (); err != nil {
152+ return fmt .Errorf ("error unmounting root device: %s" , err )
153+ }
154+ } else {
155+ ui .Say ("Skipping Unmounting the root device, it is manually unmounted via manual mount command script..." )
135156 }
136-
137- cmd := common .ShellCommand (unmountCommand )
138- if err := cmd .Run (); err != nil {
139- return fmt .Errorf ("error unmounting root device: %s" , err )
140- }
141-
142157 s .mountPath = ""
143158 return nil
144159}
0 commit comments