Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

疑難 Bug 排查:Linux 內核啟動時 ramdisk 加載失敗導致系統崩潰

編輯:關於 Android 開發

疑難 Bug 排查:Linux 內核啟動時 ramdisk 加載失敗導致崩潰

Bug 描述:Linux 內核啟動過程中,ramdisk 加載失敗,系統崩潰。

日志信息:

RAMDISK: Couldn't find valid RAM disk image starting at 0.
UDF-fs: No partition found (1)
NILFS: Can't find nilfs on dev ram0.
(1,15):ocfs2_fill_super:1001 ERROR: superblock probe
VFS: Cannot open root device "ram0" or unknown-block(1,0)
Please append a correct "root=" boot option; here are the available partitions:
0800 8003520 sda driver: sd
0801 14048 sda1
...
Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(1,0)
Pid: 1, comm: swapper Not tainted 2.6.32.15-hermes-1 #23
Call Trace:
[<ffffffff8151f1d5>] panic+0x7a/0x12d
[<ffffffff818780ba>] mount_block_root+0x257/0x275
[<ffffffff8187812e>] mount_root+0x56/0x5a
[<ffffffff8187829d>] prepare_namespace+0x16b/0x198
[<ffffffff81877685>] kernel_init+0x178/0x188

首先看到 "Couldn't find valid RAM disk image starting at 0.",順藤摸瓜找到打印此信息的代碼:

static int __init
identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
{
    const int size = 512;
    struct minix_super_block *minixsb;
    struct ext2_super_block *ext2sb;
    struct romfs_super_block *romfsb;
    struct cramfs_super *cramfsb;
    struct squashfs_super_block *squashfsb;
    int nblocks = -1;
    unsigned char *buf;
    const char *compress_name;

    buf = kmalloc(size, GFP_KERNEL);
    if (!buf)
        return -1;

    minixsb = (struct minix_super_block *) buf;
    ext2sb = (struct ext2_super_block *) buf;
    romfsb = (struct romfs_super_block *) buf;
    cramfsb = (struct cramfs_super *) buf;
    squashfsb = (struct squashfs_super_block *) buf;
    memset(buf, 0xe5, size);

    /* 讀取 block 0 測試是否為壓縮內核 */
    sys_lseek(fd, start_block * BLOCK_SIZE, 0);
    sys_read(fd, buf, size);
    // Eric Ju Jul 27th 2016
    printk("start_block:%d\n", start_block);
    for (i = 0; i < size; i++)
        printk("0x%x ", *(buf+i));
    printk("\n");

    *decompressor = decompress_method(buf, size, &compress_name);
    if (compress_name) {
        ...
        nblocks = 0;
        goto done;
    }
    /* 此處省略 romfs/cramfs/squashfs/minix/ext2 的匹配分支 */
    printk(KERN_NOTICE
        "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
        start_block);
done:
    sys_lseek(fd, start_block * BLOCK_SIZE, 0);
    kfree(buf);
    return nblocks;
}

可見,打印此日志是因為函數中所有分支都沒匹配成功。正常情況應走入第一個分支並跳轉到 done。為何沒走入第一分支?猜測 fd 指向 initrd 的文件描述符,第一分支前的 read 應是讀取 initrd 首個扇區並比較 magic,匹配成功說明 initrd 是正確鏡像,並調用相應解壓函數。通過打印讀出的 buf 內容來確認 initrd 是否正確——結果打印內容全為 0xFF,證明該 initrd 文件錯誤。

為何 initrd 會錯?磁盤上的 initrd.img 明明是正確的。繼續跟蹤 identify_ramdisk_image 的調用處,看 fd 到底是什麼。發現以下函數,位於內核源碼 init/do_mounts_initrd.c:

int __init initrd_load(void)
{
    if (mount_initrd) {
        create_dev("/dev/ram", Root_RAM0);
        if (rd_load_image("/initrd.image") && ROOT_DEV != Root_RAM0) {
            sys_unlink("/initrd.image");
            handle_initrd();
            return 1;
        }
    }
    sys_unlink("/initrd.image");
    return 0;
}

initrd.image?不對啊,磁盤上的文件名是 initrd.img,怎麼變成 initrd.image 了,而且路徑也不對。猜測是有啟動代碼創建了指向 initrd.img 的符號鏈接。繼續查找 initrd.image 的創建位置,找到如下代碼,位於 /init/initramfs.c:

static int __init populate_rootfs(void)
{
    char *err = unpack_to_rootfs(__initramfs_start,
            __initramfs_end - __initramfs_start);
    if (err)
        panic(err);
    if (initrd_start) {
#ifdef CONFIG_BLK_DEV_RAM
        int fd;
        printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
        err = unpack_to_rootfs((char *)initrd_start, initrd_end - initrd_start);
        if (!err) {
            free_initrd();
            return 0;
        } else {
            clean_rootfs();
            unpack_to_rootfs(__initramfs_start, __initramfs_end - __initramfs_start);
        }
        printk(KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n", err);
        fd = sys_open("/initrd.image", O_WRONLY|O_CREAT, 0700);
        if (fd >= 0) {
            sys_write(fd, (char *)initrd_start, initrd_end - initrd_start);
            sys_close(fd);
            free_initrd();
        }
#endif
    }
    return 0;
}

原來 initrd.image 是在這裡創建的。那句 sys_write(fd, (char *)initrd_start, initrd_end - initrd_start) 表明內核是把內存中的數據寫到 /initrd.image,並非軟鏈接。那 initrd_start 從哪來?打印後發現 initrd_start 為 0xffff880000100000,估計是已轉換的虛擬地址。既然 initrd.image 是從內存寫入根文件系統的,那一定有程序把 initrd.img 讀入了內存。想到 lilo.multi.conf 中指定了 initrd.img 路徑,那一定是 LILO 在啟動時將其讀入內存並把地址傳給內核。

繼續看串口日志,剛啟動時有:

RAMDISK: 7fa36000 - 7ffff40e
Allocated new RAMDISK: 00100000 - 006c940e
Move RAMDISK from 000000007fa36000 - 000000007ffff40d to 00100000 - 006c940d

可見 RAMDISK 初始起始地址為 0x7fa36000,而內核將其移動到了 0x00100000。對比虛擬地址 0xffff880000100000,可確定它正是從 0x00100000 映射而來(內核在低物理地址映射時,設虛擬高端地址後偏移實際物理地址)。打印 RAMDISK 移動前、後的內容,發現移動前就已是 0xFF,可斷定 LILO 讀取 initrd.img 時就已經錯了。從全 0xFF 看,物理內存應未被寫過,是上電後的初始狀態。

突然想起另一個信息:替換內核後執行 lilo64 -C lilo.multi.conf -s `pwd` 時,LILO 報了一個警告:

Normally any initial ramdisk (initrd) loaded with a kernel is loaded as
high in memory as possible, but never above 15Mb. This is due to a BIOS
limitation on older systems. On newer systems, this option enables using
memory above 15Mb (up to a kernel imposed limit, around 768Mb) for
passing the initrd to the kernel. ...

再看 RAMDISK 初始起始地址 0x7fa36000,顯然高於 15MB,說明 LILO 認為內核和 initrd.img 的大小超過了某個固定限制,把 initrd 放到了高端內存。那 LILO 為何寫入失敗?從提示看,應該是剛上電啟動時 BIOS 不支持訪問高端內存,LILO 調用 BIOS 寫入程序時出錯,但它並未在意這個錯誤。

在 LILO 官網找到一篇技術文檔,明確指出 LILO 會把 initrd.img 加載在內存低端地址尾部(16MB 以下)。16MB 的限制源於 BIOS 只用 24 位地址空間傳輸數據。閱讀 LILO 代碼後發現,LILO 會按內核鏡像大小的 3 倍與 initrd.img 大小求和,當總和大於 14MB,LILO 便認為 14MB 以下低地址空間放不下,於是判定 BIOS 支持 16MB 以上地址空間,將 initrd 放在高地址空間加載。

LILO 技術文檔:https://lilo.alioth.debian.org/olddoc/html/tech_21-5.html,感興趣的同學可以看看。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved