podman
38 строк · 1.4 Кб
1package mkcw
2
3import (
4"fmt"
5"os/exec"
6"strings"
7
8"github.com/sirupsen/logrus"
9)
10
11// MakeFS formats the imageFile as a filesystem of the specified type,
12// populating it with the contents of the directory at sourcePath.
13// Recognized filesystem types are "ext2", "ext3", "ext4", and "btrfs".
14// Note that krun's init is currently hard-wired to assume "ext4".
15// Returns the stdout, stderr, and any error returned by the mkfs command.
16func MakeFS(sourcePath, imageFile, filesystem string) (string, string, error) {
17var stdout, stderr strings.Builder
18// N.B. mkfs.xfs can accept a protofile via its -p option, but the
19// protofile format doesn't allow us to supply timestamp information or
20// specify that files are hard linked
21switch filesystem {
22case "ext2", "ext3", "ext4":
23logrus.Debugf("mkfs -t %s --rootdir %q %q", filesystem, sourcePath, imageFile)
24cmd := exec.Command("mkfs", "-t", filesystem, "-d", sourcePath, imageFile)
25cmd.Stdout = &stdout
26cmd.Stderr = &stderr
27err := cmd.Run()
28return stdout.String(), stderr.String(), err
29case "btrfs":
30logrus.Debugf("mkfs -t %s --rootdir %q %q", filesystem, sourcePath, imageFile)
31cmd := exec.Command("mkfs", "-t", filesystem, "--rootdir", sourcePath, imageFile)
32cmd.Stdout = &stdout
33cmd.Stderr = &stderr
34err := cmd.Run()
35return stdout.String(), stderr.String(), err
36}
37return "", "", fmt.Errorf("don't know how to make a %q filesystem with contents", filesystem)
38}
39