4.1 df命令
4.2 du命令
4.3/4.4 磁盘分区
4.1 df命令
扩展学习 parted分区gpt格式
磁盘监控的使用率在日常监控工作中是必须要做的,磁盘被写满是很要命的,严重的情况会导致磁盘损坏。
命令df(disk filesystem的简写)用于查看已挂载磁盘的总容量、使用容量、剩余容量等,可以不加任何参数,默认以KB为单位。
使用man df查看df命令。
df - report file system disk space usage(汇报文件系统磁盘空间使用情况)
示例如下:
[root@centos7 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 18802688 1337364 17465324 8% /
devtmpfs 491212 0 491212 0% /dev
tmpfs 500668 0 500668 0% /dev/shm
tmpfs 500668 6776 493892 2% /run
tmpfs 500668 0 500668 0% /sys/fs/cgroup
/dev/sda1 201388 105144 96244 53% /boot
tmpfs 100136 0 100136 0% /run/user/0
[root@centos7 ~]#
第1列是文件系统,也就是磁盘分区的名字。
第2列是磁盘的总大小,单位是KB。
第3列是已经使用多少,单位是KB。
第4列是还有多少能用的,单位是KB。
第5列是已用百分比,这一列最为重要,到了80%就要注意了。
第6列是挂载点,这个挂载点实际就是系统里的一个目录。linux磁盘是不能直接访问的,必须要有一个挂载点,我们通过这个挂载点才能找到这块磁盘,才能进入这块磁盘里,读数据,写数据。
/和/boot使我们在安装系统时划分出来的。/dev、/dev/shm为内存分区,默认大小为物理内存大小的1/2,如果我们把文件放到这个分区下,相当于存到了内存中,好处是读写非常快,坏处是系统重启时文件就会丢失。后面的/run、/sys/fs/cgroup等分区都是tmpfs,跟/dev/shm类似,为临时文件系统,我们不用碰它们。df命令的常用选项有-i、-h、-k和-m,下面介绍这4个选项的用法。
-h:表示可以根据磁盘的大小,适当的显示单位。
[root@centos7 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.3G 17G 8% /
devtmpfs 480M 0 480M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.7M 483M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 197M 103M 94M 53% /boot
tmpfs 98M 0 98M 0% /run/user/0
[root@centos7 ~]#
磁盘可用单位:Byte(字节)、KB、MB、GB、TB。
/swap用df命令看不到,使用free可以看到。
[root@centos7 ~]# free
total used free shared buff/cache available
Mem: 1001336 144928 716964 6780 139444 709428
Swap: 1952764 0 1952764
[root@centos7 ~]#
-i:表示查看inodes的使用情况,如已使用100%,即使磁盘空间有富余,也会提示磁盘空间已满。
[root@centos7 ~]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda3 18812928 35727 18777201 1% /
devtmpfs 122803 366 122437 1% /dev
tmpfs 125167 1 125166 1% /dev/shm
tmpfs 125167 427 124740 1% /run
tmpfs 125167 13 125154 1% /sys/fs/cgroup
/dev/sda1 204800 330 204470 1% /boot
tmpfs 125167 1 125166 1% /run/user/0
[root@centos7 ~]#
电脑下面有多少个inode,以及inode使用情况。我们创建了分区以后,还要格式化,然后才能往里面写数据。格式化的过程,就是创建inode的过程。也就是这个inode在你格式化的时候给你预分配好的了,你这个分区多大,有多少inode,是事先弄好了的。inode多少跟磁盘分区大小有关系。
有这么一种情况,磁盘空间还有,但是就是写不进入数据,就可以通过df -i 查看一下inode的使用情况,看看是不是inode使用满了。
-k、-m:分别表示以KB和MB为单位显示。
[root@centos7 ~]# df -k | grep -v tmpfs
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 18802688 1337364 17465324 8% /
/dev/sda1 201388 105144 96244 53% /boot
[root@centos7 ~]# df -m | grep -v tmpfs
Filesystem 1M-blocks Used Available Use% Mounted on
/dev/sda3 18362 1307 17056 8% /
/dev/sda1 197 103 94 53% /boot
[root@centos7 ~]#
4.2 du命令
命令du(disk useage)用来查看某个目录或文件所占空间的大小,其格式如下:
du [-abckmsh] [文件或者目录名],常用的选项如下:
-a:表示全部文件和目录的大小都列出来。如果后面不加任何选项和参数,则只会列出目录(包括子目录)的大小。如果du命令不指定单位的话,默认显示单位为KB。
[root@tianqi-01 tmp]# du /tmp/aming1
0 /tmp/aming1/1/2
0 /tmp/aming1/1
0 /tmp/aming1/2
0 /tmp/aming1/aminglinux/1/2
0 /tmp/aming1/aminglinux/1
0 /tmp/aming1/aminglinux/2
0 /tmp/aming1/aminglinux
0 /tmp/aming1
[root@tianqi-01 tmp]# du -a /tmp/aming1
0 /tmp/aming1/1/2
0 /tmp/aming1/1
0 /tmp/aming1/2
0 /tmp/aming1/aminglinux/1/2
0 /tmp/aming1/aminglinux/1
0 /tmp/aming1/aminglinux/2
0 /tmp/aming1/aminglinux
0 /tmp/aming1
[root@tianqi-01 tmp]#
-b:表示列出的值以B为单位输出。
-k:表示以KB为单位输出,这和默认不加任何选项的输出值是一样的。
-m:表示以MB为单位输出。
-h:表示系统自动调节单位。例如,如果文件太小,可能就几千字节,就以KB为单位显示;如果文件大到千兆字节,就以GB为单位显示。若一个文件小于4KB,当使用-k选项时,也会显示4KB。同理,使用-m选项时,也会有类似问题。
[root@tianqi-01 tmp]# du -b /etc/passwd
2270 /etc/passwd
[root@tianqi-01 tmp]# du -k /etc/passwd
4 /etc/passwd
[root@tianqi-01 tmp]# du -m /etc/passwd
1 /etc/passwd
[root@tianqi-01 tmp]# du -h /etc/passwd
4.0K /etc/passwd
[root@tianqi-01 tmp]#
-c:表示最后加总,这个选项不常用。
[root@tianqi-01 tmp]# du -c /tmp/aming1
0 /tmp/aming1/1/2
0 /tmp/aming1/1
0 /tmp/aming1/2
0 /tmp/aming1/aminglinux/1/2
0 /tmp/aming1/aminglinux/1
0 /tmp/aming1/aminglinux/2
0 /tmp/aming1/aminglinux
0 /tmp/aming1
0 总用量
[root@tianqi-01 tmp]#
-s:表示只列出总和,这个选项用的多。
[root@tianqi-01 tmp]# du -s /tmp/aming1
0 /tmp/aming1
[root@tianqi-01 tmp]#
常用的是du -sh命令,用来查看一个文件的大小。
[root@centos7 ~]# du -sh /root
40K /root
[root@centos7 ~]# du -sh /boot/
93M /boot/
[root@centos7 ~]# du -sh /etc/passwd
4.0K /etc/passwd
[root@centos7 ~]#
之前使用的命令
[root@centos7 ~]# ls -lh /etc/passwd
-rw-r--r--. 1 root root 1.2K Dec 27 05:41 /etc/passwd
[root@centos7 ~]#
/etc/passwd文件只有1.2k大小,使用du -sh查看的/etc/passwd有4.0k,大小不同的原因是因为涉及到了块的概念。
如果文件大小小于4KB,也会显示4KB,因为文件划分成了很多块,每一块就是4KB。不够一个块,也会占一个块的大小,所以一个块只会有一个文件。
[root@centos7 ~]# du /etc/passwd
4 /etc/passwd
[root@centos7 ~]#
du一个目录,会列出里面所有的文件
[root@tianqi-01 ~]# du /root/
16 /root/split_dir
0 /root/testdir
0 /root/.pki/nssdb
0 /root/.pki
0 /root/.cache
12 /root/.ssh
4 /root/dir2
0 /root/234/am
0 /root/234/am1
0 /root/234
0 /root/111
0 /root/123
152 /root/
[root@tianqi-01 ~]#
这样显示不太友好,第一我们不知道单位,第二我们也不清楚它列的都是什么,没有目的性。所以,建议以后du后面加-s,
[root@tianqi-01 ~]# du -s /root/
152 /root/
[root@tianqi-01 ~]#
如果没有显示任何单位,就是152k。
[root@tianqi-01 ~]# du -sh /root/
152K /root/
[root@tianqi-01 ~]#
加上之后就很容易识别了。
4.3/4.4 磁盘分区
在日常工作中,接触比较多的是给系统增加一些磁盘,然后再划分分区,再挂载。
下面给虚拟机增加一块磁盘,步骤如下:
1、在当前虚拟机选项卡点击鼠标右键,选择设置;
2、出现虚拟机设置,点击添加,选择硬盘,确定。保持默认,下一步,下一步,磁盘大小给10G,下一步,完成,确定。
其实,在服务器上是支持热拔插硬盘的,也就是在服务器开机的情况下直接把硬盘插上去,就可以识别。但是在虚拟机这儿,是无法识别的。
命令fdisk
fdisk是linux下硬盘的分区工具,是一个非常实用的命令,但是fdisk只能划分小于2TB的分区。该命令格式如下:
fdisk [-l] [设备名称],其选项只有-l。选项-l后面不加设备名称,会直接列出系统中所有的磁盘设备以及分区表;加上设备名称,则会列出该设备的分区表。
[root@centos7 ~]# fdisk -l
Disk /dev/sda: 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: 0x0007b41a
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 4317183 1952768 82 Linux swap / Solaris
/dev/sda3 4317184 41943039 18812928 83 Linux
[root@centos7 ~]#
这里没有识别到,只有一块盘。要想识别新插的磁盘,重启即可。
[root@centos7 ~]# fdisk -l
Disk /dev/sdb: 10.7 GB, 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
Disk /dev/sda: 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: 0x0007b41a
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 4317183 1952768 82 Linux swap / Solaris
/dev/sda3 4317184 41943039 18812928 83 Linux
[root@centos7 ~]#
重启过后,就是别到了一块新的硬盘Disk /dev/sdb。
使用w命令查看负载情况。
[root@centos7 ~]# w
05:33:13 up 2 min, 2 users, load average: 0.08, 0.13, 0.06
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 05:30 2:33 0.01s 0.01s -bash
root pts/0 192.168.11.1 05:30 1.00s 0.02s 0.01s w
[root@centos7 ~]#
load average: 0.08, 0.13, 0.06,0.08这里的数字越高,说明负载越高。
fdisk命令不加-l选项,则会进入另一个模式,在该模式下,可以对磁盘进行分区操作。只有给磁盘划分分区,才可以使用它。
有的人也会不划分分区,整块盘直接格式化也是可以的。
[root@centos7 ~]# 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 0x1daff5be.
Command (m for help):
输入m可以获取帮助信息,会列出常用的命令:
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system 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:创建一个新的分区
P:打印当前磁盘的分区情况
w:保存
d:删除一个分区
q:退出
先p一下,发现没有任何分区
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0x1daff5be
Device Boot Start End Blocks Id System
Command (m for help):
现在增加一个分区,输入n
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
之后又出现2个,需要选择分区类型,p是主分区,e是扩展分区。这里的分区和windows相似,这里的fdisk有一个格式:MBR分区,MBR分区有个特点,最高支持2TB。超过2TB就不能使用fdisk划分分区了,而且这个分区最多只有4个主分区。
主分区最多有4个,如果想有多个,可以划分3个主分区,1个扩展分区;扩展分区里面再划分,可以划分逻辑分区。逻辑分区分的有很多,足够使用。
有一个公式:主分区+扩展分区<=4。
下面划分一个主分区,输入p,会提示分区数,默认为1,因为这是第1个分区(也可以写成2或3,单最多是4);
如果直接回车,会继续提示你必须输入一个数字,紧接着又提示你起始扇区从哪里开始,默认是2048,可以写成2048或者直接回车(这里可以写成大于2048的其他数,但会造成空间的浪费);
然后提示你输入最后一个扇区的数值,即需要给这个扇区划分多大空间,这里写成+2G即可。
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-20971519, default 2048): 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +2G
Partition 1 of type Linux and of size 2 GiB is set
Command (m for help):
此时p一下,会发现多了一个分区。
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0x1daff5be
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
Command (m for help):
按照上面的步骤,一直创建主分区到4。
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +2G
Partition 1 of type Linux and of size 2 GiB is set
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (4196352-20971519, default 4196352):
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-20971519, default 20971519): +2G
Partition 2 of type Linux and of size 2 GiB is set
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (8390656-20971519, default 8390656): +1G
Value out of range.
First sector (8390656-20971519, default 8390656):
Using default value 8390656
Last sector, +sectors or +size{K,M,G} (8390656-20971519, default 20971519):
Using default value 20971519
Partition 3 of type Linux and of size 6 GiB is set
Command (m for help): d
Partition number (1-3, default 3): 3
Partition 3 is deleted
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (8390656-20971519, default 8390656):
Using default value 8390656
Last sector, +sectors or +size{K,M,G} (8390656-20971519, default 20971519): +1G
Partition 3 of type Linux and of size 1 GiB is set
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0xfee3b991
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 8390655 2097152 83 Linux
/dev/sdb3 8390656 10487807 1048576 83 Linux
Command (m for help): n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): p
Selected partition 4
First sector (10487808-20971519, default 10487808):
Using default value 10487808
Last sector, +sectors or +size{K,M,G} (10487808-20971519, default 20971519): +1G
Partition 4 of type Linux and of size 1 GiB is set
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0xfee3b991
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 8390655 2097152 83 Linux
/dev/sdb3 8390656 10487807 1048576 83 Linux
/dev/sdb4 10487808 12584959 1048576 83 Linux
Command (m for help): n
If you want to create more than four partitions, you must replace a
primary partition with an extended partition first.
Command (m for help):
建立了4个主分区以后,就不能再建立分去了,那么现在需要删除一个分区了。
Command (m for help): d
Partition number (1-4, default 4): 4
Partition 4 is deleted
Command (m for help):
查看一下,现在有3个主分区
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0xfee3b991
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 8390655 2097152 83 Linux
/dev/sdb3 8390656 10487807 1048576 83 Linux
Command (m for help):
再分一个扩展分区,分配4个G。
Command (m for help): n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): e
Selected partition 4
First sector (10487808-20971519, default 10487808):
Using default value 10487808
Last sector, +sectors or +size{K,M,G} (10487808-20971519, default 20971519): +4G
Partition 4 of type Extended and of size 4 GiB is set
Command (m for help):
再p一下查看4个分区
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0xfee3b991
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 8390655 2097152 83 Linux
/dev/sdb3 8390656 10487807 1048576 83 Linux
/dev/sdb4 10487808 18876415 4194304 5 Extended
Command (m for help):
如果再创建一个分区,此时会发生变化,不需要再选择主分区还是扩展分区,而是直接定义分区大小。
Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (10489856-18876415, default 10489856):
Using default value 10489856
Last sector, +sectors or +size{K,M,G} (10489856-18876415, default 18876415): +1G
Partition 5 of type Linux and of size 1 GiB is set
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0xfee3b991
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 8390655 2097152 83 Linux
/dev/sdb3 8390656 10487807 1048576 83 Linux
/dev/sdb4 10487808 18876415 4194304 5 Extended
/dev/sdb5 10489856 12587007 1048576 83 Linux
Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (12589056-18876415, default 12589056):
Using default value 12589056
Last sector, +sectors or +size{K,M,G} (12589056-18876415, default 18876415): +1G
Partition 6 of type Linux and of size 1 GiB is set
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0xfee3b991
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 8390655 2097152 83 Linux
/dev/sdb3 8390656 10487807 1048576 83 Linux
/dev/sdb4 10487808 18876415 4194304 5 Extended
/dev/sdb5 10489856 12587007 1048576 83 Linux
/dev/sdb6 12589056 14686207 1048576 83 Linux
Command (m for help):
现在删除分区1,则分区从2开始。再删除一个逻辑分区5,再看一下。然后现在还有一个db5,现在的db5跟之前的db5是不一样的,可以从start和end处查看不同,原来的db6变成了db5。
如果此时按q键,不保存就退出的话,就可以重新划分分区。
Command (m for help): d
Partition number (2-6, default 6): 5
Partition 5 is deleted
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0xc668f9cb
Device Boot Start End Blocks Id System
/dev/sdb2 4196352 8390655 2097152 83 Linux
/dev/sdb3 8390656 10487807 1048576 83 Linux
/dev/sdb4 10487808 18876415 4194304 5 Extended
/dev/sdb5 12589056 14686207 1048576 83 Linux
Command (m for help):
[root@localhost ~]# 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 0x66bb2f44.
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0x66bb2f44
Device Boot Start End Blocks Id System
Command (m for help):
我们不划分主分区,直接划分扩展分区。
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): e
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +3G
Partition 1 of type Extended and of size 3 GiB is set
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0x66bb2f44
Device Boot Start End Blocks Id System
/dev/sdb1 2048 6293503 3145728 5 Extended
再增加一个主分区,此时提示发生了改变,此时会提示是要主分区还是逻辑分区。默认为2,可以选择3。
Command (m for help): n
Partition type:
p primary (0 primary, 1 extended, 3 free)
l logical (numbered from 5)
Select (default p): p
Partition number (2-4, default 2): 3
First sector (6293504-20971519, default 6293504):
Using default value 6293504
Last sector, +sectors or +size{K,M,G} (6293504-20971519, default 20971519): +1G
Partition 3 of type Linux and of size 1 GiB is set
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0x66bb2f44
Device Boot Start End Blocks Id System
/dev/sdb1 2048 6293503 3145728 5 Extended
/dev/sdb3 6293504 8390655 1048576 83 Linux
Command (m for help):
现在划分一个逻辑分区。
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
First sector (4096-6293503, default 4096):
Using default value 4096
Last sector, +sectors or +size{K,M,G} (4096-6293503, default 6293503): +1G
Partition 5 of type Linux and of size 1 GiB is set
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0x66bb2f44
Device Boot Start End Blocks Id System
/dev/sdb1 2048 6293503 3145728 5 Extended
/dev/sdb3 6293504 8390655 1048576 83 Linux
/dev/sdb5 4096 2101247 1048576 83 Linux
Command (m for help):
逻辑分区是从db5开始的,前4个分区是留给主分区和扩展分区的,逻辑分区是连续的。
继续划分逻辑分区,到8逻辑分区。
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 6
First sector (2103296-6293503, default 2103296):
Using default value 2103296
Last sector, +sectors or +size{K,M,G} (2103296-6293503, default 6293503): +100M
Partition 6 of type Linux and of size 100 MiB 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 7
First sector (2310144-6293503, default 2310144):
Using default value 2310144
Last sector, +sectors or +size{K,M,G} (2310144-6293503, default 6293503): +100M
Partition 7 of type Linux and of size 100 MiB 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 8
First sector (2516992-6293503, default 2516992):
Using default value 2516992
Last sector, +sectors or +size{K,M,G} (2516992-6293503, default 6293503): +100M
Partition 8 of type Linux and of size 100 MiB is set
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0x66bb2f44
Device Boot Start End Blocks Id System
/dev/sdb1 2048 6293503 3145728 5 Extended
/dev/sdb3 6293504 8390655 1048576 83 Linux
/dev/sdb5 4096 2101247 1048576 83 Linux
/dev/sdb6 2103296 2308095 102400 83 Linux
/dev/sdb7 2310144 2514943 102400 83 Linux
/dev/sdb8 2516992 2721791 102400 83 Linux
Command (m for help):
此时删除6逻辑分区,再看
Command (m for help): d
Partition number (1,3,5-8, default 8): 6
Partition 6 is deleted
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0x66bb2f44
Device Boot Start End Blocks Id System
/dev/sdb1 2048 6293503 3145728 5 Extended
/dev/sdb3 6293504 8390655 1048576 83 Linux
/dev/sdb5 4096 2101247 1048576 83 Linux
/dev/sdb6 2310144 2514943 102400 83 Linux
/dev/sdb7 2516992 2721791 102400 83 Linux
Command (m for help):
此时退出,查看没有任何分区。
Command (m for help): q
[root@localhost ~]# fdisk -l
Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000c51b7
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 782335 390144 83 Linux
/dev/sda2 782336 9170943 4194304 82 Linux swap / Solaris
/dev/sda3 9170944 62914559 26871808 83 Linux
Disk /dev/sdb: 10.7 GB, 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
[root@localhost ~]#
此时重新分区,就分1个区,给5G,w保存退出。
[root@localhost ~]# 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 0xb0c618aa.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +5G
Partition 1 of type Linux and of size 5 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]#
[root@localhost ~]# fdisk -l
Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000c51b7
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 782335 390144 83 Linux
/dev/sda2 782336 9170943 4194304 82 Linux swap / Solaris
/dev/sda3 9170944 62914559 26871808 83 Linux
Disk /dev/sdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0xb0c618aa
Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux
[root@localhost ~]#
如果不想要这个分区,可以重新分区,删掉之前的分区,保存即可。
[root@localhost ~]# 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.
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0xb0c618aa
Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux
Command (m for help): d
Selected partition 1
Partition 1 is deleted
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]#
扩展分区不能格式化,它只是一个壳子,里面真正装的是逻辑分区。
扩展学习 parted分区gpt格式
我们在课上讲的fdisk分区工具,它的分区格式为MBR,特点是,最多分4个主分区,磁盘大小不能超过2T。而GPT分区格式,突破了这些限制,它没有主分区、扩展分区、逻辑分区之分,在一块磁盘上最多可以分128个分区出来,支持大于2T的分区,最大卷可达18EB。 相信,随着存储级别的升级,将来的分区格式逐渐会淘汰MBR,而GPT成为主流。
parted 工具常用功能:
当在命令行输入parted后,进入parted命令的交互模式。输入help会显示帮助信息。下面就简单介绍一下常用的功能
1、check 简单检查文件系统。建议用其他命令检查文件系统,比如fsck
2、help 显示帮助信息
3、mklabel 创建分区表, 即是使用msdos(MBR)还是使用gpt,或者是其他方式分区表
4、 mkfs 创建文件系统。该命令不支持ext3 格式,因此建议不使用,最好是用parted分好区,然后退出parted交互模式,用其他命令进行分区,比如:mkfs.ext3
5、mkpart 创建新分区。
格式:mkpart PART-TYPE [FS-TYPE] START END
PART-TYPE 类型主要有primary(主分区), extended(扩展分区), logical(逻辑区). 扩展分区和逻辑分区只对msdos。
fs-type 文件系统类型,主要有fs32,NTFS,ext2,ext3等
start end 分区的起始和结束位置。
6、mkpartfs 建立分区及其文件系统。目前还不支持ext3文件系统,因此不建议使用该功能。最后是分好区后,退出parted,然后用其他命令建立文件系统。
7、print 输出分区信息。该功能有3个选项,
free 显示该盘的所有信息,并显示磁盘剩余空间
number 显示指定的分区的信息
all 显示所有磁盘信息
8、resize 调整指定的分区的大小。目前对ext3格式支持不是很好,所以不建议使用该功能。
9、rescue 恢复不小心删除的分区。如果不小心用parted的rm命令删除了一个分区,那么可以通过rescue功能进行恢复。恢复时需要给出分区的起始和结束的位置。然后parted就会在给定的范围内去寻找,并提示恢复分区。
10、rm 删除分区。命令格式 rm number 。如:rm 3 就是将编号为3的分区删除
11、select 选择设备。当输入parted命令后直接回车进入交互模式是,如果有多块硬盘,需要用select 选择要操作的硬盘。如:select /dev/sdb
12、set 设置标记。更改指定分区编号的标志。标志通常有如下几种:boot hidden raid lvm 等。
boot 为引导分区,hidden 为隐藏分区,raid 软raid,lvm 为逻辑分区。
如:set 3 boot on 设置分区号3 为启动分区
注:以上内容为parted常用的功能,由于该工具目前对ext3支持得不是很好,因此有些功能无法应用,比如move(移动分区)和resize等。
parted分区功能事例。
1、用命令模式 为/dev/sdb创建gpt类型文件分区表,并分500G分区。然后为该分区创建ext3文件系统。并将该分区挂载在/test文件夹下。
# parted /dev/sdb mklabel —创建分区表
# parted /dev/sdb mkpart ext3 0 500000 —创建500G分区/dev/sdb1
# mkfs.ext3 /dev/sdb1 —-将分区/dev/sdb1格式化成ext3格式文件系统
# mount /dev/sdb1 /test —将/dev/sdb1 挂载在/test下
如果让系统自动挂载/dev/sdb1 需手工编辑/etc/fstab文件。并在文件末尾添加如下内容:
/dev/sdb1 /test ext3 defaults 0 0
2、创建大小为4G的交互分区。由于已经创建了500G的/dev/sdb1 ,因此再创建的分区为/dev/sdb2
# parted /dev/sdb mkpart swap 500000 504000 —创建4G分区/dev/sdb2
# mkswap /dev/sdb2 —-将/dev/sdb2创建为交换分区
# swapon /dev/sdb2 —-激活/dev/sdb2
如果让系统自动挂载/dev/sdb2这个交换分区,需手工编辑/etc/fstab文件。并在文件末尾添加如下内容:
/dev/sdb2 swap swap defaults 0 0
3、恢复被误删除的分区(也可以参考testdisk命令)。由于parted直接写磁盘,因此一旦不小心删除了某一分区,建议立即用rescue恢复。下面通过事例来理解恢复过程。
# parted /dev/sdb mkpart ext3 504000 514000 —-创建10G分区/dev/sdb3
# mkfs.ext3 /dev/sdb3 —将/dev/sdb3格式化成ext3文件系统。
# parted /dev/sdb rm 3 —-删除/dev/sdb3
# parted /dev/sdb rescue 504000 514000 —依照屏幕提示,输入yes即可恢复被误删除分区
1、首先类似fdisk一样,先选择要分区的硬盘,此处为/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)
2、选择了/dev/sdb作为我们操作的磁盘,接下来需要创建一个分区表(在parted中可以使用help命令打印帮助信息):
(parted) mklabel
New disk label type? gpt
#默认为msdos形式的分区,我们要正确分区大于2TB的磁盘,应该使用gpt方式的分区表,输入gpt后回车。
Warning: The existing disk label on /dev/sdb will be
destroyed and all data on this disk will be lost. Do you want
to continue?
Yes/No? yes
(parted)
3、创建好分区表以后,接下来就可以进行分区操作了,执行mkpart命令,分别输入分区名称,文件系统和分区 的起止位置
(parted) mkpart
Partition name? []? /dev/sdb1
File system type? [ext2]? ext3
Start? 0
End? 5G
Warning: The resulting partition is not properly aligned for
best performance.
Ignore/Cancel? I
(parted)
4、分好区后可以使用print命令打印分区信息,下面是一个print的样例
(parted) print
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 /dev/sdb1
(parted)
5、如果分区错了,可以使用rm命令删除分区,比如我们要删除上面的分区,然后打印删除后的结果
(parted) rm 1
(parted)
6、按照上面的方法把整个硬盘都分好区,下面是一个分完后的样例
(parted) mkpart
Partition name? []? /dev/sdb1
File system type? [ext2]? ext3
Start? 0
End? 5G
Warning: The resulting partition is not properly aligned for
best performance.
Ignore/Cancel? i
(parted) mkpart
Partition name? []? /dev/sdb2
File system type? [ext2]? ext3
Start? 5G
End? 7G
(parted) print
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 /dev/sdb1
2 5001MB 7000MB 2000MB /dev/sdb2
(parted) q
Information: You may need to update /etc/fstab.
7、由于parted内建的mkfs还不够完善,所以完成以后我们可以使用quit命令退出parted并使用 系统的mkfs命令对分区进行格式化了,此时如果使用fdisk -l命令打印分区表会出现警告信息,这是正常的
[root@centos7 /]# fdisk -l
Disk /dev/sda: 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: 0x0007b41a
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 4317183 1952768 82 Linux swap / Solaris
/dev/sda3 4317184 41943039 18812928 83 Linux
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/sdb: 10.7 GB, 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
Disk label type: gpt
# Start End Size Type Name
1 34 9765625 4.7G Microsoft basic /dev/sdb1
2 9766912 13672447 1.9G Microsoft basic /dev/sdb2
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/sdc: 10.7 GB, 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
Disk label type: gpt
# Start End Size Type Name
1 34 9765625 4.7G Microsoft basic /dev/sdc1
2 9766912 13672447 1.9G Microsoft basic /dev/sdc2
[root@centos7 /]#
[root@centos7 /]# mkfs.ext3 /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
305216 inodes, 1220699 blocks
61034 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1254096896
38 block groups
32768 blocks per group, 32768 fragments per group
8032 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 /]# mkfs.ext3 /dev/sdb2
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
122160 inodes, 488192 blocks
24409 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=503316480
15 block groups
32768 blocks per group, 32768 fragments per group
8144 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
[root@centos7 /]# mkdir /dp1 /dp2
[root@centos7 /]# mount /dev/sdb1 /dp1
[root@centos7 /]# mount /dev/sdb1 /dp2
[root@centos7 /]#
友情链接:阿铭Linux