forked from apptainer/singularity
/
packer_squashfs.go
61 lines (50 loc) · 1.8 KB
/
packer_squashfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (c) 2018, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package sources
import (
"io/ioutil"
"os/exec"
"strconv"
"github.com/sylabs/singularity/internal/pkg/build/types"
"github.com/sylabs/singularity/internal/pkg/sylog"
"github.com/sylabs/singularity/pkg/util/loop"
)
// SquashfsPacker holds the locations of where to pack from and to, aswell as image offset info
type SquashfsPacker struct {
srcfile string
b *types.Bundle
info *loop.Info64
}
// Pack puts relevant objects in a Bundle!
func (p *SquashfsPacker) Pack() (*types.Bundle, error) {
rootfs := p.srcfile
err := p.unpackSquashfs(p.b, p.info, rootfs)
if err != nil {
sylog.Errorf("unpackSquashfs Failed: %s", err)
return nil, err
}
return p.b, nil
}
// unpackSquashfs removes the image header with dd and then unpackes image into bundle directories with unsquashfs
func (p *SquashfsPacker) unpackSquashfs(b *types.Bundle, info *loop.Info64, rootfs string) (err error) {
trimfile, err := ioutil.TempFile(p.b.Path, "trim.squashfs")
//trim header
sylog.Debugf("Creating copy of %s without header at %s\n", rootfs, trimfile.Name())
cmd := exec.Command("dd", "bs="+strconv.Itoa(int(info.Offset)), "skip=1", "if="+rootfs, "of="+trimfile.Name())
err = cmd.Run()
if err != nil {
sylog.Errorf("Trimming header Failed: %s", err)
return err
}
//copy filesystem into bundle rootfs
sylog.Debugf("Unsquashing %s to %s in Bundle\n", trimfile.Name(), b.Rootfs())
cmd = exec.Command("unsquashfs", "-f", "-d", b.Rootfs(), trimfile.Name())
err = cmd.Run()
if err != nil {
sylog.Errorf("unsquashfs Failed: %s", err)
return err
}
return err
}