@@ -17,6 +17,7 @@ package disktool
1717import (
1818 "fmt"
1919 "math"
20+ "strconv"
2021 "strings"
2122
2223 "yunion.io/x/jsonutils"
@@ -229,7 +230,7 @@ type DiskPartitions struct {
229230 partitions []* Partition
230231}
231232
232- func newDiskPartitions (driver string , adapter int , raidConfig string , sizeMB int64 , blockSize int64 , diskType string , tool * PartitionTool ) * DiskPartitions {
233+ func newDiskPartitions (driver string , adapter int , raidConfig string , sizeMB int64 , blockSize int64 , diskType string , softRaidIdx * int , tool * PartitionTool ) * DiskPartitions {
233234 ps := new (DiskPartitions )
234235 ps .driver = driver
235236 ps .adapter = adapter
@@ -239,9 +240,32 @@ func newDiskPartitions(driver string, adapter int, raidConfig string, sizeMB int
239240 ps .blockSize = blockSize
240241 ps .diskType = diskType
241242 ps .partitions = make ([]* Partition , 0 )
243+
244+ // soft raid, mdadm
245+ if softRaidIdx != nil {
246+ ps .GetMdadmInfo (softRaidIdx )
247+ }
242248 return ps
243249}
244250
251+ func (ps * DiskPartitions ) GetMdadmInfo (softRaidIdx * int ) {
252+ devLinkName := fmt .Sprintf ("/dev/md/md%d" , * softRaidIdx )
253+ devLinkNickname := fmt .Sprintf ("/dev/md/md%d_0" , * softRaidIdx )
254+ cmd := fmt .Sprintf ("readlink -f $(test -e %s && echo %s || echo %s)" , devLinkName , devLinkName , devLinkNickname )
255+ out , err := ps .tool .Run (cmd )
256+ if err != nil || len (out ) == 0 {
257+ log .Errorf ("failed readlink of %s: %s" , devLinkName , err )
258+ return
259+ }
260+
261+ ps .dev = strings .TrimSpace (out [0 ])
262+ ps .devName = ps .dev
263+ uuid , sectors := ps .tool .GetMdadmUuidAndSector (ps .dev )
264+ ps .pciPath = uuid
265+ ps .sectors = sectors
266+ ps .blockSize = 512
267+ }
268+
245269func (p * DiskPartitions ) IsRaidDriver () bool {
246270 return utils .IsInStringArray (p .driver , []string {
247271 baremetal .DISK_DRIVER_MEGARAID ,
@@ -324,7 +348,7 @@ func (ps *DiskPartitions) IsReady() bool {
324348
325349func (ps * DiskPartitions ) GetDevName () string {
326350 devName := ps .devName
327- if ! ps . IsRaidDriver () || ps .raidConfig == baremetal .DISK_CONF_NONE {
351+ if ps .raidConfig == baremetal .DISK_CONF_NONE {
328352 return devName
329353 }
330354 raidDrv , err := raiddrivers .GetDriverWithInit (ps .driver , ps .tool .runner .Term ())
@@ -690,12 +714,13 @@ func (tool *PartitionTool) parseLsDisk(lines []string, driver string) {
690714
691715func (tool * PartitionTool ) FetchDiskConfs (diskConfs []baremetal.DiskConfiguration ) * PartitionTool {
692716 for _ , d := range diskConfs {
693- disk := newDiskPartitions (d .Driver , d .Adapter , d .RaidConfig , d .Size , d .Block , d .DiskType , tool )
717+ disk := newDiskPartitions (d .Driver , d .Adapter , d .RaidConfig , d .Size , d .Block , d .DiskType , d . SoftRaidIdx , tool )
694718 tool .disks = append (tool .disks , disk )
719+ isSoftRaid := d .RaidConfig != baremetal .DISK_CONF_NONE
695720 var key string
696- if d .Driver == baremetal .DISK_DRIVER_LINUX {
721+ if d .Driver == baremetal .DISK_DRIVER_LINUX && ! isSoftRaid {
697722 key = NONRAID_DRIVER
698- } else if d .Driver == baremetal .DISK_DRIVER_PCIE {
723+ } else if d .Driver == baremetal .DISK_DRIVER_PCIE && ! isSoftRaid {
699724 key = PCIE_DRIVER
700725 } else {
701726 key = RAID_DRVIER
@@ -768,7 +793,42 @@ func (tool *PartitionTool) IsAllDisksReady() bool {
768793 return true
769794}
770795
796+ func (tool * PartitionTool ) GetMdadmUuidAndSector (devPath string ) (string , int64 ) {
797+ var uuid string
798+ var sectorsRet int64
799+ // get md uuid as pci path
800+ cmd := fmt .Sprintf ("/sbin/mdadm --detail %s | grep UUID" , devPath )
801+ output , err := tool .Run (cmd )
802+ if err == nil && len (output ) > 0 {
803+ uuidSeg := output [0 ]
804+ segs := strings .SplitN (strings .TrimSpace (uuidSeg ), ":" , 2 )
805+ if len (segs ) == 2 {
806+ uuid = strings .TrimSpace (segs [1 ])
807+ }
808+ }
809+
810+ // get block size
811+ cmd = fmt .Sprintf ("blockdev --getsz %s 2>/dev/null || echo 0" , devPath )
812+ output , err = tool .Run (cmd )
813+ if err == nil && len (output ) > 0 {
814+ if sectors , err := strconv .ParseInt (strings .TrimSpace (output [0 ]), 10 , 64 ); err == nil {
815+ sectorsRet = sectors
816+ }
817+ }
818+ return uuid , sectorsRet
819+ }
820+
771821func (tool * PartitionTool ) RetrieveDiskInfo (rootMatcher * api.BaremetalRootDiskMatcher ) error {
822+ for _ , disk := range tool .disks {
823+ if baremetal .DISK_DRIVERS_SOFT_RAID .Has (disk .driver ) && disk .raidConfig != baremetal .DISK_CONF_NONE {
824+ log .Infof ("Soft raid mdadm set diskinfo dev %s" , disk .dev )
825+ uuid , sectors := tool .GetMdadmUuidAndSector (disk .dev )
826+ disk .pciPath = uuid
827+ disk .sectors = sectors
828+ disk .blockSize = 512
829+ }
830+ }
831+
772832 for _ , driver := range []string {RAID_DRVIER , NONRAID_DRIVER , PCIE_DRIVER } {
773833 cmd := fmt .Sprintf ("/lib/mos/lsdisk --%s" , driver )
774834 ret , err := tool .Run (cmd )
0 commit comments