zoukankan      html  css  js  c++  java
  • Linux磁盘管理命令

    1、磁盘分割: fdisk
    [root@linux ~]# fdisk [-l] 装置名称
    参数:
    -l :输出后面接的装置所有的partition内容。若仅有fdisk -l时, 则系统将会把整个系统内能够搜寻到的装置的partition均列出来。
    范例1:查阅您的第一颗硬盘内的相关信息
    [root@linux ~]# fdisk /dev/hda <== 仔细看,不要加上数字喔!
    The number of cylinders for this disk is set to 2494.
    There is nothing wrong with that, but this is larger than 1024,
    and could in certain setups cause problems with:
    1) software that runs at boot time (e.g., old versions of LILO)
    2) booting and partitioning software from other OSs
    (e.g., DOS FDISK, OS/2 FDISK)
      在你进入fdisk这支程序的工作画面后,如果您的硬盘太大的话,就会出现如上讯息。这个讯息仅是在告知你,因为某些旧版的软件与操作系统并无法支持大于1024磁柱(cylinter)后的扇区使用,不过我们Linux是没问题啦!
    Command (m for help): m <== 输入 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)
      这里注意一下,使用 fdisk 这支程序是完全不需要背指令的,因为按下m之后,立刻就会有一堆指令说明跑出来了!在上面的指令当中,比较重要的有:
    d 删除一个磁盘分割槽
    n 新增一个磁盘分割槽
    p 将目前的磁盘分割槽列出来
    q 不储存离开!这个重要!
    w 写入磁盘分割表后离开!这个危险!
    Command (m for help): p <== 这里可以输出目前磁盘的状态
    Disk /dev/hda: 20.5 GB, 20520493056 bytes <== 硬盘的信息在这底下三行
    255 heads, 63 sectors/track, 2494 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot Start End Blocks Id System
    /dev/hda1 * 1 765 6144831 83 Linux
    /dev/hda2 766 1147 3068415 83 Linux
    /dev/hda3 1148 1274 1020127+ 82 Linux swap
    /dev/hda4 1275 2494 9799650 5 Extended
    /dev/hda5 1275 2494 9799618+ 83 Linux
      由上面的信息可以知道,我的硬盘是20GB的,而Head/Sector/Cylinder的数量为255/63/2494,另外可以看到上头的Boot吗?那个地方代表有开机信息的partition!另外,那个start与end则是指每一个partition的开始与结束的Cylinder号码!这样可以了解我们前面一直强调的,partition最小单位为cylinder,此外,上头显示的那个Id 为主要档案格式的代号,你可以按下 l ( L 的小写 )就可以知道我们linux的fdisk认识多少档案系统啰!至于Blocks则以KBytes来显示该partition的容量的
    Command (m for help): q    #想要不储存离开吗?按下q就对了!不要随便按w!

    范例2:查阅目前系统内的所有partition有哪些?
    [root@linux ~]# fdisk -l
    Disk /dev/hda: 20.5 GB, 20520493056 bytes
    255 heads, 63 sectors/track, 2494 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot Start End Blocks Id System
    /dev/hda1 * 1 765 6144831 83 Linux
    /dev/hda2 766 1147 3068415 83 Linux
    /dev/hda3 1148 1274 1020127+ 82 Linux swap
    /dev/hda4 1275 2494 9799650 5 Extended
    /dev/hda5 1275 2494 9799618+ 83 Linux
    Disk /dev/hdb: 30.7 GB, 30735581184 bytes
    255 heads, 63 sectors/track, 3736 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot Start End Blocks Id System
    /dev/hdb1 * 1 3633 29182041 83 Linux
      由于我的这个系统有两颗硬盘,下达fdisk -l的话,所有的partition 都看到了!另外,我可以确定我的 /dev/hdb 还有剩余空间喔!因为由上面的信息看来,我的 /dev/hdb的磁柱应该可以到3736 ,但是目前只用到3633 ,所以就肯定还有剩余空间拉!等一下我们就用这个来测试啰!


    2、删除磁盘分区
    [root@linux ~]#fdisk /dev/hdb
    2.1)先看看整个结果是如何~
    Command (m for help): p
    Disk /dev/hdb: 30.7 GB, 30735581184 bytes
    255 heads, 63 sectors/track, 3736 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot Start End Blocks Id System
    /dev/hdb1 * 1 3633 29182041 83 Linux
    2.2)按下d给他删除吧!
    Command (m for help): d
    Selected partition 1    #因为我们这个磁盘仅有1个partition,所以系统会自动帮我们~
    Command (m for help): p
    Disk /dev/hdb: 30.7 GB, 30735581184 bytes
    255 heads, 63 sectors/track, 3736 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot Start End Blocks Id System     #『看』不见了!partition就这样不见了!
    Command (m for help): q


    3、新增磁盘分区
    [root@linux ~]# fdisk /dev/hdb
    Command (m for help): n
    Command action
    e extended
    p primary partition (1-4)
    p <==就是这里!可以自行决定是p还是e喔!
    Partition number (1-4): 4 <==编号可以随意!
    First cylinder (3634-3736, default 3634): <==这里按下 Enter 就会使用默认值
    Using default value 3634
    Last cylinder or +size or +sizeM or +sizeK (3634-3736, default 3736): +100M
       这个地方有趣了!我们知道partition是由n1到n2的磁柱 (cylinder), 但是我们对于磁柱的大小不容易掌握!这个时候可以填入+100M来让系统自动帮我们找出『最接近100M的那个cylinder号码』!因为不可能刚好等于100MBytes啦!
    Command (m for help): p
    Disk /dev/hdb: 30.7 GB, 30735581184 bytes
    255 heads, 63 sectors/track, 3736 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot Start End Blocks Id System
    /dev/hdb1 * 1 3633 29182041 83 Linux
    /dev/hdb4 3634 3646 104422+ 83 Linux    #这个就是刚刚建立起来的primary partition啰!再继续吧!
    Command (m for help): n
    Command action
    e extended
    p primary partition (1-4)
    e
    Partition number (1-4): 2
    First cylinder (3647-3736, default 3647): <==这里按下 Enter 就会使用默认值
    Using default value 3647
    Last cylinder or +size or +sizeM or +sizeK (3647-3736, default 3736): <==Enter
    Using default value 3736
    Command (m for help): p
    Disk /dev/hdb: 30.7 GB, 30735581184 bytes
    255 heads, 63 sectors/track, 3736 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot Start End Blocks Id System
    /dev/hdb1 * 1 3633 29182041 83 Linux
    /dev/hdb2 3647 3736 722925 5 Extended
    /dev/hdb4 3634 3646 104422+ 83 Linux   #1-4 号是磁盘保留的号码,这个号码可以随意设定,不一定要由1开始,但是等一下做的logical就一定是由5开始累加!
    Command (m for help): n
    Command action
    l logical (5 or over)
    p primary partition (1-4)
    l <== 使用的是logical的partition喔!
    First cylinder (3647-3736, default 3647):<==Enter
    Using default value 3647
    Last cylinder or +size or +sizeM or +sizeK (3647-3736, default 3736):<==Enter
    Using default value 3736
    Command (m for help): p
    Disk /dev/hdb: 30.7 GB, 30735581184 bytes
    255 heads, 63 sectors/track, 3736 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot Start End Blocks Id System
    /dev/hdb1 * 1 3633 29182041 83 Linux
    /dev/hdb2 3647 3736 722925 5 Extended
    /dev/hdb4 3634 3646 104422+ 83 Linux
    /dev/hdb5 3647 3736 722893+ 83 Linux    # 这可就OK啰~虽然新作出三个partition ,不过仅有/dev/hdb4与/dev/hdb5可以用!
    Command (m for help): w
    The kernel still uses the old table.
    The new table will be used at the next reboot.
    Syncing disks.    # 有的时候,磁盘分割表变动之后,得要重新开机,有的则不需要~上面的讯息告诉我们,需要重新开机呢!那就reboot!


    4、磁盘格式化
    [root@linux ~]# mke2fs [-bicLj] 装置名称
    参数:
    -b :可以设定每个block的大小,目前支持 1024,2048,4096bytes三种;
    -i :多少容量给予一个inode呢?
    -c :检查磁盘错误,仅下达一次-c时,会进行快速读取测试;如果下达两次-c -c的话,会测试读写(read-write),会很慢~
    -L :后面可以接表头名称(Label),这个 label 是有用的喔!后面会讲~
    -j :本来mke2fs是EXT2 ,加上-j后,会主动加入journal而成为 EXT3。 
    范例一:将刚刚建立的/dev/hdb5格式化成为ext3吧!且名称为logical
    [root@linux ~]# mke2fs -j -L "logical" /dev/hdb5
    mke2fs 1.37 (21-Mar-2005)
    Filesystem label=logical
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    90432 inodes, 180723 blocks
    9036 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=188743680
    6 block groups
    32768 blocks per group, 32768 fragments per group
    15072 inodes per group
    Superblock backups stored on blocks:
    32768, 98304, 163840
    Writing inode tables: done
    Creating journal (4096 blocks): done
    Writing superblocks and filesystem accounting information: done
    This filesystem will be automatically checked every 27 mounts or
    180 days, whichever comes first. Use tune2fs -c or -i to override.  
      这样子就能够将我们的系统给他建立起来啰~

    范例二:承上题,如果将block改为2048 ,且inode改为4096?
    [root@linux ~]# mke2fs -j -L "logical" -b 2048 -i 4096 /dev/hdb5
      呈现出来的结果大致与范例一相似~不过就是block大小与inode数量会改变!


    5、磁盘检查
    5.1)fsck

    [root@linux ~]# fsck [-AtCary] 装置名称
    参数:
    -t :fsck 可以检查好几种不同的filesystem ,而fsck只是一支综合程序而已。 个别的filesystem的检验程序都在/sbin底下,您可以使用ls -l /sbin/fsck* 去检查看看,就知道有几种filesystem啰。预设的FC4情况下,至少有:ext2, ext3, vfat, msdos等等filesystem。
    -A :依据/etc/fstab 的内容,将所有的装置都扫瞄一次 (通常开机过程中就会执行此一指令)
    -a :自动修复检查到的有问题的扇区,所以你不用一直按y啰!
    -r :一定要让使用者决定是否需要修复,这与上一个-a刚好相反!
    -y :与-a类似,但是某些filesystem仅支持-y这个参数,所以您也可以利用-y啦!
    -C :可以在检验的过程当中,使用一个长条图来显示目前的进度!
    -f :强制检查!一般来说,如果fsck没有发现任何unclean的旗标,不会主动进入细部检查的,如果您想要强制fsck进入细部检查,就得加上-f旗标啰! 
    范例一:将前面我们建立的/dev/hdb5这个装置给他检验一下!
    [root@linux ~]# fsck -C -t ext3 /dev/hdb5
    fsck 1.37 (21-Mar-2005)
    e2fsck 1.37 (21-Mar-2005)
    logical: clean, 11/181056 files, 21706/361446 blocks
    # 如果一切没有问题,就会出现上述的讯息~


    5.2)badblocks
    [root@linux ~]# badblocks -[svw] 装置名称
    参数:
    -s :在屏幕上列出进度
    -v :可以在屏幕上看到进度
    -w :使用写入的方式来测试,建议不要使用此一参数,尤其是待检查的装置已有档案时!
    范例:
    [root@linux ~]# badblocks -sv /dev/hdb5
    Checking blocks 0 to 722893
    Checking for bad blocks (read-only test): done  893
    Pass completed, 0 bad blocks found.


    6、磁盘挂载与卸载
    [root@linux ~]# mount -a
    [root@linux ~]# mount [-tonL] 装置名称代号 挂载点
    参数:
    -a :依照/etc/fstab的内容将所有相关的磁盘都挂上来!
    -n :一般来说,当我们挂载档案系统到Linux上头时,Linux会主动的将目前的partition与filesystem还有对应的挂载点,都记录到/etc/mtab那个档案中。不过,有些时刻 (例如不正常关机导致一些问题,而进入单人模式)系统无法写入/etc/mtab时,就可以加上-n这个参数来略过写入mtab的动作。
    -L :系统除了利用装置名称代号 (例如/dev/hda1)之外,还可以利用partition的表头名称( Label )来进行挂载喔!所以最好为您的partition取一个在您系统当中独一无二的名称吧!
    -t :您的Linux支持的档案格式,就写在这里吧!举例来说,我们在上面建立/dev/hdb5是ext3档案系统,那么要挂载时,就得要加上-t ext3来告知系统,用ext3的档案格式来挂载该partition呢! 至于系统支持的filesystem类型在/lib/modules/`uname -r`/kernel/fs当中。
    常见的有:
    ext2,ext3,reiserfs,等Linux惯用 filesystem
    vfat, msdos等Windows常见filesystem
    iso9660为光盘片的格式
    nfs, smbfs等为网络相关档案系统。
       若mount后面没有加-t档案系统格式时,则 Linux 在预设的情况下,会主动以/etc/filesystems这个档案内规范的档案系统格式来尝试主动的挂载!
    -o:后面可以接一些挂载时,额外加上的参数喔!比方说账号、密码、读写权限等:
    ro, rw: 此partition为只读(ro) 或可擦写(rw)
    async, sync: 此partition为同步写入 sync)或异步(async),这个与我们之前提到的档案系统运作方式有关,预设是async
    auto, noauto: 允许此partition被以mount -a自动挂载(auto)
    dev, nodev: 是否允许此partition上,可建立装置档案?dev为可允许
    suid, nosuid: 是否允许此partition含有 suid/sgid的档案格式?
    exec, noexec: 是否允许此partition上拥有可执行binary档案?
    user, nouser: 是否允许此partition让 user 执行mount?一般来说mount仅有root可以进行,但下达user参数,则可让一般 user 也能够对此partition进行mount
    defaults: 默认值为:rw, suid, dev, exec, auto, nouser, andasync
    remount: 重新挂载,这在系统出错,或重新更新参数时很有用!

    范例一:将刚刚建立的/dev/hdb5挂载到/mnt/hdb5上面!
    [root@linux ~]# mkdir /mnt/hdb5
    [root@linux ~]# mount -t ext3 /dev/hdb5 /mnt/hdb5
    [root@linux ~]# df
    Filesystem 1K-blocks Used Available Use% Mountedon
    ....中间省略.....
    /dev/hdb5 700144 20664 643336 4% /mnt/hdb5

    范例二:挂载光盘
    [root@linux ~]# mount -t iso9660 /dev/cdrom /mnt/cdrom
    [root@linux ~]# mount /dev/cdrom
      上面的参数当中提到,如果没有加上-t这个参数时,系统会主动的以/etc/filesystems 里面规范的内容给他测试一下是否挂载~另外,因为我们的/etc/fstab里面会规范 /dev/cdrom 应该挂载到那个挂载点,因此直接下达mount /dev/cdrom也是可以的喔!(当然要看/etc/fstab 设定啦!)

    范例三:挂载Window fat软盘!
    [root@linux ~]# mount -t vfat /dev/fd0 /mnt/floppy

    范例四:将/重新挂载,并加入参数为rw!
    [root@linux ~]# mount -o remount,rw /

    范例五:将Label名为logical的partition挂载到/mnt/hdb5中
    [root@linux ~]# mount -t ext3 -L logical /mnt/hdb5

    范例六:将系统所有的以挂载的partition数据列出来
    [root@linux ~]# mount
    /dev/hda1 on / type ext3 (rw)
    /dev/proc on /proc type proc (rw)
    /dev/shm on /dev/shm type tmpfs (rw)
    /dev/hda5 on /home type ext3 (rw)
    /dev/hdb5 on /mnt/hdb5 type ext3 (rw)
       不加任何参数,则mount会将目前系统的所有partition与相关对应的filesystem及mount point都列出来!

    范例七:将/home这个目录暂时挂载到/tmp/home 底下
    [root@linux ~]# mkdir /tmp/home
    [root@linux ~]# mount --bind /home /tmp/home
    [root@linux ~]# ls -lid /home/ /tmp/home
    159841 drwxr-xr-x 6 root root 4096 May 30 20:07 /home/
    159841 drwxr-xr-x 6 root root 4096 May 30 20:07 /tmp/home

    范例八:将/tmp/home卸载
    [root@linux ~]# umount /tmp/home

    6.1)umount (将装置档案卸载)
    [root@linux ~]# umount 装置代号或挂载点
    [root@linux ~]# umount /dev/hdb5
    [root@linux ~]# umount /mnt/hdb5

    6.2)开机自动挂载
    设置开机挂载配置文件:/etc/fstab及/etc/mtab

    6.3)磁盘挂载注意事项
    a、单一文件系统不应被重复挂载在不同的挂载点(目录)中
    b、单一目录不应重复挂载多个文件系统
    c、作为挂载点的目录理论上应该是空目录(若非空,则挂载后原数据暂时消失)


    7、磁盘参数修改
    7.1)mknod
    [root@linux ~]# mknod 装置名称 [bcp] [Major] [Minor]
    装置种类:
    b:设定装置名称成为一个周边储存设备档案,例如硬盘等;
    c:设定装置名称成为一个周边输入设备档案,例如鼠标/键盘等;
    p:设定装置名称成为一个 FIFO 档案;
    Major :主要装置代码;
    Minor :次要装置代码;
    范例一:建立/dev/hda10这个磁盘储存装置
    [root@linux ~]# mknod /dev/hda10 b 3 10 #上面那个3与10是有意义的,不要随意设定!

    7.2)e2label
    [root@linux ~]# e2label 装置名称 新的 Label 名称
    [root@linux ~]# e2label /dev/hdb5 hdb5
    [root@linux ~]# dumpe2fs -h /dev/hdb5
    Filesystem volume name: hdb5
    .....其它省略.....

    7.3)tune2fs
    [root@linux ~]# tune2fs [-jlL] 装置代号
    参数:
    -j :将 ext2 的 filesystem 转换为 ext3 的档案系统;
    -l :类似 dumpe2fs -h 的功能~将 superblock 内的数据读出来~
    -L :类似 e2label 的功能,可以修改 filesystem 的 Label 喔!
    范例:
    [root@linux ~]# tune2fs -l /dev/hdb5

    7.4)hdparm
    DMA管理,需要您的硬盘支持DMA模式功能。


    8、建立虚拟内存swap
       如果您需要使用到很大量的虚拟内存,当初给的swap扇区不够大时就需要新建立虚拟内存。
    8.1)建立虚拟内存装置。
    8.2)建立虚拟内存档案。

  • 相关阅读:
    团队冲刺第四天
    团队冲刺第三天
    团队冲刺第二天
    团队冲刺第一天
    全球疫情地图显示
    团队博客——keep running视频+PPT介绍
    周总结7
    人月神话阅读笔记03
    Java枚举类型的使用,数值的二进制表示
    四则运算器
  • 原文地址:https://www.cnblogs.com/defias/p/3418563.html
Copyright © 2011-2022 走看看