Linux格式化數據盤

環境

  • 文件系統: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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章