鸟哥的linux私房菜学习笔记《十四》磁盘分区、格式化、检验

来源:互联网 发布:淘宝客建立推广网站 编辑:程序博客网 时间:2024/05/29 08:35
  1. fdisk:磁盘分区
    参数:
    -l:输出后面的设备所有的分区内容。若仅有fdisk -l时,则系统将会把整个系统内能找到的设备的分区均列出来

    # 找出系统中的根目录所在磁盘,并查阅该硬盘内的相关信息[root@CentOS tmp]# df /Filesystem     1K-blocks    Used Available Use% Mounted on/dev/sda2       10321208 6765512   3031408  70% /# 对分区进行操作(后面不加数字):[root@CentOS tmp]# fdisk /dev/sdaWARNING: 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 (m for help): mCommand actiona   toggle a bootable flagb   edit bsd disklabelc   toggle the dos compatibility flagd   delete a partition        <==删除一个分区l   list known partition typesm   print this menun   add a new partition       <==新增一个分区o   create a new empty DOS partition tablep   print the partition table <==在屏幕上显示分区表q   quit without saving changes<==不存储,离开fdisk程序s   create a new empty Sun disklabelt   change a partition's system idu   change display/entry unitsv   verify the partition tablew   write table to disk and exit    <==将刚才的操作写入分区表x   extra functionality (experts only)Command (m for help): # 只要离开fdisk时按下q,那么所有的操作都不会生效# 相反离开fdisk时按下w,就是操作生效# 输出目前磁盘的状态:Command (m for help): p# 这个磁盘的文件名与容量:Disk /dev/sda: 85.9 GB, 85899345920 bytes# 磁头、扇区及柱面:255 heads, 63 sectors/track, 10443 cylinders# 每个柱面的大小Units = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00017e60Device Boot      Start         End      Blocks   Id  System/dev/sda1   *           1          26      204800   83  LinuxPartition 1 does not end on cylinder boundary./dev/sda2              26        1332    10485760   83  Linux/dev/sda3            1332        2637    10485760   83  Linux/dev/sda4            2637       10444    62708736    5  Extended/dev/sda5            2637        2768     1048576   82  Linux swap / Solaris# 想要离开时按q,不要随便按w

    这里写图片描述
    这里写图片描述

    # 查阅目前系统内的所有分区有哪些:[root@CentOS tmp]# fdisk -lDisk /dev/sda: 85.9 GB, 85899345920 bytes255 heads, 63 sectors/track, 10443 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00017e60Device Boot Start End Blocks Id  System/dev/sda1   *           1          26      204800   83  LinuxPartition 1 does not end on cylinder boundary./dev/sda2              26        1332    10485760   83  Linux/dev/sda3            1332        2637    10485760   83  Linux/dev/sda4            2637       10444    62708736    5  Extended/dev/sda5            2637        2768     1048576   82  Linux swap / Solaris
  2. 删除磁盘分区:
    这里写图片描述

    # 先进入fdisk的界面当中:[root@CentOS ~]# fdisk /dev/sdaWARNING: 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): pDisk /dev/sda: 85.9 GB, 85899345920 bytes255 heads, 63 sectors/track, 10443 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00017e60   Device Boot      Start         End      Blocks   Id  System/dev/sda1   *           1          26      204800   83  LinuxPartition 1 does not end on cylinder boundary./dev/sda2              26        1332    10485760   83  Linux/dev/sda3            1332        2637    10485760   83  Linux/dev/sda4            2637       10444    62708736    5  Extended/dev/sda5            2637        2768     1048576   82  Linux swap / Solaris# 按d删除分区:Command (m for help): d# 选择哪一个分区:Partition number (1-5): 5# 显示整个分区情况:Command (m for help): pDisk /dev/sda: 85.9 GB, 85899345920 bytes255 heads, 63 sectors/track, 10443 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00017e60   Device Boot      Start         End      Blocks   Id  System/dev/sda1   *           1          26      204800   83  LinuxPartition 1 does not end on cylinder boundary./dev/sda2              26        1332    10485760   83  Linux/dev/sda3            1332        2637    10485760   83  Linux/dev/sda4            2637       10444    62708736    5  Extended# 删除分区:Command (m for help): d# 选择哪一个分区:Partition number (1-5): 2# 显示整个分区情况:Command (m for help): pDisk /dev/sda: 85.9 GB, 85899345920 bytes255 heads, 63 sectors/track, 10443 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00017e60   Device Boot      Start         End      Blocks   Id  System/dev/sda1   *           1          26      204800   83  LinuxPartition 1 does not end on cylinder boundary./dev/sda3            1332        2637    10485760   83  Linux/dev/sda4            2637       10444    62708736    5  Extended# 退出fdisk,且不保存Command (m for help): q
  3. 新增磁盘分区
    此处,我给虚拟机新加了一块SCAS硬盘,然后重启,就可以多一块硬盘。
    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述
    然后重启虚拟机。
    这里写图片描述

    # 进入fdisk的分区界面:[root@CentOS ~]# fdisk /dev/sdbDevice contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabelBuilding a new DOS disklabel with disk identifier 0x54dbfa04.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').# 新增一个主分区,且指定为4号:Command (m for help): nCommand action   e   extended   p   primary partition (1-4)# p是指定主分区:p# 指定分区号码为4号Partition number (1-4): 4First cylinder (1-10443, default 1): # 此处直接敲击回车键Using default value 1Last cylinder, +cylinders or +size{K,M,G} (1-10443, default 10443): +512M# 柱面的每块磁盘大小都不相同,此处+512M是为了找出最接近512M的那个sylinder号码。# 这个地方输入的方式有两种:# (1)直接输入柱面号码,你的要自己计算柱面/分区的大小才行# (2)用+XXM来输入分区的大小,让系统自分柱面的号码# +与M是必须要有的,XX为数字。Command (m for help): pDisk /dev/sdb: 85.9 GB, 85899345920 bytes255 heads, 63 sectors/track, 10443 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x54dbfa04   Device Boot      Start         End      Blocks   Id  System/dev/sdb4               1          66      530113+  83  Linux# 此处只有4号,1~3号被保留下来了。
    # 进入fdisk的分区界面:[root@CentOS ~]# fdisk /dev/sdbWARNING: 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): pDisk /dev/sdb: 85.9 GB, 85899345920 bytes255 heads, 63 sectors/track, 10443 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x7fb20bfc  Device Boot      Start         End      Blocks   Id  System/dev/sdb4               1          66      530113+  83  Linux# 新建一个分区:Command (m for help): nCommand action  e   extended  p   primary partition (1-4)# 选择扩展分区ePartition number (1-4): 1First cylinder (67-10443, default 67): # 此处直接回车键Using default value 67Last cylinder, +cylinders or +size{K,M,G} (67-10443, default 10443): # 此处直接回车键Using default value 10443# 前面讲到,扩展分区最好包含所有未分区的空间,所以在这个练习中,我们将所有未配置的柱面都给了这个分区,所以在开始/结束柱面上,按下两次回车键# 显示分区情况:Command (m for help): pDisk /dev/sdb: 85.9 GB, 85899345920 bytes255 heads, 63 sectors/track, 10443 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x7fb20bfc  Device Boot      Start         End      Blocks   Id  System/dev/sdb1              67       10443    83353252+   5  Extended/dev/sdb4               1          66      530113+  83  Linux# 如上所示:所有剩下的柱面都在/dev/sdb1里面Partition table entries are not in disk orderCommand (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.
    # 这次随便新增一个2GB的分区:[root@CentOS ~]# fdisk /dev/sdbWARNING: 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): nCommand action   l   logical (5 or over)   p   primary partition (1-4)pPartition number (1-4): 2No free sectors available# 没有多余的柱面可供提供Command (m for help): nCommand action   l   logical (5 or over)   p   primary partition (1-4)# 选择逻辑分区:lFirst cylinder (67-10443, default 67): Using default value 67Last cylinder, +cylinders or +size{K,M,G} (67-10443, default 10443): +2018M# 显示分区情况:Command (m for help): pDisk /dev/sdb: 85.9 GB, 85899345920 bytes255 heads, 63 sectors/track, 10443 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x7fb20bfc   Device Boot      Start         End      Blocks   Id  System/dev/sdb1              67       10443    83353252+   5  Extended/dev/sdb4               1          66      530113+  83  Linux/dev/sdb5              67         324     2072353+  83  LinuxPartition table entries are not in disk order# 保存并退出:Command (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.

    这里写图片描述

    # 依照自己情况新建一个大约1GB左右的分区,并显示该分区的相关信息。[root@CentOS ~]# fdisk /dev/sdbWARNING: 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): nCommand action   l   logical (5 or over)   p   primary partition (1-4)lFirst cylinder (325-10443, default 325): Using default value 325Last cylinder, +cylinders or +size{K,M,G} (325-10443, default 10443): +2044M# 显示分区情况:Command (m for help): pDisk /dev/sdb: 85.9 GB, 85899345920 bytes255 heads, 63 sectors/track, 10443 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x7fb20bfc   Device Boot      Start         End      Blocks   Id  System/dev/sdb1              67       10443    83353252+   5  Extended/dev/sdb4               1          66      530113+  83  Linux/dev/sdb5              67         324     2072353+  83  Linux/dev/sdb6             325         586     2104483+  83  LinuxPartition table entries are not in disk order# 保存并退出:Command (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.# 强制让内核执重新找一次分区表[root@CentOS ~]# partprobe

    这里写图片描述
    这里写图片描述

  4. 磁盘格式化:mkfs与mke2fs
    1 . mkfs
    参数:
    -t:可以接文件系统格式,例如ext2,ext3,vfat等

    [root@CentOS ~]# fdisk -l........................................   Device Boot      Start         End      Blocks   Id  System/dev/sdb1              67       10443    83353252+   5  Extended/dev/sdb4               1          66      530113+  83  Linux/dev/sdb5              67         324     2072353+  83  Linux/dev/sdb6             325         586     2104483+  83  LinuxPartition table entries are not in disk order[root@CentOS ~]# mkfs -t ext3 /dev/sdb6mke2fs 1.41.12 (17-May-2010)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks131648 inodes, 526120 blocks26306 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=54106521617 block groups32768 blocks per group, 32768 fragments per group7744 inodes per groupSuperblock backups stored on blocks:     32768, 98304, 163840, 229376, 294912Writing inode tables: done                            Creating journal (16384 blocks): done# 此处有创建日志记录Writing superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 26 mounts or180 days, whichever comes first.  Use tune2fs -c or -i to override.# 这样就创建了ext3的日志文件系统
    # 将系统格式化为windows支持的vfat格式[root@CentOS ~]# mkfs -t vfat /dev/sdb6

    这里写图片描述
    2 . mke2fs(make ext2 files ystem)
    参数:
    -b:可以设置每个block的大小,目前支持1024,2048,4096三种
    -i:多少容量给予一个inode
    -c:检查磁盘错误,仅下达一次-c时,会进行快读读取测试,如果下达两次-c -c的话,会测试读写
    -L:后面可以接受卷标名称(lable),这个lable是有用的,e2lable命令介绍会谈到
    -j:本来mke2fs是ext2,加上-j后,会主动加入journal而成为ext3
    这里写图片描述

    # 指定卷标,指定inode,指定block,格式化为日志系统[root@CentOS ~]# mke2fs -j -L "kevin_logical" -b 2048 -i 8192 /dev/sdb6mke2fs 1.41.12 (17-May-2010)Filesystem label=kevin_logicalOS type: LinuxBlock size=2048 (log=1)Fragment size=2048 (log=1)Stride=0 blocks, Stripe width=0 blocks263120 inodes, 1052240 blocks52612 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=53896806465 block groups16384 blocks per group, 16384 fragments per group4048 inodes per groupSuperblock backups stored on blocks:     16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816Writing inode tables: done                            Creating journal (32768 blocks): doneWriting superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 29 mounts or180 days, whichever comes first.  Use tune2fs -c or -i to override.

    注意:若没有指定-j选项是,mke2fs使用ext2为格式化格式,若加入-j时,则格式化为ext3这个journaling的文件系统
    如果没有特殊需求的话,使用“mkfs -t ext3”

  5. 磁盘检验:fsck和badblocks
    1 . fsck
    参数:
    -t:指定文件系统,但是linxu可以自动从superblock中识别文件系统类型,所以不需要这个参数
    -A:依据/etc/fstab的内容,将需要的设备扫描一次。通常开机时会执行
    -a:自动修复检查到的有问题的扇区,所以你不用一直按y
    -y:与-a类似,但是某些文件系统仅支持-y这个参数
    -C:可以再检验的过程中使用一个脂肪图来显示目前的进度
    ext2/ext3的额外功能:
    -f:强制检查,一般来说,如果fsck没有发现认可unclean的标志,不会主动进入细化检查的,如果要强制fsck进入细化检查,就得加入-f标志
    -D:针对文件系统下的目录进行优化配置

    # 强制将前面我们创建的/dev/sdb6这个设备检验一下:[root@CentOS ~]# fsck -C -f -t ext3 /dev/sdb6fsck from util-linux-ng 2.17.2e2fsck 1.41.12 (17-May-2010)Pass 1: Checking inodes, blocks, and sizesPass 2: Checking directory structure                                           Pass 3: Checking directory connectivityPass 4: Checking reference countsPass 5: Checking group summary informationkevin_logical: 11/263120 files (0.0% non-contiguous), 70498/1052240 blocks     # 如果没有加上-f的参数,则由于这个文件系统不曾出现问题# 检查非常快速,若加上-f强制检查,才会一项一项显示过程
# 查看系统有多少文件系统的fsck软件[root@CentOS ~]# fsck [tab][tab]fsck          fsck.ext2     fsck.ext4     fsck.gfs2     fsck.vfat     fsck.cramfs   fsck.ext3     fsck.ext4dev  fsck.msdos

这里写图片描述
2 . badblocks
参数:
-s:在屏幕上列出进度
-v:可以再屏幕上看到进度
-w:使用写入的方式来测试,建议不要使用此参数,尤其是待检查的设备已有文件时

```[root@CentOS ~]# badblocks -sv /dev/sdb6Checking blocks 0 to 2104482Checking for bad blocks (read-only test): done                                Pass completed, 0 bad blocks found.```
1 0
原创粉丝点击