zoukankan      html  css  js  c++  java
  • 磁盘管理

    目录
    一、df命令
    二、du命令
    三、磁盘分区
    四、磁盘格式化
    五、磁盘挂载
    六、手动增加swap空间
    七、lvm讲解
    八、Parted
    九、dd命令
    十、raid
    十一、存储基础知识

    一、df 命令

    df命令用来显示系统磁盘分区使用情况

    常用选项

    -h,以人类友好的方式显示大小

    //不带参数,默认显示全部分区,大小以kbytes为单位
    [root@localhost ~]# df
    Filesystem     1K-blocks    Used Available Use% Mounted on
    /dev/sda3       18658304 1754500  16903804  10% /
    devtmpfs          490024       0    490024   0% /dev
    tmpfs             499860       0    499860   0% /dev/shm
    tmpfs             499860    6756    493104   2% /run
    tmpfs             499860       0    499860   0% /sys/fs/cgroup
    /dev/sda1         201380   99356    102024  50% /boot
    tmpfs              99972       0     99972   0% /run/user/0
    
    //加上-h参数以后,分区大小的单位为K,M,G等人类易读的方式显示
    [root@localhost ~]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda3        18G  1.7G   17G  10% /
    devtmpfs        479M     0  479M   0% /dev
    tmpfs           489M     0  489M   0% /dev/shm
    tmpfs           489M  6.6M  482M   2% /run
    tmpfs           489M     0  489M   0% /sys/fs/cgroup
    /dev/sda1       197M   98M  100M  50% /boot
    tmpfs            98M     0   98M   0% /run/user/0
    
    //tmpfs,临时文件系统,是一种基于内存的文件系统。
    //df命令不显示swap分区大小使用情况,如需查看,可使用free命令
    [root@localhost ~]# free -h
                  total        used        free      shared  buff/cache   available
    Mem:           976M        139M        678M        6.6M        158M        665M
    Swap:          2.0G          0B        2.0G
    

    -i,显示i节点信息

    [root@localhost ~]# df -i
    Filesystem      Inodes IUsed   IFree IUse% Mounted on
    /dev/sda3      9334272 59933 9274339    1% /
    devtmpfs        122506   337  122169    1% /dev
    tmpfs           124965     1  124964    1% /dev/shm
    tmpfs           124965   416  124549    1% /run
    tmpfs           124965    16  124949    1% /sys/fs/cgroup
    /dev/sda1       102400   327  102073    1% /boot
    tmpfs           124965     1  124964    1% /run/user/0
    //有时候分区还有空间,但是就是写入不了数据,需要考虑inode是否已经用完这种情况。
    

    -m,以m为单位显示磁盘空间大小

    [root@localhost ~]# df -m
    Filesystem     1M-blocks  Used Available Use% Mounted on
    /dev/sda3          18221  1713     16509  10% /
    devtmpfs             479     0       479   0% /dev
    tmpfs                489     0       489   0% /dev/shm
    tmpfs                489     7       482   2% /run
    tmpfs                489     0       489   0% /sys/fs/cgroup
    /dev/sda1            197    98       100  50% /boot
    tmpfs                 98     0        98   0% /run/user/0
    

    二、du命令

    可以用来显示文件的大小

    常见用法

    //不带参数以字节为单位显示当前目录下文件及目录大小
    [root@localhost ~]# du
    0	./.pki/nssdb
    0	./.pki
    4	./.config/htop
    4	./.config
    4	./.ssh
    8	./demo/stor1
    4	./demo/stor2/stor1
    4	./demo/stor2
    0	./demo/fd3/fd1/fd1
    0	./demo/fd3/fd1
    8	./demo/fd3
    34940	./demo/fd1
    0	./demo/extra
    3400	./demo/tt
    0	./demo/music
    38364	./demo
    243228	.
    
    //带上具体的文件名,则以字节为单位显示文件大小
    [root@localhost ~]# du /etc/passwd
    4	/etc/passwd
    
    //-sh以人类易读的方式显示文件大小,这是du命令最常用的方式
    [root@localhost ~]# du -sh /etc/passwd
    4.0K	/etc/passwd
    

    三、磁盘分区

    通过使用fdisk命令来划分磁盘分区,演示过程如下:

    1.在虚拟机增加一块20G的

    25ctz.jpg

    25EGh.jpg

    25LLH.jpg

    25e7N.jpg

    25KYu.jpg

    2.检查虚拟机是否识别到新添加的硬盘

    [root@localhost ~]# fdisk -l
    
    Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0x000e777e
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048      411647      204800   83  Linux
    /dev/sda2          411648     4605951     2097152   82  Linux swap / Solaris
    /dev/sda3         4605952    41943039    18668544   83  Linux
    

    3.执行下述命令使系统重新扫描新添加的硬件,再次查看是否识别到新硬盘。

    [root@localhost ~]# cd /sys/class/scsi_host/
    [root@localhost scsi_host]# ll
    total 0
    lrwxrwxrwx. 1 root root 0 May 20 20:52 host0 -> ../../devices/pci0000:00/0000:00:07.1/ata1/host0/scsi_host/host0/
    lrwxrwxrwx. 1 root root 0 May 20 20:52 host1 -> ../../devices/pci0000:00/0000:00:07.1/ata2/host1/scsi_host/host1/
    lrwxrwxrwx. 1 root root 0 May 20 20:52 host2 -> ../../devices/pci0000:00/0000:00:10.0/host2/scsi_host/host2/
    [root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host0/scan
    [root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host1/scan
    [root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host2/scan
    
    //再次检查是否识别是否发现新硬盘
    [root@localhost scsi_host]# fdisk -l
    
    Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0x000e777e
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048      411647      204800   83  Linux
    /dev/sda2          411648     4605951     2097152   82  Linux swap / Solaris
    /dev/sda3         4605952    41943039    18668544   83  Linux
    //新硬盘的设备名为/dev/sdb
    Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    

    4.对新硬盘进行分区操作

    [root@localhost scsi_host]# fdisk /dev/sdb
    Welcome to fdisk (util-linux 2.23.2).
    
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    Device does not contain a recognized partition table
    Building a new DOS disklabel with disk identifier 0xfc58ea93.
    //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
       g   create a new empty GPT partition table
       G   create an IRIX (SGI) partition table
       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)
    
    //常用命令选项,n新建分区,p打印分区表,d删除分区,t更新分区系统id,w保存
    //新建一个2GB的分区
    //Command (m for help): n1^H^H^H^H,如果输错,退格键无法删除,可ctrl+退格键删除
    Command (m for help): n
    Partition type:
       p   primary (0 primary, 0 extended, 4 free)
       e   extended
    //这里的 p 是主分区的意思,e是扩展分区。MBR类型的分区表中,最多允许4个主分区,
    //主分区与扩展分区的和小于等于4个
    //如果已经分了4个主分区,磁盘还有空间未分,则剩余空间无法利用。
    //扩展分区可以是第一个分区,逻辑分区从5开始,并且是连续的,主分区可以不连续
    Select (default p): p
    Partition number (1-4, default 1): 1
    First sector (2048-41943039, default 2048):
    Using default value 2048
    Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +2G
    Partition 1 of type Linux and of size 2 GiB is set
    
    //打印分区表,显示已经建立了主分区/dev/sdb1
    Command (m for help): p
    
    Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0xfc58ea93
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048     4196351     2097152   83  Linux
    
    //更新分区系统id
    Command (m for help): n
    Partition type:
       p   primary (1 primary, 0 extended, 3 free)
       e   extended
    Select (default p): p
    Partition number (2-4, default 2):
    First sector (4196352-41943039, default 4196352):
    Using default value 4196352
    Last sector, +sectors or +size{K,M,G} (4196352-41943039, default 41943039): +2G
    Partition 2 of type Linux and of size 2 GiB is set
    
    Command (m for help): t
    Partition number (1,2, default 2):
    //L可以显示所有的系统id代码
    Hex code (type L to list all codes): 8e
    Changed type of partition 'Linux' to 'Linux LVM'
    
    Command (m for help): p
    
    Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0xbd3cbf9d
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048     4196351     2097152   83  Linux
    /dev/sdb2         4196352     8390655     2097152   8e  Linux LVM
    
    //删除分区
    Command (m for help): p
    
    Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0xbd3cbf9d
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048     4196351     2097152   83  Linux
    /dev/sdb2         4196352     8390655     2097152   8e  Linux LVM
    /dev/sdb3         8390656    41943039    16776192    5  Extended
    /dev/sdb5         8392704    12587007     2097152   83  Linux
    /dev/sdb6        12589056    20977663     4194304   83  Linux
    /dev/sdb7        20979712    23076863     1048576   83  Linux
    
    Command (m for help): d
    Partition number (1-3,5-7, default 7): 6
    Partition 6 is deleted
    
    Command (m for help): p
    
    Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0xbd3cbf9d
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048     4196351     2097152   83  Linux
    /dev/sdb2         4196352     8390655     2097152   8e  Linux LVM
    /dev/sdb3         8390656    41943039    16776192    5  Extended
    /dev/sdb5         8392704    12587007     2097152   83  Linux
    /dev/sdb6        20979712    23076863     1048576   83  Linux
    //原来的分区/dev/sdb7变为/dev/sdb6,但是所在磁盘的位置不变。参见分区起始扇区的位置
    
    //保存对分区的更改
    //如果不输入w保存,直接输入q退出,则对磁盘做的更改不会生效
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
    

    四、磁盘格式化

    磁盘分好区以后,还需要进行格式化以后才能进行使用

    Linux支持的文件系统很丰富,如下:

    [root@localhost scsi_host]# cat /etc/filesystems
    xfs
    ext4
    ext3
    ext2
    nodev proc
    nodev devpts
    iso9660
    vfat
    hfs
    hfsplus
    *
    //如需支持window的NTFS,可通过安装ntfs-3g来实现
    

    mke2fs支持将分区格式化为ext2,ext3.ext4,不支持将分区格式为xfs文件系统

    //-t可以指定要格式化的文件系统类型,可以是ext2,ext3,ext4
    [root@localhost scsi_host]# mke2fs -t ext4 /dev/sdb6
    mke2fs 1.42.9 (28-Dec-2013)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    65536 inodes, 262144 blocks
    13107 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=268435456
    8 block groups
    32768 blocks per group, 32768 fragments per group
    8192 inodes per group
    Superblock backups stored on blocks:
    	32768, 98304, 163840, 229376
    
    Allocating group tables: done
    Writing inode tables: done
    Creating journal (8192 blocks): done
    Writing superblocks and filesystem accounting information: done
    //验证格式化后的分区为ext4
    [root@localhost scsi_host]# lsblk -f | grep sdb6
    └─sdb6 ext4                    5285e8ef-f5b5-4cd8-bcef-3d78d6f6629e
    
    //-b可以指定磁盘存储的块大小
    //确定分区的默认块大小
    //默认块大小为4K
    [root@localhost demo]# du -h essay1
    0	essay1
    [root@localhost demo]# echo 1 > essay1
    [root@localhost demo]# du -h essay1
    4.0K	essay1
    //指定分区的块大小
    [root@localhost demo]# mke2fs -t ext4 -b 2048 /dev/sdb5
    mke2fs 1.42.9 (28-Dec-2013)
    Filesystem label=
    OS type: Linux
    Block size=2048 (log=1)
    Fragment size=2048 (log=1)
    Stride=0 blocks, Stripe width=0 blocks
    131072 inodes, 1048576 blocks
    52428 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=269484032
    64 block groups
    16384 blocks per group, 16384 fragments per group
    2048 inodes per group
    Superblock backups stored on blocks:
    	16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816
    
    Allocating group tables: done
    Writing inode tables: done
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done
    
    //-m指定保留给超级用户的空间
    [root@localhost demo]# mke2fs -m 2 /dev/sdb5
    mke2fs 1.42.9 (28-Dec-2013)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    131072 inodes, 524288 blocks
    10485 blocks (2.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=536870912
    16 block groups
    32768 blocks per group, 32768 fragments per group
    8192 inodes per group
    Superblock backups stored on blocks:
    	32768, 98304, 163840, 229376, 294912
    
    Allocating group tables: done
    Writing inode tables: done
    Writing superblocks and filesystem accounting information: done
    
    //-i一个inode对应的容量
    [root@localhost demo]# mke2fs -b 2048 -i 4096 /dev/sdb5
    mke2fs 1.42.9 (28-Dec-2013)
    Filesystem label=
    OS type: Linux
    Block size=2048 (log=1)
    Fragment size=2048 (log=1)
    Stride=0 blocks, Stripe width=0 blocks
    524288 inodes, 1048576 blocks
    52428 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=537919488
    64 block groups
    16384 blocks per group, 16384 fragments per group
    8192 inodes per group
    Superblock backups stored on blocks: 
    	16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816
    
    Allocating group tables: done
    Writing inode tables: done
    Writing superblocks and filesystem accounting information: done
    

    格式化为xfs分区需要使用mkfs.xfs, mkfs.ext2,mkfs.ext3,mkfs.ext4也可以实现将分区格式化为相对应的文件系统。

    //将/dev/sdb1格式化为xfs文件系统格式
    [root@localhost demo]# mkfs.xfs /dev/sdb1
    meta-data=/dev/sdb1              isize=512    agcount=4, agsize=131072 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=0, sparse=0
    data     =                       bsize=4096   blocks=524288, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal log           bsize=4096   blocks=2560, version=2
             =                       sectsz=512   sunit=0 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    

    磁盘坏道的修复

    fsck/e2fsck -a 分区名,参考 https://www.2cto.com/os/201204/126370.html

    避免数据丢失的几个策略
    1)做raid1、raid10、raid5、raid6
    2)做好数据的备份策略

    五、挂载

    格式化好的分区,需要挂载到系统文件目录树,挂载点就是该分区所在的目录。

    [root@localhost demo]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda3        18G  1.8G   17G  10% /
    devtmpfs        479M     0  479M   0% /dev
    tmpfs           489M     0  489M   0% /dev/shm
    tmpfs           489M  6.7M  482M   2% /run
    tmpfs           489M     0  489M   0% /sys/fs/cgroup
    /dev/sda1       197M   98M  100M  50% /boot
    tmpfs            98M     0   98M   0% /run/user/0
    
    //挂载/dev/sdb1分区到/mnt
    [root@localhost demo]# mount /dev/sdb1 /mnt/
    [root@localhost demo]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda3        18G  1.8G   17G  10% /
    devtmpfs        479M     0  479M   0% /dev
    tmpfs           489M     0  489M   0% /dev/shm
    tmpfs           489M  6.7M  482M   2% /run
    tmpfs           489M     0  489M   0% /sys/fs/cgroup
    /dev/sda1       197M   98M  100M  50% /boot
    tmpfs            98M     0   98M   0% /run/user/0
    /dev/sdb1       2.0G   33M  2.0G   2% /mnt
    
    //卸载挂载的分区
    [root@localhost demo]# umount /mnt/
    [root@localhost demo]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda3        18G  1.8G   17G  10% /
    devtmpfs        479M     0  479M   0% /dev
    tmpfs           489M     0  489M   0% /dev/shm
    tmpfs           489M  6.7M  482M   2% /run
    tmpfs           489M     0  489M   0% /sys/fs/cgroup
    /dev/sda1       197M   98M  100M  50% /boot
    tmpfs            98M     0   98M   0% /run/user/0
    
    //如果工作目录在要卸载的分区,可能无法正常卸载,可使用-l选项解决
    [root@localhost demo]# cd /mnt/
    [root@localhost mnt]# pwd
    /mnt
    [root@localhost mnt]# umount /mnt/
    umount: /mnt: target is busy.
            (In some cases useful info about processes that use
             the device is found by lsof(8) or fuser(1))
    //解决方法1,将工作目录切换到其他分区
    [root@localhost mnt]# cd 
    [root@localhost ~]# umount /mnt/
    
    //解决方法2,加上-l选项
    [root@localhost mnt]# pwd
    /mnt
    [root@localhost mnt]# umount -l /mnt
    [root@localhost mnt]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda3        18G  1.8G   17G  10% /
    devtmpfs        479M     0  479M   0% /dev
    tmpfs           489M     0  489M   0% /dev/shm
    tmpfs           489M  6.7M  482M   2% /run
    tmpfs           489M     0  489M   0% /sys/fs/cgroup
    /dev/sda1       197M   98M  100M  50% /boot
    tmpfs            98M     0   98M   0% /run/user/0
    

    mount命令的常用选项

    -a 按/etc/fstab的内容将所有相关的磁盘都挂载上来

    [root@localhost mnt]# cat /etc/fstab
    
    #
    # /etc/fstab
    # Created by anaconda on Mon May  7 03:54:51 2018
    #
    # 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
    #
    UUID=f31ae553-a4e6-437d-986c-a90738ca8583 /                       xfs     defaults        0 0
    UUID=9cd1151e-3afd-4d1b-9b20-1fc6c19441cb /boot                   xfs     defaults        0 0
    UUID=c17e0848-125f-4d68-b59a-381bf40baf24 swap                    swap    defaults        0 0
    UUID=fded654f-4d18-4967-b4ec-9c3fb2a27f7b /mnt/fd1                ext4    defaults        0 0
    UUID=5285e8ef-f5b5-4cd8-bcef-3d78d6f6629e /mnt/fd2                ext4    defaults        0 0
    //当前/mnt/fd1, /mnt/fd2都没有挂载,但是在/etc/fstab文件中有记录
    [root@localhost mnt]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda3        18G  1.8G   17G  10% /
    devtmpfs        479M     0  479M   0% /dev
    tmpfs           489M     0  489M   0% /dev/shm
    tmpfs           489M  6.7M  482M   2% /run
    tmpfs           489M     0  489M   0% /sys/fs/cgroup
    /dev/sda1       197M   98M  100M  50% /boot
    tmpfs            98M     0   98M   0% /run/user/0
    
    [root@localhost mnt]# mount -a
    [root@localhost mnt]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda3        18G  1.8G   17G  10% /
    devtmpfs        479M     0  479M   0% /dev
    tmpfs           489M     0  489M   0% /dev/shm
    tmpfs           489M  6.7M  482M   2% /run
    tmpfs           489M     0  489M   0% /sys/fs/cgroup
    /dev/sda1       197M   98M  100M  50% /boot
    tmpfs            98M     0   98M   0% /run/user/0
    /dev/sdb5       2.0G  6.0M  1.8G   1% /mnt/fd1
    /dev/sdb6       976M  2.6M  907M   1% /mnt/fd2
    

    -t 指明挂载的文件系统类型,默认是不用指明,系统会自动识别,但是有些文件系统可能需要手工指定

    //支持的文件系统类型包括:autofs, cifs, debugfs, ext, ext2, ext3, ext4,  msdos, ncpfs, nfs, nfs4,  ntfs, 具体取决于内核
    //挂载iso镜像文件
    [root@localhost demo]# mount -t iso9660 -o loop ikuai.iso /media/iso/
    mount: /dev/loop0 is write-protected, mounting read-only
    [root@localhost demo]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda3        18G  1.9G   16G  11% /
    devtmpfs        479M     0  479M   0% /dev
    tmpfs           489M     0  489M   0% /dev/shm
    tmpfs           489M  6.7M  482M   2% /run
    tmpfs           489M     0  489M   0% /sys/fs/cgroup
    /dev/sda1       197M   98M  100M  50% /boot
    tmpfs            98M     0   98M   0% /run/user/0
    /dev/sdb5       2.0G  6.0M  1.8G   1% /mnt/fd1
    /dev/sdb6       976M  2.6M  907M   1% /mnt/fd2
    /dev/loop0       29M   29M     0 100% /media/iso
    

    -o 后面可以接一些挂载时,额外的参数,如帐号,密码,读写权限

    //defaults 默认包括rw,suid,dev,exec,auto,nouser,和async
    //rw 读写权限, suid 允许设置suid权限,dev 在文件系统中翻译字符设备或块设备
    //exec允许二进制文件执行,auto 可以通过mount -a挂载,nouser 禁止普通用户挂载目录 async异步方式同步文件内容到磁盘
    
    //remount,重新挂载,在系统出错,或重新更新参数时,很有用
    [root@localhost demo]# umount /mnt/fd1
    [root@localhost demo]# mount -o ro /dev/sdb5 /mnt/fd1
    [root@localhost demo]# touch /mnt/fd1/1.txt
    touch: cannot touch ‘/mnt/fd1/1.txt’: Read-only file system
    [root@localhost demo]# mount -o remount /dev/sdb5
    [root@localhost demo]# touch /mnt/fd1/1.txt
    

    自动挂载配置文件/etc/fstab

    [root@localhost demo]# cat /etc/fstab
    
    #
    # /etc/fstab
    # Created by anaconda on Mon May  7 03:54:51 2018
    #
    # 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
    #
    UUID=f31ae553-a4e6-437d-986c-a90738ca8583 /                       xfs     defaults        0 0
    UUID=9cd1151e-3afd-4d1b-9b20-1fc6c19441cb /boot                   xfs     defaults        0 0
    UUID=c17e0848-125f-4d68-b59a-381bf40baf24 swap                    swap    defaults        0 0
    UUID=fded654f-4d18-4967-b4ec-9c3fb2a27f7b /mnt/fd1                ext4    defaults        0 0
    UUID=5285e8ef-f5b5-4cd8-bcef-3d78d6f6629e /mnt/fd2                ext4    defaults        0 0
    //格式:   UUID号或者设备名 挂载点 分区格式 选项 备份 磁盘检查
    
    //以挂载/dev/sdb1为例
    //查看/dev/sdb1的uuid
    [root@localhost demo]# blkid 
    /dev/sda1: UUID="9cd1151e-3afd-4d1b-9b20-1fc6c19441cb" TYPE="xfs" 
    /dev/sda2: UUID="c17e0848-125f-4d68-b59a-381bf40baf24" TYPE="swap" 
    /dev/sda3: UUID="f31ae553-a4e6-437d-986c-a90738ca8583" TYPE="xfs" 
    /dev/sr0: UUID="2017-09-06-10-51-00-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
    /dev/sdb1: UUID="ed41b0f0-e04a-4b05-ac51-16a8857912d7" TYPE="xfs" 
    /dev/sdb5: UUID="fded654f-4d18-4967-b4ec-9c3fb2a27f7b" TYPE="ext4" 
    /dev/sdb6: UUID="5285e8ef-f5b5-4cd8-bcef-3d78d6f6629e" TYPE="ext4" 
    
    //创建挂载点
    [root@localhost demo]# mkdir /mnt/fd3
    [root@localhost demo]# ls -ld /mnt/fd3
    drwxr-xr-x. 2 root root 6 May 21 04:35 /mnt/fd3/
    
    //分区格式为xfs,选项为默认,不备份,不检查
    //手动挂载验证
    [root@localhost demo]# mount UUID="ed41b0f0-e04a-4b05-ac51-16a8857912d7" /mnt/fd3
    [root@localhost demo]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda3        18G  1.9G   16G  11% /
    devtmpfs        479M     0  479M   0% /dev
    tmpfs           489M     0  489M   0% /dev/shm
    tmpfs           489M  6.7M  482M   2% /run
    tmpfs           489M     0  489M   0% /sys/fs/cgroup
    /dev/sda1       197M   98M  100M  50% /boot
    tmpfs            98M     0   98M   0% /run/user/0
    /dev/sdb5       2.0G  6.0M  1.8G   1% /mnt/fd1
    /dev/sdb6       976M  2.6M  907M   1% /mnt/fd2
    /dev/sdb1       2.0G   33M  2.0G   2% /mnt/fd3
    
    //将配置写入/etc/fstab,保存配置
    [root@localhost demo]# vim /etc/fstab
    UUID=ed41b0f0-e04a-4b05-ac51-16a8857912d7 /mnt/fd3         xfs     defaults        0 0
    
    //验证
    [root@localhost demo]# umount /dev/sdb1
    [root@localhost demo]# df
    Filesystem     1K-blocks    Used Available Use% Mounted on
    /dev/sda3       18658304 1989556  16668748  11% /
    devtmpfs          490024       0    490024   0% /dev
    tmpfs             499860       0    499860   0% /dev/shm
    tmpfs             499860    6792    493068   2% /run
    tmpfs             499860       0    499860   0% /sys/fs/cgroup
    /dev/sda1         201380   99356    102024  50% /boot
    tmpfs              99972       0     99972   0% /run/user/0
    /dev/sdb5        1998672    6144   1871288   1% /mnt/fd1
    /dev/sdb6         999320    2564    927944   1% /mnt/fd2
    [root@localhost demo]# mount -a
    [root@localhost demo]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda3        18G  1.9G   16G  11% /
    devtmpfs        479M     0  479M   0% /dev
    tmpfs           489M     0  489M   0% /dev/shm
    tmpfs           489M  6.7M  482M   2% /run
    tmpfs           489M     0  489M   0% /sys/fs/cgroup
    /dev/sda1       197M   98M  100M  50% /boot
    tmpfs            98M     0   98M   0% /run/user/0
    /dev/sdb5       2.0G  6.0M  1.8G   1% /mnt/fd1
    /dev/sdb6       976M  2.6M  907M   1% /mnt/fd2
    /dev/sdb1       2.0G   33M  2.0G   2% /mnt/fd3
    
    //修改/etc/fstab文件后强烈建议用mount -a测试,否则如果配置有问题,可能导致无法启动。
    

    六、手动增加swap空间

    1.查看当前swap分区的大小

    [root@localhost demo]# free -h
                  total        used        free      shared  buff/cache   available
    Mem:           976M        149M        347M        6.6M        479M        591M
    Swap:          2.0G          0B        2.0G
    
    

    2.生成一个空文件

    //生成一个100M的空文件
    [root@localhost demo]# dd if=/dev/zero of=new_swap bs=1M count=100
    100+0 records in
    100+0 records out
    104857600 bytes (105 MB) copied, 2.20227 s, 47.6 MB/s
    [root@localhost demo]# ls -l new_swap 
    -rw-r--r--. 1 root root 104857600 May 21 02:44 new_swap
    

    3.将该文件转为swap文件格式

    [root@localhost demo]# mkswap new_swap 
    Setting up swapspace version 1, size = 102396 KiB
    no label, UUID=151946dd-46a1-441f-85fd-b5ec98ef3294
    

    3.将该文件挂载为swap分区

    [root@localhost demo]# swapon /root/demo/new_swap
    swapon: /root/demo/new_swap: insecure permissions 0644, 0600 suggested.
    //查看当前swap分区大小,已经增加了100M
    [root@localhost demo]# free -h
                  total        used        free      shared  buff/cache   available
    Mem:           976M        149M        346M        6.6M        479M        591M
    Swap:          2.1G          0B        2.1G
    //如需卸载该swap分,可执行swapoff /root/demo/new_swap即可
    //如需开机自动加载新增的swap分,修改/etc/fstab文件
    

    七、LVM

    LVM是Logical Volume Manager的简称,意为逻辑卷管理。是Linux系统中进行磁盘管理的一种机制,它可以将多个分区整合到一起,使这些分区看起来就一个硬盘。通过LVM可以弹性的调整系统分区的容易。

    LVM的相关概念

    PV:即物理卷。PV是指硬盘分区或从逻辑上看起来和硬盘分区类似的设备,是LVM的基本存储逻辑块。

    VG:即卷组。它是由一个或多个物理卷组成的

    LV:即逻辑卷,对于LVM而言,逻辑卷类似于非LVM系统中的硬盘分区。

    PE:是Physical Extend的简称。是物理卷的基本单元,是可以被寻址的最小单元。物理卷是由同等大小的PE组成。

    LVM配置(以3个分区组成的LVM为例,每分区为3GB)

    1.准备磁盘分区

    //对硬盘进行分区,
    Command (m for help): n
    Partition type:
       p   primary (0 primary, 0 extended, 4 free)
       e   extended
    Select (default p): 
    Using default response p
    Partition number (1-4, default 1): 
    First sector (2048-41943039, default 2048): 
    Using default value 2048
    Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +3G
    Partition 1 of type Linux and of size 3 GiB is set
    //并将分区的系统id改为8e
    Command (m for help): t
    Partition number (1-3, default 3): 1
    Hex code (type L to list all codes): 8e
    Changed type of partition 'Linux' to 'Linux LVM'
    //依次建立三个分区并修改分区的系统id
    Command (m for help): p
    
    Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0xd2f9c4bf
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048     6293503     3145728   8e  Linux LVM
    /dev/sdb2         6293504    12584959     3145728   8e  Linux LVM
    /dev/sdb3        12584960    18876415     3145728   8e  Linux LVM
    

    2.PV阶段

    //检查系统中有无PV(如无pvscan命令,yum -y install lvm2-2.02.177-4.el7.x86_64)
    [root@bogon scsi_host]# pvscan 
      No matching physical volumes found
    
    //创建物理卷
    [root@bogon scsi_host]# pvcreate /dev/sdb{1,2,3}
      Physical volume "/dev/sdb1" successfully created.
      Physical volume "/dev/sdb2" successfully created.
      Physical volume "/dev/sdb3" successfully created.
    
    //查看PV状态
    [root@bogon scsi_host]# pvscan 
    [root@bogon scsi_host]# pvdisplay 
      "/dev/sdb1" is a new physical volume of "3.00 GiB"
      --- NEW Physical volume ---
      PV Name               /dev/sdb1
      VG Name               
      PV Size               3.00 GiB
      Allocatable           NO
      PE Size               0   
      Total PE              0
      Free PE               0
      Allocated PE          0
      PV UUID               yc1XjR-923R-qkVu-Qq1t-3vXW-3gqQ-GHbAF0
       
      "/dev/sdb2" is a new physical volume of "3.00 GiB"
      --- NEW Physical volume ---
      PV Name               /dev/sdb2
      VG Name               
      PV Size               3.00 GiB
      Allocatable           NO
      PE Size               0   
      Total PE              0
      Free PE               0
      Allocated PE          0
      PV UUID               i3VapW-8nY5-npvy-tetM-tx6Y-4KSj-wF31nJ
       
      "/dev/sdb3" is a new physical volume of "3.00 GiB"
      --- NEW Physical volume ---
      PV Name               /dev/sdb3
      VG Name               
      PV Size               3.00 GiB
      Allocatable           NO
      PE Size               0   
      Total PE              0
      Free PE               0
      Allocated PE          0
      PV UUID               OGUix7-S1Fy-iIDF-1x1Z-clU7-MYZV-PEo4ZS
    

    3.VG阶段

    //查看系统中是否存在VG
    [root@bogon scsi_host]# vgscan
      Reading volume groups from cache.
    
    //将/dev/sdb1,/dev/sdb2创建为一个VG
    [root@bogon scsi_host]# vgcreate lvm_test /dev/sdb1 /dev/sdb2
      Volume group "lvm_test" successfully created
    
    //查看VG状态
    [root@bogon scsi_host]# vgdisplay 
      --- Volume group ---
      VG Name               lvm_test
      System ID             
      Format                lvm2
      Metadata Areas        2
      Metadata Sequence No  1
      VG Access             read/write
      VG Status             resizable
      MAX LV                0
      Cur LV                0
      Open LV               0
      Max PV                0
      Cur PV                2
      Act PV                2
      VG Size               5.99 GiB
      PE Size               4.00 MiB
      Total PE              1534
      Alloc PE / Size       0 / 0   
      Free  PE / Size       1534 / 5.99 GiB
      VG UUID               QeVKgh-PIrY-HrmX-GY9G-25oF-r9E4-n5Mbdk
    
    //查看PV状态
    [root@bogon scsi_host]# pvscan 
      PV /dev/sdb1   VG lvm_test        lvm2 [<3.00 GiB / <3.00 GiB free]
      PV /dev/sdb2   VG lvm_test        lvm2 [<3.00 GiB / <3.00 GiB free]
      PV /dev/sdb3                      lvm2 [3.00 GiB]
      Total: 3 [8.99 GiB] / in use: 2 [5.99 GiB] / in no VG: 1 [3.00 GiB]
    
    //如需扩展这个VG容易,将/dev/sdb3添加入这个VG,操作如下
    [root@bogon scsi_host]# vgextend lvm_test /dev/sdb3
      Volume group "lvm_test" successfully extended
    [root@bogon scsi_host]# vgdisplay 
      --- Volume group ---
      VG Name               lvm_test
      System ID             
      Format                lvm2
      Metadata Areas        3
      Metadata Sequence No  2
      VG Access             read/write
      VG Status             resizable
      MAX LV                0
      Cur LV                0
      Open LV               0
      Max PV                0
      Cur PV                3
      Act PV                3
      VG Size               <8.99 GiB
      PE Size               4.00 MiB
      Total PE              2301
      Alloc PE / Size       0 / 0   
      Free  PE / Size       2301 / <8.99 GiB
      VG UUID               QeVKgh-PIrY-HrmX-GY9G-25oF-r9E4-n5Mbdk
    

    4.LV阶段

    //新建逻辑卷lvm,将刚刚建立的整个"lvm_test"磁盘都分配给"lvm"
    [root@bogon scsi_host]# lvcreate -l 2301 -n lvm lvm_test
      Logical volume "lvm" created.
    
    //查看lv状态
    [root@bogon scsi_host]# lvdisplay 
      --- Logical volume ---
      LV Path                /dev/lvm_test/lvm
      LV Name                lvm
      VG Name                lvm_test
      LV UUID                G2mlWA-fnlc-ClsK-wq5X-2Pfc-qvPB-1srSk7
      LV Write Access        read/write
      LV Creation host, time bogon, 2018-05-21 23:45:54 +0800
      LV Status              available
      # open                 0
      LV Size                <8.99 GiB
      Current LE             2301
      Segments               3
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     8192
      Block device           253:0
    

    5.文件系统阶段

    //格式化分区为ext4格式
    [root@bogon scsi_host]# mkfs.ext4 /dev/lvm_test/lvm
    mke2fs 1.42.9 (28-Dec-2013)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    589824 inodes, 2356224 blocks
    117811 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=2151677952
    72 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
    
    Allocating group tables: done                            
    Writing inode tables: done                            
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done 
    

    6.挂载该分区

    [root@bogon scsi_host]# mkdir /lvm
    [root@bogon scsi_host]# mount /dev/lvm_test/lvm /lvm/
    [root@bogon scsi_host]# df -h
    Filesystem                Size  Used Avail Use% Mounted on
    /dev/sda3                  14G  1.6G   13G  12% /
    devtmpfs                  479M     0  479M   0% /dev
    tmpfs                     489M     0  489M   0% /dev/shm
    tmpfs                     489M  6.6M  482M   2% /run
    tmpfs                     489M     0  489M   0% /sys/fs/cgroup
    /dev/sda1                 187M   97M   91M  52% /boot
    tmpfs                      98M     0   98M   0% /run/user/0
    /dev/mapper/lvm_test-lvm  8.8G   37M  8.3G   1% /lvm
    

    7.LVM容量的放大与缩小(ext4分区格式)

    //放大LVM容量
    //增加新分区/dev/sdb4,并创建为PV
    [root@bogon ~]# pvcreate /dev/sdb4
      Physical volume "/dev/sdb4" successfully created.
    
    //查看PV状态
    [root@bogon ~]# pvscan 
      PV /dev/sdb1   VG lvm_test        lvm2 [<3.00 GiB / 0    free]
      PV /dev/sdb2   VG lvm_test        lvm2 [<3.00 GiB / 0    free]
      PV /dev/sdb3   VG lvm_test        lvm2 [<3.00 GiB / 0    free]
      PV /dev/sdb4                      lvm2 [5.00 GiB]
      Total: 4 [<13.99 GiB] / in use: 3 [<8.99 GiB] / in no VG: 1 [5.00 GiB]
    
    //利用vgextend扩展VG
    [root@bogon ~]# vgextend lvm_test /dev/sdb4
      Volume group "lvm_test" successfully extended
    [root@bogon ~]# vgdisplay 
      --- Volume group ---
      VG Name               lvm_test
      System ID             
      Format                lvm2
      Metadata Areas        4
      Metadata Sequence No  4
      VG Access             read/write
      VG Status             resizable
      MAX LV                0
      Cur LV                1
      Open LV               0
      Max PV                0
      Cur PV                4
      Act PV                4
      VG Size               13.98 GiB
      PE Size               4.00 MiB
      Total PE              3580
      Alloc PE / Size       2301 / <8.99 GiB
      Free  PE / Size       1279 / <5.00 GiB
      VG UUID               QeVKgh-PIrY-HrmX-GY9G-25oF-r9E4-n5Mbdk
    
    //利用lvresize放大LV
    [root@bogon ~]# lvresize -l +1279 /dev/lvm_test/lvm
      Size of logical volume lvm_test/lvm changed from <8.99 GiB (2301 extents) to 13.98 GiB (3580 extents).
      Logical volume lvm_test/lvm successfully resized.
    
    [root@bogon ~]# lvdisplay 
      --- Logical volume ---
      LV Path                /dev/lvm_test/lvm
      LV Name                lvm
      VG Name                lvm_test
      LV UUID                G2mlWA-fnlc-ClsK-wq5X-2Pfc-qvPB-1srSk7
      LV Write Access        read/write
      LV Creation host, time bogon, 2018-05-21 23:45:54 +0800
      LV Status              available
      # open                 0
      LV Size                13.98 GiB
      Current LE             3580
      Segments               4
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     8192
      Block device           253:0
    
    [root@bogon ~]# e2fsck -f /dev/lvm_test/lvm
    e2fsck 1.42.9 (28-Dec-2013)
    Pass 1: Checking inodes, blocks, and sizes
    Pass 2: Checking directory structure
    Pass 3: Checking directory connectivity
    Pass 4: Checking reference counts
    Pass 5: Checking group summary information
    /dev/lvm_test/lvm: 11/589824 files (0.0% non-contiguous), 79025/2356224 blocks
    
    [root@bogon ~]# resize2fs /dev/lvm_test/lvm
    resize2fs 1.42.9 (28-Dec-2013)
    Resizing the filesystem on /dev/lvm_test/lvm to 3665920 (4k) blocks.
    The filesystem on /dev/lvm_test/lvm is now 3665920 blocks long.
    
    //缩小LVM容量
    //查看/dev/sdb3的大小
    [root@bogon ~]# pvdisplay 
      --- Physical volume ---
      PV Name               /dev/sdb1
      VG Name               lvm_test
      PV Size               3.00 GiB / not usable 4.00 MiB
      Allocatable           yes (but full)
      PE Size               4.00 MiB
      Total PE              767
      Free PE               0
      Allocated PE          767
      PV UUID               yc1XjR-923R-qkVu-Qq1t-3vXW-3gqQ-GHbAF0
       
      --- Physical volume ---
      PV Name               /dev/sdb2
      VG Name               lvm_test
      PV Size               3.00 GiB / not usable 4.00 MiB
      Allocatable           yes (but full)
      PE Size               4.00 MiB
      Total PE              767
      Free PE               0
      Allocated PE          767
      PV UUID               i3VapW-8nY5-npvy-tetM-tx6Y-4KSj-wF31nJ
       
      --- Physical volume ---
      PV Name               /dev/sdb3
      VG Name               lvm_test
      PV Size               3.00 GiB / not usable 4.00 MiB
      Allocatable           yes (but full)
      PE Size               4.00 MiB
      Total PE              767
      Free PE               0
      Allocated PE          767
      PV UUID               OGUix7-S1Fy-iIDF-1x1Z-clU7-MYZV-PEo4ZS
       
      --- Physical volume ---
      PV Name               /dev/sdb4
      VG Name               lvm_test
      PV Size               5.00 GiB / not usable 4.00 MiB
      Allocatable           yes (but full)
      PE Size               4.00 MiB
      Total PE              1279
      Free PE               0
      Allocated PE          1279
      PV UUID               afA2IN-HkJs-jEfX-0aot-nnXJ-ka2b-M8CF75
      
    //利用resize2fs减小文件系统容易
    //如分区已挂载,需要先卸载
    [root@bogon ~]# e2fsck -f /dev/lvm_test/lvm
    e2fsck 1.42.9 (28-Dec-2013)
    Pass 1: Checking inodes, blocks, and sizes
    Pass 2: Checking directory structure
    Pass 3: Checking directory connectivity
    Pass 4: Checking reference counts
    Pass 5: Checking group summary information
    /dev/lvm_test/lvm: 11/917504 files (0.0% non-contiguous), 100612/3665920 blocks
    
    [root@bogon ~]# resize2fs /dev/lvm_test/lvm 11G
    resize2fs 1.42.9 (28-Dec-2013)
    Resizing the filesystem on /dev/lvm_test/lvm to 2883584 (4k) blocks.
    The filesystem on /dev/lvm_test/lvm is now 2883584 blocks long.
    
    [root@bogon ~]# umount /dev/lvm_test/lvm
    
    [root@bogon ~]# lvresize -l -767 /dev/lvm_test/lvm 
    WARNING: Reducing active and open logical volume to <10.99 GiB.
    THIS MAY DESTROY YOUR DATA (filesystem etc.)
    Do you really want to reduce lvm_test/lvm? [y/n]: y
    Size of logical volume lvm_test/lvm changed from 13.98 GiB (3580 extents) to <10.99 GiB (2813 extents).
    Logical volume lvm_test/lvm successfully resized.
    [root@bogon ~]# vgreduce lvm_test /dev/sdb3
      Removed "/dev/sdb3" from volume group "lvm_test"
    [root@bogon ~]# pvscan 
      PV /dev/sdb1   VG lvm_test        lvm2 [<3.00 GiB / 0    free]
      PV /dev/sdb2   VG lvm_test        lvm2 [<3.00 GiB / 0    free]
      PV /dev/sdb4   VG lvm_test        lvm2 [<5.00 GiB / 0    free]
      PV /dev/sdb3                      lvm2 [3.00 GiB]
      Total: 4 [<13.99 GiB] / in use: 3 [<10.99 GiB] / in no VG: 1 [3.00 GiB]
    [root@bogon ~]# pvremove /dev/sdb3
      Labels on physical volume "/dev/sdb3" successfully wiped.
    

    8.lvm分区调整(xfs)

    
    [root@bogon ~]# lvextend -L +1G /dev/lvm_test/lvm 
    Size of logical volume lvm_test/lvm changed from <5.86 GiB (1500 extents) to <6.86 GiB (1756 extents).
    Logical volume lvm_test/lvm successfully resized.
    
    [root@bogon ~]# xfs_growfs /dev/lvm_test/lvm 
    meta-data=/dev/mapper/lvm_test-lvm isize=512    agcount=4, agsize=384000 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=0 spinodes=0
    data     =                       bsize=4096   blocks=1536000, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal               bsize=4096   blocks=2560, version=2
             =                       sectsz=512   sunit=0 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    data blocks changed from 1536000 to 1798144
    [root@bogon ~]# df -h
    Filesystem                Size  Used Avail Use% Mounted on
    /dev/sda3                  14G  1.6G   13G  12% /
    devtmpfs                  479M     0  479M   0% /dev
    tmpfs                     489M     0  489M   0% /dev/shm
    tmpfs                     489M  6.6M  482M   2% /run
    tmpfs                     489M     0  489M   0% /sys/fs/cgroup
    /dev/sda1                 187M   97M   91M  52% /boot
    tmpfs                      98M     0   98M   0% /run/user/0
    /dev/mapper/lvm_test-lvm  6.9G   33M  6.9G   1% /lvm
    

    lvm的使用场景:参考阿里云磁盘扩容文档 https://help.aliyun.com/document_detail/25452.html

    八、Parted

    MBR(Master Boot Record)称为主引导记录,是一种传统的分区机制,这种分区支持的磁盘大小有限,只可以支持 2T 以下的磁盘分区,(指整个磁盘不超过 2T)超过 2T 的磁盘,也只能划分出 2T 的使用空间,剩余出来的用不了,而 MBR 分区,最多4个主分区。

    GPT 全称 GUID Partition Table ,称为全局唯一标识分区表,是一种新的分机管理机制,解决了 MBR 分区表机制存在很多缺点,可以支持超过 2T 的硬盘,可以兼容 MBR。GPT 分区机制,不再有扩展分区和逻辑分区的概念,只有主分区,GPT 最大支持的分区可以达到 128 个。

    fdisk分区工具以前只支持MBR分区,对于小于2TB的磁盘,fdisk可以很好的完成对其分区的任务。但是当磁盘超过2TB时,fdisk就无法对其进行分区。目前fdisk已经实验性支持gpt分区了,但是需要慎重使用。所以更好的办法是借助parted这个工具。Parted支持MBR和GPT两种分区表类型。

    parted的常见用法如下

    1.将磁盘/dev/sdb更改为gpt分区表

    [root@localhost ~]# parted /dev/sdb
    (parted) p
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 21.5GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags:
    
    Number  Start  End  Size  Type  File system  Flags
    //修改为gpt分区
    
    (parted) mklabel gpt
    Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to
    continue?
    Yes/No? yes
    (parted) p
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 21.5GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags:
    
    Number  Start  End  Size  File system  Name  Flags
    

    2.新建分区

    //新建一个3G的主分区
    (parted) mkpart primary 1 3G
    (parted) p                                                                
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 21.5GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags: 
    
    Number  Start   End     Size    File system  Name     Flags
     1      1049kB  3000MB  2999MB               primary
    

    3.删除分区

    parted: invalid token: p
    Ignore/Cancel? Ignore
    (parted) p                                                                
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 21.5GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags: 
    
    Number  Start   End     Size    File system  Name     Flags
     1      1049kB  3000MB  2999MB               primary
     2      3000MB  6000MB  3000MB               primary
     3      6000MB  9000MB  3000MB               primary
    
    (parted) rm 3                                                             
    (parted) p                                                                
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 21.5GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags: 
    
    Number  Start   End     Size    File system  Name     Flags
     1      1049kB  3000MB  2999MB               primary
     2      3000MB  6000MB  3000MB               primary
    

    4.修改分区为mbr

    (parted) mklabel msdos
    Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to
    continue?
    Yes/No? yes                                                               
    (parted)                                                                  
    (parted)                                                                  
    (parted) p                                                                
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 21.5GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags: 
    
    Number  Start  End  Size  Type  File system  Flags
    

    5.新建主区和扩展分区

    (parted) mkpart primary 1 3G
    (parted) p                                                                
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 21.5GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags: 
    
    Number  Start   End     Size    Type     File system  Flags
     1      1049kB  3000MB  2999MB  primary
    
    (parted) mkpart primary 3G 6G
    (parted) p                                                                
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 21.5GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags: 
    
    Number  Start   End     Size    Type     File system  Flags
     1      1049kB  3000MB  2999MB  primary
     2      3000MB  6000MB  3000MB  primary
    
    (parted) p                                                                
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 21.5GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags: 
    
    Number  Start   End     Size    Type     File system  Flags
     1      1049kB  3000MB  2999MB  primary
     2      3000MB  6000MB  3000MB  primary
    
    (parted) mkpart extended 6G -1
    (parted) p                                                                
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 21.5GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags: 
    
    Number  Start   End     Size    Type      File system  Flags
     1      1049kB  3000MB  2999MB  primary
     2      3000MB  6000MB  3000MB  primary
     3      6000MB  21.5GB  15.5GB  extended               lba
    
    (parted) mkpart logical 6G 10G                                            
    (parted) p                                                                
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 21.5GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags: 
    
    Number  Start   End     Size    Type      File system  Flags
     1      1049kB  3000MB  2999MB  primary
     2      3000MB  6000MB  3000MB  primary
     3      6000MB  21.5GB  15.5GB  extended               lba
     5      6001MB  10.0GB  3999MB  logical
    

    注意: parted 不论是使用 交互模式,还是命令行模式,每次执行的命令立即生效,也就是说你,你创建一个分区,立即就会写入磁盘,而不是像 fdisk 那样,最后执行 w 的时候才会保存并写入到磁盘,parted 由于是实时生效,所以也不会有类似 fdisk 那样的 w 命令。

    九、DD命令

    dd命令可以用来创建空文件,还可以备份文件,创建物理硬盘的镜像。

    常见用法

    1.备份及恢复文件

    //备份/dev/sda1设备内容到/tmp/boot.dd
    //dd命令输入和输出都是文件,if表示从哪个文件读取,of表示写入到哪个文件去。bs表示块大小
    [root@localhost ~]# dd if=/dev/sda1 of=/tmp/boot.dd bs=1M
    200+0 records in
    200+0 records out
    209715200 bytes (210 MB) copied, 13.8842 s, 15.1 MB/s
    [root@localhost ~]# ll -h /tmp/boot.dd 
    -rw-r--r--. 1 root root 200M May 22 01:48 /tmp/boot.dd
    
    //如/dev/sda1损坏,可将备份文件/tmp/boot.dd恢复
    [root@localhost ~]# dd if=/tmp/boot.dd of=/dev/sda1 bs=1M
    200+0 records in
    200+0 records out
    209715200 bytes (210 MB) copied, 4.76358 s, 44.0 MB/s
    [root@localhost ~]# ls /boot/
    System.map-3.10.0-693.el7.x86_64
    config-3.10.0-693.el7.x86_64
    efi/
    grub/
    grub2/
    initramfs-0-rescue-2b3527d9534847f3abfab60f25abb1a1.img
    initramfs-3.10.0-693.el7.x86_64.img
    initrd-plymouth.img
    symvers-3.10.0-693.el7.x86_64.gz
    vmlinuz-0-rescue-2b3527d9534847f3abfab60f25abb1a1*
    vmlinuz-3.10.0-693.el7.x86_64*
    
    //备份磁盘
    [root@localhost ~]# dd if=/dev/sda of=/dev/sdb                            
    41943040+0 records in
    41943040+0 records out
    21474836480 bytes (21 GB) copied, 769.329 s, 27.9 MB/s
    //备份磁盘为img文件
    [root@localhost ~]# dd if=/dev/sda of=/mnt/fdb1/disk.img
    dd: writing to ‘/mnt/fdb1/disk.img’: No space left on device
    40895593+0 records in
    40895592+0 records out
    20938543104 bytes (21 GB) copied, 306.317 s, 68.4 MB/s
    //以压缩的方式备份磁盘为img
    //以gizp压缩
    [root@localhost ~]# dd if=/dev/sda | gzip > disk.img.gz
    41943040+0 records in
    41943040+0 records out
    21474836480 bytes (21 GB) copied, 654.122 s, 32.8 MB/s
    [root@localhost ~]# ls -lh disk.img.gz 
    -rw-r--r--. 1 root root 4.9G May 22 03:15 disk.img.gz
    //以bzip2压缩
    [root@localhost ~]# dd if=/dev/sda | bzip2 > disk.img.bz2
    41943040+0 records in
    41943040+0 records out
    21474836480 bytes (21 GB) copied, 2007.82 s, 10.7 MB/s
    //查看dd执行状态,因为dd执行过程比较久,可以将其放入后台执行
    [root@localhost ~]# ps aux | grep -w dd | grep -v grep | awk '{print $2}'
    2855
    查看状态
    [root@localhost ~]# kill -USR1 2855
    [root@localhost ~]# 2888921+0 records in
    2888921+0 records out
    1479127552 bytes (1.5 GB) copied, 177.429 s, 8.3 MB/s
    

    2.销毁数据

    [root@localhost ~]# dd if=/dev/urandom of=/dev/sdb5
    dd: writing to ‘/dev/sdb5’: No space left on device
    7811073+0 records in
    7811072+0 records out
    3999268864 bytes (4.0 GB) copied, 94.7671 s, 42.2 MB/s
    

    3.测试磁盘性能

    //测试纯写入速度
    //if=/dev/zero不生产I/O
    //默认写缓冲起作用(buffer)
    [root@localhost ~]# dd if=/dev/zero of=/tmp/dd.write bs=1024 count=1000000
    1000000+0 records in
    1000000+0 records out
    1024000000 bytes (1.0 GB) copied, 17.0442 s, 60.1 MB/s
    //同步写入数据,数据和属性同步更新
    [root@localhost ~]# dd if=/dev/zero of=/tmp/dd.write bs=1024 count=1000000 conv=fsync
    1000000+0 records in
    1000000+0 records out
    1024000000 bytes (1.0 GB) copied, 20.6838 s, 49.5 MB/s
    //同步写入数据,只影响文件的数据部分
    [root@localhost ~]# dd if=/dev/zero of=/tmp/dd.write bs=1024 count=1000000 conv=fdatasync
    1000000+0 records in
    1000000+0 records out
    1024000000 bytes (1.0 GB) copied, 12.1371 s, 84.4 MB/s
    
    //测试纯读取速度
    // /dev/null不生产I/O
    [root@localhost ~]# dd if=/tmp/dd.write of=/dev/null bs=128k
    7812+1 records in
    7812+1 records out
    1024000000 bytes (1.0 GB) copied, 8.72556 s, 117 MB/s
    
    //测试读写速度
    [root@localhost ~]# dd if=/tmp/dd.write of=/tmp/dd1 bs=64k
    15625+0 records in
    15625+0 records out
    1024000000 bytes (1.0 GB) copied, 26.0144 s, 39.4 MB/s
    

    十、raid

    独立硬盘冗余阵列(RAID, Redundant Array of Independent Disks),旧称廉价磁盘冗余阵列(Redundant Array of Inexpensive Disks),简称磁盘阵列。

    RAID比单颗硬盘有以下一个或多个方面的好处:增强数据集成度,增强容错功能,增加处理量或容量。另外,磁盘阵列对于电脑来说,看起来就像一个单独的硬盘或逻辑存储单元。

    RAID 级别有许多种,各有各的限制和优点。选择哪种 RAID 级别较为合适完全取决于用户自身的需要。

    RAID 0:条带集 -(或条带卷)将数据平均分布到两个或两个以上磁盘中,不带冗余奇偶校验信息。

    RAID 1:镜像集 – 在两个或两个以上磁盘上创建一组数据的镜像或原样副本,提供针对单一磁盘故障的保护。

    RAID 0+1:条带的镜像 – 此种级别的 RAID 用于在多个磁盘之间共享和复制数据。

    RAID 5:带奇偶校验的条带集– 需要至少三个磁盘,将带有奇偶校验数据的数据块级条带分布到所有磁盘中,这是一种实现低价冗余的理想方法。

    RAID 10:镜像的条带 – 有时也称为 RAID 1+0,它与 RAID 0+1 类似,但既然 RAID 10 是镜像的条带,所以它与 RAID 0+1 恰好相反。

    串联:JBOD – “简单磁盘捆绑”,是一种将多个磁盘组合成单一虚拟硬盘的常见方法,这种方法无法享受 RAID 优势。

    RAID 术语

    为更好地了解 RAID 的工作方式,首先应熟悉以下术语:

    条带化是指将数据分到多个驱动器上。条带 RAID 阵列通常用于将最大的容量合并到单个卷中。

    镜像是指将数据复制到多个磁盘上。镜像 RAID 阵列通常能在阵列中有磁盘(至少一个)发生故障时确保数据不丢失,具体取决于阵列的 RAID 级别。

    容错可让 RAID 阵列在磁盘发生故障时继续工作(即用户仍然可以使用阵列中存储的数据)。不过,并不是所有镜像 RAID 阵列都是用户友好的。例如,有些 RAID 设备必须在关闭后才能更换发生故障的磁盘,而设备支持“热插拔”式磁盘更换,因此可以在设备开启、而且数据可供访问的情况下更换故障磁盘。

    常见RAID 级别

    RAID 0

    RAID 0(在某些设备中也称为 FAST 模式)是速度最快的一种 RAID 模式。它需要至少两个驱动器,并且会将数据分条到每个磁盘。所有磁盘的可用容量合在一起,成为计算机上的一个逻辑卷。一旦阵列中有一个物理磁盘发生故障,所有磁盘的数据都将不可访问,因为有部分数据已被写入所有磁盘。

    2zGHY.png

    RAID 1

    RAID 1(在某些设备中也称为 SAFE 模式)是一种安全的 RAID 模式,它至少需要两个驱动器,并且驱动器成对工作。计算机上将安装一个逻辑卷,两个驱动器合并后的可用容量限制为不超过最小磁盘的容量。如果其中一个物理磁盘出现故障,可以立即从第二个磁盘上获取数据。即使一个磁盘出现故障,也不会丢失任何数据。

    2zOty.png

    RAID 3

    RAID 3 使用字节级别的条带化技术,并采用专用的奇偶校验磁盘(右侧图例中的磁盘 4),因此计算机上会安装一个卷。RAID 3 阵列能在一个磁盘出现故障的情况下确保数据不丢失。如果一个物理磁盘出现故障,该磁盘上的数据可以重建到更换磁盘上。如果数据尚未重建到更换驱动器上,而此时又有一个磁盘出现故障,那么阵列中的所有数据都将丢失。

    2zp3X.png

    RAID 3+Spare

    RAID 3+Spare模式下,阵列中会有一个磁盘保持空置状态。如果阵列中有驱动器发生故障,故障磁盘中的数据就会自动重建到空磁盘(或称为“备用”磁盘)上。

    RAID 5

    RAID 5 综合了 RAID 0 的条带化技术以及阵列数据冗余技术(阵列最少包括三个磁盘)。

    RAID 3 和 RAID 5 之间的区别在于,RAID 3 配置提供的性能更高,但总容量略低。数据会在所有磁盘之间分条,并且每个数据块的奇偶校验块 (P) 写入到同一条带上。如果一个物理磁盘出现故障,该磁盘上的数据可以重建到更换磁盘上。单个磁盘出现故障时,数据不会丢失,但如果数据尚未重建到更换驱动器上,而此时又有一个磁盘出现故障,那么阵列中的所有数据都将丢失。

    2zuLJ.jpg

    RAID 6

    在 RAID 6 中,数据会在所有磁盘(最少四个)间进行分条,并且每个数据块的两个奇偶校验块(如右图中的 p 和 q)写入到同一条带上。如果一个物理磁盘出现故障,该磁盘上的数据可以重建到更换磁盘上。这种 RAID 模式最多允许两个磁盘出故障而不丢失数据,而且它能更快地重建故障磁盘上的数据。

    2zZY6.jpg

    RAID 0+1

    RAID 0+1 是一种安全的 RAID 模式,由条带集的镜像组成。RAID 0+1 阵列包含的磁盘数应为四的倍数。在 RAID 0+1 阵列中,具有五个磁盘的存储产品的第五个磁盘将成为备用磁盘或空置磁盘。在右图中,阵列 B 是阵列 A 的镜像。

    在 RAID 0+1 阵列中,最多允许两个磁盘出现故障而不会丢失数据,但故障磁盘必须属于同一 RAID 0 队列。在图中,也就是当磁盘 1 和磁盘 2 出现故障时,数据会保存到磁盘 3 和磁盘 4。
    2zhPp.jpg

    RAID 10

    RAID 10(也称为 RAID 1+0)是合并了其他级别(尤其是 RAID 1 和 RAID 0)特点的另一种 RAID 级别。这是一种“镜像集条带”,意思是数据在两个镜像阵列间分条。“条带化”在阵列之间发生,而“镜像”是在相同的阵列中出现,两种技术的组合加快了重建的速度。RAID 10 阵列包含的磁盘数应为四的倍数。在 RAID 10 阵列中第五个磁盘将成为备用磁盘或空置磁盘。请参见下图。

    在 RAID 10 阵列中,每个镜像对中可以有一个磁盘出现故障而不丢失数据。不过,故障磁盘所在阵列的工作磁盘会成为整个阵列中的弱点。如果镜像对中的另一个磁盘也发生故障,则会丢失整个阵列。

    2za4G.jpg

    十一、存储基础知识

    常用的硬盘

    1、SATA盘:比较早的硬盘,现在基本不用了,特点是存储容量大,价格低;缺点是读取速度慢,不适合频繁的操作。

    2、SAS盘:串行SCSI盘,现在比较流行的盘,特点是读取速度快,价格适中,性价比高。

    3、SSD盘:固态硬盘,特点是读取速度极快,价格昂贵,生命周期短暂,性价比底。

    4、NL-SAS:近线SAS,盘体采用的SATA,接口采用SAS,结合了两者的优点于一身,主要用在容量盘中。

    存储常用的协议

    1、ATA和SATA:其实这个定义可以不用记住,因为是比较老的东西,现在用的也不多,大家了解一下就可以了。

    ATA是一种很早就出现的接口类型,后者是前者的改进即串行的ATA。

    2、SCSI:这个出现的也是比较早,最先由IBM公司提出来,前期用于小型机的接口,全称叫“小型计算机系统接口”S(small)C(computer)S(system)I(interface)。后来又衍生出ISCSI。

    3、SAS:就是串行的SCSI。

    4、FC:光纤通道协议,它是光纤传输使用的协议。

    网络存储的常见模式

    DAS
    直连式存储(Direct-AttachedStorage)。在这种方式中,存储设备是通过电缆(通常是SCSI接口电缆)直接到服务器的。DAS方案中外接式存储设备目前主要是指RAID、JBOD等。

    NAS
    网络附属存储(Network Attached Storage),NAS是在网络中放置一个单独的存储服务器,此存储服务器开启网络共享。

    SAN
    存储区域网络(Storage Area Network),SAN连接又分ISCSI(网口)SAS(SAS口)以及FC(光纤口)连接
    注:这种连接需要单独的存储产品。可以通过交换机连接。

    2YVzd.png

    iSCSI
    iSCSI是IETF提出的经TCP/IP/以太网传送SCSI指令的协议。通过iSCSI协议,标准的SCSI命令和数据将被封装成一个以iSCSI头在先的连续字节串,该字节串被送到TCP/IP层,并被分解成适合网络传输的数据组后,交给主机和适用与iSCSI协议的存储设备

    2YYhR.png

    参考文章
    https://my.oschina.net/u/3804239/blog/1792030
    https://my.oschina.net/u/3804239/blog/1793641'
    http://blog.51cto.com/13646023/2097201
    https://www.2cto.com/os/201204/126370.html
    https://www.cnblogs.com/kevingrace/p/5825963.html
    https://blog.csdn.net/caipeichao2/article/details/52661825
    http://zuyunfei.com/2015/06/04/linux-dd/
    http://forum.huawei.com/enterprise/zh/thread-274145-1-1.html
    https://blog.csdn.net/JesseYoung/article/details/38367217

  • 相关阅读:
    java实现快速排序
    java实现简单回文算法
    HTML条件注释
    LeetCode 将有序数组转换为二叉搜索树
    LeetCode 有效的数独
    Leetcode 买卖股票的最佳时机 II
    模拟登陆163邮箱
    oracle 视图的相关操作
    oracle 约束类别
    软件测试用例知识点梳理
  • 原文地址:https://www.cnblogs.com/minn/p/9074443.html
Copyright © 2011-2022 走看看