2012-03-13 19 views
2

私はこのコードをプロジェクトソースの一部です。 このコードは、GRUBまたはLILOというMBRタイプを検出し、それに応じてフラグを設定します。MBRタイプが見つかりません

驚いたことに、SLES 10-SP1(SUSE Linux Enterprise Server)では、それを特定できません。 /dev/sda1は私のスワップです。 /dev/sda2は、MBRを含めて、全体で/が存在する場所です。

同じコードがSLES11などで動作します。 ここでMBR_SIZE#defined~0x1beです。

int lnxfsGetBootType(int pNumber) 
{ 
    int     i, retval = -1, ccode; 
    PartInfo   *p = &cpuParts[pNumber]; 
    char    buffer[SECTOR_SIZE]; 
    var64    offset = 0; 

    isdLogFileOut(ZISD_LOG_DEVELOPER,"[lnxGBT]\n"); 
    if (getenv("ZENDEVICE") || gUtilPart == 1) { 
     offset = p->pOffset;  // look at the partition BPB 
    } 

    //Now try to find the installed boot loader... 
    lseek64(p->handle, (var64)offset, SEEK_SET); // either MBR or BPB 
    ccode = read(p->handle, buffer, SECTOR_SIZE); 

    for (i=0; i<MBR_SIZE-4;i++) { 
     if (strncmp(&buffer[i], "LILO", 4) == 0) { 
      if (offset == 0){ 
       retval = FLAG_LNXFS_LILO; 
       isdLogFileOut(ZISD_LOG_WARNING,"\tLILO MBR found on %s\n",p->header.deviceName); 
      } else { 
       retval = FLAG_LNXFS_LILO; // 10.31.06 _BPB; 
       isdLogFileOut(ZISD_LOG_WARNING,"\tLILO BPB found on %s\n",p->header.deviceName); 
      } 
     } 
     if (strncmp(&buffer[i], "GRUB", 4) == 0) { 
      if (offset == 0){ 
       retval = FLAG_LNXFS_GRUB; 
       isdLogFileOut(ZISD_LOG_WARNING,"\tGRUB MBR found on %s\n",p->header.deviceName); 
      } else { 
       retval = FLAG_LNXFS_GRUB; // 10.31.06 _BPB; 
       isdLogFileOut(ZISD_LOG_WARNING,"\tGRUB BPB found on %s\n",p->header.deviceName); 
      } 
     } 
    } 
    if (retval == -1) { 
     isdLogFileOut(ZISD_LOG_WARNING,"\tLILO or GRUB mbr/bpb not found on %s\n",p->header.deviceName); 
    } 

    return retval; 
} // lnxfsGetBootType 

Here partinfo, is a struct of partition type: 
//Data structure used internally by the image engine to store information about the 
//partitions. It encapsulates the PartHeader struct, whcih is used to store partition 
//information in image archives 
typedef struct _PartInfo 
{ 
    PartHeader header; 
    int   handle;   //file handle for reading/writing physical device 
    var32  flags;   //Various flags as needed. Defined above. 
    var64  pOffset;  //offset to partition from start of physical device 
    int   deviceNumber; //index into 'devices' where this partition's 
           // physical device is located 
    int   archIndex;  //for restoring only. Index into imgParts of the 
           // archive partition this physical partition is 
           // mapped to 
    int   bytesWritten; //track number of sectors written so the device-level 
           // cache can be flushed 
    void  *info;   //partition-type-specific info struct 

/* snip */ 

テストは、VMWareの下で異なる仮想ディスクイメージで実行されています。私は、ディスクがGPRではなくMBRでフォーマットされていることを確認しました。

+0

@sarnold:いいえ、VMwareと個別の仮想ディスクでテストされています。 – kingsmasher1

+0

@sarnold:いいえ、GPTは使用していません.MBRです。ちょうどチームから確認。 – kingsmasher1

+0

@sarnold:vmware4ディスクイメージ:) – kingsmasher1

答えて

1

動作しないと言ったらどういう意味なのか分かりません。あなたのコードが-1を返すという点がある場合は、私たちにMBRのコピーを表示できますか?あなたはそれをキャプチャするために、このコマンドを使用することができます。

sudo dd if=/dev/sda bs=512 count=1 | xxd 

あなたのMBRが/dev/sda2であることに言及します。それは確かに非常に珍しいことです。ブートコードがインストールされている場所であれば、それはまったく違うことです。 MBRは常にディスクの最初のセクタに保持されます(DOS形式のMBRであると仮定します)。

いくつかの障害ケースでの問題は、シークの失敗または短い読み取りである可能性があります。私はエラー処理を追加し、少し単純化するためにいくつかの調整を行った。

#define MBR_SIZE 0x1be 

int lnxfsGetBootType(int pNumber) 
{ 
    int     retval = -1, ccode; 
    PartInfo   *p = &cpuParts[pNumber]; 
    char    buffer[SECTOR_SIZE]; 
    off64_t offset = 0; 
    void *plilo, *pgrub; 
    const char *what = "MBR"; 

    isdLogFileOut(ZISD_LOG_DEVELOPER,"[lnxGBT]\n"); 
    if (getenv("ZENDEVICE") || gUtilPart == 1) { 
     offset = p->pOffset;  // look at the partition BPB 
     what = "BPB"; 
    } 

    // Now try to find the installed boot loader... 
    if (lseek64(p->handle, offset, SEEK_SET) == -1) { 
     isdLogFileOut(ZISD_LOG_ERROR,"\tFailed to seek to %s: %s\n", what, strerror(errno)); 
     return -1; 
    } 
    ccode = read(p->handle, buffer, SECTOR_SIZE); 
    if (ccode != SECTOR_SIZE) { 
      isdLogFileOut(ZISD_LOG_ERROR,"\tFailed to read BPB/MBR: %s\n", 
           strerror(errno)); 
      return -1; 
    } 
    plilo = memmem(buffer, ccode, "LILO", 4); 
    pgrub = memmem(buffer, ccode, "GRUB", 4); 
    if (plilo) { 
     retval = FLAG_LNXFS_LILO; 
     if (pgrub && pgrub < plilo) 
      retval = FLAG_LNXFS_GRUB; 
     } 
    } else if (pgrub) { 
     retval = FLAG_LNXFS_GRUB; 
    } 
    if (-1 == retval) { 
     isdLogFileOut(ZISD_LOG_WARNING,"\tLILO or GRUB %s not found on %s\n", what, p->header.deviceName); 
    } else { 
     isdLogFileOut(ZISD_LOG_WARNING,"\t%s %s not found on %s\n", 
       (retval == FLAG_LNXFS_GRUB ? "GRUB" : "LILO"), 
       what, p->header.deviceName); 
    } 
    return retval; 
} // lnxfsGetBootType 
+0

'/ dev/sda1'は'/dev/sda'の最初のパーティションで、通常は(ディスクイメージがパーティションに仮想化されていない限り)MBRを持ちません。 – wallyk

+0

@wallyk:そうです。 –

+0

私はパーティショニングを行い、1番目のパーティションにスワップスペースを作成しているので、/ dev/sda2に実際にMBRを持っています。また、訂正されたコードのおかげで、すぐに実行後に元に戻ります。しかし、不思議なことに、この問題はSLES 10-SP1およびSP2でのみ表示されます。 SP3上では正常に動作します – kingsmasher1

関連する問題