Skip to content

Commit 6c3a10e

Browse files
author
dwc0011
committed
Add manual mount logic into the StepMountDevice
1 parent d21dc8f commit 6c3a10e

File tree

3 files changed

+60
-168
lines changed

3 files changed

+60
-168
lines changed

builder/azure/chroot/builder.go

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -616,28 +616,12 @@ func buildsteps(
616616
&chroot.StepPreMountCommands{
617617
Commands: config.PreMountCommands,
618618
},
619-
)
620-
621-
if config.ManualMountCommand == "" {
622-
addSteps(
623-
&StepMountDevice{
624-
MountOptions: config.MountOptions,
625-
MountPartition: config.MountPartition,
626-
MountPath: config.MountPath,
627-
},
628-
)
629-
} else {
630-
addSteps(
631-
&StepManualMountCommand{
632-
Command: config.ManualMountCommand,
633-
MountPartition: config.MountPartition,
634-
MountPath: config.MountPath,
635-
},
636-
)
637-
638-
}
639-
640-
addSteps(
619+
&StepMountDevice{
620+
MountOptions: config.MountOptions,
621+
Command: config.ManualMountCommand,
622+
MountPartition: config.MountPartition,
623+
MountPath: config.MountPath,
624+
},
641625
&chroot.StepPostMountCommands{
642626
Commands: config.PostMountCommands,
643627
},

builder/azure/chroot/step_manual_mount_command.go

Lines changed: 0 additions & 110 deletions
This file was deleted.

builder/azure/chroot/step_mount_device.go

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,25 @@ import (
2525
var _ multistep.Step = &StepMountDevice{}
2626

2727
type StepMountDevice struct {
28+
Command string
2829
MountOptions []string
2930
MountPartition string
3031
MountPath string
3132

3233
mountPath string
34+
isManualMount bool
3335
}
3436

3537
func (s *StepMountDevice) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
3638
ui := state.Get("ui").(packersdk.Ui)
3739
device := state.Get("device").(string)
3840
config := state.Get("config").(*Config)
39-
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
41+
42+
isManualMount := s.Command != ""
43+
44+
if !isManualMount {
45+
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
46+
}
4047

4148
ictx := config.ctx
4249

@@ -59,12 +66,13 @@ func (s *StepMountDevice) Run(ctx context.Context, state multistep.StateBag) mul
5966
}
6067

6168
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
69+
if !isManualMount {
70+
if err := os.MkdirAll(mountPath, 0755); err != nil {
71+
err := fmt.Errorf("error creating mount directory: %s", err)
72+
state.Put("error", err)
73+
ui.Error(err.Error())
74+
return multistep.ActionHalt
75+
}
6876
}
6977

7078
var deviceMount string
@@ -79,23 +87,29 @@ func (s *StepMountDevice) Run(ctx context.Context, state multistep.StateBag) mul
7987

8088
ui.Say("Mounting the root device...")
8189
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
90+
if !isManualMount{
91+
// build mount options from mount_options config, useful for nouuid options
92+
// or other specific device type settings for mount
93+
opts := ""
94+
if len(s.MountOptions) > 0 {
95+
opts = "-o " + strings.Join(s.MountOptions, " -o ")
96+
}
97+
mountCommand, err := wrappedCommand(
98+
fmt.Sprintf("mount %s %s %s", opts, deviceMount, mountPath))
99+
if err != nil {
100+
err := fmt.Errorf("error creating mount command: %s", err)
101+
state.Put("error", err)
102+
ui.Error(err.Error())
103+
return multistep.ActionHalt
104+
}
105+
log.Printf("[DEBUG] (step mount) mount command is %s", mountCommand)
106+
cmd := common.ShellCommand(mountCommand)
107+
108+
}else {
109+
log.Printf("[DEBUG] (step mount) mount command is %s", s.Command)
110+
cmd := common.ShellCommand(fmt.Sprintf("%s %s", s.Command, mountPath))
96111
}
97-
log.Printf("[DEBUG] (step mount) mount command is %s", mountCommand)
98-
cmd := common.ShellCommand(mountCommand)
112+
99113
cmd.Stderr = stderr
100114
if err := cmd.Run(); err != nil {
101115
err := fmt.Errorf(
@@ -107,6 +121,7 @@ func (s *StepMountDevice) Run(ctx context.Context, state multistep.StateBag) mul
107121

108122
// Set the mount path so we remember to unmount it later
109123
s.mountPath = mountPath
124+
s.isManualMount = isManualMount
110125
state.Put("mount_path", s.mountPath)
111126
state.Put("mount_device_cleanup", s)
112127

@@ -126,19 +141,22 @@ func (s *StepMountDevice) CleanupFunc(state multistep.StateBag) error {
126141
}
127142

128143
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)
144+
if !s.isManualMount {
145+
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
146+
147+
ui.Say("Unmounting the root device...")
148+
unmountCommand, err := wrappedCommand(fmt.Sprintf("umount -R %s", s.mountPath))
149+
if err != nil {
150+
return fmt.Errorf("error creating unmount command: %s", err)
151+
}
152+
153+
cmd := common.ShellCommand(unmountCommand)
154+
if err := cmd.Run(); err != nil {
155+
return fmt.Errorf("error unmounting root device: %s", err)
156+
}
157+
} else {
158+
ui.Say("Skipping Unmounting the root device, it is manually unmounted via manual mount command script...")ui.Say("Skipping Unmounting the root device, it is manually unmounted via manual mount command script...")
135159
}
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-
142-
s.mountPath = ""
160+
s.mountPath = ""
143161
return nil
144162
}

0 commit comments

Comments
 (0)