Skip to content

Commit

Permalink
Bind mount disk devices in chroot
Browse files Browse the repository at this point in the history
Some operations need access to the disk device backing the image that's
being build, in particular for installing grub-pc. e.g. grub-install needs to write to the MBR to add
it's boot imgae and probed information.

To allow this bind mount the image device file,  all devices files for
the created partitions and the /dev/disk directory (for udev alias symlinking)
into a chroot when running commands in it.

Signed-off-by: Sjoerd Simons <sjoerd.simons@collabora.co.uk>
  • Loading branch information
sjoerdsimons committed Dec 22, 2017
1 parent d64043a commit aa28780
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion actions/apt_action.go
Expand Up @@ -41,7 +41,7 @@ func (apt *AptAction) Run(context *debos.DebosContext) error {
aptOptions = append(aptOptions, "install")
aptOptions = append(aptOptions, apt.Packages...)

c := debos.NewChrootCommand(context.Rootdir, context.Architecture)
c := debos.NewChrootCommandForContext(*context)
c.AddEnv("DEBIAN_FRONTEND=noninteractive")

err := c.Run("apt", "apt-get", "update")
Expand Down
4 changes: 2 additions & 2 deletions actions/debootstrap_action.go
Expand Up @@ -70,7 +70,7 @@ func (d *DebootstrapAction) RunSecondStage(context debos.DebosContext) error {
cmdline = append(cmdline, fmt.Sprintf("--components=%s", s))
}

c := debos.NewChrootCommand(context.Rootdir, context.Architecture)
c := debos.NewChrootCommandForContext(context)
// Can't use nspawn for debootstrap as it wants to create device nodes
c.ChrootMethod = debos.CHROOT_METHOD_CHROOT

Expand Down Expand Up @@ -140,7 +140,7 @@ func (d *DebootstrapAction) Run(context *debos.DebosContext) error {
}
srclist.Close()

c := debos.NewChrootCommand(context.Rootdir, context.Architecture)
c := debos.NewChrootCommandForContext(*context)

return c.Run("apt clean", "/usr/bin/apt-get", "clean")
}
2 changes: 1 addition & 1 deletion actions/run_action.go
Expand Up @@ -81,7 +81,7 @@ func (run *RunAction) doRun(context debos.DebosContext) error {
var cmd debos.Command

if run.Chroot {
cmd = debos.NewChrootCommand(context.Rootdir, context.Architecture)
cmd = debos.NewChrootCommandForContext(context)
} else {
cmd = debos.Command{}
}
Expand Down
24 changes: 22 additions & 2 deletions commands.go
Expand Up @@ -66,8 +66,28 @@ func (w *commandWrapper) flush() {
w.out(true)
}

func NewChrootCommand(chroot, architecture string) Command {
return Command{Architecture: architecture, Chroot: chroot, ChrootMethod: CHROOT_METHOD_NSPAWN}
func NewChrootCommandForContext(context DebosContext) Command {
c := Command{Architecture: context.Architecture, Chroot: context.Rootdir, ChrootMethod: CHROOT_METHOD_NSPAWN}

if context.Image != "" {
path, err := RealPath(context.Image)
if err == nil {
c.AddBindMount(path, "")
} else {
log.Printf("Failed to get realpath for %s, %v", context.Image, err)
}
for _, p := range context.ImagePartitions {
path, err := RealPath(p.DevicePath)
if err != nil {
log.Printf("Failed to get realpath for %s, %v", p.DevicePath, err)
continue
}
c.AddBindMount(path, "")
}
c.AddBindMount("/dev/disk", "")
}

return c
}

func (cmd *Command) AddEnv(env string) {
Expand Down

0 comments on commit aa28780

Please sign in to comment.