linux 磁盘挂载

1、磁盘的格式化

1.1、查看当前文件目录

使用 df -h 命令来查看当前已经挂载的磁盘以及磁盘的信息:

root:~>df -h
文件系统        容量  已用  可用 已用% 挂载点
/dev/sda3       259G  229G   30G   89% /
devtmpfs         32G     0   32G    0% /dev
tmpfs            32G   16M   32G    1% /dev/shm
tmpfs            32G  3.2G   29G   11% /run
tmpfs            32G     0   32G    0% /sys/fs/cgroup
/dev/sda1       497M  191M  306M   39% /boot
tmpfs           6.3G     0  6.3G    0% /run/user/0
cm_processes     32G  2.7G   29G    9% /run/cloudera-scm-agent/process
tmpfs           6.3G   12K  6.3G    1% /run/user/42

1.2、磁盘分区

  • 查找已经安装并且未格式化的磁盘
    使用命令 fdisk -l 列出所有的磁盘,使用命令的例子以及结果如下:

    root:~>fdisk -l

    磁盘 /dev/sda:299.4 GB, 299439751168 字节,584843264 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    磁盘标签类型:dos
    磁盘标识符:0x000de8f6

     设备 Boot      Start         End      Blocks   Id  System
    

    /dev/sda1 * 2048 1026047 512000 83 Linux
    /dev/sda2 1026048 42969087 20971520 82 Linux swap / Solaris
    /dev/sda3 42969088 584843263 270937088 83 Linux

    磁盘 /dev/sdb:3000.0 GB, 3000034656256 字节,5859442688 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    磁盘标签类型:gpt
    Disk identifier: 69D07AFC-584D-4926-81B8-8DEF8087445F

可以看出 /dev.sdb 还没有格式化以及挂载。

  • 格式化磁盘
    在 CentOS 7.0 以后 fdisk 也支持了大磁盘的格式话。本文章是再 CentOS 7.4 上进行的。
    使用 fdisk /dev/sdb 根据帮助提示分区,这里是把 /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 0x0adfd119.
    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 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
    p print the partition table
    q quit without saving changes
    s create a new empty Sun disklabel
    t change a partition's system 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-6527, default 1):
    Using default value 1
    Last cylinder, +cylinders or +size{K,M,G} (1-6527, default 6527):
    Using default value 6527

    Command (m for help): p

    Disk /dev/sdb: 53.7 GB, 3000034656256 bytes
    255 heads, 63 sectors/track, 6527 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: 0x69D07AFC-584D-4926-81B8-8DEF8087445F

      Device Boot      Start         End      Blocks   Id  System
    

    /dev/sdb1 1 6527 5859442688 83 Linux

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

    Calling ioctl() to re-read partition table.
    Syncing disks.

  • 格式化磁盘

命令 mkfs.ext4 /dev/sdb1 分区为 ext4 的文件系统格式。

[root@localhost ~]# mkfs.ext4 /dev/sdb1 
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
3276800 inodes, 13107024 blocks
655351 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
400 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, 11239424

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

This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

2、挂载磁盘

2.1、手动挂载

手动挂载每次重启都需要执行一次,比较麻烦:

手动挂载步骤:

  • 准备挂载的目录:
    mkdir /home/data
    chmod 777 /home/data
  • 使用 mount 命令挂载
    mount /dev/sdb1 /home/data

2.2、自动挂载

自动挂载不需要重启后在执行,推荐使用:

自动挂载的步骤为:

  • 准备挂载的目录:
    mkdir /home/data
    chmod 777 /home/data

  • 挂载步骤:

  1. fdisk -l

  2. 找到要挂载的磁盘(举例/dev/sdb)

  3. 查看磁盘文件

    vim /ext/fstab

    # /etc/fstab
    # Created by anaconda on Tue Jul 4 05:46:31 2017

    # Accessible filesystems, by reference, are maintained under '/dev/disk'
    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

    #
    /dev/mapper/cl-root / xfs defaults 0 0
    UUID=619a7a33-84ff-4b7f-befd-c798c04f68a4 /boot xfs defaults 0 0
    /dev/mapper/cl-swap swap swap defaults 0 0

  4. 添加新挂载的硬盘

    /dev/sdc /home/data ext4 defaults 0 0

参数说明:

  • 第一列:实际的分区名,也可以是实际的分区的卷标;

  • 第二列:挂载点,最好文件夹已存在并且权限为 777;

  • 第三列:此分区文件系统类型;

  • 第四列:挂载的选项,用于设置挂载的参数。

    常见参数如下:

    • auto 系统自动挂载,默认选项;
    • defaults rw,suid,dev,exec,auto,nouser,ansyc;
    • noauto 开机不自动挂载;
    • nouser 只有超级用户可以挂载;
    • ro 按照只读权限挂载;
    • rw 按照可读可写权限挂载;
    • user 任何用户都可以挂载。
  • 第五列:dump备份设置

    • 1 允许 dump 备份程序备份;
    • 0 忽略备份操作;
  • 第六列:fsck 磁盘检查设置

  • 0 永不检查

  • 目录永远为 1

  • 其他分区从 2 开始,数字越小越先检查。

你可能感兴趣的:(linux 磁盘挂载)