如何在Linux分区或逻辑卷中创建文件系统
学习在你的系统中创建一个文件系统,并且长期或者非长期地挂载它。
在计算技术中,文件系统控制如何存储和检索数据,并且帮助组织存储媒介中的文件。如果没有文件系统,信息将被存储为一个大数据块,而且你无法知道一条信息在哪结束,下一条信息在哪开始。文件系统通过为存储数据的文件提供名称,并且在文件系统中的磁盘上维护文件和目录表以及它们的开始和结束位置、总的大小等来帮助管理所有的这些信息。
在 Linux 中,当你创建一个硬盘分区或者逻辑卷之后,接下来通常是通过格式化这个分区或逻辑卷来创建文件系统。这个操作方法假设你已经知道如何创建分区或逻辑卷,并且你希望将它格式化为包含有文件系统,并且挂载它。
创建文件系统
假设你为你的系统添加了一块新的硬盘并且在它上面创建了一个叫 /dev/sda1
的分区。
1、为了验证 Linux 内核已经发现这个分区,你可以 cat
出 /proc/partitions
的内容,就像这样:
<span class="pun">[</span><span class="pln">root@localhost </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">cat</span><span class="pln"> </span><span class="pun">/</span><span class="pln">proc</span><span class="pun">/</span><span class="pln">partitions</span>
<span class="pln">major minor </span><span class="com">#</span><span class="pln">blocks name</span>
<span class="pln"> </span><span class="lit">253</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">10485760</span><span class="pln"> vda</span>
<span class="pln"> </span><span class="lit">253</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span class="lit">8192000</span><span class="pln"> vda1</span>
<span class="pln"> </span><span class="lit">11</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">1048575</span><span class="pln"> sr0</span>
<span class="pln"> </span><span class="lit">11</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span class="lit">374</span><span class="pln"> sr1</span>
<span class="pln"> </span><span class="lit">8</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">10485760</span><span class="pln"> sda</span>
<span class="pln"> </span><span class="lit">8</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span class="lit">10484736</span><span class="pln"> sda1</span>
<span class="pln"> </span><span class="lit">252</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">3145728</span><span class="pln"> dm</span><span class="pun">-</span><span class="lit">0</span>
<span class="pln"> </span><span class="lit">252</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span class="lit">2097152</span><span class="pln"> dm</span><span class="pun">-</span><span class="lit">1</span>
<span class="pln"> </span><span class="lit">252</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="lit">1048576</span><span class="pln"> dm</span><span class="pun">-</span><span class="lit">2</span>
<span class="pln"> </span><span class="lit">8</span><span class="pln"> </span><span class="lit">16</span><span class="pln"> </span><span class="lit">1048576</span><span class="pln"> sdb</span>
2、决定你想要去创建的文件系统种类,比如 ext4、XFS,或者其他的一些。这里是一些可选项:
<span class="pun">[</span><span class="pln">root@localhost </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">mkfs</span><span class="pun">.<</span><span class="pln">tab</span><span class="pun">><</span><span class="pln">tab</span><span class="pun">></span>
<span class="kwd">mkfs</span><span class="pun">.</span><span class="pln">btrfs </span><span class="kwd">mkfs</span><span class="pun">.</span><span class="pln">cramfs </span><span class="kwd">mkfs</span><span class="pun">.</span><span class="pln">ext2 </span><span class="kwd">mkfs</span><span class="pun">.</span><span class="pln">ext3 </span><span class="kwd">mkfs</span><span class="pun">.</span><span class="pln">ext4 </span><span class="kwd">mkfs</span><span class="pun">.</span><span class="pln">minix </span><span class="kwd">mkfs</span><span class="pun">.</span><span class="pln">xfs</span>
3、为了这次练习的目的,选择 ext4。(我喜欢 ext4,因为如果你需要的话,它可以允许你去压缩文件系统,这对于 XFS 并不简单。)这里是完成它的方法(输出可能会因设备名称或者大小而不同):
<span class="pun">[</span><span class="pln">root@localhost </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">mkfs</span><span class="pun">.</span><span class="pln">ext4 </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda1</span>
<span class="kwd">mke2fs</span><span class="pln"> </span><span class="lit">1.42</span><span class="pun">.</span><span class="lit">9</span><span class="pln"> </span><span class="pun">(</span><span class="lit">28</span><span class="pun">-</span><span class="typ">Dec</span><span class="pun">-</span><span class="lit">2013</span><span class="pun">)</span>
<span class="typ">Filesystem</span><span class="pln"> label</span><span class="pun">=</span>
<span class="pln">OS type</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Linux</span>
<span class="typ">Block</span><span class="pln"> </span><span class="kwd">size</span><span class="pun">=</span><span class="lit">4096</span><span class="pln"> </span><span class="pun">(</span><span class="pln">log</span><span class="pun">=</span><span class="lit">2</span><span class="pun">)</span>
<span class="typ">Fragment</span><span class="pln"> </span><span class="kwd">size</span><span class="pun">=</span><span class="lit">4096</span><span class="pln"> </span><span class="pun">(</span><span class="pln">log</span><span class="pun">=</span><span class="lit">2</span><span class="pun">)</span>
<span class="typ">Stride</span><span class="pun">=</span><span class="lit">0</span><span class="pln"> blocks</span><span class="pun">,</span><span class="pln"> </span><span class="typ">Stripe</span><span class="pln"> width</span><span class="pun">=</span><span class="lit">8191</span><span class="pln"> blocks</span>
<span class="lit">194688</span><span class="pln"> inodes</span><span class="pun">,</span><span class="pln"> </span><span class="lit">778241</span><span class="pln"> blocks</span>
<span class="lit">38912</span><span class="pln"> blocks </span><span class="pun">(</span><span class="lit">5.00</span><span class="pun">%)</span><span class="pln"> reserved </span><span class="kwd">for</span><span class="pln"> the </span><span class="kwd">super</span><span class="pln"> user</span>
<span class="typ">First</span><span class="pln"> data block</span><span class="pun">=</span><span class="lit">0</span>
<span class="typ">Maximum</span><span class="pln"> filesystem blocks</span><span class="pun">=</span><span class="lit">799014912</span>
<span class="lit">24</span><span class="pln"> block </span><span class="kwd">groups</span>
<span class="lit">32768</span><span class="pln"> blocks per group</span><span class="pun">,</span><span class="pln"> </span><span class="lit">32768</span><span class="pln"> fragments per group</span>
<span class="lit">8112</span><span class="pln"> inodes per group</span>
<span class="typ">Superblock</span><span class="pln"> backups stored on blocks</span><span class="pun">:</span>
<span class="pln"> </span><span class="lit">32768</span><span class="pun">,</span><span class="pln"> </span><span class="lit">98304</span><span class="pun">,</span><span class="pln"> </span><span class="lit">163840</span><span class="pun">,</span><span class="pln"> </span><span class="lit">229376</span><span class="pun">,</span><span class="pln"> </span><span class="lit">294912</span>
<span class="typ">Allocating</span><span class="pln"> group tables</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">done</span>
<span class="typ">Writing</span><span class="pln"> inode tables</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">done</span>
<span class="typ">Creating</span><span class="pln"> journal </span><span class="pun">(</span><span class="lit">16384</span><span class="pln"> blocks</span><span class="pun">):</span><span class="pln"> </span><span class="kwd">done</span>
<span class="typ">Writing</span><span class="pln"> superblocks </span><span class="kwd">and</span><span class="pln"> filesystem accounting information</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">done</span>
4、在上一步中,如果你想去创建不同的文件系统,请使用不同变种的 mkfs
命令。
挂载文件系统
当你创建好文件系统后,你可以在你的操作系统中挂载它。
1、首先,识别出新文件系统的 UUID 编码。使用 blkid
命令列出所有可识别的块存储设备并且在输出信息中查找 sda1
:
<span class="pun">[</span><span class="pln">root@localhost </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">blkid</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">vda1</span><span class="pun">:</span><span class="pln"> UUID</span><span class="pun">=</span><span class="str">"716e713d-4e91-4186-81fd-c6cfa1b0974d"</span><span class="pln"> TYPE</span><span class="pun">=</span><span class="str">"xfs"</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sr1</span><span class="pun">:</span><span class="pln"> UUID</span><span class="pun">=</span><span class="str">"2019-03-08-16-17-02-00"</span><span class="pln"> LABEL</span><span class="pun">=</span><span class="str">"config-2"</span><span class="pln"> TYPE</span><span class="pun">=</span><span class="str">"iso9660"</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda1</span><span class="pun">:</span><span class="pln"> UUID</span><span class="pun">=</span><span class="str">"wow9N8-dX2d-ETN4-zK09-Gr1k-qCVF-eCerbF"</span><span class="pln"> TYPE</span><span class="pun">=</span><span class="str">"LVM2_member"</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">mapper</span><span class="pun">/</span><span class="kwd">test</span><span class="pun">-</span><span class="pln">test1</span><span class="pun">:</span><span class="pln"> PTTYPE</span><span class="pun">=</span><span class="str">"dos"</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda1</span><span class="pun">:</span><span class="pln"> UUID</span><span class="pun">=</span><span class="str">"ac96b366-0cdd-4e4c-9493-bb93531be644"</span><span class="pln"> TYPE</span><span class="pun">=</span><span class="str">"ext4"</span>
<span class="pun">[</span><span class="pln">root@localhost </span><span class="pun">~]#</span>
2、运行下面的命令挂载 /dev/sd1
设备:
<span class="pun">[</span><span class="pln">root@localhost </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">mkdir</span><span class="pln"> </span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">mount_point_for_dev_sda1</span>
<span class="pun">[</span><span class="pln">root@localhost </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">ls</span><span class="pln"> </span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span>
<span class="pln">mount_point_for_dev_sda1</span>
<span class="pun">[</span><span class="pln">root@localhost </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">mount</span><span class="pln"> </span><span class="pun">-</span><span class="pln">t ext4 </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda1 </span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">mount_point_for_dev_sda1</span><span class="pun">/</span>
<span class="pun">[</span><span class="pln">root@localhost </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">df</span><span class="pln"> </span><span class="pun">-</span><span class="pln">h</span>
<span class="typ">Filesystem</span><span class="pln"> </span><span class="typ">Size</span><span class="pln"> </span><span class="typ">Used</span><span class="pln"> </span><span class="typ">Avail</span><span class="pln"> </span><span class="typ">Use</span><span class="pun">%</span><span class="pln"> </span><span class="typ">Mounted</span><span class="pln"> on</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">vda1 </span><span class="lit">7.9G</span><span class="pln"> </span><span class="lit">920M</span><span class="pln"> </span><span class="lit">7.0G</span><span class="pln"> </span><span class="lit">12</span><span class="pun">%</span><span class="pln"> </span><span class="pun">/</span>
<span class="pln">devtmpfs </span><span class="lit">443M</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">443M</span><span class="pln"> </span><span class="lit">0</span><span class="pun">%</span><span class="pln"> </span><span class="pun">/</span><span class="pln">dev</span>
<span class="pln">tmpfs </span><span class="lit">463M</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">463M</span><span class="pln"> </span><span class="lit">0</span><span class="pun">%</span><span class="pln"> </span><span class="str">/dev/</span><span class="pln">shm</span>
<span class="pln">tmpfs </span><span class="lit">463M</span><span class="pln"> </span><span class="lit">30M</span><span class="pln"> </span><span class="lit">434M</span><span class="pln"> </span><span class="lit">7</span><span class="pun">%</span><span class="pln"> </span><span class="pun">/</span><span class="pln">run</span>
<span class="pln">tmpfs </span><span class="lit">463M</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">463M</span><span class="pln"> </span><span class="lit">0</span><span class="pun">%</span><span class="pln"> </span><span class="str">/sys/</span><span class="pln">fs</span><span class="pun">/</span><span class="pln">cgroup</span>
<span class="pln">tmpfs </span><span class="lit">93M</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">93M</span><span class="pln"> </span><span class="lit">0</span><span class="pun">%</span><span class="pln"> </span><span class="str">/run/</span><span class="pln">user</span><span class="pun">/</span><span class="lit">0</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda1 </span><span class="lit">2.9G</span><span class="pln"> </span><span class="lit">9.0M</span><span class="pln"> </span><span class="lit">2.7G</span><span class="pln"> </span><span class="lit">1</span><span class="pun">%</span><span class="pln"> </span><span class="str">/mnt/</span><span class="pln">mount_point_for_dev_sda1</span>
<span class="pun">[</span><span class="pln">root@localhost </span><span class="pun">~]#</span>
命令 df -h
显示了每个文件系统被挂载的挂载点。查找 /dev/sd1
。上面的挂载命令使用的设备名称是 /dev/sda1
。用 blkid
命令中的 UUID 编码替换它。注意,在 /mnt
下一个被新创建的目录挂载了 /dev/sda1
。
3、直接在命令行下使用挂载命令(就像上一步一样)会有一个问题,那就是挂载不会在设备重启后存在。为使***性地挂载文件系统,编辑 /etc/fstab
文件去包含你的挂载信息:
<span class="pln">UUID</span><span class="pun">=</span><span class="pln">ac96b366</span><span class="pun">-</span><span class="lit">0cdd</span><span class="pun">-</span><span class="lit">4e4c</span><span class="pun">-</span><span class="lit">9493</span><span class="pun">-</span><span class="pln">bb93531be644 </span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">mount_point_for_dev_sda1</span><span class="pun">/</span><span class="pln"> ext4 defaults </span><span class="lit">0</span><span class="pln"> </span><span class="lit">0</span>
4、编辑完 /etc/fstab
文件后,你可以 umount /mnt/mount_point_for_fev_sda1
并且运行 mount -a
命令去挂载被列在 /etc/fstab
文件中的所有设备文件。如果一切顺利的话,你可以使用 df -h
列出并且查看你挂载的文件系统:
<span class="pln">root@localhost </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">umount</span><span class="pln"> </span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">mount_point_for_dev_sda1</span><span class="pun">/</span>
<span class="pun">[</span><span class="pln">root@localhost </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">mount</span><span class="pln"> </span><span class="pun">-</span><span class="pln">a</span>
<span class="pun">[</span><span class="pln">root@localhost </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">df</span><span class="pln"> </span><span class="pun">-</span><span class="pln">h</span>
<span class="typ">Filesystem</span><span class="pln"> </span><span class="typ">Size</span><span class="pln"> </span><span class="typ">Used</span><span class="pln"> </span><span class="typ">Avail</span><span class="pln"> </span><span class="typ">Use</span><span class="pun">%</span><span class="pln"> </span><span class="typ">Mounted</span><span class="pln"> on</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">vda1 </span><span class="lit">7.9G</span><span class="pln"> </span><span class="lit">920M</span><span class="pln"> </span><span class="lit">7.0G</span><span class="pln"> </span><span class="lit">12</span><span class="pun">%</span><span class="pln"> </span><span class="pun">/</span>
<span class="pln">devtmpfs </span><span class="lit">443M</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">443M</span><span class="pln"> </span><span class="lit">0</span><span class="pun">%</span><span class="pln"> </span><span class="pun">/</span><span class="pln">dev</span>
<span class="pln">tmpfs </span><span class="lit">463M</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">463M</span><span class="pln"> </span><span class="lit">0</span><span class="pun">%</span><span class="pln"> </span><span class="str">/dev/</span><span class="pln">shm</span>
<span class="pln">tmpfs </span><span class="lit">463M</span><span class="pln"> </span><span class="lit">30M</span><span class="pln"> </span><span class="lit">434M</span><span class="pln"> </span><span class="lit">7</span><span class="pun">%</span><span class="pln"> </span><span class="pun">/</span><span class="pln">run</span>
<span class="pln">tmpfs </span><span class="lit">463M</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">463M</span><span class="pln"> </span><span class="lit">0</span><span class="pun">%</span><span class="pln"> </span><span class="str">/sys/</span><span class="pln">fs</span><span class="pun">/</span><span class="pln">cgroup</span>
<span class="pln">tmpfs </span><span class="lit">93M</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">93M</span><span class="pln"> </span><span class="lit">0</span><span class="pun">%</span><span class="pln"> </span><span class="str">/run/</span><span class="pln">user</span><span class="pun">/</span><span class="lit">0</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda1 </span><span class="lit">2.9G</span><span class="pln"> </span><span class="lit">9.0M</span><span class="pln"> </span><span class="lit">2.7G</span><span class="pln"> </span><span class="lit">1</span><span class="pun">%</span><span class="pln"> </span><span class="str">/mnt/</span><span class="pln">mount_point_for_dev_sda1</span>
5、你也可以检测文件系统是否被挂载: