安装grub到U盘

在尝试将GRUB安装到U盘过程中遇到错误,由于vfat格式不支持,导致安装失败。成功案例是将U盘分为2个区,其中一个为ext2格式,GRUB安装在此区,作为可引导区。然而,这并不保证能启动所有硬盘中的Linux系统。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

失败案例

将U盘分为1个区,文件系统格式为vfat格式

1、查看计算机存储设备信息

[root@localhost ~]# fdisk -l
...
Disk /dev/sdb: 4008 MB, 4008706048 bytes
255 heads, 63 sectors/track, 487 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb4   *           1         488     3914689    c  W95 FAT32 (LBA)
Partition 4 has different physical/logical endings:
     phys=(486, 254, 63) logical=(487, 91, 53)

[root@localhost ~]# ls /dev/sd* | grep "sdb"
/dev/sdb
/dev/sdb4
这里U盘是/dev/sdb,只有一个分区/dev/sdb4

2、给U盘分区
[root@localhost ~]# fdisk /dev/sdb

Command (m for help): m
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): d        # delete the old partition(s)
Selected partition 4

Command (m for help): n        # create new partition(s)
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-487, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-487, default 487):
Using default value 487

Command (m for help): w        # 提交修改并退出
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

3、格式化分区
The kernel still uses the old table.
The new table will be used at the next reboot.
所以现卸载U盘:在桌面右键点击“删除文件卷”
[root@localhost ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sda4  /dev/sda5  /dev/sda6  /dev/sda7  /dev/sda8    # no /dev/sdb

插上U盘
[root@localhost ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sda4  /dev/sda5  /dev/sda6  /dev/sda7  /dev/sda8  /dev/sdb  /dev/sdb1

格式化分区
[root@localhost ~]# mkfs.vfat /dev/sdb1
mkfs.vfat 2.11 (12 Mar 2005)
mkfs.vfat: /dev/sdb1 contains a mounted file system.
此时分区已经被挂载,要格式化应先卸载分区

卸载分区,然后格式化分区
[root@localhost ~]# umount /dev/sdb1
[root@localhost ~]# mkfs.vfat /dev/sdb1
mkfs.vfat 2.11 (12 Mar 2005)


4、激活可引导分区(否则不能引导)
[root@localhost ~]# fdisk /dev/sdb

Command (m for help): a
Partition number (1-4): 1    # /dev/sdb1作为可引导分区

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

Calling ioctl() to re-read partition table.
Syncing disks.



[root@localhost ~]# df -l
文件系统               1K-块        已用     可用 已用% 挂载点
/dev/sda7             40631956   3154200  35380472   9% /
tmpfs                  1036808         0   1036808   0% /dev/shm
/dev/sdb1              3904152         4   3904148   1% /media/disk
/dev/sda5             52427180  33302524  19124656  64% /mnt


[root@localhost ~]# grub-install --root-directory=/media/disk --no-floppy /dev/sdb1
The file /media/disk/boot/grub/stage1 not read correctly.

错误:The file /media/disk/boot/grub/stage1 not read correctly.

出现这个问题的原因是/dev/sdb1的格式为vfat格式,这里不支持将 grub 安装在 vfat 格式的分区中


成功案例

将U盘分为2个区,第一个区为vfat格式,第二个区为ext2格式,grub安装到第二个区,将第二个区作为可引导区

1、分区
[root@localhost ~]# fdisk /dev/sdb

Command (m for help): d
Selected partition 1

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-487, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-487, default 487): +3072M

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (375-487, default 375):
Using default value 375
Last cylinder or +size or +sizeM or +sizeK (375-487, default 487):
Using default value 487

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

2、格式化分区
卸载U盘:在桌面右键点击“删除文件卷”
插上U盘
[root@localhost ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sda4  /dev/sda5  /dev/sda6  /dev/sda7  /dev/sda8  /dev/sdb  /dev/sdb1  /dev/sdb2

[root@localhost ~]# umount /dev/sdb1
[root@localhost ~]# umount /dev/sdb2
[root@localhost ~]# mkfs.vfat /dev/sdb1
mkfs.vfat 2.11 (12 Mar 2005)
[root@localhost ~]# mkfs.ext2 /dev/sdb2
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
113568 inodes, 226918 blocks
11345 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=234881024
7 block groups
32768 blocks per group, 32768 fragments per group
16224 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840

Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.


3、激活/dev/sdb2
[root@localhost ~]# fdisk /dev/sdb

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

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

Calling ioctl() to re-read partition table.
Syncing disks.

[root@localhost ~]# fdisk -l
...
Disk /dev/sdb: 4008 MB, 4008706048 bytes
255 heads, 63 sectors/track, 487 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         374     3004123+  83  Linux
/dev/sdb2   *         375         487      907672+  83  Linux        # 可引导分区

4、安装grub到U盘
查看U盘各分区的挂载情况
[root@localhost ~]# df
...
/dev/sdb2               893388       904    847104   1% /media/disk
/dev/sdb1              2998248         4   2998244   1% /media/disk-1

[root@localhost ~]# grub-install --root-directory=/media/disk --no-floppy /dev/sdb2
Probing devices to guess BIOS drives. This may take a long time.
Installation finished. No error reported.
This is the contents of the device map /media/disk/boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.

(hd0)   /dev/sda
(hd1)   /dev/sdb

5、安装到U盘的MBR
[root@localhost ~]# grub
    GNU GRUB  version 0.97  (640K lower / 3072K upper memory)

 [ Minimal BASH-like line editing is supported.  For the first word, TAB
   lists possible command completions.  Anywhere else TAB lists the possible
   completions of a device/filename.]

grub> root (hd1,
 Possible partitions are:
   Partition num: 0,  Filesystem type unknown, partition type 0x83
   Partition num: 1,  Filesystem type is ext2fs, partition type 0x83

grub> root (hd1,1)
 Filesystem type is ext2fs, partition type 0x83

grub> setup (hd1)
 Checking if "/boot/grub/stage1" exists... yes
 Checking if "/boot/grub/stage2" exists... yes
 Checking if "/boot/grub/e2fs_stage1_5" exists... yes
 Running "embed /boot/grub/e2fs_stage1_5 (hd1)"...  15 sectors are embedded.
succeeded
 Running "install /boot/grub/stage1 (hd1) (hd1)1+15 p (hd1,1)/boot/grub/stage2 /boot/grub/grub.conf"... succeeded
Done.

grub> quit

应用:用 U 盘 grub 启动硬盘中的 Linux

注意:上述方案并不一定能启动硬盘中的 Linux, 笔者测试的一个U盘就不行

未知品牌 U 盘(4G)
分区1分区2能否启动硬盘中的 Linux
vfat(3G)ext2(grub)不能
ext2(grub, 512M)vfat

Kingston(4G)
分区1分区2能否启动硬盘中的 Linux
ext2(grub, 512M)vfat
ext2


参考资料

Booting from USB, http://wiki.linuxquestions.org/wiki/Booting_from_USB

安装GRUB到U盘,http://bbs.chinaunix.net/thread-2295595-1-1.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值