//.Y / 100 117 / / / / 70 / / / / / / //.YT 3 2 0 0 3 3 1 / / / //.H!a!0:forth#092/io#092/disk_#092/raid#092/linux#092/cpp#092/Test.CPP!0.!c // 28sep2013 Thomas Brücker // 07dec2016: works principally! // Raid Test // +++++++++ #include // __u64 #include // errno #include // open #include // MD_MAJOR #include // MD_DISK_ACTIVE, etc. #include // constants for ioctl on // raid #include #include // _IOR etc. #include // close int AddDisk(int DiskMajor, int DiskMinor, int DiskIndex); int CloseRaid(void); int hFile = -1; // avoid closing stdin ! int OpenRaid(int Md); void PrintUsage(void); int AddDisk(int DiskMajor, int DiskMinor, int DiskIndex) { // * after calling this function successfully, (kernel-2.4.37) the corres- // ponding disk appears in "/proc/mdstat" under the corresponding md as // disk. mdu_disk_info_t Disk; int State; State = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE); // active disk printf("DiskIndex: %i\n", DiskIndex); Disk.number = DiskIndex; // starts with 0 . Disk.major = DiskMajor; Disk.minor = DiskMinor; Disk.raid_disk = DiskIndex; // starts with 0 . Disk.state = State; // * active disk: "(1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE) // ev.: | (1 << MD_DISK_WRITEMOSTLY);" // * spare-disk: 0; if( ioctl(hFile, ADD_NEW_DISK, &Disk) ) { // it is an error. printf("errno = %i\n", errno); return -1; } // else DiskIndex++; return 0; } int CloseRaid(void) { if(-1 != hFile) { // avoid closing a null-hFile . if( close(hFile) ) { // it is an error. printf("errno = %i\n", errno); return -1; } // else } return 0; } int main( int argc, char* argv[] ) { int DiskIndex; int DiskMajor; int DiskMinor; int MdMinor; if( (1 + 4) != argc) { // printf("argc = %i\n", argc); PrintUsage(); return 0; } // else DiskMajor = strtol( argv[1], NULL, 10); DiskMinor = strtol( argv[2], NULL, 10); DiskIndex = strtol( argv[4], NULL, 10); MdMinor = strtol( argv[3], NULL, 10); /* debug stuff: printf("%i\n", DiskMajor); printf("%i\n", DiskMinor); printf("%i\n", MdMinor); printf("%i\n", DiskIndex); */ if( OpenRaid(MdMinor) ) // it is an error. return 255; // else if( AddDisk(DiskMajor, DiskMinor, DiskIndex) ) { // it is an error. if( CloseRaid() ) // it is an error. return 253; // else return 254; } // else if( CloseRaid() ) // it is an error. return 253; // else return 0; } int OpenRaid(int Md) { // * ok: Md in [0, 255]sub(N) (the device must exist in "/dev".). // * you may open successfully a raid-device ("/dev/md*") that is not yet // started. // * kernel > kernel-2.6: after calling this function successfully, the // corresponding md is visible in "/sys/block". int MdMinor; char pStringBuffer[1024]; MdMinor = Md; // md# --> String: sprintf(pStringBuffer, "/dev/md%i", MdMinor); hFile = open(pStringBuffer, O_RDWR); if(-1 == hFile) { // it is an error. printf("errno = %i\n", errno); return -1; } // else return 0; } void PrintUsage(void) { printf("Usage: AddDisk \n"); }