虚拟设备创建多分区虚拟磁盘(软盘,硬盘)

************************************************************
虚拟设备创建多分区虚拟磁盘(软盘,硬盘)
************************************************************
author: hjjdebug
date: 2015年 05月 09日 星期六 09:52:17 CST
----------------------------------------
1. 首先创建一个 1.44M 大小的空文件
----------------------------------------
利用 /dev/zero 设备和dd 命令
$ dd if=/dev/zero of=img1M bs=512 count=2880
dd 只关心 bs * count, 所以它与bs=1024 count=1440的影像完全一致

----------------------------------------
2. 将磁盘镜象分区
----------------------------------------
$ fdisk img1M
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xbdb75e35.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
You must set cylinders.
You can do this from the extra functions menu.

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

Syncing disks.

### 出现了一个警告,我直接按w, 看存储的img, 知道它写了一个disklabel 0xbdb75e35,dos 标签
### 同时在第一扇区末尾写了0x55AA标记

b. 分区
hjj@hjj-Inspiron:~/temp$ fdisk img1M
You must set cylinders.
You can do this from the extra functions menu.
###这个警告用fdisk 无法消除,可能fdisk 根本不修改磁盘参数,不用跟它较劲了。


通过new partition 命令,创建4个分区,如下图是。
Command (m for help): p

Disk img2: 1 MB, 1474560 bytes
255 heads, 63 sectors/track, 0 cylinders, total 2880 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: 0xbdb75e35

Device Boot      Start         End      Blocks   Id  System
img2p1               1         500         250   83  Linux
img2p2             501        1000         250   83  Linux
img2p3            1001        1500         250   83  Linux
img2p4            1501        2879         689+   5  Extended

通过2进制工具,我们知道,它在1扇区做了记录。
$ partprobe -s img1M
/home/hjj/temp/img2: msdos partitions 1 2 3 4 <>


----------------------------------------
3. 格式化磁盘
----------------------------------------
就是往对应的分区写点东西,登记可使用的资源,不同的管理对应不同的文件系统
$ sudo losetup -f
/dev/loop0
........................................
### 找一个可用的设备
........................................
$ sudo kpartx -av img1M
add map loop0p1 (252:0): 0 500 linear /dev/loop0 1
add map loop0p2 (252:1): 0 500 linear /dev/loop0 501
add map loop0p3 (252:2): 0 500 linear /dev/loop0 1001
add map loop0p4 (252:3): 0 2 linear /dev/loop0 1501
........................................
### 该命令把img1M各个分区都映射到子设备,在/dev/mapper下创建了对应设备 当需要解除映射时,可用下面命令 sudo kpartx -av -d img1M

##
........................................
$ ls /dev/mapper/
control  loop0p1  loop0p2  loop0p3  loop0p4  

........................................
### 格式化为不同的文件系统
........................................
hjj@hjj-Inspiron:~/temp$ sudo mkfs.msdos /dev/mapper/loop0p1
mkfs.fat 3.0.26 (2014-03-07)
unable to get drive geometry, using default 255/63
........................................
### 格式化为不同的文件系统
........................................
hjj@hjj-Inspiron:~/temp$ sudo mkfs.ext2 /dev/mapper/loop0p2
mke2fs 1.42.9 (4-Feb-2014)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
32 inodes, 248 blocks
12 blocks (4.84%) reserved for the super user
First data block=1
1 block group
8192 blocks per group, 8192 fragments per group
32 inodes per group

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

........................................
### 格式化为不同的文件系统
........................................
hjj@hjj-Inspiron:~/temp$ sudo mkfs.msdos /dev/mapper/loop0p3
mkfs.fat 3.0.26 (2014-03-07)
unable to get drive geometry, using default 255/63
hjj@hjj-Inspiron:~/temp$ sudo mkfs.ext2 /dev/mapper/loop0p4
mke2fs 1.42.9 (4-Feb-2014)
mkfs.ext2: inode_size (128) * inodes_count (0) too big for a
    filesystem with 0 blocks, specify higher inode_ratio (-i)
    or lower inode count (-N).

hjj@hjj-Inspiron:~/temp$ sudo mkfs.ext2 /dev/mapper/loop0p
loop0p1  loop0p2  loop0p3  loop0p4  
hjj@hjj-Inspiron:~/temp$ sudo mkfs.ext2 /dev/mapper/loop0p4
mke2fs 1.42.9 (4-Feb-2014)
mkfs.ext2: inode_size (128) * inodes_count (0) too big for a
    filesystem with 0 blocks, specify higher inode_ratio (-i)
    or lower inode count (-N).

hjj@hjj-Inspiron:~/temp$ sudo mkfs.msdos /dev/mapper/loop0p4
mkfs.fat 3.0.26 (2014-03-07)
unable to get drive geometry, using default 255/63
mkfs.msdos: Attempting to create a too large filesystem
hjj@hjj-Inspiron:~/temp$

第4个分区没分好,重新分第4,要留点余数就可以了,default 最大值有问题.

----------------------------------------
4. 使用
----------------------------------------
现在可以分别向不同分区copy 东西了, 先mount, 再copy
$  sudo mount /dev/mapper/loop0p1 /mnt/m1
$  sudo mount /dev/mapper/loop0p2 /mnt/m2
$  sudo mount /dev/mapper/loop0p3 /mnt/m3
$  sudo mount /dev/mapper/loop0p4 /mnt/m4

补充:

硬盘分区表MBR 结构: 4个区, 0x1be开始,0x1fd结束
每个区结构: 16bytes
1. Boot signature    (1byte) 00,非活动分区;80,活动分区,其它无意义
2. Start head        (1byte)
3. Start sector        (低位6bits)
4. Start cylinder    (10 bits)
5. System signature (1byte) 文件系统标志位
6. End head            (1byte)
7. End sector        (6 bits)
8. End cylinder        (10 bits)
9. No. of sectors before the partition    (4bytes) 相对扇区号
10.No. of sector in the partion            (4bytes) 总的扇区数

通过实例研判,对于文件而言柱面,磁头,扇区意义不大了,但相对扇区号和总的扇区数还是有意义的.代表了数据位置.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值