Centos中硬盘分区与挂载

本文章来自【知识林】

以下是以Centos系统为例描述对磁盘的一些常用操作,如:

  1. 创建分区
  2. 创建文件系统
  3. 挂载
  4. 取消挂载与重新挂载
  • ### 以 root 用户登陆操作系统。
  • ### 查看磁盘状态 fdisk -l,得到以下结果:
[root@localhost ~]# fdisk -l

Disk /dev/sda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b0942

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          66      524288   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              66         196     1048576   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             196        1959    14154752   83  Linux

Disk /dev/sdb: 37.6 GB, 37580963840 bytes
255 heads, 63 sectors/track, 4568 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

可以看出,已使用的磁盘是 /dev/sda 并分了三个分区,未使用的是 /dev/sdb ,存储大小为37.6GB。

  • ### 输入命令 fdisk /dev/sdb 对此分区进行操作,得到以下结果:
[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x1e2875d9.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help):

根据提示可输入 m 获得帮助:

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition : 删除一个分区
   l   list known partition types : 列表已知分区类型
   m   print this menu : 再次显示此菜单
   n   add a new partition : 创建一个新分区
   o   create a new empty DOS partition table : 新创建一个空的DOS分区
   p   print the partition table : 查看当前分区
   q   quit without saving changes : 不保存退出
   s   create a new empty Sun disklabel 
   t   change a partition's system id : 修改分区的系统Id
   u   change display/entry units : 修改状态
   v   verify the partition table : 校验分区
   w   write table to disk and exit : 保存修改并退出
   x   extra functionality (experts only)
  • ### 接下来开始创建分区:
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-4568, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-4568, default 4568):
Using default value 4568

Command (m for help): p

Disk /dev/sdb: 37.6 GB, 37580963840 bytes
255 heads, 63 sectors/track, 4568 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x92838edd

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        4568    36692428+  83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
  1. 命令 n 创建分区
  2. 命令 p 创建主分区
  3. 输入 1 表示第一个分区
  4. 接下来直接回车
  5. 也直接回车(默认分配空间大小)
  6. 输入命令 p 查看分区列表,即可看到刚刚分配的一个分区
  7. 输入命令 w 保存并退出。

    • ### 创建文件系统
      此时使用命令 df -h 是没有刚刚的分区的,因为还没有创建文件系统。
      输入 mke2fs -t ext4 /dev/sdb1为sdb1分区创建文件系统,效果如下:
[root@localhost ~]# mke2fs -t ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
2293760 inodes, 9173107 blocks
458655 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=4294967296
280 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624

正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
  • ### 挂载文件系统
    此使用命令 df -h 也还是看不到相应的文件系统的,因为还没有将此文件系统挂载到任何目录下。

使用 mount进行挂载,下面将/dev/sdb1 挂载到 /mnt 目录下。

mount /dev/sdb1 /mnt

挂载完成后即可看到文件系统信息:

[root@localhost ~]# mount /dev/sdb1 /mnt
[root@localhost ~]# df -h
文件系统              容量  已用  可用 已用%% 挂载点
/dev/sda3              14G  2.4G   11G  19% /
tmpfs                 1.9G     0  1.9G   0% /dev/shm
/dev/sda1             504M   43M  436M   9% /boot
/dev/sdb1              35G  176M   33G   1% /mnt
  • 取消挂载

    挂载使用命令 mount,而取消挂载则用命令 umount ,如需要刚刚挂载的 /mnt 重新挂载到 /web 下,则需要:

    1. 取消挂载
    2. 创建目录
    3. 重新挂载
umount /mnt
mkdir /web
mount /dev/sdb1 /web

再使用命令df -h即可查看到最新的挂载信息:

[root@localhost ~]# df -h
文件系统              容量  已用  可用 已用%% 挂载点
/dev/sda3              14G  2.4G   11G  19% /
tmpfs                 1.9G     0  1.9G   0% /dev/shm
/dev/sda1             504M   43M  436M   9% /boot
/dev/sdb1              35G  176M   33G   1% /web

最后将挂载设置为开机启动:

echo "/dev/sdb1 /data ext4 defaults 0 0" >> /etc/fstab

或在/etc/fstab文件末尾加上/dev/sdb1 /data ext4 defaults 0 0,和上面命令一样的效果

本文章来自【知识林】

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值