Shell脚本"mountimg",将磁盘映像文件挂载到目录。

在学习bochs模拟器的使用过程中,为了能直接和其磁盘映像文件交换文件,其中一种方法是先用 losetup 命令将映像联结到回环设备,再用 mount 命令挂载, 卸载时再用相反的步骤(见: bochs的使用)。为了让操作更简便,我自己写了一个Shell脚本,这是我写的第一个Shell脚本,让我体会到了Linux过滤器的组合乐趣,我把代码贴出来,和大家分享,也请大虾们指点指点。

# !/bin/bash
#
    #filename:mountimg
#
set up and mount image file with loop devices
#
Author: Hawkxp
#
Date  : 2006/8/27
#

ERR_USAGE
= " The usage is wrong! Please try "--help" option. "

if  [ $ #  -gt 3 ]
then
    
echo   - " $ERR_USAGE "
    
exit   1
fi

opt
= ''
opt_img
= ''
opt_dir
= ''

for  arg in  " $@ "
do
    
if  [  " ${arg:0:1} "   =   " - "  ]
    then
        opt
= $arg
    elif [ 
- $arg   - - " $opt_img "  ]
    then
        opt_img
= $arg
    elif [ 
- $arg   - - " $opt_dir "  ]
    then
        opt_dir
= $arg
    fi
done

if  [  - " $opt "   - - " $opt_img "   - - " $opt_dir "  ]
then
    
if  [ `ls  - $opt_img   |  awk  ' {print $5} ' =   " 1474560 "  ]
    then
        opt
= ' -o512 '
    
else
        opt
= ' -o32256 '
    fi
fi

mntimg_list(){
    local lodev
= ''

    
for  lodev in `mount  |  grep  ' /dev/loop '   |  awk  ' {print $1} ' `
    
do
        
echo   - en  " ${lodev} "
        
echo   - en  " `mount | grep $lodev | awk '{print $3}'` "
        
echo   " `losetup $lodev | cut -f 2 -d '(' | cut -f 1 -d ')'` "
    
done

    
if  [  - " $lodev "  ]
    then
        
echo   " none is mounted yet. "
    fi
}

mntimg_mount(){
    local newdev
= `losetup  - f`
    
if  [  " ${newdev%?} "   !=   " /dev/loop "  ]
    then
        
echo   " no more loop device, Please umount one first! "
        
exit   1
    fi

    
if  [  - n `mount  |  grep  " $3"` ]
    then
        echo 
" Directory $ 3  was mounted ! "
        exit 1
    fi
    
    if [ -n 
" `losetup  - o $ 1   $newdev  $ 2 ` "  ]
    then
        echo 
" failed on set up to loop device . "
        exit 1
    fi

    if [ -n 
" `mount  - o loop  $newdev  $ 3 ` "  ]
    then
        echo 
" failed on mount to ${ 3 } . "
        losetup -d $newdev
        exit 1
    fi

    echo 
" success mount . "
}

mntimg_umount(){
    local lodev=''
    local no=0
    if [ -d $1 ]
    then
        lodev=`mount | grep 
" ${ 1 %/ } "  | awk '{print $1}'`
    else
        until [ $no -eq 8 ]
        do
            lodev=`losetup /dev/loop$no 2>/dev/null | grep 
" ${ 1 # #*/}" | awk '{print $1}'`
             if  [  - " $lodev "  ]
            then
                lodev
= ${lodev %: }
                no
= 8
            
else
                no
= $((  $no + 1  ))
            fi
        
done
    fi

    
if  [  - " $lodev "  ]
    then
        
echo   " not mounted yet. "
        
exit   1
    fi

    umount 
$lodev
    losetup 
- $lodev

    
echo   " umount success "
}

case   " $opt "  in
    
-- help  |   - h)
        
echo   " usage: "
        
echo   "   mountimg {--help | -h}            :print this help "
        
echo   "   mountimg {--list | -l}            :list info of the mounted image "
        
echo   "   mountimg [-o{offset}]  img  dir   :mount image to directory "
        
echo   "   mountimg {--umount|-u} {img|dir}  :umount "
        
exit   1
        ;;
    
-- list   |   - l)
        mntimg_list
        ;;
    
- o * )
        
if  [  - " $opt_img "  ]
        then
            
echo   " Image file is not found. "
            
echo   - $ERR_USAGE
            
exit   1
        fi
        
if  [  - " $opt_dir "  ]
        then
            
echo   " Directory is not found. "
            
echo   - $ERR_USAGE
            
exit   1
        fi
        mntimg_mount ${opt
# -o} $opt_img $opt_dir
        ;;
    
-- umount  |   - u)
        
if  [  - " $opt_img "   - - " $opt_dir "  ]
        then
            
echo   " Image or directory is not found. "
            
echo   - $ERR_USAGE
            
exit   1
        elif [ 
- " $opt_dir "  ]
        then
            mntimg_umount 
$opt_dir
        
else
            mntimg_umount 
$opt_img
        fi
        ;;
    
* )
        
echo   - $ERR_USAGE
        ;;
esac

 

将脚本文件保存为 ~/bin/mountimg 中,执行命令 “chmod a+x ~/bin/mountimg" 给文件加上执行属性,就可以使用了,输入 ”mountimg --help" 查看使用帮助:


mountimg  --list                                            :查看当前已挂载的映像列表

mountimg  [-o{offset}]  img  dir                  :将映像文件(img)挂载到目录(dir),

                                                                            -o是可选项,后跟映像分区的起始地址(512B × 扇区数)

                                                                            注意和“-o”间无空格。默认为第一分区

mountimg  --umount  {img | dir}                :卸载指定的已挂载映像


使用的参考资料:Shell编程系列

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值