工作中经常会遇到将备份的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"
bname=$(basename $line)
imagePath="$dirName/$bname"
mntDirPath="/mnt/$bname"
if [[ $bname == *".vhd" || $bname == *"-do
Linux批量装载与卸载raw、qcow2镜像的shell脚本

本文介绍了如何在Linux环境中通过shell脚本批量装载和卸载raw、qcow2等类型的系统镜像。首先,需要加载nbd驱动。然后,提供了一个脚本,根据文件格式(默认为raw,可通过file命令确认)将镜像装载到/mnt目录下。完成使用后,可以通过指定挂载目录来批量卸载这些镜像。
最低0.47元/天 解锁文章
454

被折叠的 条评论
为什么被折叠?



