且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

菜鸟学Linux 第023篇笔记 磁盘管理

更新时间:2022-08-28 09:38:57

菜鸟学Linux 第023篇笔记  磁盘管理



ln (make links between files)

ln [-s -v] SRC DEST


硬链接:

1、只能对文件创建,不能应用于目录;

2、不能跨文件系统;

3、创建硬链接会增加文件被链接的次数;


符号链接:

1、可应用于目录;

2、可以跨文件系统;

3、不会增加链接文件的链接次数;

4、其大小为指定的路径所包含的字符个数;

du (estimate file space usage)

  -s (display only a total for each argument)

  -h (print sizes in human readable format (e.g., 1K 234M 2G))

df (report file system disk space usage)


设备文件

b 按块为单位,随机访问的设备;硬盘

c 按字符为单位,线性设备;键盘

/dev

主设备号(major number)

标识设备类型

次设备号(minor number)

标识同一类型中不同设备

mknod (make block or character special files)

mknod [OPTION]... NAME TYPE [MAJOR MINOR]

-m set permission mode (as in chmod), not a=rw - umask



硬盘设备的设备文件名:

IDE,ATA hd

STAT sd

SCSI sd

USB sd

a,b,c...来区别同一种类型下的不同设备


IDE

第一个IDE口:主、从

/dev/hda, /dev/hdb

第二个IDE口:主、从

/dev/hdc, /dev/hdd

STAT, SCSI, USB

sda, sdb, sdc...

hda:

hda1 第一个主分区

hda2

hda3

hda4

hda5 第一个逻辑分区


查看当前系统几块硬盘命令

fdisk -l [/dev/..]


管理磁盘分区命令

fdisk /dev/sda

   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)

   (输入字符错误时,如要删除需同时按住Ctrl再按删除即可)

   依照相应命令即可管理硬盘分区

查看当前系统的文件系统

cat /proc/partitions

通知系统重读分区表

partprobe

Description

   partprobe is a program that informs the operating system kernel of par-

   tition table changes, by requesting that the operating  system  re-read

   the partition table.

cat /proc/filesystems

查看当前系统所支持的文件系统

   

   

   

VFS (Virtual FileSystem)

ext3, ext4, ext2, extended

reiserfs(suse)

iso9660, swap, cifs, nfs,

xfs, jfs

ocfs2, gfs2

fat32, ntfs...

Disk

Boot Block

Block Group 0

Super Block, GDT, block bitmap, inode bitmap, inode table, data blocks

Block Group 1

Block Group ...

block size: 1027, 2048, 4096

block group

inode: index node

super block

date structure: bitmap

Group description(GDT)

ext3, ext2区别

3 have journal file system


创建文件系统

mkfs (build a Linux file system)

mkfs.ext3

mkfs.ext2 

mkfs.vfat format fat32

mke2fs  (create an ext2/ext3 filesystem)

-j = mkfs.ext3

-b  block-size

              Specify the size of blocks in bytes.  Valid block size vales are

              1024, 2048 and 4096 bytes per block.

-L new-volume-label

              Set the volume label for  the  filesystem  to  new-volume-

              The maximum length of the volume label is 16 bytes.

-m reserved-blocks-percentage

              Specify the percentage of the filesystem blocks reserved for the

              super-user.  This avoids fragmentation,  and  allows  root-owned

              daemons,  such  as syslogd(8), to continue to function correctly

              after non-privileged processes are prevented from writing to the

              filesystem.  The default percentage is 5%.

  e.g. mke2fs -m 5 /dev/sda1

-N number-of-inodes

              overrides  the  default calculation of the number of inodes that

              should be reserved for the filesystem (which  is  based  on  the

              number  of  blocks  and the bytes-per-inode ratio).  This allows

              the user to specify the number of desired inodes directly.

  e.g. mke2fs -i 20480 /dev/sdb1

mke2fs -N 3028 /dev/sdb1

-F    Force mke2fs to run, even if the specified device is not a block

              special device, or appears to be mounted.

blkid (command-line utility to locate/print block device attributes)

UUID

TYPE

LABLE

e2lable 设备卷标

e2lable 设备文件 卷标(不加则显示设备卷标)

tune2fs (adjust tunable filesystem parameters on ext2/ext3 filesystems)

注意是无损文件!

-j: 不损害原有数据,将ext2升级为ext3;

-L: 设定或修改卷标

-m: Set the percentage of reserved filesystem blocks.

-o: [^]mount-option[,...] 设定默认挂载选项

-r reserved-blocks-count

              Set the number of reserved filesystem blocks.

-l     List the contents of the filesystem superblock.

dumpe2fs  dump ext2/ext3 filesystem information

-h     only display the superblock information and not any of the block

               group descriptor detail information.

   

fsck (check and repair a Linux file system)

-t fslist

              Specifies the type(s) of file system to be checked.

  

e2fsck (check a Linux ext2/ext3 file system)(专用于修复ext2,ext3文件系统)

-f     Force checking even if the file system seems clean.

-p     Automatically repair ("preen") the  file  system.


mkfs -t TYPE /dev/sdb# #为数字

mount挂载 /etc/fstab  /etc/mtab

mount 显示当前系统已经挂载的设备及挂载点

-a     Mount all filesystems (of the given types) mentioned in /etc/fstab.

-n     Mount without writing in /etc/mtab.  This is necessary for exam-

              ple when /etc is on a read-only file system.

-t vfstype

              The  argument following the -t is used to indicate the file sys-

              tem type.

-r     Mount the file system read-only. A synonym is -o ro.

    -w     Mount the file system read/write. This is the default. A synonym

           is -o rw.


mount device 挂载点

device:

设备文件: /dev/sdb*

卷标 LABLE=""

UUID UUID=""

挂载点

要求

1.此目录没有其它进程使用;

2.目录必须事先存在;

3.如目录中有文件,则文件会暂时隐藏;

umount 设备|挂载点

注意事项

挂载的设备没有进程使用

fuser (fuser - identify processes using files or sockets)

当umount时如果有人在使用该设备时,可使用该命令来查看谁在使用

-v  Verbose mode. 

  Processes are shown in a ps-like style. The fields

-k  Kill processes accessing  the  file.  Unless  changed

              with -signal, SIGKILL is sent. An fuser process never

              kills itself, but may kill other fuser processes. The

              effective  user  ID of the process executing fuser is

              set to its real user ID before attempting to kill.

-m  name specifies a file on a mounted file system  or  a

              block device that is mounted. All processes accessing

              files on that file system are listed.  If a directory

              file  is  specified,  it  is automatically changed to

              name/. to use any file system that might  be  mounted

              on that directory.

e.g. fuser -vm /dev/sda1

开机自动挂载

os在启动时,会自动挂载下述文件中定义的每个文件系统

文件系统配置文件/etc/fstab

字段含义:

1. 设备(要挂载的设备) LABEL, UUID, 设备文件

2. 挂载点

3. 文件系统类型

4. 挂载选项(默认defaults)

5. 转储频率 数字表示多少天备份,0不备份

6. 开机自检次序(一般次序根为1)0表示不检查

THE LOOP DEVICE

       One further possible type is a mount via the loop device. For  example,

       the command


         mount /tmp/fdimage /mnt -t msdos -o loop=/dev/loop3,blocksize=1024


       will  set  up  the  loop  device  /dev/loop3  to correspond to the file

       /tmp/fdimage, and then mount this device on /mnt.

e.g.

挂载iso镜像,用来查看iso镜像内容

mount -o loop /path/name.iso /media/

swap partition分区

允许内存过载使用(不推荐)

当系统内存满载,如有新进程又需要内存,则内核会将当前物理内存中不被访问的

页框移至swap分区中,以保证新进程有内存空间可被使用;


command:

free (Display amount of free and used memory in the system)

-m Mb

mkswap (mkswap - set up a Linux swap area)(切记先设置分区类型为swap)

mkswap /dev/sda#

-L


swapon swapoff

e.g. swapon /dev/sdb6

swapon, swapoff - enable/disable devices and files for paging and swap-

       ping

Swapon is used to specify devices on which paging and swapping  are  to

       take place.


创建虚拟硬盘

dd (dd - convert and copy a file)

if=FILE

              read from FILE instead of stdin

of=FILE

              write to FILE instead of stdout

bs=BYTES

              force ibs=BYTES and obs=BYTES

count=BLOCKS

              copy only BLOCKS input blocks

seek=BLOCKS

              skip BLOCKS obs-sized blocks at start of output

  

  

e.g.

backup MBR dd if=/dev/sdb of=/mnt/usb/mbr.backup bs=512 count=1

restore MBR dd if=/usb/mbr.backup of=/dev/sdb bs=512 count=1

CDROM > iso  cat /dev/cdrom > /root/name.iso

临时救急创建交换分区

1.创建虚拟块文件 dd if=/dev/zero of=/var/swapfile bs=1M count=1024

2.格式化为swap分区 mkswap /var/swapfile

3.挂载swap swapon /var/swapfile

本文转自Winthcloud博客51CTO博客,原文链接http://blog.51cto.com/winthcloud/1870559如需转载请自行联系原作者

Winthcloud