环境说明

vmware虚拟机

操作系统

# lsb_release -a
LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID: CentOS
Description:    CentOS release 6.5 (Final)
Release:        6.5
Codename:       Final

 

查看磁盘

一、df命令查看磁盘分区情况

# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        18G  5.5G   12G  33% /
tmpfs           936M  224K  936M   1% /dev/shm
/dev/sda1       194M   34M  151M  19% /boot
/dev/sr0        4.2G  4.2G     0 100% /media/CentOS_6.5_Final

 

解释下输出的第一行中某些字段的含义:

文件系统容量已用可用挂载点
代表磁盘的分区和内存中的某一个区域。磁盘总共的容量磁盘已经使用的容量磁盘未使用的容量该文件系统挂载在哪一个目录下

 

使用iT参数,查看 inodes号和文件类型可man查看参数

# df -iT 
Filesystem     Type     Inodes  IUsed  IFree IUse% Mounted on
/dev/sda3      ext4    1166880 248576 918304   22% /
tmpfs          tmpfs    239440      5 239435    1% /dev/shm
/dev/sda1      ext4      51200     39  51161    1% /boot
/dev/sr0       iso9660       0      0      0     - /media/CentOS_6.5_Final

 

 

二、利用du命令查询当前目录下的所有目录/文件所占的容量

显示目录或者文件所占空间:

 

 

# du
16      ./triWeb/templates
84      ./triWeb
28      ./article
32      ./pyweb
152     .

 

只显示当前目录下面的子目录的目录大小和当前目录的总的大小,最下面的152为当前目录的总大小 

查看指定文件所占空间: 

 

[root@cj pyweb]# du -b views.py 
26      views.py

 

显示多个文件所占空间: 

 

[root@cj triWeb]# du view.py urls.py
4       view.py
4       urls.py

 

查看指定目录的所占空间:

 

[root@cj django]# du -h django/ 
16K     django/triWeb/templates
84K     django/triWeb
28K     django/article
32K     django/pyweb
152K    django/

 

只显示总和的大小: 

[root@cj django]# du -sh
152K 

 

 

三、dumpe2fs查看分区的具体使用情况

Filesystem state:         clean             
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              51200
Block count:              204800
Reserved block count:     10240
Free blocks:              163923
Free inodes:              51161
First block:              1
Block size:               1024
Fragment size:            1024
Reserved GDT blocks:      256
Blocks per group:         8192
Fragments per group:      8192
Inodes per group:         2048
Inode blocks per group:   256
Flex block group size:    16
Filesystem created:       Mon Jul  3 23:36:10 2017

 

磁盘的分区–命令fdisk

为了测试分区,给虚拟机增加一块硬盘

[root@cj django]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        18G  5.5G   12G  33% /
tmpfs           936M  224K  936M   1% /dev/shm
/dev/sda1       194M   34M  151M  19% /boot
/dev/sr0        4.2G  4.2G     0 100% /media/CentOS_6.5_Final
[root@cj django]# dumpe2fs /dev/sda1
dumpe2fs 1.41.12 (17-May-2010)
Filesystem volume name:   <none>
Last mounted on:          /boot
Filesystem UUID:          af3030df-64ee-4996-8101-dcc0cbf0171d
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash 
Default mount options:    user_xattr acl
Filesystem state:         clean             
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              51200
Block count:              204800
Reserved block count:     10240
Free blocks:              163923
Free inodes:              51161
First block:              1
Block size:               1024
Fragment size:            1024
Reserved GDT blocks:      256
Blocks per group:         8192
Fragments per group:      8192
Inodes per group:         2048
Inode blocks per group:   256
Flex block group size:    16
Filesystem created:       Mon Jul  3 23:36:10 2017
[root@cj django]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000bf882

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26         287     2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             287        2611    18668544   83  Linux

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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


/dev/sdb就是新增的第二块硬盘,接下来演示如何分区

 

[root@cj django]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x0cab72a4.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

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): 
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): 



Command (m for help): p   //显示分区信息

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x0cab72a4

   Device Boot      Start         End      Blocks   Id  System
p   //显示分区信息

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x0cab72a4

   Device Boot      Start         End      Blocks   Id  System


Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-652, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): 652   //或者用+size的方式 如+3G

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x0cab72a4

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         652     5237158+  83  Linux
n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-652, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): 652   //或者用+size的方式 如+3G

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x0cab72a4

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         652     5237158+  83  Linux
Command (m for help): w //写入
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
 w //写入
The partition table has been altered!

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

w和q需要注意

wq
此命令会使你的所有对磁盘的操作全部生效,如果不清楚自己所作所为的后果,此命令请慎用。退出磁盘分区程序,之前所有的操作都无效,当然前提是你没有用w。


再次查看分区情况

 

 

[root@cj django]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000bf882

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26         287     2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             287        2611    18668544   83  Linux

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x0cab72a4

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         652     5237158+  83  Linux

 

 

 

将分好的磁盘格式化

选择一种文件格式将新建的分区初始化,是为了让Linux能够识别这个分区里面的文件。所谓的文件格式就是,磁盘的文件是按照怎样的一种形式组织起来,之前提到的文件由两部分组成的,比如inode,block等,就是Linux的所能够识别的文件格式的一种。

 

格式化的命令 - mkfs

mkfs -t 文件格式 设备文件名

mkfs.ext4 设备文件名

[root@cj django]# mkfs.
mkfs.cramfs   mkfs.ext2     mkfs.ext3     mkfs.ext4     mkfs.ext4dev  mkfs.msdos    mkfs.vfat     
[root@cj django]# mkfs.ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1309289 blocks
65464 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 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

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override

 

挂载/卸载磁盘

这里我打算把sdb1挂载到“/data”这个目录,这个目录是不存在的,所以要创建。

挂载的命令是mount,命令有点复杂,自行man。

mount  设备文件名  挂载点

 

[root@cj django]# mount /dev/sdb1 /data
[root@cj django]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        18G  5.5G   12G  33% /
tmpfs           936M  224K  936M   1% /dev/shm
/dev/sda1       194M   34M  151M  19% /boot
/dev/sr0        4.2G  4.2G     0 100% /media/CentOS_6.5_Final
/dev/sdb1       5.0G  138M  4.6G   3% /data

 

 

注意命令:mount <设备名称> <挂载的目录> 
弄完了用df查看一下,看最后一行的/mnt/sda5,是不是成功了!

以下几点要注意:

  • 挂载的目录必须为空,否则目录里面的数据会被隐藏,直到设备被卸载才会显现。
  • 挂载的目录每次只能挂载一个设备。
  • 同一个设备每次只能挂载到一个目录。

 

卸载

命令:umount <设备名字/挂载点>

 

[root@cj django]# umount /dev/sr0 
[root@cj django]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        18G  5.5G   12G  33% /
tmpfs           936M  224K  936M   1% /dev/shm
/dev/sda1       194M   34M  151M  19% /boot
/dev/sdb1       5.0G  138M  4.6G   3% /data

 

 

检查磁盘是否有问题

命令 fsck

内容略,后续补充。


 

开机挂载

 

一般情况下,如果你再次开机,就会发现之前已经挂载的磁盘必须手动再次挂载一次,不信可以用df命令查看一下。

为了开机自动挂载磁盘,只需要配置一下 /etc/fstab文件即可。

查询UUID号码

 

 

 

[root@cj django]# blkid /dev/sdb1
/dev/sdb1: UUID="7b8a81fb-ea9c-4963-b7dd-b9f98a85d002" TYPE="ext4"

 

修改/etc/fstab文件

 

#
# /etc/fstab
# Created by anaconda on Mon Jul  3 23:43:48 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=84afdcda-5428-4018-8131-1f99beeb73be /                       ext4    defaults        1 1
UUID=af3030df-64ee-4996-8101-dcc0cbf0171d /boot                   ext4    defaults        1 2
UUID=4133fb7b-8ead-43a8-9b3d-8e6fdc4ad795 swap                    swap    defaults        0 0
UUID=7b8a81fb-ea9c-4963-b7dd-b9f98a85d002 /data                   ext4    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
UUID=7b8a81fb-ea9c-4963-b7dd-b9f98a85d002 /data                   ext4    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

 

字段解释:

file systemmount pointtypeoptionsdumppass
设备名称。磁盘号的UUID挂载点分区类型.linux分区一般为ext4。挂载参数。一般为defaults。常用参数如下: auto:开机自动挂载。 default:按照大多数永久文件系统的缺省值设置挂载定义。 noauto:开机不自动挂载。 nouser:只有超级用户可以挂载。 ro:按只读权限挂载。 rw:按可读可写权限挂载。 user:任何用户都可以挂载。磁盘备份。默认为0,表示不备份。磁盘检查。默认为0,表示不检查。


至此分区完成

ps :本文目的在于记录自己的学习过程,以便后续回顾,参考如下博客,如侵犯版权请联系我,谢谢!

http://blog.csdn.net/phoenix_wang_cheng/article/details/52743821#t0

 

 

 

 

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐