Linux磁盘管理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux磁盘管理相关的知识,希望对你有一定的参考价值。

参考技术A 是一个创建和维护分区表的程序,它兼容DOS类型的分区表、BSD或者SUN类型的磁盘列表。

语法

fdisk [必要参数][选择参数]

必要参数:

-l 列出素所有分区表

-u 与"-l"搭配使用,显示分区数目

选择参数:

-s<分区编号> 指定分区

-v 版本信息

菜单操作说明

m :显示菜单和帮助信息

a :活动分区标记/引导分区

d :删除分区

l :显示分区类型

n :新建分区

p :显示分区信息

q :退出不保存

t :设置分区号

v :进行分区检查

w :保存修改

x :扩展应用,高级功能

(英文全拼:make file system)命令用于在特定的分区上建立 linux 文件系统。

使用方式  :

mkfs [-V] [-t fstype] [fs-options] filesys [blocks]

参数  :

device : 预备检查的硬盘分区,例如:/dev/sda1

-V : 详细显示模式

-t : 给定档案系统的型式,Linux 的预设值为 ext2

-c : 在制做档案系统前,检查该partition 是否有坏轨

-l bad_blocks_file : 将有坏轨的block资料加到 bad_blocks_file 里面

block : 给定 block 的大小

命令是经常会使用到的命令,它用于挂载Linux系统外的文件。

语法

mount [-hV]mount -a [-fFnrsvw] [-t vfstype]mount [-fnrsvw] [-o options [,...]] device | dir

mount [-fnrsvw] [-t vfstype] [-o options] device dir

参数说明:

-V:显示程序版本

-h:显示辅助讯息

-v:显示较讯息,通常和 -f 用来除错。

-a:将 /etc/fstab 中定义的所有档案系统挂上。

-F:这个命令通常和 -a 一起使用,它会为每一个 mount 的动作产生一个行程负责执行。在系统需要挂上大量 NFS 档案系统时可以加快挂上的动作。

-f:通常用在除错的用途。它会使 mount 并不执行实际挂上的动作,而是模拟整个挂上的过程。通常会和 -v 一起使用。

-n:一般而言,mount 在挂上后会在 /etc/mtab 中写入一笔资料。但在系统中没有可写入档案系统存在的情况下可以用这个选项取消这个动作。

-s-r:等于 -o ro

-w:等于 -o rw

-L:将含有特定标签的硬盘分割挂上。

-U:将档案分割序号为 的档案系统挂下。-L 和 -U 必须在/proc/partition 这种档案存在时才有意义。

-t:指定档案系统的型态,通常不必指定。mount 会自动选择正确的型态。

-o async:打开非同步模式,所有的档案读写动作都会用非同步模式执行。

-o sync:在同步模式下执行。

-o atime、-o noatime:当 atime 打开时,系统会在每次读取档案时更新档案的『上一次调用时间』。当我们使用 flash 档案系统时可能会选项把这个选项关闭以减少写入的次数。

-o auto、-o noauto:打开/关闭自动挂上模式。

-o defaults:使用预设的选项 rw, suid, dev, exec, auto, nouser, and async.

-o dev、-o nodev-o exec、-o noexec允许执行档被执行。

-o suid、-o nosuid:

允许执行档在 root 权限下执行。

-o user、-o nouser:使用者可以执行 mount/umount 的动作。

-o remount:将一个已经挂下的档案系统重新用不同的方式挂上。例如原先是唯读的系统,现在用可读写的模式重新挂上。

-o ro:用唯读模式挂上。

-o rw:用可读写模式挂上。

-o loop=:使用 loop 模式用来将一个档案当成硬盘分割挂上系统。

(base) zfy@zfy-PC:~$ sudo fdisk -l

Disk /dev/sda: 80 GiB, 85899345920 bytes, 167772160 sectors

#块设备名称为/dev/sda,此设备的大小为80 GB,85899345920 bytes是转换成字节后的大小,共167772160 个扇区

Disk model: VMware Virtual S

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disklabel type: dos

Disk identifier: 0x3b1ffeba

Device    Boot    Start      End  Sectors  Size Id Type

#分区序列 | 引导 | 开始 | 终止 | Sectors | 容量 | 分区类型ID | 分区类型

/dev/sda1  *        2048  3147775  3145728  1.5G 83 Linux

/dev/sda2        3147776 138412031 135264256 64.5G  5 Extended

/dev/sda3      138412032 161480703  23068672  11G 83 Linux

/dev/sda4      161480704 167770111  6289408    3G 82 Linux swap / Solaris

/dev/sda5        3149824  34605055  31455232  15G 83 Linux

/dev/sda6        34607104  66062335  31455232  15G 83 Linux

/dev/sda7        66064384 138412031  72347648 34.5G 83 Linux

Partition table entries are not in disk order.

a. 在虚拟机中添加硬盘,添加后使用fdisk -l命令查看,磁盘变成两个,在sda中还没有分区

root@zfy-PC:/home/zfy# fdisk -l

Disk /dev/sda: 2 GiB, 2147483648 bytes, 4194304 sectors

Disk model: VMware Virtual S

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/sdb: 80 GiB, 85899345920 bytes, 167772160 sectors

Disk model: VMware Virtual S

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disklabel type: dos

Disk identifier: 0x3b1ffeba

Device    Boot    Start      End  Sectors  Size Id Type

/dev/sdb1  *        2048  3147775  3145728  1.5G 83 Linux

/dev/sdb2        3147776 138412031 135264256 64.5G  5 Extended

/dev/sdb3      138412032 161480703  23068672  11G 83 Linux

/dev/sdb4      161480704 167770111  6289408    3G 82 Linux swap / Solaris

/dev/sdb5        3149824  34605055  31455232  15G 83 Linux

/dev/sdb6        34607104  66062335  31455232  15G 83 Linux

/dev/sdb7        66064384 138412031  72347648 34.5G 83 Linux

Partition table entries are not in disk order.

b.下面对sda进行分区

root@zfy-PC:/home/zfy# fdisk /dev/sda

Welcome to fdisk (util-linux 2.33.1).                                                                       

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.

Created a new DOS disklabel with disk identifier 0xb985e226.

Command (m for help): m

#输入m查看帮助

Help:

  DOS (MBR)

  a  toggle a bootable flag

  b  edit nested BSD disklabel

  c  toggle the dos compatibility flag

  Generic

  d  delete a partition

  F  list free unpartitioned space

  l  list known partition types

  n  add a new partition

  p  print the partition table

  t  change a partition type

  v  verify the partition table

  i  print information about a partition

  Misc

  m  print this menu

  u  change display/entry units

  x  extra functionality (experts only)

  Script

  I  load disk layout from sfdisk script file

  O  dump disk layout to sfdisk script file

  Save & Exit

  w  write table to disk and exit

  q  quit without saving changes

  Create a new label

  g  create a new empty GPT partition table

  G  create a new empty SGI (IRIX) partition table

  o  create a new empty DOS partition table

  s  create a new empty Sun partition table

Command (m for help): n

#输入n创建一个分区

Partition type

  p  primary (0 primary, 0 extended, 4 free)

  e  extended (container for logical partitions)

Select (default p): p

#选择主分区

Partition number (1-4, default 1): 1

#选择编号为1

First sector (2048-4194303, default 2048): 2500000

#起始sector 设为2500000

Last sector, +/-sectors or +/-sizeK,M,G,T,P (2500000-4194303, default 4194303): 4194303

#终止sector 设为4194303

Created a new partition 1 of type 'Linux' and of size 827.3 MiB.

#分区完成,分区大小为827.3 MiB

Command (m for help): n

#继续创建一个分区

Partition type

  p  primary (1 primary, 0 extended, 3 free)

  e  extended (container for logical partitions)

Select (default p): e

#新增扩展分区

Partition number (2-4, default 2): 2

#选择编号为2

First sector (2048-4194303, default 2048): 2048

#起始sector为2048

Last sector, +/-sectors or +/-sizeK,M,G,T,P (2048-2499999, default 2499999):

#终止sector为2499999

Created a new partition 2 of type 'Extended' and of size 1.2 GiB.

#新增完成,大小为1.2GiB

Command (m for help): w

#保存

The partition table has been altered.

Calling ioctl() to re-read partition table.

Syncing disks.

c.查看分区后磁盘信息,此时sda磁盘已经有两个分区

Disk /dev/sda: 2 GiB, 2147483648 bytes, 4194304 sectors

Disk model: VMware Virtual S

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disklabel type: dos

Disk identifier: 0xb985e226

Device    Boot  Start    End Sectors  Size Id Type

/dev/sda1      2500000 4194303 1694304 827.3M 83 Linux

/dev/sda2          2048 2499999 2497952  1.2G  5 Extended

Partition table entries are not in disk order.

Disk /dev/sdb: 80 GiB, 85899345920 bytes, 167772160 sectors

Disk model: VMware Virtual S

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disklabel type: dos

Disk identifier: 0x3b1ffeba

Device    Boot    Start      End  Sectors  Size Id Type

/dev/sdb1  *        2048  3147775  3145728  1.5G 83 Linux

/dev/sdb2        3147776 138412031 135264256 64.5G  5 Extended

/dev/sdb3      138412032 161480703  23068672  11G 83 Linux

/dev/sdb4      161480704 167770111  6289408    3G 82 Linux swap / Solaris

/dev/sdb5        3149824  34605055  31455232  15G 83 Linux

/dev/sdb6        34607104  66062335  31455232  15G 83 Linux

/dev/sdb7        66064384 138412031  72347648 34.5G 83 Linux

Partition table entries are not in disk order.

d.用ext3的文件系统对分区进行格式化

root@zfy-PC:/home/zfy# mkfs -t ext3 /dev/sda1

mke2fs 1.44.5 (15-Dec-2018)

Creating filesystem with 211788 4k blocks and 52976 inodes

Filesystem UUID: db384338-4af1-4eec-a62d-f2b2f8e0ec96

Superblock backups stored on blocks:

        32768, 98304, 163840

Allocating group tables: done                           

Writing inode tables: done                           

Creating journal (4096 blocks): done

Writing superblocks and filesystem accounting information: done

e.新建一个分区目录 /data,并挂载

root@zfy-PC:/home/zfy# mkdir data

root@zfy-PC:/home/zfy# mount /dev/sda1 /data

root@zfy-PC:/home/zfy# df -TH /data/#查看是否挂载成功

文件系统      类型  容量  已用  可用 已用% 挂载点

/dev/sda1      ext3  838M  881k  793M    1% /data

f. 用vim打开 etc/fstab并假如 /dev/sda1/data ext3 defaults 0 0,这样重启后便不需要再挂载

参考文献:

【1】 Linux mount命令 | 菜鸟教程 (runoob.com)

【2】 linux添加新磁盘和创建分区 - IT(小菜鸡) - 博客园 (cnblogs.com)

Linux磁盘管理的Linux管理命令

磁盘空间管理
系统软件和应用软件,都要以文件的形式存储在计算机的磁盘空间中。因此,应该随时监视磁盘空间的使用情况。Linux系统提供了一组有关磁盘空间管理的命令。
df命令
功能:检查文件系统的磁盘空间占用情况。可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息。
语法:df [选项]
说明:df命令可显示所有文件系统对i节点和磁盘块的使用情况。
该命令各个选项的含义如下:
-a 显示所有文件系统的磁盘使用情况,包括0块(block)的文件系统,如/proc文件系统。
-k 以k字节为单位显示。
-i 显示i节点信息,而不是磁盘块。
-t 显示各指定类型的文件系统的磁盘空间使用情况。
-x 列出不是某一指定类型文件系统的磁盘空间使用情况(与t选项相反)。
-T 显示文件系统类型。
例1:列出各文件系统的磁盘空间使用情况。
$ df
Filesystem 1 K-blocks Used Available Use% Mounted on
/dev/hda2 1361587 1246406 44823 97% /
df命令的输出清单的第1列是代表文件系统对应的设备文件的路径名(一般是硬盘上的分区);第2列给出分区包含的数据块(1024字节)的数目;第3,4列分别表示已用的和可用的数据块数目。用户也许会感到奇怪的是,第3,4列块数之和不等于第2列中的块数。这是因为缺省的每个分区都留了少量空间供系统管理员使用。即使遇到普通用户空间已满的情况,管理员仍能登录和留有解决问题所需的工作空间。清单中Use% 列表示普通用户空间使用的百分比,即使这一数字达到100%,分区仍然留有系统管理员使用的空间。最后,Mounted on列表示文件系统的安装点。
例2:列出各文件系统的i节点使用情况。
$ df -ia
Filesystem Inodes IUsed IFree Iused% Mounted on
/dev/ hda2 352256 75043 277213 21% /
none 0 0 0 0% /proc localhost:(pid221) 0 0 0 0% /net
例3:列出文件系统的类型。
$ df -T
Filesystem Type 1K-blocks Used Available use% Mounted on
/dev/hda2 ext2 1361587 1246405 44824 97% /
本例中的文件系统是ext2类型的。
du命令
du的英文原义为“disk usage”,含义为显示磁盘空间的使用情况。
功能:统计目录(或文件)所占磁盘空间的大小。
语法:du [选项] [Names…]
说明:该命令逐级进入指定目录的每一个子目录并显示该目录占用文件系统数据块(1024字节)的情况。若没有给出Names,则对当前目录进行统计。
该命令的各个选项含义如下:
-s 对每个Names参数只给出占用的数据块总数。
-a 递归地显示指定目录中各文件及子孙目录中各文件占用的数据块数。若既不指定-s,也不指定-a,则只显示Names中的每一个目录及其中的各子目录所占的磁盘块数。
-b 以字节为单位列出磁盘空间使用情况(系统缺省以k字节为单位)。
-k 以1024字节为单位列出磁盘空间使用情况。
-c 最后再加上一个总计(系统缺省设置)。
-l 计算所有的文件大小,对硬链接文件,则计算多次。
-x 跳过在不同文件系统上的目录不予统计。
下面举例说明du命令的使用:
例1:查看/mnt目录占用磁盘空间的情况。
$ cd /mnt $ ls -lFR total 10 -rwxrwxrwx 2 root root ll0 Ju1 3l 00:33 aa*
drwxr-xr-x 2 root root l024 Ju1 20 14:16 dev/
-rw-r--r-- 1 root root 6229 Aug 2 0l:39 s1ack
drwxrwxrwx 2 root root 1024 Aug 2 02:09 var/
1rwxrwxrwx 1 root root l0 Aug 2 0l:51 wei->/home/wei/
dev:
tota1 0
lrwxrwxrwx 1 root root 8 Ju1 20 l4:l6 cdrom->/dev/hdb
var:
tata1 37
-r-xr-xr-x l root root 36064 Aug 2 02:09 rawrite. exe*
例2:列出各目录所占的磁盘空间,但不详细列出每个文件所占的空间。
$ du
l . /dev
38 . /var
48 .
输出清单中的第一列是以块为单位计的磁盘空间容量,第二列列出目录中使用这些空间的目录名称。 注意不带选项的du命令将从当前目录开始沿着目录结构向下工作直到列出所有目录的容量为止。这可能是一个很长的清单,有时只需要一个总数。这时可在du命令中加-s选项来取得总数:
$ du –s /mnt
/mnt
例3:列出所有文件和目录所占的空间(使用a选项),而且以字节为单位(使用b选项)来计算大小。
$ du -ab
8 ./dev/cdrom
l032 ./dev
36064 ./var/rawrite. exe
37088 ./var
6229 ./s1ack
1l0 ./aa
l0 ./wei
45493 .
磁 盘 操 作
dd命令
功能:把指定的输入文件拷贝到指定的输出文件中,并且在拷贝过程中可以进行格式转换。可以用该命令实现DOS下的diskcopy命令的作用。先用dd命令把软盘上的数据写成硬盘的一个寄存文件,再把这个寄存文件写入第二张软盘上,完成diskcopy的功能。需要注意的是,应该将硬盘上的寄存文件用rm命令删除掉。系统默认使用标准输入文件和标准输出文件。
语法:dd [选项]
if =输入文件(或设备名称)。
of =输出文件(或设备名称)。
ibs = bytes 一次读取bytes字节,即读入缓冲区的字节数。
skip = blocks 跳过读入缓冲区开头的ibs*blocks块。
obs = bytes 一次写入bytes字节,即写入缓冲区的字节数。
bs = bytes 同时设置读/写缓冲区的字节数(等于设置ibs和obs)。
cbs = byte 一次转换bytes字节。
count=blocks 只拷贝输入的blocks块。
conv = ASCII 把EBCDIC码转换为ASCIl码。
conv = ebcdic 把ASCIl码转换为EBCDIC码。
conv = ibm 把ASCIl码转换为alternate EBCDIC码。
conv = block 把变动位转换成固定字符。
conv = ublock 把固定位转换成变动位。
conv = ucase 把字母由小写转换为大写。
conv = lcase 把字母由大写转换为小写。
conv = notrunc 不截短输出文件。
conv = swab 交换每一对输入字节。
conv = noerror 出错时不停止处理。
conv = sync 把每个输入记录的大小都调到ibs的大小(用NUL填充)。
例1:要把一张软盘的内容拷贝到另一张软盘上,利用/tmp作为临时存储区。把源盘插入驱动器中,输入下述命令:
$ dd if =/dev/fd0 of = /tmp/tmpfile
拷贝完成后,将源盘从驱动器中取出,把目标盘插入,输入命令:
$ dd if = /tmp/tmpfile of =/dev/fd0
软盘拷贝完成后,应该将临时文件删除:
$ rm /tmp/tmpfile
例2:把net.i这个文件写入软盘中,并设定读/写缓冲区的数目。 (注意:软盘中的内容会被完全覆盖掉)
$ dd if = net.i of = /dev/fd0 bs = 16384
例3:将文件sfile拷贝到文件 dfile中。
$ dd if=sfile of=dfile
fdformat 命令
软盘是用户常用的存储介质之一。软盘在使用之前必须先作格式化操作,然后可以用tar、dd、cpio等命令存储数据,也可以在软盘上建立可安装的文件系统。
功能:低级格式化软盘
语法:format [-n] device
说明:该命令的作用是对软盘进行格式化。
-n 软盘格式化后不作检验。
device 指定要进行格式化的设备,通常是下述设备之一:
/dev/fd0d360
/dev/fd0h1200
/dev/fd0D360
/dev/fd0H360
/dev/fd0D720
/dev/fd0H720
/dev/fd0h360
/dev/fd0h720
/dev/fd0H1440

参考技术A

Linux磁盘管理常用三个命令为df、du、fdisk。

df,全称:disk full,列出文件系统的整体磁盘使用量

du,全称:disk used,检查磁盘空间使用量

fdisk,用于磁盘分区。

1、df命令

检查文件系统的磁盘空间占用情况,可以利用该命令获取磁盘被占用了多少空间,目前还剩下多少空间等信息。

语法:df[-ahikHTm][目录或文件名]

参数:

-a 列出所有文件系统,包括系统特有的/proc等文件系统;

-k 以KBytes的容量显示各文件系统;

-m 以MBytes的容量显示各文件系统;

-h 以较易阅读的格式自行显示,比如:GBytes、MBytes、KBytes;

-H 以M=1000K取代M=1024K的进位方式;

-T 显示文件系统类型;

-i 不用磁盘容量,以inode的数量来显示。

2、du命令

也是查看使用空间的,但是与df命令不同的是,Linux du命令是对文件和目录磁盘使用的空间进行查看,和df命令有一定的区别。

语法:du[-ahskm]文件或目录名称

参数:

-a 列出所有的文件与目录容量;

-h 以较易阅读的格式显示;

-s 列出总量而已,而不列出每个各别的目录占用容量;

-S 不包括子目录下的统计,与-s存在差别;

-k 以KBytes列出容量显示;

-m 以MBytes列出容量显示。

3、fdisk命令

fdisk是Linux的磁盘分区表操作工具。

语法:fdisk[-l]装置名称

参数:

-l 输出后面接的装置所有的分区内容。若仅有fdisk -l时,则系统将会把整个系统内能够搜寻到的装置的分区均列出来。

以上是关于Linux磁盘管理的主要内容,如果未能解决你的问题,请参考以下文章

【Linux命令】磁盘管理(逻辑卷与物理卷)

Linux 下磁盘管理--逻辑卷--LV

20磁盘管理—linux磁盘管理详解(分区)

Linux 磁盘管理

Linux 磁盘管理

Linux磁盘管理(fdisk磁盘管理工具|swap交换分区|LVM逻辑卷管理)