|
| 1 | +package test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +type TempBlockDevice struct { |
| 14 | + TempFile string |
| 15 | + LoopDevice string |
| 16 | +} |
| 17 | + |
| 18 | +// returns the temporary file, the device path and the error. |
| 19 | +func CreateTempFormattedLoopDevice(t *testing.T, name string) (*TempBlockDevice, error) { |
| 20 | + blockDev, err := CreateTempLoopDevice(t, name) |
| 21 | + if err != nil { |
| 22 | + return nil, err |
| 23 | + } |
| 24 | + |
| 25 | + //nolint:gosec |
| 26 | + cmd := exec.Command("/sbin/mkfs.ext4", "-F", "-q", blockDev.LoopDevice) |
| 27 | + log.Printf("[DEBUG] executing command: %s", strings.Join(cmd.Args, " ")) |
| 28 | + if err := cmd.Run(); err != nil { |
| 29 | + if err := cleanupLoop(blockDev.LoopDevice); err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + |
| 33 | + if err := cleanupFile(blockDev.TempFile); err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + |
| 37 | + return nil, fmt.Errorf("error formatting file system: %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + return blockDev, nil |
| 41 | +} |
| 42 | + |
| 43 | +// returns the temporary file, the device and the error. |
| 44 | +func CreateTempLVMGroupDevice(t *testing.T, name string) (*TempBlockDevice, error) { |
| 45 | + blockDev, err := CreateTempLoopDevice(t, name) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + //nolint:gosec |
| 51 | + cmd := exec.Command("sudo", "pvcreate", blockDev.LoopDevice) |
| 52 | + log.Printf("[DEBUG] executing command: %s", strings.Join(cmd.Args, " ")) |
| 53 | + if err := cmd.Run(); err != nil { |
| 54 | + if err := cleanupLoop(blockDev.LoopDevice); err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + if err := cleanupFile(blockDev.TempFile); err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + |
| 62 | + return nil, fmt.Errorf("error creating LVM partition on %s: %w", blockDev.LoopDevice, err) |
| 63 | + } |
| 64 | + |
| 65 | + //nolint:gosec |
| 66 | + cmd = exec.Command("sudo", "vgcreate", name, blockDev.LoopDevice) |
| 67 | + log.Printf("[DEBUG] executing command: %s", strings.Join(cmd.Args, " ")) |
| 68 | + if err := cmd.Run(); err != nil { |
| 69 | + if err := cleanupLoop(blockDev.LoopDevice); err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + |
| 73 | + if err := cleanupFile(blockDev.TempFile); err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + return nil, fmt.Errorf("error creating LVM partition on %s: %w", blockDev.LoopDevice, err) |
| 78 | + } |
| 79 | + |
| 80 | + return blockDev, nil |
| 81 | +} |
| 82 | + |
| 83 | +// returns the temporary file, the device and the error. |
| 84 | +func CreateTempLoopDevice(t *testing.T, name string) (*TempBlockDevice, error) { |
| 85 | + log.Print("[DEBUG] creating a temporary file for loop device") |
| 86 | + |
| 87 | + // Create a 1MB temp file |
| 88 | + filename := filepath.Join(t.TempDir(), name) |
| 89 | + |
| 90 | + //nolint:gosec |
| 91 | + cmd := exec.Command("dd", "if=/dev/zero", fmt.Sprintf("of=%s", filename), "bs=1024", "count=2048") |
| 92 | + log.Printf("[DEBUG] executing command: %s\n", strings.Join(cmd.Args, " ")) |
| 93 | + if err := cmd.Run(); err != nil { |
| 94 | + if err := cleanupFile(filename); err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + |
| 98 | + return nil, fmt.Errorf("Error creating file %s: %w", filename, err) |
| 99 | + } |
| 100 | + |
| 101 | + // Find an available loop device. |
| 102 | + cmd = exec.Command("sudo", "/sbin/losetup", "--find") |
| 103 | + loopdevStr, err := cmd.Output() |
| 104 | + log.Printf("[DEBUG] executing command: %s", strings.Join(cmd.Args, " ")) |
| 105 | + if err != nil { |
| 106 | + if err := cleanupFile(filename); err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + |
| 110 | + return nil, fmt.Errorf("Error searching for available loop device: %w", err) |
| 111 | + } |
| 112 | + loopdev := filepath.Clean(strings.TrimRight(string(loopdevStr), "\n")) |
| 113 | + |
| 114 | + // give the same permissions to the loop device as the backing file. |
| 115 | + cmd = exec.Command("sudo", "chown", "--reference", filename, loopdev) |
| 116 | + log.Printf("[DEBUG] executing command: %s", strings.Join(cmd.Args, " ")) |
| 117 | + if err := cmd.Run(); err != nil { |
| 118 | + if err := cleanupFile(filename); err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + |
| 122 | + return nil, fmt.Errorf("Error copying permissions from %s: %w", filename, err) |
| 123 | + } |
| 124 | + |
| 125 | + // attach the file to a loop device. |
| 126 | + cmd = exec.Command("sudo", "/sbin/losetup", loopdev, filename) |
| 127 | + log.Printf("[DEBUG] executing command: %s", strings.Join(cmd.Args, " ")) |
| 128 | + if err := cmd.Run(); err != nil { |
| 129 | + if err := cleanupFile(filename); err != nil { |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + |
| 133 | + return nil, fmt.Errorf("Error setting up loop device: %w", err) |
| 134 | + } |
| 135 | + |
| 136 | + log.Printf("[DEBUG] temporary file %s attached to loop device %s", filename, loopdev) |
| 137 | + |
| 138 | + return &TempBlockDevice{TempFile: filename, LoopDevice: loopdev}, nil |
| 139 | +} |
| 140 | + |
| 141 | +func (b *TempBlockDevice) Cleanup() error { |
| 142 | + err := cleanupLoop(b.LoopDevice) |
| 143 | + if err != nil { |
| 144 | + return err |
| 145 | + } |
| 146 | + |
| 147 | + err = cleanupFile(b.TempFile) |
| 148 | + if err != nil { |
| 149 | + return err |
| 150 | + } |
| 151 | + |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +func cleanupLoop(loopDevice string) error { |
| 156 | + cmd := exec.Command("sudo", "losetup", "-d", loopDevice) |
| 157 | + if err := cmd.Run(); err != nil { |
| 158 | + log.Printf("[DEBUG] error detaching loop device %s: %s", loopDevice, err) |
| 159 | + return err |
| 160 | + } |
| 161 | + log.Printf("[DEBUG] detaching loop device %s", loopDevice) |
| 162 | + |
| 163 | + return nil |
| 164 | +} |
| 165 | + |
| 166 | +func cleanupFile(tempFile string) error { |
| 167 | + if err := os.Remove(tempFile); err != nil { |
| 168 | + log.Printf("[DEBUG] error removing temporary file %s: %s", tempFile, err) |
| 169 | + return err |
| 170 | + } |
| 171 | + log.Printf("[DEBUG] removing temporary file %s", tempFile) |
| 172 | + |
| 173 | + return nil |
| 174 | +} |
| 175 | + |
| 176 | +func (b *TempBlockDevice) String() string { |
| 177 | + return fmt.Sprintf("TempFile: %s, LoopDevice: %s", b.TempFile, b.LoopDevice) |
| 178 | +} |
0 commit comments