在Linux live system中创建loop设备并挂载镜像文件


一般在live system中,尤其是mini live system,在制作的时候作者为了把系统容量压到最低,会把他认为不常用到的东西剔除以减少系统体重.这里要讨论的是如果你的live system的内核中没有编译loop设备模块该如何创建他.

1.一般来说如果Linux系统内核没有loop模块的时候,当你使用"mount -o loop"挂载某个镜像文件的时候,通常会出现如下错误
Error message "couldn't find any free loop device"

2.这时你要使用iso文件需要以下几个步骤
# 首先创建一个新的loop设备
mknod /dev/loop/300 b 7 300

# 然后将你的要挂载的镜像文件(如iso,img)分配给刚才创建的loop设备
losetup /dev/loop/300 your_loop_file.iso

# 最后挂载你的loop设备(事实上是你的镜像文件)到你的挂载点即可
mount /dev/loop/300 /your_mount_point/

# 如果要卸载,使用如下命令
umount /your_mount_point/
losetup -d /dev/loop/300

如果你想以后自动创建loop设备并挂载镜像,可以自己写个脚本,我的脚本如下:
#!/bin/bash
#Usage:$1 is your iso files,$2 is your mountpoint.

if [ -z $1 ];then
echo 'Image file is not specified.'
echo 'Usage: [loop_mount.sh ]'
exit 0
elif [ ! -f $1 ];then
echo 'Image file is not found'
exit 0
fi

if [ -z $2 ];then
echo 'Mount point is not specified.'
echo 'Usage: [loop_mount.sh ]'
exit 0
elif [ ! -d $2 ];then
echo 'Mount point is not created.'
exit 0
fi

# create a new loop device
$(mknod /dev/loop/loop300 b 7 300)

NEW_LOOP="/dev/loop/loop300"

# assign your loop file to created loop device
losetup $NEW_LOOP $1

# mount the loop device insetad of the file
mount $NEW_LOOP $2

相关内容