三月 29, 2011

用SD卡制作Linux启动盘

Written by

搞嵌入式开发的朋友对从SD卡引导系统并不陌生,尤其是对类Linux的引导,一般把SD卡分成两个分区,一个是boot,基本上都是FAT32的,因为支持范围比较多,兼容性好。另一个就是Linux文件系统了。由于要把SD卡分成两个分区,不像通常一样格式化这么简单,需要一些不一样的操作手段。

前两天看了一篇文章《Linux Boot Disk Format》,虽然讲得相当详细,但是毕竟有些版本不一致的地方,很容易导致失败。所以将我的操作过程记录于此,希望能帮到需要的人。

按照Linux Boot Disk Format一文中介绍的方法,虽然成功格式化了我的SD卡,但有些地方不太一样,需要特别注意,因为一旦弄错,比如格式化了别的硬盘,那你就搬个小凳子墙角哭去吧:)

首先,用[dmesg | tail]命令显示如下信息:

[27544.407777] mmc0: new SD card at address 00c3
[27544.408026] mmcblk0: mmc0:00c3       475 MiB
[27544.408173]  mmcblk0: p1
[27577.892153] mmc0: card 00c3 removed
[27587.296689] mmc0: card claims to support voltages below the defined range. These will be ignored.
[27587.296700] mmc0: SD card claims to support the incompletely defined ‘low voltage range’. This will be ignored.

这个需要仔细看的,刚开始我去找‘sdx’,结果没有,很是奇怪。实际上,我的Ubuntu已经是10.04了,很多地方与ubuntu7还是不太一样的,比如设备显示信息会有些不同:)。
Linux Boot Disk Format》中的显示

[ 6854.215659] sdc: sdc1

我这里显示

[27544.408173]  mmcblk0: p1

所以,下面的[df -h]命令也会不太一样:

hyh@hyh-laptop:~$ df -h
文件系统            容量  已用 可用 已用% 挂载点
/dev/sdb6              49G  5.2G   41G  12% /
none                  490M  360K  489M   1% /dev
none                  494M  1.3M  492M   1% /dev/shm
none                  494M   88K  494M   1% /var/run
none                  494M     0  494M   0% /var/lock
none                  494M     0  494M   0% /lib/init/rw
/dev/sdb2              98G   59G   40G  60% /media/photo
/dev/sdb3             391G  161G  231G  41% /media/software
/dev/sdb5              49G  2.2G   47G   5% /media/sys
/dev/sdb4             346G  129G  217G  38% /media/doc
/dev/mmcblk0p1        476M   32M  444M   7% /media/A46E-7FA0

你可以看到,我的SD卡挂载到了mmcblk0,分区是p1:

/dev/mmcblk0p1        476M   32M  444M   7% /media/A46E-7FA0

现在非常非常非常的确认了我的SD卡是mmcblk0了,因为你也得非常非常非常的肯定你没搞错分区,否则又要找小凳子去了…
其实有一种可以非常非常肯定的手段来看你的SD卡是哪个的:
首先拔出SD卡,然后敲入[df -h],看看显示列表;然后插入SD卡,正确识别出来后,敲入[df -h],再看看显示列表,看看多出来的就是你的SD卡信息。

下面umount SD卡:

hyh@hyh-laptop:~$ umount /media/A46E-7FA0

选择/dev/mmcblk0,进入fdisk

hyh@hyh-laptop:~$ sudo fdisk /dev/mmcblk0
[sudo] password for hyh:

WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
switch off the mode (command ‘c’) and change display units to
sectors (command ‘u’).

显示“Command (m for help):”就是已经进入fdisk了,并且所有操作都是针对mmcblk0的,打印一下mmcblk0的信息看看:

Command (m for help): p

Disk /dev/mmcblk0: 498 MB, 498860032 bytes
255 heads, 63 sectors/track, 60 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00819e01

Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1   *           1          61      487136+   e  W95 FAT16 (LBA)
Partition 1 has different physical/logical endings:
phys=(59, 254, 63) logical=(60, 165, 41)

删除已有分区:

Command (m for help): d
Selected partition 1

如果上面你pint你的sd卡没有显示255 heads, 63 sectors/track信息,那么你还的重做一下你的SD卡,具体操作见Linux Boot Disk Format第七步。
我的当然显示正常,所以现在略过第七步,继续往下走:

新建一个FAT32分区:

Command (m for help): n
Command action
e   extended
p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-60, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-60, default 60): +5

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): c
Changed system type of partition 1 to c (W95 FAT32 (LBA))

OK了,把它标记为启动盘:

Command (m for help): a
Partition number (1-4): 1

创建Linux分区:

Command (m for help): n
Command action
e   extended
p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (7-60, default 7):
Using default value 7
Last cylinder, +cylinders or +size{K,M,G} (7-60, default 60):
Using default value 60

打印一下看看:

Command (m for help): p

Disk /dev/mmcblk0: 498 MB, 498860032 bytes
255 heads, 63 sectors/track, 60 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00819e01

Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1   *           1           6       48163+   c  W95 FAT32 (LBA)
/dev/mmcblk0p2               7          60      433755   83  Linux

一切OK,保存以上所有操作,注意,上面的所有操作实际上只是yy,没有真正修改SD卡!这也是高明之处!!!

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: If you have created or modified any DOS 6.x
partitions, please see the fdisk manual page for additional
information.
Syncing disks.

格式化FAT32分区,并将卷标设为“LABEL1”,当然你可以另外叫个你喜欢的名字:

hyh@hyh-laptop:~$ sudo mkfs.msdos -F 32 /dev/mmcblk0p1 -n LABEL1
mkfs.msdos 3.0.7 (24 Dec 2009)

格式化Linux分区,并将卷标设为“LABEL2”,当然你可以另外叫个你喜欢的名字:
hyh@hyh-laptop:~$ sudo mkfs.ext3 -L LABEL2 /dev/mmcblk0p2
mke2fs 1.41.11 (14-Mar-2010)
文件系统标签=LABEL2
操作系统:Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
108544 inodes, 433752 blocks
21687 blocks (5.00%) reserved for the super user
第一个数据块=1
Maximum filesystem blocks=67633152
53 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

正在写入inode表: 完成
Creating journal (8192 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
hyh@hyh-laptop:~$

现在再输入[dmesg | tail],显示如下:

[27544.407777] mmc0: new SD card at address 00c3
[27544.408026] mmcblk0: mmc0:00c3       475 MiB
[27544.408173]  mmcblk0: p1
[27577.892153] mmc0: card 00c3 removed
[27587.296689] mmc0: card claims to support voltages below the defined range. These will be ignored.
[27587.296700] mmc0: SD card claims to support the incompletely defined ‘low voltage range’. This will be ignored.
[27587.323780] mmc0: new SD card at address 00c3
[27587.324030] mmcblk0: mmc0:00c3       475 MiB
[27587.324687]  mmcblk0: p1
[28479.035485]  mmcblk0: p1 p2

下面多了这么几行:

[27587.323780] mmc0: new SD card at address 00c3
[27587.324030] mmcblk0: mmc0:00c3       475 MiB
[27587.324687]  mmcblk0: p1
[28479.035485]  mmcblk0: p1 p2

打完收工!

注意:在 ubuntu 下格式化好 FAT 和 EXT3 双分区后,FAT 分区需要在 window 下重新
格式化一次,否则可能会出现无法从 SD 卡启动的情况.

另外,有时候SD卡分区被搞得很乱,或者有很多个分区,所以在清除已有分区时,一定要清除干净。比如我的一个SD卡,由于第一次分区不成功,导致里面余留了很多很奇怪的信息,其实就是分区表被破坏了,所以必须清除干净。

hyh@hyh-laptop:/media$ sudo fdisk /dev/mmcblk0
[sudo] password for hyh:

WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
switch off the mode (command ‘c’) and change display units to
sectors (command ‘u’).

Command (m for help): p

Disk /dev/mmcblk0: 498 MB, 498860032 bytes
4 heads, 16 sectors/track, 15224 cylinders
Units = cylinders of 64 * 512 = 32768 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x6f20736b

This doesn’t look like a partition table
Probably you selected the wrong device.

Device Boot Start End Blocks Id System
/dev/mmcblk0p1 ? 12158374 29994462 570754815+ 72 Unknown
Partition 1 does not end on cylinder boundary.
/dev/mmcblk0p2 ? 2635774 32886216 968014120 65 Novell Netware 386
Partition 2 does not end on cylinder boundary.
/dev/mmcblk0p3 ? 29216898 59467339 968014096 79 Unknown
Partition 3 does not end on cylinder boundary.
/dev/mmcblk0p4 ? 45088769 45089636 27749+ d Unknown
Partition 4 does not end on cylinder boundary.

Partition table entries are not in disk order

Command (m for help):

够乱吧?4 heads, 16 sectors/track, 15224 cylinders、四个分区。。。

那就先清了吧,重做一下SD卡!

删除第一分区:

Command (m for help): d
Partition number (1-4): 1

显示看看

Command (m for help): p

Disk /dev/mmcblk0: 498 MB, 498860032 bytes
4 heads, 16 sectors/track, 15224 cylinders
Units = cylinders of 64 * 512 = 32768 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x6f20736b

This doesn’t look like a partition table
Probably you selected the wrong device.

Device Boot Start End Blocks Id System
/dev/mmcblk0p2 ? 2635774 32886216 968014120 65 Novell Netware 386
Partition 2 does not end on cylinder boundary.
/dev/mmcblk0p3 ? 29216898 59467339 968014096 79 Unknown
Partition 3 does not end on cylinder boundary.
/dev/mmcblk0p4 ? 45088769 45089636 27749+ d Unknown
Partition 4 does not end on cylinder boundary.

由于分区表被破坏,没法对分区表做正确操作,还是先把heads和簇修正。

进入专家模式,重建分区表:

Command (m for help):
Command (m for help): x

Expert command (m for help): h
Number of heads (1-256, default 4): 255

Expert command (m for help): s
Number of sectors (1-63, default 16): 63
Warning: setting sector offset for DOS compatiblity

Expert command (m for help): c
Number of cylinders (1-1048576, default 15224): 60

Expert command (m for help): r

退出专家模式,打印看看:

Command (m for help): p

Disk /dev/mmcblk0: 498 MB, 498860032 bytes
255 heads, 63 sectors/track, 60 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x6f20736b

This doesn’t look like a partition table
Probably you selected the wrong device.

Device Boot Start End Blocks Id System
/dev/mmcblk0p2 ? 10501 131013 968014120 65 Novell Netware 386
Partition 2 has different physical/logical beginnings (non-Linux?):
phys=(288, 115, 43) logical=(10500, 111, 30)
Partition 2 has different physical/logical endings:
phys=(367, 114, 50) logical=(131012, 158, 28)
Partition 2 does not end on cylinder boundary.
/dev/mmcblk0p3 ? 116395 236907 968014096 79 Unknown
Partition 3 has different physical/logical beginnings (non-Linux?):
phys=(366, 32, 33) logical=(116394, 188, 12)
Partition 3 has different physical/logical endings:
phys=(357, 32, 43) logical=(236906, 234, 25)
Partition 3 does not end on cylinder boundary.
/dev/mmcblk0p4 ? 179626 179629 27749+ d Unknown
Partition 4 has different physical/logical beginnings (non-Linux?):
phys=(372, 97, 50) logical=(179625, 87, 47)
Partition 4 has different physical/logical endings:
phys=(0, 10, 0) logical=(179628, 203, 42)
Partition 4 does not end on cylinder boundary.

Command (m for help):

255 heads, 63 sectors/track, 60 cylinders,一切正常。

手工清除所有分区:

Command (m for help): p

Disk /dev/mmcblk0: 498 MB, 498860032 bytes
255 heads, 63 sectors/track, 60 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x6f20736b

This doesn’t look like a partition table
Probably you selected the wrong device.

Device Boot Start End Blocks Id System
/dev/mmcblk0p1 * 1 6 48163+ c W95 FAT32 (LBA)
/dev/mmcblk0p2 ? 10501 131013 968014120 65 Novell Netware 386
Partition 2 has different physical/logical beginnings (non-Linux?):
phys=(288, 115, 43) logical=(10500, 111, 30)
Partition 2 has different physical/logical endings:
phys=(367, 114, 50) logical=(131012, 158, 28)
Partition 2 does not end on cylinder boundary.
/dev/mmcblk0p3 ? 116395 236907 968014096 79 Unknown
Partition 3 has different physical/logical beginnings (non-Linux?):
phys=(366, 32, 33) logical=(116394, 188, 12)
Partition 3 has different physical/logical endings:
phys=(357, 32, 43) logical=(236906, 234, 25)
Partition 3 does not end on cylinder boundary.
/dev/mmcblk0p4 ? 179626 179629 27749+ d Unknown
Partition 4 has different physical/logical beginnings (non-Linux?):
phys=(372, 97, 50) logical=(179625, 87, 47)
Partition 4 has different physical/logical endings:
phys=(0, 10, 0) logical=(179628, 203, 42)
Partition 4 does not end on cylinder boundary.

看看上面有很多分区,分区太多,分区表可能在Windows下格式化的时候就被破坏了。

一个一个的收工删除:

Command (m for help): d
Partition number (1-4): 1

Command (m for help): d
Partition number (1-4): 2

Command (m for help): p

Disk /dev/mmcblk0: 498 MB, 498860032 bytes
255 heads, 63 sectors/track, 60 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x6f20736b

This doesn’t look like a partition table
Probably you selected the wrong device.

Device Boot Start End Blocks Id System
/dev/mmcblk0p3 ? 116395 236907 968014096 79 Unknown
Partition 3 has different physical/logical beginnings (non-Linux?):
phys=(366, 32, 33) logical=(116394, 188, 12)
Partition 3 has different physical/logical endings:
phys=(357, 32, 43) logical=(236906, 234, 25)
Partition 3 does not end on cylinder boundary.
/dev/mmcblk0p4 ? 179626 179629 27749+ d Unknown
Partition 4 has different physical/logical beginnings (non-Linux?):
phys=(372, 97, 50) logical=(179625, 87, 47)
Partition 4 has different physical/logical endings:
phys=(0, 10, 0) logical=(179628, 203, 42)
Partition 4 does not end on cylinder boundary.

Command (m for help): d
Partition number (1-4): 3

Command (m for help): d
Selected partition 4

Command (m for help): 4
4: unknown command
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition’s system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

Command (m for help): p

Disk /dev/mmcblk0: 498 MB, 498860032 bytes
255 heads, 63 sectors/track, 60 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x6f20736b

Device Boot Start End Blocks Id System

Command (m for help):

没有了,看看输出,与第八步一致。可以开始第九步,开始分区了。

新建一个分区:

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-60, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-60, default 60): +5

将新分区改为与DOS兼容

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): c
Changed system type of partition 1 to c (W95 FAT32 (LBA))

Command (m for help):

将刚刚分的第一分区设置为启动盘:

Command (m for help): a
Partition number (1-4): 1

Command (m for help):

添加第二个分区:Linux分区:

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (7-60, default 7):
Using default value 7
Last cylinder, +cylinders or +size{K,M,G} (7-60, default 60):
Using default value 60

打印看看:

Command (m for help): p

Disk /dev/mmcblk0: 498 MB, 498860032 bytes
255 heads, 63 sectors/track, 60 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x6f20736b

Device Boot Start End Blocks Id System
/dev/mmcblk0p1 * 1 6 48163+ c W95 FAT32 (LBA)
/dev/mmcblk0p2 7 60 433755 83 Linux

Command (m for help):

OK了,保存工作成功:

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: If you have created or modified any DOS 6.x
partitions, please see the fdisk manual page for additional
information.
Syncing disks.
hyh@hyh-laptop:/media$

格式化一下第一分区,命名为boot:

hyh@hyh-laptop:/media$ sudo mkfs.msdos -F 32 /dev/mmcblk0p1 -n boot
[sudo] password for hyh:
mkfs.msdos 3.0.7 (24 Dec 2009)

格式化第二分区,命名为Linux:

hyh@hyh-laptop:/media$ sudo mkfs.ext3 -L Linux /dev/mmcblk0p2
mke2fs 1.41.11 (14-Mar-2010)
文件系统标签=Linux
操作系统:Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
108544 inodes, 433752 blocks
21687 blocks (5.00%) reserved for the super user
第一个数据块=1
Maximum filesystem blocks=67633152
53 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

正在写入inode表: 完成
Creating journal (8192 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
hyh@hyh-laptop:/media$

退出,到Windows下再格式化一下第一分区。
我第一次就是没做这一步就直接往里面考东西了,启动时没成功。本以为在Windows下格式化一下第一分区,再重新把东西拷贝进去不就行了吗?结果格式化时根本没看到第一分区(上次是LABEL,这次是boot),看那容量,应该是第二分区,把第二分区给干了。。。

Category : DevKit8000Linux/UnixOMAP3530开发板

Tags :

发表评论

电子邮件地址不会被公开。

Proudly powered by WordPress and Sweet Tech Theme