Linux下文件系统管理
分区
Linux下最常用的分区工具是fdisk操作简单,易用,但使用这个分区工具最多分区分15个。
fdisk –l 查看系统上所有磁盘分区信息。
fdisk –l /dev/sd*
[root@www ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Sector size (logical/physical): 512 bytes /512 bytes
I/O size (minimum/optimal): 512 bytes / 512bytes
Disk identifier: 0x07a95eb7
Device Boot Start End Blocks Id System
fdisk /dev/sd*
几个常见的参数
d delete a partition 删除分区
l list known partition types 各分区类型所对应的systemID
n add a new partition 创建一个新分区
p print the partition table 打印出分区表相当于fdisk–l /dev/sd*
q quit without saving changes 不保存当前操作退出fdisk分区工具
t change a partition's system id改变一个分区的system ID
w write table to disk and exit 保存当前操作退出fdisk
创建实例
[root@www ~]# fdisk/dev/sdb对/dev/sdb进行分区
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').
Command (m for help): nn表示新建一个分区
Command action
e extended 表示扩展分区
p primary partition (1-4) 表示主分区
p输入批表示创建一个主分区
Partition number (1-4): 1输入分区编号/dev/sdb1
First cylinder (1-2610, default 1): 敲回车键表示从第一个柱面开始
Using default value 1
Last cylinder, +cylinders or +size{K,M,G}(1-2610, default 2610): +5G 分区大小为5G这里也可以键入到第几柱面
Command (m for help): p 查看该磁盘的分区信息
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Sector size (logical/physical): 512 bytes /512 bytes
I/O size (minimum/optimal): 512 bytes / 512bytes
Disk identifier: 0x07a95eb7
设备名称 开始柱面 结束柱面 块大小system Id 类型
Device Boot Start End Blocks Id System
/dev/sdb1 1 654 5253223+ 83 Linux
Command (m for help): w保存并退出
Thepartition table has been altered!
Callingioctl() to re-read partition table.
Syncingdisks.
查看分区操作是否写入系统内核
[root@www ~]# cat/proc/partitions
major minor #blocks name
8 0 26214400 sda
8 1 512000 sda1
8 2 25701376 sda2
8 16 20971520 sdb
8 17 5253223 sdb1
内核能读取到硬盘分区表但是有时内核会读取不到这时就使用partx命令命令如下
[root@www~]# partx -a /dev/sdb1 /dev/sdb
创建文件系统
使用mkfsmkfs.ext[2|3|4],mke2fs来进行文件系统的创建一般使用mke2fs
mkfs –t ext2= mkfs.ext2 = mke2fs –t ext2
mkfs –t ext3= mkfs.ext3 = mke2fs –t ext3
mkfs –t ext4= mkfs.ext4 = mke2fs –t ext4
mke2fs
mke2fs –t type Device:创建什么类型的文件系统
mke2fs –b{1024|2048|4096} Device分区的块大小
[root@www ~]# mke2fs -text4 -b 2048 /dev/sdc2
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=2048 (log=1)
Fragment size=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
197880 inodes, 1578386 blocks
78919 blocks (5.00%) reserved for the superuser
First data block=0
Maximum filesystem blocks=538968064
97 block groups
16384 blocks per group, 16384 fragments pergroup
2040 inodes per group
Superblock backups stored on blocks:
16384,49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystemaccounting information: done
This filesystem will be automaticallychecked every 24 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@www ~]# tune2fs -l/dev/sdc2 | grep "Block size"
Block size: 2048
[root@www ~]# blkid/dev/sdc2
/dev/sdc2:UUID="ac40a50d-ddee-44b0-a6a8-481e7b33a548" TYPE="ext4"
mke2fs创建默认是ext2
[root@www ~]# mke2fs /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
328656 inodes, 1313305 blocks
65665 blocks (5.00%) reserved for the superuser
First data block=0
Maximum filesystem blocks=1346371584
41 block groups
32768 blocks per group, 32768 fragments pergroup
8016 inodes per group
Superblock backups stored on blocks:
32768,98304, 163840, 229376, 294912, 819200, 884736
Writing inode tables: done
Writing superblocks and filesystemaccounting information: done
This filesystem will be automaticallychecked every 24 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
mke2fs配置文件/etc/mke2fs.conf
[defaults]
base_features = sparse_super,filetype,resize_inode,dir_index,ext_attr 默认使用ext2文件系统
blocksize = 4096 默认块大小
inode_size = 256
inode_ratio = 16384
将下面的features后的赋值补充到base_features后面系统默认分区的文件系统就是ext3或ext4
[fs_types]
ext3 = {
features = has_journal
}
ext4 = {
features =has_journal,extent,huge_file,flex_bg,uninit_bg,dir_nlink,extra_isize
inode_size = 256
{
tune2fs -l [DEVICE] 其显示的信息十分丰富
查看其块大小
[root@www ~]# tune2fs -l/dev/sdb1 | grep "Block size"
Block size: 4096
blkid命令查看UUID和文件系统类型
[root@www ~]# blkid /dev/sdb1
/dev/sdb1:UUID="8595c196-9c41-4bfb-8b76-4930705e6c12" TYPE="ext2"
挂载文件系统
mount –t type Device 挂载点
mount命令查看当前系统挂载的文件系统
[root@www ~]# mount
/dev/mapper/vg_centos-lv_root on / typeext4 (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)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc typebinfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs typerpc_pipefs (rw)
nfsd on /proc/fs/nfsd type nfsd (rw)
/etc/mtab文件也可以查看当前系统的挂载情况mount命令所看到的结果均来自此文件
[root@www ~]# cat/etc/mtab
/dev/mapper/vg_centos-lv_root / ext4 rw 0 0
proc /proc proc rw 0 0
sysfs /sys sysfs rw 0 0
devpts /dev/pts devpts rw,gid=5,mode=620 00
tmpfs /dev/shm tmpfs rw 0 0
/dev/sda1 /boot ext4 rw 0 0
none /proc/sys/fs/binfmt_misc binfmt_miscrw 0 0
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefsrw 0 0
nfsd /proc/fs/nfsd nfsd rw 0 0
使用mount命令挂载当时有效,系统重启失效将要挂载的文件系统吸入/etc/fstab文件中
LVM
LVM是逻辑盘卷管理Logical VolumeManager,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性
LVM创建顺序是PV——〉VG——〉LV 不知道大家注意没有使用默认安装CentOS时系统默认使用的LVM
创建前准备
将分区的system ID改成8e(Linux LVM)
[root@www ~]# fdisk/dev/sdc
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e(Linux LVM)
Command (m for help): p
Disk /dev/sdc: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Sector size (logical/physical): 512 bytes /512 bytes
I/O size (minimum/optimal): 512 bytes / 512bytes
Disk identifier: 0xbeaf1b91
Device Boot Start End Blocks Id System
/dev/sdc1 1 654 5253223+ 83 Linux
/dev/sdc2 655 1047 3156772+ 8e Linux LVM
LVM的创建流程pvcreate Device
[root@www ~]# pvcreate/dev/sdc2
Physical volume "/dev/sdc2" successfully created
[root@www ~]# pvcreate/dev/sdb1
Physical volume "/dev/sdb1" successfully created
2. [root@www ~]# vgcreate testvg/dev/sdb1 /dev/sdc2
Volume group "testvg" successfully created
3. [root@www ~]# lvcreate -L 5G -ntestlv testvg
Logical volume "testlv" created
验证lv创建起没有
[root@www ~]# lvdisplay/dev/testvg/testlv
---Logical volume ---
LVPath /dev/testvg/testlv
LV Name testlv
VG Name testvg
LVUUID UiuqXG-22fG-qKoz-dFzw-oHvP-OZYY-aOII0F
LVWrite Access read/write
LVCreation host, time www.linux.com, 2014-09-26 20:21:14 +0800
LVStatus available
#open 0
LVSize 5.00 GiB
Current LE 1280
Segments 1
Allocation inherit
Read ahead sectors auto
-currently set to 256
Block device 253:2
创建文件系统
[root@www ~]# mke2fs -text4 /dev/testvg/testlv
挂载文件系统[root@www ~]# mount -t ext4 /dev/testvg/testlv /mnt
[root@www ~]# mount
/dev/mapper/vg_centos-lv_rooton / type ext4 (rw)
procon /proc type proc (rw)
sysfson /sys type sysfs (rw)
devptson /dev/pts type devpts (rw,gid=5,mode=620)
tmpfson /dev/shm type tmpfs (rw)
/dev/sda1on /boot type ext4 (rw)
noneon /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpcon /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
nfsdon /proc/fs/nfsd type nfsd (rw)
gvfs-fuse-daemonon /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
/dev/sr0on /media/CentOS_6.4_Final type iso9660(ro,nosuid,nodev,uhelper=udisks,uid=0,gid=0,iocharset=utf8,mode=0400,dmode=0500)
/dev/mapper/testvg-testlvon /mnt type ext4 (rw)
LVM详细使用介绍
vgvg卷组扩容
准备一个pv
[root@www ~]# pvcreate /dev/sdb2
Physical volume "/dev/sdb2" successfully created
扩展vg的容量
使用vgextend 命令 vgextend vgname Device
[root@www ~]# vgextendtestvg /dev/sdb2
Volume group "testvg" successfully extended
[root@www ~]# vgs
Couldn't find device with uuid 2QpQ6S-B9Oz-7PB1-fIKo-Lhdw-h00t-3EtX00.
VG #PV #LV #SN Attr VSize VFree
VG 2 0 0wz-pn- 8.02g 8.02g
testvg 3 1 0 wz--n- 15.02g 10.02g
vg_centos 1 2 0wz--n- 24.51g 0
2. vg卷组的缩减
将要移除pv上的数据移动到其他pv上去
[root@www ~]# pvmove/dev/sdb1
/dev/sdb1: Moved: 0.1%
/dev/sdb1: Moved: 64.8%
/dev/sdb1: Moved: 100.0%
vgreduce命令缩减vg vgreduce vgname Device
[root@www ~]# vgreducetestvg /dev/sdb1
Removed "/dev/sdb1" from volume group "testvg"
3. 删除vg卷组
[root@www ~]# vgremove testvg
Volume group "testvg" successfully removed
lv
lv的扩展
首先确定lv所在的卷组的空间是不是足够多
1. 扩展物理边界
[root@www ~]# lvextend -L+2G /dev/testvg/testlv
Extending logical volume testlv to 7.00 GiB
Logical volume testlv successfully resized
2. 扩展逻辑边界
[root@www ~]# resize2fs/dev/testvg/testlv
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/testvg/testlv is mountedon /mnt; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of/dev/testvg/testlv to 1835008 (4k) blocks.
The filesystem on /dev/testvg/testlv is now1835008 blocks long.
3. 验证lv扩展成功没
[root@www ~]# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root
23G 17G 4.2G 81% /
tmpfs 935M 0 935M 0% /dev/shm
/dev/sda1 485M 47M 414M 11% /boot
/dev/mapper/testvg-testlv
6.9G 140M 6.5G 3% /mnt
lv缩减
缩减lv之前一定要将其卸载并强行检测文件系统
[root@www ~]# umount/dev/testvg/testlv
[root@www ~]# fsck -f /dev/testvg/testlvfsck是文件系统检测工具加上–f参数表示强制检测
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/testvg-testlv: 12/458752 files(0.0% non-contiguous), 64448/1835008 blocks
如果不检测文件系统在以后使用过程中可能会产生乱码缩减逻辑卷
resizefs Device size
[root@www ~]# resize2fs/dev/testvg/testlv 5G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on/dev/testvg/testlv to 1310720 (4k) blocks.
2. 缩减物理卷
[root@www ~]# lvreduce -L5G /dev/testvg/testlv
WARNING: Reducing active logical volume to 5.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce testlv? [y/n]:y
Reducing logical volume testlv to 5.00 GiB
Logical volume testlv successfully resized
3. 移除lv卷
a)卸载lv卷mount
b)使用lvremove Device移除lv卷
[root@www ~]# umount /mnt
[root@www ~]# lvremove/dev/testvg/testlv
Do you really want toremove active logical volume testlv? [y/n]: y
Logical volume"testlv" successfully removed
验证
[root@www ~]# lvs
Couldn't find device with uuid 2QpQ6S-B9Oz-7PB1-fIKo-Lhdw-h00t-3EtX00.
LV VG Attr LSize Pool Origin Data% Move LogCpy%Sync Convert
lv_root vg_centos -wi-ao--- 22.57g
lv_swap vg_centos -wi-ao--- 1.94g
注意
lvreduce –L [+|-]size Device 表示在原卷大小的基础上加或者减少多少空间
lvreduce –L size Device 表示直接将原卷扩大多少空间。
LVM裁减顺序是 LV——〉VG——〉PV