@@ -2,18 +2,18 @@ package loop
22
33import (
44 "context"
5- "encoding/json"
65 "fmt"
6+ "strings"
77 "time"
88
99 "github.com/opensvc/fcntllock"
1010 "github.com/opensvc/flock"
1111 "github.com/rs/zerolog"
1212
1313 "github.com/opensvc/om3/v3/util/command"
14- "github.com/opensvc/om3/v3/util/sessioncache"
1514 "github.com/opensvc/om3/v3/util/funcopt"
1615 "github.com/opensvc/om3/v3/util/plog"
16+ "github.com/opensvc/om3/v3/util/sessioncache"
1717 "github.com/opensvc/om3/v3/util/udevadm"
1818)
1919
@@ -25,19 +25,16 @@ type (
2525 T struct {
2626 log * plog.Logger
2727 }
28+
2829 Info struct {
2930 LoopDevices []InfoEntry `json:"loopdevices"`
3031 }
32+
3133 InfoEntry struct {
32- Name string `json:"name"` // "/dev/loop1"
33- SizeLimit int64 `json:"sizelimit"` // 0
34- Offset int64 `json:"offset"` // 0
35- AutoClear bool `json:"autoclear"` // true
36- ReadOnly bool `json:"ro"` // true
37- BackFile string `json:"back-file"` // "/var/lib/snapd/snaps/gnome-3-34-1804_66.snap"
38- DirectIO bool `json:"dio"` // false
39- LogSec int64 `json:"log-sec"` // 512
34+ Name string `json:"name"` // "/dev/loop1"
35+ BackFile string `json:"back-file"` // "/var/lib/snapd/snaps/gnome-3-34-1804_66.snap"
4036 }
37+
4138 InfoEntries []InfoEntry
4239)
4340
@@ -91,14 +88,14 @@ func (t T) Get(ctx context.Context, name string) (*InfoEntry, error) {
9188
9289func (t T ) Data (ctx context.Context ) (InfoEntries , error ) {
9390 var (
94- out []byte
95- err error
91+ out []byte
92+ err error
93+ entries InfoEntries
9694 )
97- data := Info {}
9895 cmd := command .New (
9996 command .WithContext (ctx ),
10097 command .WithName (losetup ),
101- command .WithVarArgs ("-J" ),
98+ command .WithVarArgs ("-O" , "NAME,BACK-FILE" ), // The -J and -n options are not supported by losetup on RHEL 7.
10299 command .WithLogger (t .log ),
103100 command .WithCommandLogLevel (zerolog .TraceLevel ),
104101 command .WithStdoutLogLevel (zerolog .TraceLevel ),
@@ -111,10 +108,25 @@ func (t T) Data(ctx context.Context) (InfoEntries, error) {
111108 if len (out ) == 0 {
112109 return InfoEntries {}, nil
113110 }
114- if err = json .Unmarshal (out , & data ); err != nil {
115- return nil , err
111+ /*
112+ Output to parse: losetup -O NAME,BACK-FILE
113+ NAME BACK-FILE
114+ /dev/loop0 /tmp/loopfile0 (deleted)
115+ /dev/loop1 /tmp/loopfile1
116+ */
117+ for line := range strings .Lines (string (out )) {
118+ l := strings .Fields (line )
119+ if len (l ) >= 2 {
120+ if l [0 ] == "NAME" {
121+ continue
122+ }
123+ entries = append (entries , InfoEntry {
124+ Name : l [0 ],
125+ BackFile : l [1 ],
126+ })
127+ }
116128 }
117- return data . LoopDevices , nil
129+ return entries , nil
118130}
119131
120132func (t T ) Add (ctx context.Context , filePath string ) error {
@@ -189,9 +201,6 @@ func (t InfoEntries) File(s string) *InfoEntry {
189201 if i .BackFile == s {
190202 return & i
191203 }
192- if i .BackFile == s + " (deleted)" {
193- return & i
194- }
195204 }
196205 return nil
197206}
0 commit comments