从头创建 SD 磁盘映像:“您必须指定文件系统类型”

从头创建 SD 磁盘映像:“您必须指定文件系统类型”

非常简短的版本:

我正在尝试创建一个磁盘映像,其中包含另一个映像中提供的 MBR/bootloader 以及一个带有 tarball 中提供的根文件系统的分区。创建映像文件、对其进行分区、使用 将其与环回关联kpartx以及应用文件系统似乎都有效,但当我尝试挂载 时/dev/mapper/loopXp1,mount 显示you must specify a filesystem type。当然,这样做没有帮助。

细节:

我已经看过大量关于如何从现有 SD 卡制作图像然后将其写入另一个 SD 卡的教程,但这意味着让我的用户下载可能与他们的 SD 卡大小不匹配的图像,其中大部分只是零或其他垃圾,因为起始文件系统不是很满。

因此,让人们下载引导加载程序的 dd 加上根文件系统的 tarball,然后运行创建所需大小的映像以供本地分发的脚本似乎是明智的。

不幸的是,尽管我可以发誓这些步骤在过去是有效的,但我仍然遇到上述问题。

这正是我正在做的事情:

#!/bin/bash
# Adapted from instructions at http://linux-sunxi.org/Bootable_SD_card

# Settings
img_fn=test.img
img_size=2 #Gigs
img_mountpoint="fs"
img_bootsect="bootsect.img"
img_rootfs="rootfs.tar.bz2"

# Start a disk image file
dd if=/dev/zero of=$img_fn bs=1024 count=10 || exit

# Apply the bootloader
dd if=$img_bootsect of=$img_fn bs=1024 seek=8 || exit

# Extend the image to the desired size
truncate -s ${img_size}G $img_fn || exit

# Create one partition that fills the "disk"
# Using '0 -0' or '0 -1' here warns produces a warning that  the
# partition "is not properly aligned for best performance."
parted -s $img_fn 'mklabel msdos mkpart primary 1 -1 print' || exit

# Associate the file with a loopback device
sudo kpartx -a $img_fn || exit

# Get the name of the loopback device
rootfs_dev=/dev/mapper/$(sudo kpartx -l $img_fn | awk '{print $1}')

# Create mountpoint and filesystem, mount the new FS
[ -e $img_mountpoint ] || mkdir -p $img_mountpoint  || exit
sudo mkfs.ext4 $rootfs_dev  || exit

###
### THINGS BREAK HERE 
### mkfs appears to succeed, but mount fails with:
### "mount: you must specify the filesystem type"
###
sudo mount $rootfs_dev $img_mountpoint  || exit

sudo tar -C $img_mountpoint -jxf $img_rootfs  || exit

sudo umount $img_mountpoint
sudo kpartx -d $img_fn
rmdir $img_mountpoint

如果有人想要的话,这是完整的bash -x输出。请注意,parted 和 mkfs 的输出都表明“磁盘”已成功分区和格式化。

如果有人能发现这里出了什么问题,我将非常感激。谢谢!

$ bash -x mkimg.sh
+ img_fn=test.img
+ img_size=2  
+ img_mountpoint=fs
+ img_bootsect=bootsect.img
+ img_rootfs=rootfs.tar.bz2
+ dd if=/dev/zero of=test.img bs=1024 count=10
10+0 records in
10+0 records out
10240 bytes (10 kB) copied, 0.000968263 s, 10.6 MB/s
+ dd if=bootsect.img of=test.img bs=1024 seek=8
254+1 records in
254+1 records out
260260 bytes (260 kB) copied, 0.067996 s, 3.8 MB/s
+ truncate -s 2G test.img
+ parted -s test.img 'mklabel msdos mkpart primary 1 -1 print'
Model:  (file)
Disk /usr/local/tunapanda/provision/x2go_mystery/test.img: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  2147MB  2146MB  primary

+ sudo kpartx -a test.img
++ sudo kpartx -l test.img
++ awk '{print $1}'
+ rootfs_dev=/dev/mapper/loop3p1
+ '[' -e fs ']'
+ sudo mkfs.ext4 /dev/mapper/loop3p1
mke2fs 1.42 (29-Nov-2011)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524287 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

+ sudo mount /dev/mapper/loop3p1 fs
mount: you must specify the filesystem type
+ exit

答案1

更新:我之前没有提供完整正确的解决方案;实际上对我有用的方法gparted也需要使用,而你不能使用。

这种方法应该有效(我测试过了);您可以根据需要调整每个步骤并将所有内容自动化到脚本中:

  1. 创建空白原始图像:(dd if=/dev/zero of=/image.img bs=1 count=100000000100MB
  2. 创建分区表和分区:sudo parted -s image.img 'mklabel msdos mkpart primary 2048s 100%'
  3. 创建文件系统:sudo mkfs.ext4 image.img

这里的关键是(我认为)这是第二步:出于某种原因,在测试期间,我无法从分区对齐不良的磁盘映像中创建可用的磁盘映像曾经。对齐分区对我来说很有帮助。

相关内容