工作中经常会遇到将备份的raw、qcow2等系统镜像装载到文件系统中的需求。方便起见,特写了批量装/卸载的shell脚本。
一、装载之前,需要加载nbd驱动
modprobe nbd max_part=8 #加载nbd驱动
二、批量装载
在运行下列代码时,需要知道镜像的文件格式,可以采用file xxx命令获取文件信息,默认是raw格式
默认装载到/mnt/目录下
#!/bin/bash
#modprobe nbd max_part=8 #we should run this code before mounting images
#file xxxx # we need run this command to know the file type of image
if [ -z "$1" ]; then echo "usage: ./xx.sh imagesDirPath [imageType(e.g., raw, qcow2)]" & exit 0; fi
dirName=$1
idx=0
ftype="raw"
if [ -n "$2" ]; then ftype=$2; fi
#echo $ftype
#ls $dirName
if [ ! -e "/dev/nbd0" ]; then modprobe nbd max_part=8; fi
ls $dirName | while read line
do
{
nbdDevName="/dev/nbd${idx}"
if [ ! -e $nbdDevName ]; then continue; fi
nbdDevPart="/dev/nbd${idx}p1&#