Skip to content
This repository was archived by the owner on Aug 26, 2022. It is now read-only.

Commit e61d12a

Browse files
committed
recursively remove subvolumes
1 parent 0f36dd1 commit e61d12a

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

btrfs/btrfs.go

+32
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package btrfs
22

33
import (
4+
"bufio"
45
"bytes"
56
"errors"
67
"fmt"
78
"os"
89
"os/exec"
910
"path"
11+
"strings"
1012
"syscall"
1113
)
1214

@@ -81,6 +83,36 @@ func (d *Driver) Remove(vol string) error {
8183
return fmt.Errorf("Volume does not exist: %s", volPath)
8284
}
8385

86+
// find sub-subvolumes
87+
cmd := exec.Command("btrfs", "subvolume", "list", "-o", volPath)
88+
89+
output, err := cmd.StdoutPipe()
90+
if err != nil {
91+
return fmt.Errorf("Can't access subvolume list of %s: %v", volPath, err)
92+
}
93+
defer output.Close()
94+
err = cmd.Start()
95+
if err != nil {
96+
return err
97+
}
98+
99+
scanner := bufio.NewScanner(output)
100+
for scanner.Scan() {
101+
line := strings.Split(scanner.Text(), " ")
102+
if len(line) > 8 {
103+
subvol := strings.Join(line[8:], " ")
104+
// remove beginning of volume path - relative to conair home
105+
subvol = strings.Replace(subvol, fmt.Sprintf("__active%s", d.home), "", 1)
106+
if err := d.Remove(subvol); err != nil {
107+
return err
108+
}
109+
}
110+
}
111+
err = scanner.Err()
112+
if err != nil {
113+
return fmt.Errorf("Can't read subvolume list of %s: %v", volPath, err)
114+
}
115+
84116
return exec.Command("btrfs", "subvolume", "delete", volPath).Run()
85117
}
86118

0 commit comments

Comments
 (0)