作死命令[root@linux ~]# mkfs.ext4 /dev/sda1
命令解释
mkfs命令是Linux系统中格式化磁盘分区的工具,mkfs后加一个点再跟上一个文件系统的格式表明要将目标分区格式化成什么文件系统,如本次的mkfs.ext4 /dev/sda1 就是把系统的第一块硬盘的第一个分区格式化成ext4格式的文件系统,一般情况下,系统的第一个磁盘的第一个分区是系统分区,不是/boot分区就是/分区,相当重要,类似于Windows中的C盘,如果将其格式化了那岂不就凉凉了>_<
命案现场
现场分析
和Windows一样,由于/dev/sda1已经作为系统根目录挂载到文件系统根结点上,此时该分区的状态是“busy”的,系统出于保护目的,无法将其格式化,正如在正在运行的Windows中格式化它的系统分区一样,无法格式化
相关拓展
Linux系统下与磁盘分区相关的有fdisk、fsck、mkfs、mount等这些命令,fdisk是用来操作磁盘分区表相关的更改,比如更改分区表格式,创建分区表,新建/删除分区等,mkfs则是在创建分区之后负责将分区格式化的工具,mount则是将分区挂载到Linux的文件树中。我以向Linux系统添加了一块全新的磁盘以拓展存储空间为例。各种命令的详细使用方法使用man查看,此处不再翻译。
向Linux添加硬盘
在添加磁盘之前,先执行fdisk -l列出系统中的物理磁盘,记录下来,方便与添加磁盘之后做对比,找到新添加的磁盘设备号。root@linux:~# fdisk -l
Disk /dev/sda: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0x25241c74
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 25165823 25163776 12G 83 Linux
/dev/sda2 25167870 41940991 16773122 8G 5 Extended
/dev/sda5 25167872 41940991 16773120 8G 82 Linux swap / Solaris
可以看到,目前系统只安装了一块硬盘sda,有三个分区,现在可以关机加硬盘了。加完硬盘后开机再执行fdisk -lroot@linux:~# fdisk -l
Disk /dev/sda: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0x25241c74
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 25165823 25163776 12G 83 Linux
/dev/sda2 25167870 41940991 16773122 8G 5 Extended
/dev/sda5 25167872 41940991 16773120 8G 82 Linux swap / Solaris
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 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
可以看到,新添加的硬盘的设备号为sdb,没有分区表,没有分区。接下来就用fdisk /dev/sdb来创建分区表和分区。root@linux:~# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.27.1).
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.
Created a new DOS disklabel with disk identifier 0x95942ae2.
Command (m for help):
不知道怎么操作的此时可以按m调出帮助界面Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
先创建一个GPT分区表,按gCommand (m for help): g
Created a new GPT disklabel (GUID: F4A12897-62F7-4ABA-9BC3-88BF53550DE3).
分区表创建后创建分区,按n回车,Partition number、First sector、Last sector参数不清楚的可以直接回车使用默认参数。Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-20971486, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971486, default 20971486):
Created a new partition 1 of type 'Linux filesystem' and of size 10 GiB.
此时已经在这块新加的10GB的硬盘中创建了一个10GB的分区。
最后按w将更改信息写入硬盘。Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
接下来mkfs.ext4 /dev/sdb1格式化该分区root@linux:~# mkfs.ext4 /dev/sdb1
mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 2621179 4k blocks and 655360 inodes
Filesystem UUID: 886f1d1a-a3ad-4bdf-89b3-54ee0c9238f2
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
在文件系统中创建一个空目录作为挂载点,并将分区挂载到挂载点root@linux:~# mkdir /mnt/new_mountpoint
root@linux:~# mount -t ext4 /dev/sdb1 /mnt/new_mountpoint/
此时/mnt/new_mountpoint/下面的存储空间就是此次新添加的10GB磁盘的存储空间,可以用df -h查看root@linux:~# df -h
Filesystem Size Used Avail Use% Mounted on
udev 3.9G 0 3.9G 0% /dev
tmpfs 799M 9.1M 790M 2% /run
/dev/sda1 12G 4.5G 6.7G 40% /
tmpfs 3.9G 188K 3.9G 1% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
tmpfs 799M 24K 799M 1% /run/user/108
tmpfs 799M 0 799M 0% /run/user/1000
/dev/sdb1 9.8G 23M 9.2G 1% /mnt/new_mountpoint