Linux磁盘与文件系统管理(一)

时间:2019-04-19
本文章向大家介绍Linux磁盘与文件系统管理(一),主要包括Linux磁盘与文件系统管理(一)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

fdisk

  常用的磁盘分区工具,受mbr分区表的限制,只能给小于2TB的磁盘划分分区,如果使用fdisk对大于2TB的磁盘进行分区,虽然可以分区,但只能识别2T的空间,一般使用parted分区工具

  -l  # 显示所有磁盘分区信息

[root@puppet105 ~]# fdisk -l
Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 sectors    # 磁盘/dev/sda 的大小
Units = sectors of 1 * 512 = 512 bytes                  # 每个柱面大小512字节
Sector size (logical/physical): 512 bytes / 512 bytes        # 每个扇区的字节数
I/O size (minimum/optimal): 512 bytes / 512 bytes          # 每次读写的字节数
Disk label type: dos
Disk identifier: 0x000a9f20

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 4196351 1048576 82 Linux swap / Solaris /dev/sda3 4196352 125829119 60816384 8e Linux LVM Disk /dev/mapper/centos_centos7-root: 62.3 GB, 62272831488 bytes, 121626624 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

     Device  # 分区名称

  Boot    # 启动分区,用*表示的是启动分区

  Start    # 表示开始的柱面

  End    # 表示结束的柱面

  Blocks    # block块数量

  Id      # 分区类型ID

  System    # 分区类型

  磁盘分区步骤

# 加了一块20G的磁盘
[root@centos7 ~]# fdisk -l /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

#  查看分区前设备状态

[root@centos7 ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sdb

# 开始分区
[root@centos7 ~]# fdisk /dev/sdb        # 后面直接跟设备文件,或者-cu参数
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 0xfe05c2f7. Command (m for help): m             # 输入m是帮助菜单 Command action a toggle a bootable flag           # 设置引导扇区 b edit bsd disklabel              # 编辑bsd卷标 c toggle the dos compatibility flag      # 设置dos兼容扇区 d delete a partition                # 删除一个分区 g create a new empty GPT partition table    # 创建空白gpt分区表 G create an IRIX (SGI) partition table      # 创建IRIX分区表 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      # 创建新的空sun卷标 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                # 创建一个新分区 Partition type: p primary (0 primary, 0 extended, 4 free)  # 主分区,只能创建4个,第五个必须为扩展分区 e extended                      # 扩展分区 Select (default p): p                  # 创建主分区标识 Partition number (1-4, default 1): 1          #编号(1-4) First sector (2048-41943039, default 2048):     # 起始位置,直接回车,默认是1 Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G # 设置主分区大小 Partition 1 of type Linux and of size 5 GiB is set Command (m for help): n              # 创建一个新分区 Partition type: p primary (1 primary, 0 extended, 3 free) e extended                    # 创建一个扩展分区 Select (default p): e Partition number (2-4, default 2): 2        # 编号2 First sector (10487808-41943039, default 10487808): Using default value 10487808 Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +5G  # 也是5G的容量 Partition 2 of type Extended and of size 5 GiB is set Command (m for help): n              # 创建一个新分区 Partition type: p primary (1 primary, 1 extended, 2 free)  # 此时因为创建了扩展分区,所以只剩下主分区和逻辑分区了 l logical (numbered from 5) Select (default p): l                # 创建逻辑分区 Adding logical partition 5              # 逻辑分区编号必须是从5开始,默认的就是5 First sector (10489856-20973567, default 10489856): Using default value 10489856 Last sector, +sectors or +size{K,M,G} (10489856-20973567, default 20973567): +2G  # 给2G的容量 Partition 5 of type Linux and of size 2 GiB is set 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: 0xfe05c2f7 Device Boot Start End Blocks Id System /dev/sdb1 2048 10487807 5242880 83 Linux /dev/sdb2 10487808 20973567 5242880 5 Extended /dev/sdb5 10489856 14684159 2097152 83 Linux Command (m for help): w                # 保存 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@centos7 ~]# ls /dev/sdb*            # 查看设备状态 /dev/sdb /dev/sdb1 /dev/sdb2 /dev/sdb5 [root@centos7 ~]# partprobe /dev/sdb        # 通知内核分区表已经更改 [root@centos7 ~]# mkfs.ext4 /dev/sdb1        #格式化文件系统 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 327680 inodes, 1310720 blocks 65536 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1342177280 40 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 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done [root@centos7 ~]# tune2fs -c 0 -i 0 /dev/sdb1    # 此命令可以米面磁盘过载自动检查 tune2fs 1.42.9 (28-Dec-2013) Setting maximal mount count to 1 [root@centos7 ~]# df -h            # 格式化后的磁盘需要挂载才能使用,此处未显示已经创建的磁盘 Filesystem Size Used Avail Use% Mounted on /dev/sda3 98G 1.7G 97G 2% / devtmpfs 901M 0 901M 0% /dev tmpfs 911M 0 911M 0% /dev/shm tmpfs 911M 9.5M 902M 2% /run tmpfs 911M 0 911M 0% /sys/fs/cgroup /dev/sda1 1014M 135M 880M 14% /boot tmpfs 183M 0 183M 0% /run/user/0 [root@centos7 ~]# mount /dev/sdb1 /mnt    # 挂载磁盘分区 [root@centos7 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 98G 1.7G 97G 2% / devtmpfs 901M 0 901M 0% /dev tmpfs 911M 0 911M 0% /dev/shm tmpfs 911M 9.5M 902M 2% /run tmpfs 911M 0 911M 0% /sys/fs/cgroup /dev/sda1 1014M 135M 880M 14% /boot tmpfs 183M 0 183M 0% /run/user/0 /dev/sdb1 4.8G 20M 4.6G 1% /mnt      # 磁盘已经挂载上去了    [root@centos7 ~]# vi /etc/fstab      # 开机启动有两种方式,此为第一种,另外一种 echo "mount /dev/sdb1 /mnt" >>/etc/rc.local # # /etc/fstab # Created by anaconda on Fri Oct 26 21:32: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=fc662da3-4f5b-480b-bae9-cc9c8698742a / xfs defaults 0 0 UUID=e1b67cc9-08ff-47e7-b1e9-3ce2d9ee5eb6 /boot xfs defaults 0 0 UUID=47b26d55-1602-48e1-adba-91fe4f420b03 swap swap defaults 0 0 /dev/sdb1 /mnt ext4 defaults 0 0

  # 关于LVM进行分区,会单独写博文

  fdisk非交互式分区(批量创建)

[root@centos7 ~]# fdisk /dev/sdb<<EOF
> n
> p
> 3
> 
> +2G
> p
> w
> EOF
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.


Command (m for help): Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): Partition number (3,4, default 3): First sector (20973568-41943039, default 20973568): Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): Partition 3 of type Linux and of size 2 GiB is set

Command (m for help): 
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: 0xfe05c2f7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux
/dev/sdb2        10487808    20973567     5242880    5  Extended
/dev/sdb3        20973568    25167871     2097152   83  Linux
/dev/sdb5        10489856    14684159     2097152   83  Linux

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

partprobe

  在硬盘分区发生改变时,更新Linux内核中的硬盘分区表数据。不需要重启系统就能生效

tnue2fs

  调整或者查看ex2/ex3/3x4文件系统的参数,如可以调整文件系统开机自检周期,用的较少

  -c  # 设置强制自检的挂载次数,没挂载一次计数就会加1,超过次数就会强制自检,设置0或-1,此功能关闭

  -i  # 强制自检的时间间隔

  -l  # 查案文件系统信息

parted

 大于2TB磁盘的分区命令,需要将磁盘转换为GPT格式

   -l  # 小时需哦有磁盘分区的信息

    -h  # 查看帮助

[root@centos7 ~]# parted 
GNU Parted 3.1    # 版本号
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) h           # 查看帮助 
align
-check TYPE N  #检查磁盘分区,type分为min/opt,N表示磁盘分区号
mklabel,mktable LABEL
-TYPE # 创建分区表 table)     mkpart PART-TYPE [FS-TYPE] START END # 创建分区 name NUMBER NAME # 为分区命名 print [devices|free|list,all|NUMBER] # 显示分区表信息,简写p quit # 退出 resizepart NUMBER END # 重设分区大小 rm NUMBER # 删除编号number的分区 select DEVICE #选择要编辑的设备 set NUMBER FLAG STATE # 改变分区标志 toggle [NUMBER [FLAG]] # 设置分区标志 unit UNIT   # 设置默认单位 copyright information of GNU Parted (parted) quit

  分区步骤,本次使用20GB来做演示

[root@centos7 ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sdb
[root@centos7 ~]# parted /dev/sdb      # 开始分区,交互式
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt              # 为sdb磁盘创建GPT分区表,大于2T必须执行此步骤,fdisk 是mdos分区表

(parted) mkpart primary 0 5G    # 创建主分区
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? Ignore             # 忽略掉                                            
(parted) p                            #显示分区表                                   
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  5000MB  5000MB               primary    #主分区创建完毕,创建完成之后系统会自动识别,

(parted) mkpart logical 5G 10G                  # 创建逻辑分区
(parted) p                                                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  5000MB  5000MB               primary
 2      5001MB  10.0GB  5000MB               logical

(parted) quit                                                             
Information: You may need to update /etc/fstab.

[root@centos7 ~]# ls /dev/sd*        # 查看分区的设备
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sdb  /dev/sdb1  /dev/sdb2

# 后面的格式化分区,挂载,开机自动挂载和fdisk分区步骤一样,因为parted在创建分区时,会自动让内核识别,所以不用使用partprobe命令


  # fdisk非交互式分区(批量创建)

  parted /dev/sdb mklabel gpt yes

  parted /dev/sdb mkpart primary 0 5G

  parted /dev/sdb p

mkfs

  创建Linux文件系统,只有格式化创建文件系统后,才能存储数据

  -t  #指定要创建的文件系统类型

  -c  # 创建文件系统时检查磁盘坏块

  -v  # 显示详细信息

  

[root@centos7 ~]# mkfs    # 此命令是一个前端命令,会通过-t参数调用mkfs.fstype 因此可以直接使用mkfs.ext4创建
mkfs         mkfs.cramfs  mkfs.ext3    mkfs.minix   
mkfs.btrfs   mkfs.ext2    mkfs.ext4    mkfs.xfs  

dumpe2fs

  用于导出ext2/ext3/ext4文件系统内部的相关信息,如,文件系统的组成bao包含超级块,块组,inode,bock等信息

  -b  # 打印文件系统中预留的块信息

  -h  # 进现实超级块信息

  -i  # 从指定的文件系统影像文件中读取文件的系统信息

  -x  # 以16进制格式打印信息块成员

resize2fs

  用于扩容或收缩未挂载的ext2,3,4文件系统,在2.6内核版本或者更高,还支持在线扩容已挂在的文件系统,常用开针对LVM扩容后的分区使用,对于fdisk和parted应事先规划好

  -p  # 打印完成任务的进度条

  -r  # 强制执行操作