磁盘分区、格式化与挂载
磁盘分区、格式化与挂载
一、给磁盘分区
分区工具介绍
fdisk:分区时只修改分区表信息;操作简单;不支持大于2T的分区;只能使用交互式来分区。
parted:直接将分区信息写入磁盘;操作比较复杂;支持大于2T的分区,并且允许调整分区的大小;可以使用交互式或非交互式进行分区。
1、fdisk分区过程
1)选择所需要分区的硬盘:fdisk -uc /dev/xvdb[root@lt1 ~]# fdisk -uc /dev/xvdb #<==-uc是分区参数,如果不加会有警告;/dev/xvdb是需要分区的磁盘
Device contains neither a valid DOSpartition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with diskidentifier 0x12c129c5.
Changes will remain in memory only,until you decide to write them.
After that, of course, the previouscontent won't be recoverable.
Warning: invalid flag 0x0000 ofpartition table 4 will be corrected by w(rite)
Command (m for help):
2)打印fdisk的功能菜单:mCommand (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibilityflag
d delete a partition #<==删除一个已有分区
l listknown partition types #<==打印分区类型
m print this menu #<==打印这个功能菜单
n add a new partition #<==添加一个新分区
o create a new empty DOSpartition table
p print the partition table #<==打印分区表
q quit without saving changes #<==不保存退出
s create a new empty Sundisklabel
t change a partition's system id #<==修改分区的ID号(改变分区类型)
u change display/entry units
v verify the partition table
w write table to disk and exit #<==保存退出
x extra functionality (expertsonly)
3)添加一个新的分区:n
注意:
主分区和拓展分区最多有4个(因为分区表只有64字节,一个分区会占用16字节)
扩展分区之后还要建立逻辑分区才能使用(逻辑分区可以有多个)
无论主分区和扩展分区有多少个,逻辑分区的分区号都是从5开始的Command (m for help): n
Command action
e extended #<==e表示新建一个扩展分区
p primary partition (1-4) #<==p表示新建一个主分区
4)新建一个主分区(扩展分区和逻辑分区同理):pp #<==如果是扩展分区就选e,逻辑分区选l
Partition number (1-4): #<==选择主分区号(1~4)
5)选择分区号:1(是第几个主分区就选几)Partition number (1-4): 1
First sector (2048-20971519, default2048): #<==选择起始的扇区,默认是2048
6)选择起始扇区:直接回车选择默认First sector (2048-20971519, default2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G}(2048-20971519, default 20971519): #<==选择结束扇区,可以通过+size设置大小,默认到最后一个扇区
7)设置分区大小:+10MLast sector, +sectors or +size{K,M,G} (2048-20971519,default 20971519): +10M
Command (m for help):
8)打印分区表信息:pCommand (m for help): p
Disk /dev/xvdb: 10.7 GB, 10737418240bytes
255 heads, 63 sectors/track, 1305cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes /512 bytes
Disk identifier: 0x12c129c5
Device Boot Start End Blocks Id System
/dev/xvdb1 2048 22527 10240 83 Linux #<==已建好分区
9)保存退出:wCommand (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partitiontable.
Syncing disks.
10)分区完成之后,使用partprobe命令将分区信息加载到内核[root@lt1 ~]# partprobe /dev/xvdb
注:如果分区过程中输入错误,可以使用快捷键Ctrl+u退回
2、parted分区过程
parted用于对磁盘(或RAID磁盘)进行分区及管理,与fdisk分区工具相比,支持2TB以上的磁盘分区,并且允许调整分区的大小。可以使用交互式或者非交互式来为磁盘分区。交互式分区:
1)选择所需要用parted分区的磁盘:parted/dev/xvdb[root@lt1 ~]# parted /dev/xvdb
GNU Parted 2.1
Using /dev/xvdb
Welcome to GNU Parted! Type 'help' toview a list of commands.
(parted)
2)查看帮助:help(parted) help
align-check TYPE N check partition N for TYPE(min|opt) alignment
check NUMBER do a simple check on the file system
cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER copy file system to another partition
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel(partition table)
mkfs NUMBER FS-TYPE make a FS-TYPE file system onpartition NUMBER
mkpart PART-TYPE [FS-TYPE] START END make a partition
mkpartfs PART-TYPE FS-TYPE START END make a partition with a file system
move NUMBER START END move partition NUMBER
name NUMBER NAME name partition NUMBER as NAME
print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all
found partitions, or a particular partition
quit exit program
rescue START END rescue a lost partition near START and END
resize NUMBER START END resize partition NUMBER and its file system
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
set NUMBER FLAG STATE change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER
unit UNIT set the default unit to UNIT
version display the version number and copyright information of GNU
Parted
3)定义分区表格式:mklabel gpt (常用的有msdos和gpt格式,msdos不支持2T以上容量的磁盘,所以大于2TB的磁盘选择gpt分区格式)(parted) mklabel gpt
Warning: The existing disk label on/dev/xvdb will be destroyed and all data on this disk will be lost. Do
you want to continue?
Yes/No? yes
(parted)
4)创建分区,名称为p1:mkpart p1(parted) mkpart p1
File system type? [ext2]?
5)定义分区格式:ext2|ext3|ext4 ----centos6默认为ext4了,centos7是xfsFile system type? [ext2]? ext4
Start?
6)定义分区起始位置(单位支持K,M,G,T)
7)定义分区结束位置(单位支持K,M,G,T)Start? 1
End? 10M
8)查看当前分区情况:print(parted) print
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvdb: 10.7GB
Sector size (logical/physical):512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
1 1049kB 10.5MB 9437kB p1
9)以上步骤也结合为一个指令
mkpart primary 0 10
mkpart primary linux-swap 11 21 Ignore
mkpart logical ext4 22 32 Ignore(parted) mkpart p1 0 10 Ignore
Warning: The resulting partition is notproperly aligned for best performance.
(parted) mkpart p2 linux-swap 11 21Ignore
Warning: The resulting partition is notproperly aligned for best performance.
(parted) mkpart logical ext4 22 32Ignore
Warning: The resulting partition is notproperly aligned for best performance.
(parted) print
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvdb: 10.7GB
Sector size (logical/physical):512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
1 17.4kB 10.0MB 9983kB p1
2 11.0MB 21.0MB 10.0MB p2
3 22.0MB 32.0MB 10.0MB logical
10)删除分区:rm(parted) rm
Partition number? 1
二、扩容swap分区
swap分区,即缓存交换空间,当计算机内存用完时,会用到这部分交换空间,否则服务器就不能正常工作。新建物理分区扩容swap
1)初始的swap空间[root@lt1 ~]# free -m
total used free shared buffers cached
Mem: 980 88 891 0 7 25
-/+ buffers/cache: 55 924
Swap: 2047 0 2047
2)利用fdisk或者parted新建一个磁盘分区Command (m for help): p
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261cylinders
Units = cylinders of 16065 * 512 =8225280 bytes
Sector size (logical/physical): 512bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes /512 bytes
Disk identifier: 0x393390bf
Device Boot Start End Blocks Id System
/dev/sdb1 1 261 2096451 83 Linux
3)将该分区格式化成swap分区:mkswap[root@lt1 ~]# mkswap /dev/sdb1
Setting up swapspace version 1, size =2096444 KiB
no label,UUID=d04b54dc-f068-4563-9a0d-33bacca1d4d5
4)将新建的分区加入swap内存中:swapon[root@lt1 ~]# swapon /dev/sdb1
[root@lt1 ~]# free -m
total used free shared buffers cached
Mem: 980 90 890 0 7 25
-/+ buffers/cache: 57 923
Swap: 4095 0 4095
5)删除新增的swap分区:swapoff[root@lt1 ~]# swapoff /dev/sdb1
[root@lt1 ~]# free -m
total used free shared buffers cached
Mem: 980 89 891 0 7 25
-/+ buffers/cache: 56 924
Swap: 2047 0 2047使用文件构建swap
1)构建一个大小为2G的文件:ddif=/dev/zert of=/tmp/swap bs=1G count=2[root@lt1 ~]# dd if=/dev/zert of=/tmp/swap bs=1G count=2
[root@lt1 ~]# ls -lh /tmp/swap
-rw-r--r-- 1 root root 2.0G Oct 14 11:45/tmp/swap
2)格式化文件为swap分区[root@lt1 ~]# mkswap /tmp/swap
mkswap: /tmp/swap: warning: don't erasebootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size =2097148 KiB
no label,UUID=c8c89301-83ec-436d-9101-718ec5e401c9
3)将新建的分区加入swap内存中:swapon[root@lt1 ~]# swapon /tmp/swap
[root@lt1 ~]# free -m
total used free shared buffers cached
Mem: 980 79 901 0 5 21
-/+ buffers/cache: 52 928
Swap: 4095 3 4092
4)删除新增的swap分区:swapoff[root@lt1 ~]# swapoff /tmp/swap
[root@lt1 ~]# free -m
total used free shared buffers cached
Mem: 980 78 902 0 5 21
-/+ buffers/cache: 51 929
Swap: 2047 3 2044
三、格式化文件系统
新建硬盘分区之后,需要对硬盘进行格式化操作,以创建相应的文件系统,格式化过程如下:
1)查看系统支持的文件系统格式[root@lt1 ~]# ls/lib/modules/`uname -r`/kernel/fs
autofs4 cachefiles configfs dlm exportfs ext3 fat fuse jbd jffs2 mbcache.ko nfs_common nls ubifs xfs
btrfs cifs cramfs ecryptfs ext2 ext4 fscache gfs2 jbd2 lockd nfs nfsd squashfs udf
2)格式化成ext4系统
ext4是centos6系列默认的文件系统[root@lt1 ~]# mkfs -t ext4 -b 4096 -I 256 /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
4016 inodes, 4008 blocks
200 blocks (4.99%) reserved for the super user
First data block=0
1 block group
32768 blocks per group, 32768 fragments per group
4016 inodes per group
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
3)设置不检查[root@lt1 ~]# tune2fs -c -1 /dev/sdb1
tune2fs 1.41.12 (17-May-2010)
Setting maximal mount count to -1
四、挂载磁盘到服务器
1)磁盘格式化完毕之后,需要挂载到系统目录才能使用,挂载命令如下:[root@lt1 ~]# mount /dev/sdb1 /mnt
[root@lt1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 3.8G 13G 23% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 194M 29M 155M 16% /boot
/dev/sdb1 2.0G 35M 1.9G 2% /mnt3.6.3
2)取消挂载:[root@lt1 ~]# umount /dev/sdb1
[root@lt1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 3.8G 13G 23% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 194M 29M 155M 16% /boot
3)挂载CD或DVD光盘mount -t iso9660 /dev/cdrom /mnt #<==iso9660是光盘的格式
4)设置开机自动挂载
设置开机挂载文件非常有用,如果没有设置,可能有些用到磁盘的服务就无法正常工作。
开机挂载硬盘文件:/etc/fstab
在文件末尾写入:/dev/sdb1 /mnt ext3 dufaults 0 0[root@lt1 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Aug 2 07:12:45 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=e04ab670-2f2e-41de-906b-4067e2fa509e / ext4 defaults 1 1
UUID=c1fa18da-eacd-40b2-8046-7dbc584825c2 /boot ext4 defaults 1 2
UUID=0a1c2136-c98c-4c5b-bf12-a4ba2a6744cc swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
第一列:磁盘设备文件或者该设备的Label,UUID
第二列:设备的挂载点
第三列:文件系统的格式,包括ext2,ext3,ext4,reiserfs,nfs等
第四列:文件系统参数,一般选择默认defaults
Async/sync设置是否为同步方式运行,默认为async
auto/noauto当下载mount -a 的命令时,此文件系统是否被主动挂载。默认为auto
rw/ro是否以以只读或者读写模式挂载
exec/noexec限制此文件系统内是否能够进行"执行"的操作
user/nouser是否允许用户使用mount命令挂载
suid/nosuid是否允许SUID的存在
Usrquota启动文件系统支持磁盘配额模式
Grpquota启动文件系统对群组磁盘配额模式的支持
Defaults同事具有rw,suid,dev,exec,auto,nouser,async等默认参数的设置第五列:是否备份,一般自己挂载的选0
0代表不要做dump备份
1代表要每天进行dump的操作
2代表不定日期的进行dump操作
第六列:是否开机检查扇区,一般不检查,选00不要检验
1最早检验(一般根目录会选择)
21级别检验完成之后进行检验
至此,磁盘的分区,格式化,挂载完毕!