Linux--磁盘管理及挂载

  硬盘:一个IO设备。设备名称: IDE硬盘为hdx(x为从a—d硬盘最多四个),SCSI,SATA,USB硬盘为sdx(x为a—z))。硬盘主分区最多为4个(sdb1-sdb4),逻辑分区从sdb5开始。本篇博客主要讲解查看和管理磁盘的基本命令,最主要内容还是磁盘分区和挂载。

查看磁盘信息

df命令

  • 查看已挂载磁盘的总容量、使用容量、剩余容量
    1
    2
    3
    4
    df -i #使用inodes 显示结果
    df -h #用合适的单位显示,G,M等
    df -k #显示单位K
    df –m #显示单位M

说明:

Filesystem #表示扇区,划分磁盘时所分的区
Used #已使用
Available #剩余
Use% #已经使用的百分比
Mounted on #扇区挂载点

du命令

查看某个文件

1
2
3
4
5
du [-abckmsh] [文件或者目录名]
du -a #全部文件与目录大小都列出来,如果不加参数列出目录(包含子目录)。
du -h #用合适的单位显示, 还有单位-b -k –m。
du -s #只显示总和。
du -c #最后显示总和。

磁盘挂载

基本步骤

硬盘分区及挂载操作步骤:

  1. fdisk -l#查看未挂载的硬盘(名称为/dev/xvdb)
    Disk /dev/xvdb doesn’t contain a valid partition table
  2. fdisk /dev/xvdb # 创建分区
  3. 输入n
    Command (m for help):n
  4. 输入p
    Command action
    e extended
    p primary partition (1-4)
    p
  5. 输入1
    Partition number (1-4): 1
  6. 回车
    First cylinder (1-2610, default 1):
    Using default value 1
  7. 回车
    Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610):
    Using default value 2610
  8. 输入w
    Command (m for help): w
    The partition table has been altered!
  9. mkfs.ext3 /dev/xvdb1 #格式化分区
  10. mkdir /data #建立挂载目录
  11. mount /dev/xvdb1 /data #挂载分区
  12. vi /etc/fstab #设置开机自动挂载
    在vi中输入i进入INERT模式,将光标移至文件结尾处并回车,将下面的内容复制/粘贴,然后按Esc键,输入:x保存并退出
    /dev/xvdb /data ext3 defaults 0 0
  13. reboot #重启服务器
  14. df #查看硬盘分区
    /dev/xvdb 20635700 176196 19411268 1% /data

示例代码

  • 第一步:查看磁盘分区,发现/dev/xvdb并没有挂载信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    [root@iZ94r8hgrjcZ /]# fdisk -l

    Disk /dev/xvda: 21.5 GB, 21474836480 bytes
    255 heads, 63 sectors/track, 2610 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: 0x00078f9c

    Device Boot Start End Blocks Id System #挂载信息
    /dev/xvda1 * 1 2611 20970496 83 Linux

    Disk /dev/xvdb: 32.2 GB, 32212254720 bytes
    255 heads, 63 sectors/track, 3916 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 ##没有挂载信息,iden=0x00000000
  • 第二步:创建磁盘分区

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    [root@iZ94r8hgrjcZ /]# fdisk /dev/xvdb
    Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
    Building a new DOS disklabel with disk identifier 0xc27ae924.
    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): n ##输入n
    Command action
    e extended
    p primary partition (1-4)
    p
    Partition number (1-4): 1 ##输入1
    First cylinder (1-3916, default 1): ##回车键
    Using default value 1
    Last cylinder, +cylinders or +size{K,M,G} (1-3916, default 3916): ##回车
    Using default value 3916

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

    Calling ioctl() to re-read partition table.
    Syncing disks.
    [root@iZ94r8hgrjcZ /]# fdisk -l ##再次查看磁盘信息

    Disk /dev/xvda: 21.5 GB, 21474836480 bytes
    255 heads, 63 sectors/track, 2610 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: 0x00078f9c

    Device Boot Start End Blocks Id System
    /dev/xvda1 * 1 2611 20970496 83 Linux

    Disk /dev/xvdb: 32.2 GB, 32212254720 bytes
    255 heads, 63 sectors/track, 3916 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: 0xc27ae924 ##已经不是0x00000000,并下面有磁盘信息

    Device Boot Start End Blocks Id System
    /dev/xvdb1 1 3916 31455238+ 83 Linux
  • 第三步:格式化分区

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    [root@iZ94r8hgrjcZ /]# mkfs.ext3 /dev/xvdb1
    mke2fs 1.41.12 (17-May-2010)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    1966080 inodes, 7863809 blocks
    393190 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=4294967296
    240 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

    Writing inode tables: done
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done

    This filesystem will be automatically checked every 29 mounts or
    180 days, whichever comes first. Use tune2fs -c or -i to override.
  • 第四步:创建挂载目录并挂载分区

    1
    2
    [root@iZ94r8hgrjcZ /]# mkdir /data ##博主创建挂载目录为/data
    [root@iZ94r8hgrjcZ /]# mount /dev/xvdb1 /data ##挂载分区到/data目录
  • 第五步:设置开机自动挂载

    1
    [root@iZ94r8hgrjcZ /]# vi /etc/fstab

开机自动挂载

  • 第六步:重启系统,并查看信息
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    [root@iZ94r8hgrjcZ /]# reboot
    [root@iZ94r8hgrjcZ /]# fdisk -l

    Disk /dev/xvda: 21.5 GB, 21474836480 bytes
    255 heads, 63 sectors/track, 2610 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: 0x00078f9c

    Device Boot Start End Blocks Id System
    /dev/xvda1 * 1 2611 20970496 83 Linux

    Disk /dev/xvdb: 32.2 GB, 32212254720 bytes
    255 heads, 63 sectors/track, 3916 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: 0xc27ae924

    Device Boot Start End Blocks Id System
    /dev/xvdb1 1 3916 31455238+ 83 Linux

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器