From 810e30e8e14f42cd64145246b0b4a0bc25b71a68 Mon Sep 17 00:00:00 2001 From: Marin Salinas Date: Tue, 12 Mar 2019 12:49:46 -0600 Subject: [PATCH] refactor: fix symlink issue on ubuntu --- builder/osc/chroot/builder_acc_test.go | 4 +--- builder/osc/chroot/device.go | 6 +++++- builder/osc/chroot/step_link_volume.go | 4 ++-- builder/osc/chroot/step_mount_device.go | 17 ++++++++++++++--- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/builder/osc/chroot/builder_acc_test.go b/builder/osc/chroot/builder_acc_test.go index cb2fdd108..f194dd49c 100644 --- a/builder/osc/chroot/builder_acc_test.go +++ b/builder/osc/chroot/builder_acc_test.go @@ -25,9 +25,7 @@ const testBuilderAccBasic = ` "region": "eu-west-2", "source_omi": "ami-65efcc11", "omi_name": "packer-test-{{timestamp}}", - "omi_virtualization_type": "hvm", - "device_path": "/dev/xvdf", - "mount_partition": "0" + "omi_virtualization_type": "hvm" }] } ` diff --git a/builder/osc/chroot/device.go b/builder/osc/chroot/device.go index f703b78ea..7f9aec94d 100644 --- a/builder/osc/chroot/device.go +++ b/builder/osc/chroot/device.go @@ -42,7 +42,7 @@ func AvailableDevice() (string, error) { // devicePrefix returns the prefix ("sd" or "xvd" or so on) of the devices // on the system. The "vd" prefix appears on outscale images. func devicePrefix() (string, error) { - available := []string{"sd", "xvd"} + available := []string{"sd", "xvd", "vd"} f, err := os.Open("/sys/block") if err != nil { @@ -56,6 +56,10 @@ func devicePrefix() (string, error) { dirBase := filepath.Base(dir) for _, prefix := range available { if strings.HasPrefix(dirBase, prefix) { + //for outscale. + if prefix != "xvd" { + prefix = "xvd" + } return prefix, nil } } diff --git a/builder/osc/chroot/step_link_volume.go b/builder/osc/chroot/step_link_volume.go index 8c5b6d941..f30b7803c 100644 --- a/builder/osc/chroot/step_link_volume.go +++ b/builder/osc/chroot/step_link_volume.go @@ -3,7 +3,6 @@ package chroot import ( "context" "fmt" - "strings" osccommon "github.com/hashicorp/packer/builder/osc/common" "github.com/hashicorp/packer/helper/multistep" @@ -30,7 +29,8 @@ func (s *StepLinkVolume) Run(ctx context.Context, state multistep.StateBag) mult volumeId := state.Get("volume_id").(string) // For the API call, it expects "sd" prefixed devices. - linkVolume := strings.Replace(device, "/xvd", "/sd", 1) + //linkVolume := strings.Replace(device, "/xvd", "/sd", 1) + linkVolume := device ui.Say(fmt.Sprintf("Attaching the root volume to %s", linkVolume)) _, err := oapiconn.POST_LinkVolume(oapi.LinkVolumeRequest{ diff --git a/builder/osc/chroot/step_mount_device.go b/builder/osc/chroot/step_mount_device.go index 51a150e19..0b564e14e 100644 --- a/builder/osc/chroot/step_mount_device.go +++ b/builder/osc/chroot/step_mount_device.go @@ -80,8 +80,19 @@ func (s *StepMountDevice) Run(_ context.Context, state multistep.StateBag) multi return multistep.ActionHalt } - //TODO: Check the symlink created - deviceMount := device + //Check the symbolic link for the device to get the real device name + cmd := ShellCommand(fmt.Sprintf("lsblk -no pkname $(readlink -f %s)", device)) + + realDeviceName, err := cmd.Output() + if err != nil { + err := fmt.Errorf( + "Error retrieving the symlink of the device %s.\n", device) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + + deviceMount := fmt.Sprintf("/dev/%s", strings.Replace(string(realDeviceName), "\n", "", -1)) log.Printf("[DEBUG] s.MountPartition = %s", s.MountPartition) @@ -108,7 +119,7 @@ func (s *StepMountDevice) Run(_ context.Context, state multistep.StateBag) multi return multistep.ActionHalt } log.Printf("[DEBUG] (step mount) mount command is %s", mountCommand) - cmd := ShellCommand(mountCommand) + cmd = ShellCommand(mountCommand) cmd.Stderr = stderr if err := cmd.Run(); err != nil { err := fmt.Errorf(