在ubuntu下创建一个文件作为硬盘

  • 创建一个大小为4G的文件
$ dd if=/dev/zero of=mmc.bin bs=$((1024*1024)) count=$((4 * 1024))
  • 使用fdisk/cfdisk分区

将分成5个区:2个主分区和3个逻辑分区:

$ fdisk mmc.bin
Command(m for help): o
# 创建第一个分区:
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-8388607, default 2048):     
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-8388607, default 8388607): +100M

# 创建第二个分区:
Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (1-4, default 2): 
Using default value 2
First sector (206848-8388607, default 206848): 
Using default value 206848
Last sector, +sectors or +size{K,M,G} (206848-8388607, default 8388607): +400M
# 创建扩展分区:
Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): e
Partition number (1-4, default 3): 3
First sector (1026048-8388607, default 1026048): 
Using default value 1026048
Last sector, +sectors or +size{K,M,G} (1026048-8388607, default 8388607): 
# 创建逻辑分区:
Command (m for help): n
Partition type:
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (1028096-8388607, default 1028096): 
Using default value 1028096
Last sector, +sectors or +size{K,M,G} (1028096-8388607, default 8388607): +400M
  • 最终的分区表如下:
Command (m for help): p

Disk mmc.bin: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders, total 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x9f922a9a

  Device Boot      Start         End      Blocks   Id  System
mmc.bin1            2048      206847      102400   83  Linux
mmc.bin2          206848     1026047      409600   83  Linux
mmc.bin3         1026048     8388607     3681280    5  Extended
mmc.bin5         1028096     1847295      409600   83  Linux
mmc.bin6         1849344     2668543      409600   83  Linux
mmc.bin7         2670592     8388607     2859008   83  Linux

可以看到扇区的大小为512字节,每个分区的起始位置和大小。

 

  • 对分区进行挂载和格式化
$ sudo losetup -o $((2048*512)) --sizelimit $((102400*512)) -f --show mmc.bin
/dev/loop0
#可以看到是挂载到了/dev/loop0上了, 下面对它进行格式化操作:
$ sudo mkfs.vfat -n boot /dev/loop0
mkfs.fat 3.0.26 (2014-03-07)
mkfs.fat: warning - lowercase labels might not work properly with DOS or Windows
Loop device does not match a floppy size, using default hd params
# 卸载,其他的分区也采用同样的操作
$ sudo losetup -d /dev/loop0
$ sudo losetup -o $((206848*512)) --sizelimit $((409600*512)) -f --show mmc.bin
/dev/loop0
 sudo mkfs.ext4 -L root /dev/loop0
mke2fs 1.42.9 (4-Feb-2014)
Discarding device blocks: done                            
Filesystem label=root
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729

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

$ sudo losetup -d /dev/loop0
$ sudo losetup -o $((1028096*512)) --sizelimit $((409600*512)) -f --show mmc.bin
/dev/loop0
$ sudo mkfs.ext4 -L system /dev/loop0
mke2fs 1.42.9 (4-Feb-2014)
Discarding device blocks: done                            
Filesystem label=system
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done 
  • 验证:

使用brilloemulator-arm @ brillo-m8-release进行验证:

$ brilloemulator-arm -m 256 -- -sd mmc.bin
# 可以看到在kernel log中有如下信息:
mmc0: new SDHC card at address 4567
mmcblk0: mmc0:4567 QEMU! 4.00 GiB 
 mmcblk0: p1 p2 p3 < p5 p6 p7 >

在去挂载/dev/block/mmcblk0p2的时候,会出现如下信息:

# mount /dev/block/mmcblk0p2 /mnt/                                         
EXT3-fs (mmcblk0p2): error: couldn't mount because of unsupported optional features (240)
EXT2-fs (mmcblk0p2): error: couldn't mount because of unsupported optional features (240)
EXT4-fs (mmcblk0p2): Filesystem with huge files cannot be mounted RDWR without CONFIG_LBDAF

由于kernel没有CONFIG_LBDAF而mkfs.ext4默认是enable的, 分区挂载失败了。可以通过下面的方法解决:

$ sudo losetup -o $((206848*512)) --sizelimit $((409600*512)) -f --show mmc.bin
/dev/loop0

$ sudo tune2fs -O ^huge_file /dev/loop0
tune2fs 1.42.9 (4-Feb-2014)

Please run e2fsck on the filesystem.

$ sudo fsck.ext4 /dev/loop0 
e2fsck 1.42.9 (4-Feb-2014)
root was not cleanly unmounted, check forced.
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
root: 11/51200 files (0.0% non-contiguous), 12109/204800 blocks

$ sudo losetup -d /dev/loop0

再去做挂载的时候,CONFIG_LBDAF相关的信息已经没有了,分区可正常挂载:

# mount /dev/block/mmcblk0p2 /mnt/
EXT3-fs (mmcblk0p2): error: couldn't mount because of unsupported optional features (240)
EXT2-fs (mmcblk0p2): error: couldn't mount because of unsupported optional features (240)
EXT4-fs (mmcblk0p2): mounted filesystem with ordered data mode. Opts: (null)
# mount
/dev/root on / type ext4 (ro,seclabel,relatime,data=ordered)
tmpfs on /dev type tmpfs (rw,seclabel,nosuid,relatime,mode=755)
devpts on /dev/pts type devpts (rw,seclabel,relatime,mode=600)
proc on /proc type proc (rw,relatime,gid=3009,hidepid=2)
sysfs on /sys type sysfs (rw,seclabel,relatime)
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
none on /acct type cgroup (rw,relatime,cpuacct)
none on /sys/fs/cgroup type tmpfs (rw,seclabel,relatime,mode=750,gid=1000)
tmpfs on /mnt type tmpfs (rw,seclabel,relatime,mode=755,gid=1000)
none on /dev/cpuctl type cgroup (rw,relatime,cpu)
none on /dev/cpuset type cgroup (rw,relatime,cpuset,noprefix,release_agent=/sbin/cpuset_release_agent)
tmpfs on /storage type tmpfs (rw,seclabel,relatime,mode=755,gid=1000)
/dev/block/mmcblk0p2 on /mnt type ext4 (rw,seclabel,relatime,data=ordered)

相关的参考文档在这里:
https://kuttler.eu/post/filesystem-with-huge-files-cannot-be-mounted-read-write-without-config_lbdaf/

 

评论

Be the First to Comment!

提醒我
avatar

wpDiscuz