osd: fix osd detection logic for old lsblk
Old lsblk can't detect bluestore signature.
So read the head of device and verify signature
as a workaround.
closes: https://github.com/rook/rook/issues/10665
Signed-off-by: taki-d <mail@orangepla.net>
(cherry picked from commit ca73efa1e5)
This commit is contained in:
1 parent
e4ab0b6dcc
commit
b7ca5457bb
2 files changed
+185
No files matched your search
@@ -17,7 +17,10 @@ limitations under the License.
|
||||
package osd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
@@ -38,6 +41,7 @@ const (
|
||||
pvcMetadataTypeDevice = "metadata"
|
||||
pvcWalTypeDevice = "wal"
|
||||
lvmCommandToCheck = "lvm"
|
||||
bluestoreSignature = "bluestore block device"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -287,6 +291,52 @@ func matchDevLinks(devLinks, deviceName string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func getOsdUUIDImpl(device *sys.LocalDisk) (string, error) {
|
||||
// Old lsblk can't detect an existing OSD.
|
||||
// See: https://github.com/rook/rook/issues/10665
|
||||
logger.Infof("old lsblk can't detect bluestore signature, so try to detect here")
|
||||
|
||||
f, err := os.Open(device.RealPath)
|
||||
if err != nil {
|
||||
err := fmt.Errorf("failed to open %q. %v", device.Name, err)
|
||||
return "", err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
readBuffer := make([]byte, 128)
|
||||
_, err = f.Read(readBuffer)
|
||||
if err != nil {
|
||||
err := fmt.Errorf("failed to read signature from %q. %v", device.Name, err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
reader := bufio.NewReader(bytes.NewReader(readBuffer))
|
||||
|
||||
signature, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
err := fmt.Errorf("failed to read signature from %q. %v", device.Name, err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
if signature[:len(signature)-1] != bluestoreSignature {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
uuid, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
err := fmt.Errorf("failed to read OSD uuid from %q. %v", device.Name, err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
return uuid[:len(uuid)-1], nil
|
||||
}
|
||||
|
||||
var getOsdUUID func(device *sys.LocalDisk) (string, error) = getOsdUUIDImpl
|
||||
|
||||
func getAvailableDevices(context *clusterd.Context, agent *OsdAgent) (*DeviceOsdMapping, error) {
|
||||
desiredDevices := agent.devices
|
||||
logger.Debugf("desiredDevices are %+v", desiredDevices)
|
||||
@@ -324,6 +374,17 @@ func getAvailableDevices(context *clusterd.Context, agent *OsdAgent) (*DeviceOsd
|
||||
logger.Infof("skipping device %q because it contains a filesystem %q", device.Name, device.Filesystem)
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
uuid, err := getOsdUUID(device)
|
||||
if err != nil {
|
||||
logger.Errorf("skipping device %q, failed to get OSD information. %v", device.Name, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if uuid != "" {
|
||||
logger.Infof("skipping device %q, detected an existing OSD. UUID=%s", device.Name, uuid)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if device.Type == sys.PartType {
|
||||
|
||||
@@ -16,6 +16,7 @@ limitations under the License.
|
||||
package osd
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -172,7 +173,121 @@ USEC_INITIALIZED=1128667
|
||||
`
|
||||
)
|
||||
|
||||
func TestGetOsdUUID(t *testing.T) {
|
||||
fileContainSignatureOnly, err := os.Create("fileContainSignatureOnly")
|
||||
if err != nil {
|
||||
t.Fatal("failed to create test file")
|
||||
}
|
||||
defer fileContainSignatureOnly.Close()
|
||||
defer os.Remove("fileContainSignatureOnly")
|
||||
|
||||
_, err = fileContainSignatureOnly.Write([]byte(bluestoreSignature + "\n"))
|
||||
if err != nil {
|
||||
t.Fatal("failed to write test file")
|
||||
}
|
||||
|
||||
fileContainSignatureAndUUID, err := os.Create("fileContainSignatureAndUUID")
|
||||
if err != nil {
|
||||
t.Fatal("failed to create test file")
|
||||
}
|
||||
defer fileContainSignatureAndUUID.Close()
|
||||
defer os.Remove("fileContainSignatureAndUUID")
|
||||
|
||||
_, err = fileContainSignatureAndUUID.Write([]byte(bluestoreSignature + "\n" + "c6416047-1e71-412a-9b61-ca398b815e50\n"))
|
||||
if err != nil {
|
||||
t.Fatal("failed to write test file")
|
||||
}
|
||||
|
||||
fileNotContainBluestore, err := os.Create("fileNotContainBluestore")
|
||||
if err != nil {
|
||||
t.Fatal("failed to create test file")
|
||||
}
|
||||
defer fileNotContainBluestore.Close()
|
||||
defer os.Remove("fileNotContainBluestore")
|
||||
|
||||
_, err = fileNotContainBluestore.Write([]byte("invalid signature\n"))
|
||||
if err != nil {
|
||||
t.Fatal("failed to write test file")
|
||||
}
|
||||
|
||||
fileNotReadable, err := os.Create("fileNotReadable")
|
||||
if err != nil {
|
||||
t.Fatal("failed to create test file")
|
||||
}
|
||||
defer fileNotReadable.Close()
|
||||
defer os.Remove("fileNotReadable")
|
||||
|
||||
err = os.Chmod("fileNotReadable", 0)
|
||||
if err != nil {
|
||||
t.Fatal("failed to set permission of test file")
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
device *sys.LocalDisk
|
||||
uuid string
|
||||
hasError bool
|
||||
}{
|
||||
{
|
||||
device: &sys.LocalDisk{
|
||||
Name: "sda",
|
||||
RealPath: "fileContainSignatureOnly",
|
||||
},
|
||||
uuid: "",
|
||||
hasError: true,
|
||||
},
|
||||
{
|
||||
device: &sys.LocalDisk{
|
||||
Name: "sda",
|
||||
RealPath: "fileContainSignatureAndUUID",
|
||||
},
|
||||
uuid: "c6416047-1e71-412a-9b61-ca398b815e50",
|
||||
hasError: false,
|
||||
},
|
||||
{
|
||||
device: &sys.LocalDisk{
|
||||
Name: "sda",
|
||||
RealPath: "fileNotContainBluestore",
|
||||
},
|
||||
uuid: "",
|
||||
hasError: false,
|
||||
},
|
||||
{
|
||||
device: &sys.LocalDisk{
|
||||
Name: "sda",
|
||||
RealPath: "fileNotReadable",
|
||||
},
|
||||
uuid: "",
|
||||
hasError: true,
|
||||
},
|
||||
{
|
||||
device: &sys.LocalDisk{
|
||||
Name: "sda",
|
||||
RealPath: "fileNotExist",
|
||||
},
|
||||
uuid: "",
|
||||
hasError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
uuid, err := getOsdUUID(c.device)
|
||||
|
||||
assert.Equal(t, c.uuid, uuid, c.device.RealPath)
|
||||
|
||||
if c.hasError {
|
||||
assert.Error(t, err, c.device.RealPath)
|
||||
} else {
|
||||
assert.NoError(t, err, c.device.RealPath, err)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestAvailableDevices(t *testing.T) {
|
||||
getOsdUUID = func(device *sys.LocalDisk) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// set up a mock function to return "rook owned" partitions on the device and it does not have a filesystem
|
||||
executor := &exectest.MockExecutor{
|
||||
MockExecuteCommandWithOutput: func(command string, args ...string) (string, error) {
|
||||
@@ -401,6 +516,15 @@ NAME="sdb1" SIZE="30" TYPE="part" PKNAME="sdb"`, nil
|
||||
mapping, err = getAvailableDevices(context, agent)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 1, len(mapping.Entries), mapping)
|
||||
|
||||
// device has an OSD, but don't detect bluestore signature by lsblk.
|
||||
// so try to detect signature by Rook
|
||||
getOsdUUID = func(device *sys.LocalDisk) (string, error) {
|
||||
return "c6416047-1e71-412a-9b61-ca398b815e50", nil
|
||||
}
|
||||
mapping, err = getAvailableDevices(context, agent)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 0, len(mapping.Entries), mapping)
|
||||
}
|
||||
|
||||
func TestGetVolumeGroupName(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user