- GPT:GUID partition table,全局唯一标识磁盘分区表
- 可以支持无限个分区
- 可以支持EB级别的硬盘
*********************************************************************************************************************
新建分区,格式化分区,挂载分区:
fdisk新建分区:
NAME
fdisk - Partition table manipulator for Linux
[root@centos6 ~]#fdisk /dev/sda
Command (m for help): n //新建分区
First cylinder (5492-6528, default 5492): //使用默认起始柱面
Using default value 5492
Last cylinder, +cylinders or +size{K,M,G} (5492-6528, default 6528): +5G //分区大小为5G
Command (m for help): w //保存设置退出
The partition table has been altered!
[root@centos6 ~]#
[root@centos6 ~]#partx -a /dev/sda //新建分区后需要让内核重读分区表,这里需要执行两次
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
BLKPG: Device or resource busy
error adding partition 6
BLKPG: Device or resource busy
error adding partition 7
[root@centos6 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 3.7G 0 rom
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 19.5G 0 part /
├─sda3 8:3 0 19.5G 0 part /data
├─sda4 8:4 0 31.5K 0 part
├─sda5 8:5 0 2G 0 part [SWAP]
├─sda6 8:6 0 343.5K 0 part
└─sda7 8:7 0 5G 0 part //内核已识别到新建的分区
mke2fs:ext系列文件系统管理工具,格式化分区
-t {ext2|ext3|ext4}: 指定文件系统
-b {1024|2048|4096}: 指定分区块大小
-L : 指定分区卷标名
-m : 指定分区预留空间百分比,默认为5
//格式化新建的分区为ext4格式,块大小为2048,卷标为‘mydata’,预留空间20%的分区
[root@centos6 ~]#mke2fs -t ext4 -b 2048 -L 'mydata' -m 20 /dev/sda7
mke2fs 1.41.12 (17-May-2010)
Filesystem label=mydata
OS type: Linux
Block size=2048 (log=1)
Fragment size=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
328440 inodes, 2625440 blocks
525088 blocks (20.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=540016640
161 block groups
16384 blocks per group, 16384 fragments per group
2040 inodes per group
Superblock backups stored on blocks:
16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104,
2048000
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@centos6 ~]#
[root@centos6 ~]#tune2fs -l /dev/sda7 //查看分区具体信息
tune2fs 1.41.12 (17-May-2010)
Filesystem volume name: mydata //卷标
Last mounted on: <not available>
Filesystem UUID: 02bbf8a1-84a1-4d06-8bdf-b395dd40e7ff
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype extent flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize
Filesystem flags: signed_directory_hash
Default mount options: (none)
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 328440
Block count: 2625440
Reserved block count: 525088 //预留空间525088/2625440=20%
Free blocks: 2545609
Free inodes: 328429
First block: 0
Block size: 2048 //块大小
Fragment size: 2048
Reserved GDT blocks: 512
Blocks per group: 16384
Fragments per group: 16384
Inodes per group: 2040
Inode blocks per group: 255
Flex block group size: 16
Filesystem created: Sat May 25 14:44:07 2019
Last mount time: n/a
Last write time: Sat May 25 14:44:08 2019
Mount count: 0
Maximum mount count: 35
Last checked: Sat May 25 14:44:07 2019
Check interval: 15552000 (6 months)
Next check after: Thu Nov 21 14:44:07 2019
Lifetime writes: 145 MB
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: 8
Default directory hash: half_md4
Directory Hash Seed: 787f19b5-5bca-4591-99fd-e4b2fefd1cb6
Journal backup: inode blocks
[root@centos6 ~]#blkid /dev/sda7 //查看块设备属性
/dev/sda7: LABEL="mydata" UUID="02bbf8a1-84a1-4d06-8bdf-b395dd40e7ff" TYPE="ext4"
mount:挂载分区
-r : readonly,只读挂载
-w :read and write,读写挂载
-L : 以卷标方式指定设备
-o option:
atime/noatime:文件和目录被访问时是否更新访问时间戳
async/sync: 异步IO/同步IO
exec/noexec: 是否允许执行此设备上的二进制程序文件
remount: 重新挂载,不卸载的情况下重新挂载
acl : 在此设备上是否支持facl,默认不支持
默认挂载选项: rw, suid, dev, exec, auto, nouser, async
[root@centos6 ~]#mkdir /mydata
[root@centos6 ~]#mount -o noexec,noatime /dev/sda7 /mydata/
[root@centos6 ~]#mount
/dev/sda2 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)
/dev/sda3 on /data type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sda7 on /mydata type ext4 (rw,noexec,noatime) //显示已挂载成功
开机自动挂载:在/etc/fstab配置文件中修改数据,开启就能自动挂载
*********************************************************************************************************************
创建swap分区,并启动
fdisk新建分区
[root@centos6 ~]#fdisk /dev/sda
Command (m for help): n //新建分区
First cylinder (6146-6528, default 6146):
Using default value 6146
Last cylinder, +cylinders or +size{K,M,G} (6146-6528, default 6528): +1G //分区大小1G
Command (m for help): t //修改分区id,swap分区id为82,可以输入'l'查看
Partition number (1-8): 8
Hex code (type L to list codes): 82
Changed system type of partition 8 to 82 (Linux swap / Solaris)
Command (m for help): w //保存设置退出
The partition table has been altered!
[root@centos6 ~]#partx -a /dev/sda //把分区表重新加载到内核
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
BLKPG: Device or resource busy
error adding partition 6
BLKPG: Device or resource busy
error adding partition 7
BLKPG: Device or resource busy
error adding partition 8
[root@centos6 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 3.7G 0 rom
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 19.5G 0 part /
├─sda3 8:3 0 19.5G 0 part /data
├─sda4 8:4 0 31.5K 0 part
├─sda5 8:5 0 2G 0 part [SWAP]
├─sda6 8:6 0 343.5K 0 part
├─sda7 8:7 0 5G 0 part /mydata
└─sda8 8:8 0 1G 0 part //新建分区内核已设备
[root@centos6 ~]#
mkswap格式化分区
[root@centos6 ~]#mkswap /dev/sda8
Setting up swapspace version 1, size = 1060252 KiB
no label, UUID=bc7769b6-6132-4025-83e7-5fd8fe7f8e4f
[root@centos6 ~]#blkid /dev/sda8
/dev/sda8: UUID="bc7769b6-6132-4025-83e7-5fd8fe7f8e4f" TYPE="swap"
[root@centos6 ~]#
激活swap分区:
swapon: 激活swap分区
swapoff: 禁用swap分区
[root@centos6 ~]#swapon /dev/sda8
[root@centos6 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 3.7G 0 rom
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 19.5G 0 part /
├─sda3 8:3 0 19.5G 0 part /data
├─sda4 8:4 0 31.5K 0 part
├─sda5 8:5 0 2G 0 part [SWAP]
├─sda6 8:6 0 343.5K 0 part
├─sda7 8:7 0 5G 0 part /mydata
└─sda8 8:8 0 1G 0 part [SWAP] //swap分区已被激活
[root@centos6 ~]#
*********************************************************************************************************************
脚本实现:给脚本传入一个文件名,计算此文件第10行和第20行id号之和:
[root@centos6 test]#vi test.sh
1 #!/bin/bash
2 #
3 #
4
5 if [ $# -ne 1 ];then #未给脚本传入参数或者参数多于1个时报错退出
6 echo 'no file'
7 exit 1
8 fi
9
10 if ! [ -f $1 ];then #传入的文件不存在或者不是普通文件时报错退出
11 echo 'not file.'
12 exit 1
13 fi
14
15 testnum=$(head -20 $1 | wc -l) #判断给的文件是否至少有20行
16
17 if [ $testnum -ge 20 ];then
18 num1=$(head -10 $1 | tail -1 | cut -d: -f3)
19 num2=$(head -20 $1 | tail -1 | cut -d: -f3)
20 let 'sum = num1 + num2'
21 else #文件没有20行时退出
22 echo "the file no 20 line."
23 exit 1
24 fi
25
26 echo "the 10-line and 20 line id sum is: $sum"
27
*********************************************************************************************************************
脚本实现:判断,当主机名为空或者为localhost时给主机名赋值为newname
#!/bin/bash
2 #
3 #
4
5 hostName=$(hostname)
6
#$hostName必须加引号,不然当hostName为空时,下面一行将会被解析为
#[ -z -o == 'localhost' ],然后报错加上引号则被解析为[ -z '' -o '' == 'localhost' ]
#这里还可以写成 [ -z "$hostName" ] || [ "$hostName" == 'localhost' ]
7 if [ -z "$hostName" -o "$hostName" == 'localhost' ];then
8 hostname newname
9 fi
*********************************************************************************************************************
脚本实现:给脚本传入一个用户名,判断用户名id是奇数还是偶数:
1 #!/bin/bash
2 #
3 #
4
5 if [ $# -ne 1 ];then #未给脚本传入参数时报错退出
6 echo "input error."
7 exit 1
8 fi
9
10 id $1
11
12 if [ $? -eq 0 ];then #判断是否存在此用户名
13 num=$(id -u $1)
14 let 'i = num % 2'
15 if [ $i -eq 0 ];then
16 echo '偶数'
17 else
18 echo '奇数'
19 fi
20 else
21 echo 'no this user.'
22 exit 1
23 fi
*********************************************************************************************************************
LVM:Logical Volume Manager,逻辑卷管理
LVM的重点在于可以弹性的调整filesystem的容量,如此一来,整个磁盘空间的使用就具有了弹性!
LVM将一些零碎的磁盘分区(PV)合并成一个较大的磁盘(VG),然后再根据需要对整个较大的
磁盘(VG)进行划分成不同的小分区(LV),这些小分区(LV)是可以动态的扩展与缩小的。
LVM的实现,先创建PV:
pvcreate: 将实体partition创建成为PV
pvscan: 搜寻目前系统里面任何具有PV的磁盘
pvdisplay: 显示出目前系统上面的PV状态
pvremove: 将PV属性移除,让partition不再具有PV属性
Command (m for help): p
Disk /dev/sda: 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: 0x000b767d
Device Boot Start End Blocks Id System
/dev/sda1 * 1 131 1048576 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 131 2681 20480000 83 Linux
/dev/sda3 2681 5230 20480000 83 Linux
/dev/sda4 5230 6528 10419200 5 Extended
/dev/sda5 5231 5492 2097152 82 Linux swap / Solaris
/dev/sda6 5230 5230 343+ 83 Linux
/dev/sda7 5492 5753 2102141 8e Linux LVM #id为8e
/dev/sda8 5754 6146 3156741 8e Linux LVM
[root@newname ~]#pvcreate /dev/sda7
Physical volume "/dev/sda7" successfully created
[root@newname ~]#pvcreate /dev/sda8
Physical volume "/dev/sda8" successfully created
[root@newname ~]#pvdisplay
"/dev/sda7" is a new physical volume of "2.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda7
VG Name
PV Size 2.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID qK5yjj-mhA9-XPg3-oGut-iKAh-LwgM-xz7EMf
"/dev/sda8" is a new physical volume of "3.01 GiB"
--- NEW Physical volume ---
PV Name /dev/sda8
VG Name
PV Size 3.01 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID n6nVW2-R1kq-lyUN-NQHE-V6bf-lUZM-g3L9AW
[root@newname ~]#
创建VG:
vgcreate: 创建VG
vgscan: 搜寻系统上面是否有VG存在
vgdispaly: 显示目前系统上面的VG状态
vgextend: 在VG内添加额外的PV
vgreduce: 在VG内移除PV
vgchange: 配置VG是否启动
vgremove: 删除VG
[root@newname ~]#vgcreate test_vg /dev/sda7 /dev/sda8
Volume group "test_vg" successfully created
[root@newname ~]#vgdisplay
--- Volume group ---
VG Name test_vg
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 5.01 GiB
PE Size 4.00 MiB #PE未默认的4M
Total PE 1282
Alloc PE / Size 0 / 0
Free PE / Size 1282 / 5.01 GiB
VG UUID mFSOK3-X3S1-Ayns-qnCE-ATjC-RaJa-zitZmr
创建LV:
lvcreate: 创建LV
lvscan:查询系统上面的LV
lvdisplay: 显示系统上面LV的状态
lvextend: 在LV里面扩展容量
lvreduce: 在LV里面缩减容量
lvremove: 从VG中移除LV
lvresize: 对LV进行容量大小调整
[root@newname ~]#lvcreate -L 1.5G -n lv1 test_vg
Logical volume "lv1" created.
[root@newname ~]#lvcreate -L 1G -n lv2 test_vg
Logical volume "lv2" created.
[root@newname ~]#lvdisplay
--- Logical volume ---
LV Path /dev/test_vg/lv1
LV Name lv1
VG Name test_vg
LV UUID Ic3b3N-hpjR-ehoB-0lqM-T2DL-DsWi-TeLqij
LV Write Access read/write
LV Creation host, time newname, 2019-05-26 04:56:59 +0800
LV Status available
# open 0
LV Size 1.50 GiB
Current LE 384
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
--- Logical volume ---
LV Path /dev/test_vg/lv2
LV Name lv2
VG Name test_vg
LV UUID FfnDyL-Lrcv-f3hl-uP1q-L840-MtHu-ZawlXF
LV Write Access read/write
LV Creation host, time newname, 2019-05-26 04:57:15 +0800
LV Status available
# open 0
LV Size 1.00 GiB
Current LE 256
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
[root@newname ~]#
[root@newname ~]#lvresize -L+1G test_vg/lv2 #增加lv2容量
Size of logical volume test_vg/lv2 changed from 1.00 GiB (256 extents) to 2.00 GiB (512 extents).
Logical volume lv2 successfully resized.
[root@newname ~]#lvresize -L-1G /dev/test_vg/lv2 #减小lv2容量
WARNING: Reducing active logical volume to 1.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce test_vg/lv2? [y/n]: y
Size of logical volume test_vg/lv2 changed from 2.00 GiB (512 extents) to 1.00 GiB (256 extents).
Logical volume lv2 successfully resized.
要真正使用LVM还得格式化LV,并挂载。
扩容LVM:lvresize先扩容lv,然后resize2fs扩容文件系统
缩减LVM:先umount,resize2fs缩减文件系统,然后再lvresize LV
LVM还可以用 lvcreate -s 来创建快照