欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

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

程序员文章站 2022-05-29 12:35:29
fdisk 常用的磁盘分区工具,受mbr分区表的限制,只能给小于2TB的磁盘划分分区,如果使用fdisk对大于2TB的磁盘进行分区,虽然可以分区,但只能识别2T的空间,一般使用parted分区工具 -l # 显示所有磁盘分区信息 Device # 分区名称 Boot # 启动分区,用*表示的是启动分 ......

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  # 强制执行操作