首页 > 代码库 > 磁盘及文件系统管理
磁盘及文件系统管理
挂载一块新的硬盘到文件系统的步骤:
1)在VMware中申请新的硬盘;
2)分区
3)高级格式化
4)挂载 (包括swap分区的挂载)
在VMware中申请新的硬盘
分区
[root@localhost ~]# fdisk /dev/sdb
...
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-6527, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-6527, default 6527): +2G
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# ls -l /dev/sd*
brw-rw----. 1 root disk 8, 0 Aug 10 13:29 /dev/sda
brw-rw----. 1 root disk 8, 1 Aug 10 13:29 /dev/sda1
brw-rw----. 1 root disk 8, 2 Aug 10 13:29 /dev/sda2
brw-rw----. 1 root disk 8, 3 Aug 10 13:29 /dev/sda3
brw-rw----. 1 root disk 8, 16 Aug 10 13:46 /dev/sdb
brw-rw----. 1 root disk 8, 17 Aug 10 13:46 /dev/sdb1
[root@localhost ~]# partprobe /dev/sdb1
[root@localhost ~]# cat /proc/partitions
major minor #blocks name
8 0 10485760 sda
8 1 102400 sda1
8 2 2048000 sda2
8 3 8334336 sda3
8 16 52428800 sdb
8 17 2104483 sdb1
高级格式化
[root@localhost ~]# mke2fs -L DATA -b 1024 -m 8 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=DATA
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
131584 inodes, 2104480 blocks
168358 blocks (8.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=69468160
257 block groups
8192 blocks per group, 8192 fragments per group
512 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409, 663553,
1024001, 1990657
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
挂载
[root@localhost ~]# mount LABEL=DATA /backup
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 8072376 2700048 4955612 36% /
tmpfs 502384 0 502384 0% /dev/shm
/dev/sda1 95054 40473 49461 46% /boot
/dev/sdb1 2070949 3086 1899505 1% /backup
附录磁盘与文件系统的管理命令
df - report file system disk space usage
-ihP
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 8072376 2700068 4955592 36% /
tmpfs 502384 0 502384 0% /dev/shm
/dev/sda1 95054 40473 49461 46% /boot
/dev/sdb1 2070949 3086 1899505 1% /backup
查看或创建分区(磁盘管理)
fdisk - Partition table manipulator for Linux
fdisk /dev/sda
p: 显示当前硬件的分区,包括没保存的改动
n: 创建新分区
e: 扩展分区 extend partition
p: 主分区
d: 删除一个分区
w: 保存退出
q: 不保存退出
t: 修改分区类型
L:
l: 显示所支持的所有类型
p print the partition table
n add a new partition
d delete a partition
w write table to disk and exit
q quit without saving changes
t change a partition‘s system id
l list known partition types
[root@localhost ~]# fdisk -l 查看系统识别了当前几块磁盘
Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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: 0x0004c0ca
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 102400 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 13 268 2048000 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 268 1306 8334336 83 Linux
Disk /dev/sdb: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0xba058f83
Device Boot Start End Blocks Id System
/dev/sdb1 1 262 2104483+ 83 Linux
partprobe - inform the OS of partition table changes
[root@localhost ~]# partprobe /dev/sdb1
[root@localhost ~]# cat /proc/partitions
major minor #blocks name
8 0 10485760 sda
8 1 102400 sda1
8 2 2048000 sda2
8 3 8334336 sda3
8 16 52428800 sdb
8 17 2104483 sdb1
文件系统管理命令
高级格式化:mkfs
mkfs - build a Linux file system
mkfs: make file system
-t FSTYPE
Specifies the type of file system to be built. If not specified, the default file system
type (currently ext2) is used.
mkfs -t ext2 = mkfs.ext2
mkfs -t ext3 = mkfs.ext3
[root@localhost ~]# mkfs[TAB][TAB]
mkfs mkfs.ext2 mkfs.ext4 mkfs.msdos
mkfs.cramfs mkfs.ext3 mkfs.ext4dev mkfs.vfat
专门管理ext系列文件:
mke2fs - create an ext2/ext3/ext4 filesystem
-j: 创建ext3类型文件系统
-b BLOCK_SIZE: 指定块大小,默认为4096;可用取值为1024、2048或4096;
-L LABEL:指定分区卷标;
-m #: 指定预留给超级用户的块数百分比
-i #: 用于指定为多少字节的空间创建一个inode,默认为8192;这里给出的数值应该为块大小的2^n倍;
-N #: 指定inode个数;
-F: 强制创建文件系统;
-E: 用户指定额外文件系统属性;
tune2fs - adjust tunable filesystem parameters on ext2/ext3/ext4 filesystems
tune2fs: 调整文件系统的相关属性 tune e2fs
-j: 不损害原有数据,将ext2升级为ext3;
-L LABEL: 设定或修改卷标;
-m #: 调整预留百分比;
-r #: 指定预留块数;
-o: 设定默认挂载选项;
acl
-c #:指定挂载次数达到#次之后进行自检,0或-1表示关闭此功能;
-i #: 每挂载使用多少天后进行自检;0或-1表示关闭此功能;
-l: 显示超级块中的信息;
[root@localhost ~]# blkid /dev/sdb1
/dev/sdb1: LABEL="MYDATA" UUID="24bd479c-d596-48a6-8423-370ea46d4a2b" TYPE="ext2"
[root@localhost ~]# tune2fs -j -L DATA -o acl /dev/sdb1
tune2fs 1.41.12 (17-May-2010)
Creating journal inode: done
This filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]# blkid /dev/sdb1
/dev/sdb1: LABEL="DATA" UUID="24bd479c-d596-48a6-8423-370ea46d4a2b" SEC_TYPE="ext2" TYPE="ext3"
[root@localhost ~]# tune2fs -l /dev/sdb1
tune2fs 1.41.12 (17-May-2010)
Filesystem volume name: DATA
Last mounted on: <not available>
Filesystem UUID: 24bd479c-d596-48a6-8423-370ea46d4a2b
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype sparse_super
Filesystem flags: signed_directory_hash
Default mount options: acl
Filesystem state: not clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 131584
Block count: 2104480
Reserved block count: 168358
Free blocks: 2067863
Free inodes: 131573
First block: 1
Block size: 1024
Fragment size: 1024
Reserved GDT blocks: 256
Blocks per group: 8192
Fragments per group: 8192
Inodes per group: 512
Inode blocks per group: 128
Filesystem created: Thu Aug 10 13:55:44 2017
Last mount time: Thu Aug 10 14:03:20 2017
Last write time: Thu Aug 10 15:11:22 2017
Mount count: 1
Maximum mount count: 39
Last checked: Thu Aug 10 13:55:44 2017
Check interval: 15552000 (6 months)
Next check after: Tue Feb 6 13:55:44 2018
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 256
Required extra isize: 28
Desired extra isize: 28
Journal inode: 12
Default directory hash: half_md4
Directory Hash Seed: c84b30d1-9ce4-4489-b99d-8d674e7691e3
dumpe2fs - dump ext2/ext3/ext4 filesystem information
dumpe2fs -h 相当于 tune2fs -l (只显示superblock中的属性内容)
fsck - check and repair a Linux file system
-t FSTYPE: 指定文件系统类型
-a: 自动修复
[root@localhost ~]# fsck /dev/sdb1
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
Backing up journal inode block information.
Moving journal from /.journal to hidden inode.
DATA: clean, 11/131584 files, 69515/2104480 blocks
[root@localhost ~]# tune2fs -l /dev/sdb1
...
Filesystem state: clean
...
e2fsck - check a Linux ext2/ext3/ext4 file system
blkid: 查询或查看磁盘设备的相关属性
UUID
TYPE
LABEL
[root@localhost ~]# blkid
/dev/sda3: UUID="f463e426-be5b-402e-bfdc-732f44d253b1" TYPE="ext4"
/dev//sdb1: UUID="24bd479c-d596-48a6-8423-370ea46d4a2b" TYPE="ext2" LABEL="DATA"
/dev/sdb1: LABEL="DATA" UUID="24bd479c-d596-48a6-8423-370ea46d4a2b" TYPE="ext2"
/dev/sda1: UUID="fdb53295-2c78-4eef-90cb-59226c6755be" TYPE="ext4"
/dev/sda2: UUID="806022d9-15f2-4fd7-9018-27175c108e59" TYPE="swap"
[root@localhost ~]# blkid /dev/sdb1
/dev/sdb1: LABEL="DATA" UUID="24bd479c-d596-48a6-8423-370ea46d4a2b" TYPE="ext2"
e2label: 用于查看或定义卷标
e2label 设备文件 卷标 : 设定卷标
[root@localhost ~]# e2label /dev/sdb1
DATA
[root@localhost ~]# e2label /dev/sdb1 MYDATA
[root@localhost ~]# e2label /dev/sdb1
MYDATA
挂载
挂载:将新的文件系统关联至当前根文件系统
卸载:将某文件系统与当前根文件系统的关联关系预以移除
mount:挂载
mount device mount_point
设备(device):
设备文件:/dev/sda5
卷标:LABEL=“”
UUID: UUID=“”
挂载点(mount_point):目录
要求:
1、此目录没有被其它进程使用;
2、目录得事先存在;
3、目录中的原有的文件将会暂时隐藏;
mount [options] [-o options] DEVICE MOUNT_POINT
-a: 表示挂载/etc/fstab文件中定义的所有文件系统
-n: 默认情况下,mount命令每挂载一个设备,都会把挂载的设备信息保存至/etc/mtab文件;使用—n选项意味着挂载设备时,不把信息写入此文件;
-t FSTYPE: 指定正在挂载设备上的文件系统的类型;不使用此选项时,mount会调用blkid命令获取对应文件系统的类型;
-r: 只读挂载,挂载光盘时常用此选项
-w: 读写挂载
-o: 指定额外的挂载选项,也即指定文件系统启用的属性;
remount: 重新挂载当前文件系统
ro: 挂载为只读
rw: 读写挂载
mount LABEL=mydisk -o noatime,nouser
mount -o remount,ro /mnt /dev/sdb6
[root@localhost ~]# mount 显示当前系统已经挂载的设备及挂载点
/dev/sda3 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
umount: 卸载某文件系统
umount 设备
umount 挂载点
卸载注意事项:
挂载的设备没有进程使用;
swap分区
swap的使用,使得系统可以过载使用。
free - Display amount of free and used memory in the system
-m (Megabytes)
[root@localhost ~]# free -m
total used free shared buffers cached
Mem: 981 187 794 0 15 58
-/+ buffers/cache: 114 867
Swap: 2517 0 2517
1) 申请swap分区磁盘
[root@localhost ~]# fdisk /dev/sdb
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (395-2353, default 395):
Using default value 395
Last cylinder, +cylinders or +size{K,M,G} (395-2353, default 2353): +512M
Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
...
Hex code (type L to list codes): 82
Changed system type of partition 5 to 82 (Linux swap / Solaris)
...
[root@localhost ~]# cat /proc/partitions
major minor #blocks name
8 0 10485760 sda
8 1 102400 sda1
8 2 2048000 sda2
8 3 8334336 sda3
8 16 52428800 sdb
8 17 2104483 sdb1
8 18 1060290 sdb2
8 20 1 sdb4
8 21 530113 sdb5
[root@localhost ~]# blkid /dev/sdb5
/dev/sdb5: UUID="a5267914-a632-4935-9a9c-bc4ab2a67d20" TYPE="swap"
2)创建swap文件系统(高级格式化)
[root@localhost ~]# mkswap -L MYSWAP /dev/sdb5
Setting up swapspace version 1, size = 530108 KiB
LABEL=MYSWAP, UUID=45be1d94-83ba-44b5-8b0d-54fcd1bee060
[root@localhost ~]# free -m
total used free shared buffers cached
Mem: 981 189 791 0 15 58
-/+ buffers/cache: 116 864
Swap: 1999 0 1999
[root@localhost ~]# swapon /dev/sdb5
[root@localhost ~]# free -m
total used free shared buffers cached
Mem: 981 190 791 0 15 58
-/+ buffers/cache: 116 864
Swap: 2517 0 2517
[root@localhost ~]# swapoff /dev/sdb5
[root@localhost ~]# free -m
total used free shared buffers cached
Mem: 981 190 790 0 15 58
-/+ buffers/cache: 116 864
Swap: 1999 0 1999
swapon DEVICE 启用新的swap到swap
swapoff DEVICE 关闭DEVICE的swap
此外mount命令,可以挂载iso镜像;
mount DEVICE MOUNT_POINT
-o loop: 挂载本地回环设备
[root@localhost ~]# ls -lh
total 14M
...
-rw-r--r--. 1 root root 14M Aug 10 23:19 rhci-5.8-1.iso
...
[root@localhost ~]# mount ~/rhci-5.8-1.iso /var
mount: /root/rhci-5.8-1.iso is not a block device (maybe try `-o loop‘?)
[root@localhost ~]# mount -o loop ~/rhci-5.8-1.iso /var
[root@localhost ~]# cd /var
[root@localhost var]# ls 可以查看镜像文件
CHANGELOG isolinux metadestroy.cfg version.txt
errata kernel-extras server-setup.sh workstation.cfg
GLS-GPG-KEY-curriculum merged.cfg TRANS.TBL
与挂载相关的配置文件:
/etc/mtab 默认将当前所有挂载的设备记录于此
/etc/fstab 操作系统在初始化时,会自动挂载此文件中定义的每个文件系统
mount -a:挂载/etc/fstab文件中定义的所有文件系统
要挂载的设备 挂载点 文件系统类型 挂载选项 转储频率(每多少天做一次完全备份) 文件系统检测次序(只有根可以为1)
/dev/sda5 /mnt/test ext3 defaults 0 0
fuser - identify processes using files or sockets
fuser: 验正进程正在使用的文件或套接字文件
-v: 查看某文件上正在运行的进程
-k:
-m
fuser -km MOUNT_POINT:终止正在访问此挂载点的所有进程
磁盘及文件系统管理