回首页

1.建立一个img文件不分区的情况
qemu-img create -f raw video.img 1GB

losetup /dev/loop0 video.img
mkfs.ext4 /dev/loop0
losetup -d /dev/loop0

## 这是挂载整个格式化好的镜像 mount image
mount -o loop /dev/loop0 /img1

## 以后的mount操作可以简化为:
mount -o loop video.img /img1


2.建立一个img文件并且分区的情况
qemu-img create -f raw /video.img 5600M

## Make and format partition
losetup /dev/loop0 /video.img

## 分区 #
fdisk /dev/loop0

##列分区信息#
fdisk -lu /dev/loop0

## 映射分区 #
kpartx -av /dev/loop0
#add map loop0p1 (253:2): 0 9815652 linear /dev/loop0 63

## 格式化分区#
mkfs.ext4 /dev/mapper/loop0p1

mount /dev/mapper/loop0p1 /img1
umount /img1

kpartx -d /dev/loop0
losetup -d /dev/loop0

# partition should start at sector 63. Check with fdisk -ul video.img
# losetup -o 32256 /dev/loop0 video.img
# mkfs.ext3 -b 4096 -I 128 -o test /dev/loop0 261048

## 这是挂载镜像中的分区 #
#Usage: lomount [-verbose] [OPTIONS] -diskimage FILE -partition NUM [OPTIONS]
#All OPTIONS are passed through to 'mount'.
#ex. lomount -t fs-type -diskimage hda.img -partition 1 /mnt

lomount -t ext4 -diskimage /video.img -partition 1 /img1

umount /img1

参考:
http://zhigang.org/blog/accessing-data-in-raw-disk-image/

3.加密镜像

准备
yum install dmsetup cryptsetup

modprobe aes
cat /proc/crypto | grep aes
modprobe dm-crypt
lsmod dm_crypt
dmsetup targets

## 建立加密镜像 然后卸掉 #
qemu-img create -f raw /sdb1/pics.img 1G
losetup /dev/loop7 /sdb1/pics.img

cryptsetup -y create myPics /dev/loop7
Enter passphrase: 
Verify passphrase:

ls /dev/mapper/
dmsetup ls

mkfs.ext4 /dev/mapper/myPics
mount /dev/mapper/myPics /sdb1/myPics
umount /sdb1/myPics

cryptsetup remove myPics
ls /dev/mapper/
dmsetup ls

losetup -d /dev/loop7

## 重新装载(少了格式化环节) 然后卸掉 #
losetup /dev/loop7 /sdb1/pics.img
cryptsetup -y create myPics /dev/loop7
mount /dev/mapper/myPics /sdb1/myPics

umount /sdb1/myPics
cryptsetup remove myPics
losetup -d /dev/loop7


参考:
cryptsetup luksFormat /dev/loop7
cryptsetup luksOpen /dev/loop7 myPics