温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux格式化数据盘

发布时间:2020-07-07 20:40:32 来源:网络 阅读:222 作者:何小帅 栏目:系统运维

环境

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

创建分区

[root@CentOS7 ~]# echo -e "n\n\n\n\n\nw" | 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 0xb2302c66.

Command (m for help): 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): Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.
[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
Disk label type: dos
Disk identifier: 0xb2302c66

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41943039    20970496   83  Linux
ps:仅对/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
1310720 inodes, 5242624 blocks
262131 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2153775104
160 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

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing 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 2
0:不自检
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 <label> | -U <uuid>

findfs:通过标签或UUID查找文件系统
Usage:
 findfs [options] {LABEL,UUID,PARTUUID,PARTLABEL}=<value

e2label:更改ext2 / ext3 / ext4文件系统上的标签
 e2label device [ new-label ]

tune2fs:调整ext2 / ext3 / ext4文件系统上的可调文件系统参数
-l 列出文件系统超级块的内容,包括可以通过该程序设置的参数的当前值

dumpe2fs:显示ext文件系统信息,将磁盘块分组管理
-h:查看超级块信息,不显示分组信息

xfs_info:显示已挂载的 xfs 文件系统信息
  xfs_info mountpoint
文件系统检测和修复

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

fsck:检查并修复Linux文件系统
fsck.FS_TYPE
fsck -t FS_TYPE
注意:FS_TYPE一定要与分区上的文件类型相同
-a 自动修复
-r 交互式修复错误

e2fsck:ext系列文件专用的检测修复工具
-y 自动回答yes
-f 强制修复
-p 自动进行安全的修复文件系统问题

xfs_repair:xfs文件系统专用检测修复工具
-f 修复设备文件
-n 只检查
-d 允许修复只读的挂载设备,在单用户下修复/时使用,然后立即reboot
挂载mount
挂载方法:mount DEVICE MOUNT_POINT
mount:通过查看/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_POINT
fuser -v MOUNT_POINT

终止所有在正访问指定的文件系统的进程
fuser -km MOUNT_POINT

卸载
umount DEVICE
umount MOUNT_POINT
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI