InitFlakey creates an filesystem on a loopback device and returns Flakey on it. The device-mapper device will be /dev/mapper/$flakeyDevice. And the filesystem image will be created at $dataStorePath/$flakeyDevice.img. By default, the device is available for 2 minutes and size is 10 GiB.
(flakeyDevice, dataStorePath string, fsType FSType, mkfsOpt string)
| 91 | // image will be created at $dataStorePath/$flakeyDevice.img. By default, the |
| 92 | // device is available for 2 minutes and size is 10 GiB. |
| 93 | func InitFlakey(flakeyDevice, dataStorePath string, fsType FSType, mkfsOpt string) (_ Flakey, retErr error) { |
| 94 | imgPath := filepath.Join(dataStorePath, fmt.Sprintf("%s.img", flakeyDevice)) |
| 95 | if err := createEmptyFSImage(imgPath, fsType, mkfsOpt); err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 | defer func() { |
| 99 | if retErr != nil { |
| 100 | os.RemoveAll(imgPath) |
| 101 | } |
| 102 | }() |
| 103 | |
| 104 | loopDevice, err := attachToLoopDevice(imgPath) |
| 105 | if err != nil { |
| 106 | return nil, err |
| 107 | } |
| 108 | defer func() { |
| 109 | if retErr != nil { |
| 110 | _ = detachLoopDevice(loopDevice) |
| 111 | } |
| 112 | }() |
| 113 | |
| 114 | imgSize, err := getBlkSize(loopDevice) |
| 115 | if err != nil { |
| 116 | return nil, err |
| 117 | } |
| 118 | |
| 119 | if err := newFlakeyDevice(flakeyDevice, loopDevice, defaultInterval); err != nil { |
| 120 | return nil, err |
| 121 | } |
| 122 | |
| 123 | return &flakey{ |
| 124 | fsType: fsType, |
| 125 | imgPath: imgPath, |
| 126 | imgSize: imgSize, |
| 127 | |
| 128 | loopDevice: loopDevice, |
| 129 | flakeyDevice: flakeyDevice, |
| 130 | }, nil |
| 131 | } |
| 132 | |
| 133 | type flakey struct { |
| 134 | fsType FSType |