Skip to content

Commit 7348686

Browse files
author
Power Cloud Robot
authored
Merge pull request ppc64le-cloud#594 from kishen-v/clear-loopdevice
Free up loopdevice after image is built
2 parents 3896ec6 + 213a270 commit 7348686

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

cmd/image/qcow2ova/prep/loop.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,6 @@ import (
2929

3030
const losetupCMD = "losetup"
3131

32-
func findFreeLoop(file string) (string, error) {
33-
exitcode, out, err := utils.RunCMD(losetupCMD, "-f")
34-
if exitcode != 0 {
35-
return "", fmt.Errorf("failed to find an unused loop device, exitcode: %d, stdout: %s, err: %s", exitcode, out, err)
36-
}
37-
38-
return strings.TrimSpace(out), nil
39-
}
40-
4132
// setupLoop allocates the free loop device for the backing store
4233
func setupLoop(file string) (string, error) {
4334
exitcode, out, err := utils.RunCMD(losetupCMD, "-f", "--show", file)
@@ -48,6 +39,14 @@ func setupLoop(file string) (string, error) {
4839
return strings.TrimSpace(out), nil
4940
}
5041

42+
func removeLoop(loopPath string) error {
43+
exitcode, stdout, stderr := utils.RunCMD(losetupCMD, "-d", loopPath)
44+
if exitcode != 0 {
45+
return fmt.Errorf("failed to remove loop device: %s, exitcode: %d, stdout: %s, err: %s", loopPath, exitcode, stdout, stderr)
46+
}
47+
return nil
48+
}
49+
5150
func partprobe(device string) error {
5251
exitcode, out, err := utils.RunCMD("partprobe", device)
5352
if exitcode != 0 {

cmd/image/qcow2ova/prep/prepare.go

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func prepare(mnt, volume, dist, rhnuser, rhnpasswd, rootpasswd string) error {
3939
if err != nil {
4040
return err
4141
}
42+
defer removeLoop(lo)
4243

4344
err = partprobe(lo)
4445
if err != nil {

0 commit comments

Comments
 (0)