Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
b75bd29b
Commit
b75bd29b
authored
Jul 30, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/amazon/chroot: find available device
parent
7857406f
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
120 additions
and
17 deletions
+120
-17
builder/amazon/chroot/builder.go
builder/amazon/chroot/builder.go
+8
-12
builder/amazon/chroot/device.go
builder/amazon/chroot/device.go
+61
-0
builder/amazon/chroot/step_attach_volume.go
builder/amazon/chroot/step_attach_volume.go
+6
-5
builder/amazon/chroot/step_prepare_device.go
builder/amazon/chroot/step_prepare_device.go
+45
-0
No files found.
builder/amazon/chroot/builder.go
View file @
b75bd29b
...
...
@@ -24,14 +24,13 @@ type Config struct {
common
.
PackerConfig
`mapstructure:",squash"`
awscommon
.
AccessConfig
`mapstructure:",squash"`
ChrootMounts
[][]
string
`mapstructure:"chroot_mounts"`
CopyFiles
[]
string
`mapstructure:"copy_files"`
DevicePath
string
`mapstructure:"device_path"`
DevicePrefix
string
`mapstructure:"device_prefix"`
MountCommand
string
`mapstructure:"mount_command"`
MountPath
string
`mapstructure:"mount_path"`
SourceAmi
string
`mapstructure:"source_ami"`
UnmountCommand
string
`mapstructure:"unmount_command"`
ChrootMounts
[][]
string
`mapstructure:"chroot_mounts"`
CopyFiles
[]
string
`mapstructure:"copy_files"`
DevicePath
string
`mapstructure:"device_path"`
MountCommand
string
`mapstructure:"mount_command"`
MountPath
string
`mapstructure:"mount_path"`
SourceAmi
string
`mapstructure:"source_ami"`
UnmountCommand
string
`mapstructure:"unmount_command"`
}
type
Builder
struct
{
...
...
@@ -68,10 +67,6 @@ func (b *Builder) Prepare(raws ...interface{}) error {
b
.
config
.
CopyFiles
=
[]
string
{
"/etc/resolv.conf"
}
}
if
b
.
config
.
DevicePath
==
""
{
b
.
config
.
DevicePath
=
"/dev/sdh"
}
if
b
.
config
.
MountCommand
==
""
{
b
.
config
.
MountCommand
=
"mount"
}
...
...
@@ -128,6 +123,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
steps
:=
[]
multistep
.
Step
{
&
StepInstanceInfo
{},
&
StepSourceAMIInfo
{},
&
StepPrepareDevice
{},
&
StepCreateVolume
{},
&
StepAttachVolume
{},
&
StepMountDevice
{},
...
...
builder/amazon/chroot/device.go
0 → 100644
View file @
b75bd29b
package
chroot
import
(
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
// AvailableDevice finds an available device and returns it. Note that
// you should externally hold a flock or something in order to guarantee
// that this device is available across processes.
func
AvailableDevice
()
(
string
,
error
)
{
prefix
,
err
:=
devicePrefix
()
if
err
!=
nil
{
return
""
,
err
}
letters
:=
"fghijklmnop"
for
_
,
letter
:=
range
letters
{
for
i
:=
1
;
i
<
16
;
i
++
{
device
:=
fmt
.
Sprintf
(
"/dev/%s%c%d"
,
prefix
,
letter
,
i
)
if
_
,
err
:=
os
.
Stat
(
device
);
err
!=
nil
{
return
device
,
nil
}
}
}
return
""
,
errors
.
New
(
"available device could not be found"
)
}
// devicePrefix returns the prefix ("sd" or "xvd" or so on) of the devices
// on the system.
func
devicePrefix
()
(
string
,
error
)
{
available
:=
[]
string
{
"sd"
,
"xvd"
}
f
,
err
:=
os
.
Open
(
"/sys/block"
)
if
err
!=
nil
{
return
""
,
err
}
defer
f
.
Close
()
dirs
,
err
:=
f
.
Readdirnames
(
-
1
)
if
dirs
!=
nil
&&
len
(
dirs
)
>
0
{
for
_
,
dir
:=
range
dirs
{
dirBase
:=
filepath
.
Base
(
dir
)
for
_
,
prefix
:=
range
available
{
if
strings
.
HasPrefix
(
dirBase
,
prefix
)
{
return
prefix
,
nil
}
}
}
}
if
err
!=
nil
{
return
""
,
err
}
return
""
,
errors
.
New
(
"device prefix could not be detected"
)
}
builder/amazon/chroot/step_attach_volume.go
View file @
b75bd29b
...
...
@@ -7,6 +7,7 @@ import (
"github.com/mitchellh/multistep"
awscommon
"github.com/mitchellh/packer/builder/amazon/common"
"github.com/mitchellh/packer/packer"
"strings"
)
// StepAttachVolume attaches the previously created volume to an
...
...
@@ -21,16 +22,17 @@ type StepAttachVolume struct {
}
func
(
s
*
StepAttachVolume
)
Run
(
state
map
[
string
]
interface
{})
multistep
.
StepAction
{
config
:=
state
[
"config"
]
.
(
*
Config
)
ec2conn
:=
state
[
"ec2"
]
.
(
*
ec2
.
EC2
)
device
:=
state
[
"device"
]
.
(
string
)
instance
:=
state
[
"instance"
]
.
(
*
ec2
.
Instance
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
volumeId
:=
state
[
"volume_id"
]
.
(
string
)
device
:=
config
.
DevicePath
// For the API call, it expects "sd" prefixed devices.
attachVolume
:=
strings
.
Replace
(
device
,
"/xvd"
,
"/sd"
,
1
)
ui
.
Say
(
"Attaching the root volume..."
)
_
,
err
:=
ec2conn
.
AttachVolume
(
volumeId
,
instance
.
InstanceId
,
devic
e
)
ui
.
Say
(
fmt
.
Sprintf
(
"Attaching the root volume to %s"
,
attachVolume
)
)
_
,
err
:=
ec2conn
.
AttachVolume
(
volumeId
,
instance
.
InstanceId
,
attachVolum
e
)
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error attaching volume: %s"
,
err
)
state
[
"error"
]
=
err
...
...
@@ -70,7 +72,6 @@ func (s *StepAttachVolume) Run(state map[string]interface{}) multistep.StepActio
return
multistep
.
ActionHalt
}
state
[
"device"
]
=
device
state
[
"attach_cleanup"
]
=
s
return
multistep
.
ActionContinue
}
...
...
builder/amazon/chroot/step_prepare_device.go
0 → 100644
View file @
b75bd29b
package
chroot
import
(
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
"os"
)
// StepPrepareDevice finds an available device and sets it.
type
StepPrepareDevice
struct
{
mounts
[]
string
}
func
(
s
*
StepPrepareDevice
)
Run
(
state
map
[
string
]
interface
{})
multistep
.
StepAction
{
config
:=
state
[
"config"
]
.
(
*
Config
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
device
:=
config
.
DevicePath
if
device
==
""
{
var
err
error
log
.
Println
(
"Device path not specified, searching for available device..."
)
device
,
err
=
AvailableDevice
()
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error finding available device: %s"
,
err
)
state
[
"error"
]
=
err
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
}
if
_
,
err
:=
os
.
Stat
(
device
);
err
==
nil
{
err
:=
fmt
.
Errorf
(
"Device is in use: %s"
,
device
)
state
[
"error"
]
=
err
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
log
.
Printf
(
"Device: %s"
,
device
)
state
[
"device"
]
=
device
return
multistep
.
ActionContinue
}
func
(
s
*
StepPrepareDevice
)
Cleanup
(
state
map
[
string
]
interface
{})
{}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment