千家信息网

Linux格式化数据盘

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,环境文件系统:ext4分区类型:MBROS:CentOS7创建分区[root@CentOS7 ~]# echo -e "n\n\n\n\n\nw" | fdisk /dev/sdbWelcome to
千家信息网最后更新 2025年02月03日Linux格式化数据盘

环境

  • 文件系统:ext4
  • 分区类型:MBR
  • OS:CentOS7

创建分区

[root@CentOS7 ~]# echo -e "n\n\n\n\n\nw" | fdisk /dev/sdbWelcome 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 tableBuilding a new DOS disklabel with disk identifier 0xb2302c66.Command (m for help): Partition type:   p   primary (0 primary, 0 extended, 4 free)   e   extendedSelect (default p): Using default response pPartition number (1-4, default 1): First sector (2048-41943039, default 2048): Using default value 2048Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): Using default value 41943039Partition 1 of type Linux and of size 20 GiB is setCommand (m for help): The partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.[root@CentOS7 ~]# fdisk -l /dev/sdbDisk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0xb2302c66   Device Boot      Start         End      Blocks   Id  System/dev/sdb1            2048    41943039    20970496   83  Linux
ps:仅对/dev/sdb磁盘划分了一个单分区,所有空间都给了它

创建文件系统

[root@CentOS7 ~]# mkfs.ext4 /dev/sdb1mke2fs 1.42.9 (28-Dec-2013)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks1310720 inodes, 5242624 blocks262131 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=2153775104160 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks:     32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,     4096000Allocating group tables: done                            Writing inode tables: done                            Creating journal (32768 blocks): doneWriting superblocks and filesystem accounting information: done

mount挂载

[root@CentOS7 ~]# mkdir /mnt/sdb1 ; mount /dev/sdb1 /mnt/sdb1[root@CentOS7 ~]# findmnt /mnt/sdb1 TARGET    SOURCE    FSTYPE OPTIONS/mnt/sdb1 /dev/sdb1 ext4   rw,relatime,data=ordered

永久挂载

[root@CentOS7 ~]# echo -e "/dev/sdb1\t/mnt/sdb1\text4\tdefaults\t0\t0" >> /etc/fstab [root@CentOS7 ~]# sed -n '$p' /etc/fstab /dev/sdb1   /mnt/sdb1   ext4    defaults    0   0
/etc/fstab解释
每行定义一个要挂载的文件系统
第一列:要挂载的设备或伪文件系统设备文件LABEL:LABEL=""UUID:UUID=""伪文件系统名称:proc, sysfs第二列:挂载点第三列:文件系统类型:ext4,xfs,iso9660,nfs,none第四列:挂载选项:defaults ,acl,bind第五列:转储频率0:不做备份 1:每天转储 2:每隔一天转储第六列:fsck检查的文件系统的顺序,允许的数字是0 1 20:不自检1:首先自检;一般只有rootfs才用2:非rootfs使用

磁盘管理常用命令

同步分区表
查看内核是否已经识别新的分区  cat /proc/partitions  通知内核重新读取硬盘分区表  CentOS6:partx /dev/DEVICE  CentOS5,7:partprobe /dev/DEVICE  
创建文件系统
mkfs命令:  1)mkfs.FS_TYPE /dev/DEVICE  2)mkfs -t FS_TYPE /dev/DEVICE  
文件系统标签
blkid:查找/打印块设备属性  Usage: blkid -L 
文件系统检测和修复

文件系统故障常发生于死机或者非正常关机之后,挂载为文件系统标记为"no clean"
注意:一定不要挂载状态下执行下面命令修复

fsck:检查并修复Linux文件系统fsck.FS_TYPEfsck -t FS_TYPE注意:FS_TYPE一定要与分区上的文件类型相同-a 自动修复-r 交互式修复错误e2fsck:ext系列文件专用的检测修复工具-y 自动回答yes-f 强制修复-p 自动进行安全的修复文件系统问题xfs_repair:xfs文件系统专用检测修复工具-f 修复设备文件-n 只检查-d 允许修复只读的挂载设备,在单用户下修复/时使用,然后立即reboot
挂载mount
挂载方法:mount DEVICE MOUNT_POINTmount:通过查看/etc/mtab文件显示当前已挂载的所有设备mount [-fnrsvw] [-t vfstype] [-o options] device dir  常用命令选项:-t vsftype 指定要挂载的设备上的文件系统类型-r readonly,只读挂载-w read and write, 读写挂载-n 不更新/etc/mtab,mount不可见-a 自动挂载所有支持自动挂载的设备(定义在了/etc/fstab文件中,且挂载选项中有auto功能)-L 'LABEL' 以卷标指定挂载设备-U 'UUID' 以UUID指定要挂载的设备-B, --bind 绑定目录到另一个目录上-o options:(挂载文件系统的选项),多个选项使用逗号分隔async 异步模式 sync 同步模式,内存更改时,同时写磁盘atime/noatime  包含目录和文件diratime/nodiratime 目录的访问时间戳auto/noauto 是否支持自动挂载,是否支持-a选项exec/noexec 是否支持将文件系统上运行应用程序dev/nodev 是否支持在此文件系统上使用设备文件suid/nosuid 是否支持suid和sgid权限remount 重新挂载ro 只读 rw 读写user/nouser 是否允许普通用户挂载此设备,/etc/fstab使用acl 启用此文件系统上的acl功能loop 使用loop设备_netdev 当网络可用时才对网络资源进行挂载,如:NFS文件系统defaults 相当于rw, suid, dev, exec, auto, nouser, async查看内核追踪到的已挂载的所有设备cat /proc/mounts
卸载
查看挂载情况  findmnt MOUNT_POINT|device查看正在访问指定文件系统的进程lsof MOUNT_POINTfuser -v MOUNT_POINT终止所有在正访问指定的文件系统的进程fuser -km MOUNT_POINT卸载umount DEVICEumount MOUNT_POINT
0