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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

工作中经常会遇到将备份的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&#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值