在Linux中批量装载raw、qcow2等镜像

工作中经常会遇到将备份的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&#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值