• 要找福利?记不住网址?搜不到?如何与我们保持联系?试试 CTRL+D 把零点博客添加进您的浏览器收藏夹,一键直达永不失联!

磁盘维护Linux losetup命令

linux系统 涛涛 124次浏览 0个评论

Linux losetup命令用于设置循环设备。

循环设备可把文件虚拟成区块设备,籍以模拟整个文件系统,让用户得以将其视为硬盘驱动器,光驱或软驱等设备,并挂入当作目录来使用。

语法

<span class="pln">losetup </span><span class="pun">[-</span><span class="pln">d</span><span class="pun">][-</span><span class="pln">e </span><span class="pun"><加密方式>][-</span><span class="pln">o </span><span class="pun"><平移数目>][循环设备代号][文件]</span>

参数

  • -d 卸除设备。
  • -e<加密方式> 启动加密编码。
  • -o<平移数目> 设置数据平移的数目。

实例

(1)创建空的磁盘镜像文件,这里创建一个1.44M的软盘

<span class="pln">$ dd </span><span class="kwd">if</span><span class="pun">=</span><span class="str">/dev/</span><span class="pln">zero of</span><span class="pun">=</span><span class="pln">floppy</span><span class="pun">.</span><span class="pln">img bs</span><span class="pun">=</span><span class="lit">512</span><span class="pln"> count</span><span class="pun">=</span><span class="lit">2880</span>

(2)使用 losetup将磁盘镜像文件虚拟成快设备

<span class="pln">$ losetup </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">loop1 floppy</span><span class="pun">.</span><span class="pln">img</span>

(3)挂载块设备

<span class="pln">$ mount </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">loop0 </span><span class="pun">/</span><span class="pln">tmp</span>

经过上面的三步之后,我们就可以通过/tmp目录,像访问真实快设备一样来访问磁盘镜像文件floppy.img。

(4) 卸载loop设备

<span class="pln">$ umount </span><span class="pun">/</span><span class="pln">tmp
$ losetup </span><span class="pun">-</span><span class="pln">d </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">loop1</span>

一个完整测试实例

1. 首先创建一个 1G 大小的空文件:

<span class="com"># dd if=/dev/zero of=loopfile.img bs=1G count=1</span>
<span class="lit">1</span><span class="pun">+</span><span class="lit">0</span><span class="pln"> records </span><span class="kwd">in</span>
<span class="lit">1</span><span class="pun">+</span><span class="lit">0</span><span class="pln"> records </span><span class="kwd">out</span>
<span class="lit">1073741824</span><span class="pln"> bytes </span><span class="pun">(</span><span class="lit">1.1</span><span class="pln"> GB</span><span class="pun">)</span><span class="pln"> copied</span><span class="pun">,</span> <span class="lit">69.3471</span><span class="pln"> s</span><span class="pun">,</span> <span class="lit">15.5</span><span class="pln"> MB</span><span class="pun">/</span><span class="pln">s</span>

2. 对该文件格式化为 ext4 格式:

<span class="com"># mkfs.ext4 loopfile.img</span>
<span class="pun">。。。。</span>

3. 用 file 命令查看下格式化后的文件类型:

<span class="com"># file loopfile.img</span><span class="pln">
loopfile</span><span class="pun">.</span><span class="pln">img</span><span class="pun">:</span> <span class="typ">Linux</span><span class="pln"> rev </span><span class="lit">1.0</span><span class="pln"> ext4 filesystem data</span><span class="pun">,</span><span class="pln"> UUID</span><span class="pun">=</span><span class="pln">a9dfb4a0</span><span class="pun">-</span><span class="lit">6653</span><span class="pun">-</span><span class="lit">4407</span><span class="pun">-</span><span class="pln">ae05</span><span class="pun">-</span><span class="lit">7044d92c1159</span> <span class="pun">(</span><span class="pln">extents</span><span class="pun">)</span> <span class="pun">(</span><span class="pln">large files</span><span class="pun">)</span> <span class="pun">(</span><span class="pln">huge files</span><span class="pun">)</span>

4. 准备将上面的文件挂载起来:

<span class="com"># mkdir /mnt/loopback</span>
<span class="com"># mount -o loop loopfile.img /mnt/loopback</span>

mount 命令的 -o loop 选项可以将任意一个 loopback 文件系统挂载。

上面的 mount 命令实际等价于下面两条命令:

<span class="com"># losetup /dev/loop0 loopfile.img</span>
<span class="com"># mount /dev/loop0 /mnt/loopback</span>

因此实际上,mount -o loop 在内部已经默认的将文件和 /dev/loop0 挂载起来了。

然而对于第一种方法(mount -o loop)并不能适用于所有的场景。比如,我们想创建一个硬盘文件,然后对该文件进行分区,接着挂载其中一个子分区,这时就不能用 -o loop 这种方法了。因此必须如下做:

<span class="com"># losetup /dev/loop1 loopfile.img</span>
<span class="com"># fdisk /dev/loop1</span>

6. 卸载挂载点:


欢迎转载本文:磁盘维护Linux losetup命令
喜欢 (0)
[ld]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址