一.磁盘管理的基本命令

linux学习之路---磁盘管理

[[email protected] ~]# df -lht tmpfs#统计tmpfs的磁盘里的以1024进制显示的磁盘容量
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           498M     0  498M   0% /dev/shm


[[email protected] ~]# df -lhTt tmpfs
Filesystem     Type   Size  Used Avail Use% Mounted on
tmpfs          tmpfs  498M     0  498M   0% /dev/shm


linux学习之路---磁盘管理


[[email protected] ~]# du -sb error.log
30 error.log
[[email protected] ~]# du -sm error.log
1 error.log


二.磁盘分区

linuxMBR分区:
        在生活中如果我们发现硬盘不够用的情况,你可以自行先添加一个硬盘.我们在这里虚拟机里模拟添加了 一个8G大小的硬盘

linux学习之路---磁盘管理
这是我在虚拟机模拟 添加的10G硬盘

接下来分区步骤:

1.执行命令 [[email protected] ~]# fdisk -l

你会发现这是你新添加的硬盘空间 

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


2.执行命令[[email protected] ~]# fdisk /dev/sdb


在光标处写上命令 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
   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

 
Command action
   e   extended#扩展分区
   p   primary partition (1-4)#主分区

p#执行主分区代码
Partition number (1-4): 1#选择分区号码
First cylinder (1-1305, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +3000M#设置主分区的大小

e#执行扩展分区代码
Partition number (1-4): 2
First cylinder (384-1305, default 384): 
Using default value 384
Last cylinder, +cylinders or +size{K,M,G} (384-1305, default 1305): 
Using default value 1305

在扩展分区里再分逻辑分区
l#执行逻辑分区代码

l
First cylinder (384-1305, default 384): 
Using default value 384
Last cylinder, +cylinders or +size{K,M,G} (384-1305, default 1305): +5000M

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (1022-1305, default 1022): 
Using default value 1022
Last cylinder, +cylinders or +size{K,M,G} (1022-1305, default 1305): 
Using default value 1305

//分了两个逻辑分区后查看结果
Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x71f9b38a

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         383     3076416   83  Linux
/dev/sdb2             384        1305     7405965    5  Extended
/dev/sdb5             384        1021     5124703+  83  Linux
/dev/sdb6            1022        1305     2281198+  83  Linux

//此时如果你觉得哪个分区不对,想重新分,可以删除掉,因为这个时候并没有完成保存操作

Command (m for help): d
Partition number (1-6): 6

//最后保存阶段
Command (m for help): w
The partition table has been altered!


Calling ioctl() to re-read partition table.
Syncing disks.

//然后你可以执行fdisk -l查看结果


linux学习之路---磁盘管理
linuxGPT分区:

//第一步:启动parted工具
[[email protected] ~]# parted
GNU Parted 2.1
使用 /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.

//选择自己刚添加的硬盘
(parted) select /dev/sdc
使用 /dev/sdc

//做个分区标识为gpt
(parted) mklabel gpt
 (parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start  End  Size  File system  Name  标志

//开始分区(交互式)

(parted) mkpart
 分区名称?  []? chp
 文件系统类型?  [ext2]? 
 起始点? 1
 结束点? 2000
                       
//命令式

(parted) mkpart abc 2000 3000 

单位是Mb

如若修改单位可以(parted) unit GB

最后,(parted) quit


三.分区格式化

[[email protected] ~]# ls -h /dev/sdb*
/dev/sdb  /dev/sdb1  /dev/sdb2  /dev/sdb5  /dev/sdb6
[[email protected] ~]# ls -l /dev/sdb*
brw-rw----. 1 root disk 8, 16 1月   9 21:51 /dev/sdb
brw-rw----. 1 root disk 8, 17 1月   9 21:48 /dev/sdb1
brw-rw----. 1 root disk 8, 18 1月   9 21:48 /dev/sdb2
brw-rw----. 1 root disk 8, 21 1月   9 21:48 /dev/sdb5
brw-rw----. 1 root disk 8, 22 1月   9 21:48 /dev/sdb6
[[email protected] ~]# mkfs -t ext4 /dev/sdb1#将dev下的sdb1文件格式化为ext4类型


注意:但只能格式化主分区,不能格式化逻辑分区


四.挂载分区

animal  cgroup  etc   lib64       misc  opt   sbin     sys  var
bin     chp     home  lost+found  mnt   proc  selinux  tmp
boot    dev     lib   media       net   root  srv      usr
[[email protected] ~]# mkdir -p /mnt/test
[[email protected] ~]# mount /dev/sdb1 /mnt/test


一般我们都在mnt系统文件夹下创立一个文件夹 .利用mount命令将文件挂载进去

自动挂载:[[email protected] test]# vim + /etc/fstab

 在该文件最后一行添加命令
/dev/sdb1               /mnt/test               ext4    defaults        0 0
设备名                    挂载点                 文件系统的名称

五.交换分区

linux学习之路---磁盘管理

 Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         383     3076416   83  Linux
/dev/sdb2             384        1305     7405965    5  Extended
/dev/sdb5             384        1021     5124703+  83  Linux
/dev/sdb6            1022        1305     2281198+  83  Linux

选择一个分区

Command (m for help): t
Partition number (1-6): 6

列出编码表
Hex code (type L to list codes): L

选择82为交换区编码

Hex code (type L to list codes): 82
Changed system type of partition 6 to 82 (Linux swap / Solaris)
Command (m for help): w


格式化分区

[[email protected] ~]# mkswap /dev/sdb6
Setting up swapspace version 1, size = 2281192 KiB
no label, UUID=3c6800a6-e1f8-40ce-89f9-e1049e5e2a3a

启用分区
[[email protected] ~]# swapon /dev/sdb6
             total       used       free     shared    buffers     cached
Mem:       1019884     183076     836808          0      14304      56868
-/+ buffers/cache:     111904     907980
Swap:      4329180          0    4329180

关闭分区
[[email protected] ~]# swapoff /dev/sdb6



相关文章: