@@ -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,23 @@ 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+ ps .dev = fmt .Sprintf ("/dev/md%d" , * softRaidIdx )
253+ ps .devName = ps .dev
254+ uuid , sectors := ps .tool .GetMdadmUuidAndSector (ps .dev )
255+ ps .pciPath = uuid
256+ ps .sectors = sectors
257+ ps .blockSize = 512
258+ }
259+
245260func (p * DiskPartitions ) IsRaidDriver () bool {
246261 return utils .IsInStringArray (p .driver , []string {
247262 baremetal .DISK_DRIVER_MEGARAID ,
@@ -324,7 +339,7 @@ func (ps *DiskPartitions) IsReady() bool {
324339
325340func (ps * DiskPartitions ) GetDevName () string {
326341 devName := ps .devName
327- if ! ps . IsRaidDriver () || ps .raidConfig == baremetal .DISK_CONF_NONE {
342+ if ps .raidConfig == baremetal .DISK_CONF_NONE {
328343 return devName
329344 }
330345 raidDrv , err := raiddrivers .GetDriverWithInit (ps .driver , ps .tool .runner .Term ())
@@ -690,12 +705,13 @@ func (tool *PartitionTool) parseLsDisk(lines []string, driver string) {
690705
691706func (tool * PartitionTool ) FetchDiskConfs (diskConfs []baremetal.DiskConfiguration ) * PartitionTool {
692707 for _ , d := range diskConfs {
693- disk := newDiskPartitions (d .Driver , d .Adapter , d .RaidConfig , d .Size , d .Block , d .DiskType , tool )
708+ disk := newDiskPartitions (d .Driver , d .Adapter , d .RaidConfig , d .Size , d .Block , d .DiskType , d . SoftRaidIdx , tool )
694709 tool .disks = append (tool .disks , disk )
710+ isSoftRaid := d .RaidConfig != baremetal .DISK_CONF_NONE
695711 var key string
696- if d .Driver == baremetal .DISK_DRIVER_LINUX {
712+ if d .Driver == baremetal .DISK_DRIVER_LINUX && ! isSoftRaid {
697713 key = NONRAID_DRIVER
698- } else if d .Driver == baremetal .DISK_DRIVER_PCIE {
714+ } else if d .Driver == baremetal .DISK_DRIVER_PCIE && ! isSoftRaid {
699715 key = PCIE_DRIVER
700716 } else {
701717 key = RAID_DRVIER
@@ -768,7 +784,41 @@ func (tool *PartitionTool) IsAllDisksReady() bool {
768784 return true
769785}
770786
787+ func (tool * PartitionTool ) GetMdadmUuidAndSector (devPath string ) (string , int64 ) {
788+ var uuid string
789+ var sectorsRet int64
790+ // get md uuid as pci path
791+ cmd := fmt .Sprintf ("/sbin/mdadm --detail %s | grep UUID" , devPath )
792+ output , err := tool .Run (cmd )
793+ if err == nil && len (output ) > 0 {
794+ segs := strings .SplitN (strings .TrimSpace (output [0 ]), ":" , 2 )
795+ if len (segs ) == 2 {
796+ uuid = strings .TrimSpace (segs [1 ])
797+ }
798+ }
799+
800+ // get block size
801+ cmd = fmt .Sprintf ("blockdev --getsz %s 2>/dev/null || echo 0" , devPath )
802+ output , err = tool .Run (cmd )
803+ if err == nil && len (output ) > 0 {
804+ if sectors , err := strconv .ParseInt (strings .TrimSpace (output [0 ]), 10 , 64 ); err == nil {
805+ sectorsRet = sectors
806+ }
807+ }
808+ return uuid , sectorsRet
809+ }
810+
771811func (tool * PartitionTool ) RetrieveDiskInfo (rootMatcher * api.BaremetalRootDiskMatcher ) error {
812+ for _ , disk := range tool .disks {
813+ if baremetal .DISK_DRIVERS_SOFT_RAID .Has (disk .driver ) && disk .raidConfig != baremetal .DISK_CONF_NONE {
814+ log .Infof ("Soft raid mdadm set diskinfo dev %s" , disk .dev )
815+ uuid , sectors := tool .GetMdadmUuidAndSector (disk .dev )
816+ disk .pciPath = uuid
817+ disk .sectors = sectors
818+ disk .blockSize = 512
819+ }
820+ }
821+
772822 for _ , driver := range []string {RAID_DRVIER , NONRAID_DRIVER , PCIE_DRIVER } {
773823 cmd := fmt .Sprintf ("/lib/mos/lsdisk --%s" , driver )
774824 ret , err := tool .Run (cmd )
@@ -869,6 +919,7 @@ func newSSHPartitionTool(term *ssh.Client) *SSHPartitionTool {
869919func NewSSHPartitionTool (term * ssh.Client , layouts []baremetal.Layout , rootMatcher * api.BaremetalRootDiskMatcher ) (* SSHPartitionTool , error ) {
870920 tool := newSSHPartitionTool (term )
871921 tool .FetchDiskConfs (baremetal .GetDiskConfigurations (layouts ))
922+ log .Errorf ("disk tables %s" , jsonutils .Marshal (tool .diskTable ))
872923 if err := tool .RetrieveDiskInfo (rootMatcher ); err != nil {
873924 return nil , errors .Wrapf (err , "RetrieveDiskInfo" )
874925 }
0 commit comments