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

************************************************************
虚拟设备创建多分区虚拟磁盘(软盘,硬盘)
************************************************************
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 imgFile
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 img2
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 img2
/home/hjj/temp/img2: msdos partitions 1 2 3 4 <>


----------------------------------------
3. 格式化磁盘
----------------------------------------
就是往对应的分区写点东西,登记可使用的资源,不同的管理对应不同的文件系统
$ sudo losetup -f
/dev/loop0
........................................
### 找一个可用的设备
........................................
$ sudo kpartx -av /dev/loop0
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
........................................
### 把各个分区都映射到子设备
........................................
$ 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

你可能感兴趣的:(linux,基础知识)