磁盤管理
LINUX主要的一些知識—>基本分區、邏輯卷LVM、EXT3/4/XFS文件系統、磁盤限額、RAID
? 硬盤的分類及使用fdisk分區工具
1、認識硬盤的分類及特性
2、使用fdisk對磁盤進行操作,分區,格式化(重點)
3、開機自動掛載分區
? 實戰使用parted分區工具及擴展swap分區
1、使用parted操作大於等於4T硬盤
2、擴展服務器swap內存空間
硬盤的分類
常見硬盤品牌:希捷 西數 日立 hp dell
硬盤的類型
IDE SCSI SAS STAT(1 -2 -3)SSD
個人理解:
SAS硬盤
中間是無口的
STAT硬盤
中間是有口的
硬盤分兩種:熱插拔 非插拔 usb
硬盤的接口:STAT SAS PCIE FC
硬盤工作原理:機械類 固態類
硬盤尺寸分類
硬盤的尺寸和用途可分爲:
? 0.85英寸,多用於手機等便攜裝置中
? 1英寸, 多用於數碼相機
? 1.8英寸,用於部分筆記本電腦
? 2.5英寸,常用於筆記本電腦
? 3.5英寸,多用於臺式電腦中。採用3.5"硬盤的外置硬盤盒需要外接電源
硬盤分區介紹
硬盤分區就是把一個硬盤驅動器劃分爲多個邏輯存儲單元,我們把這些單元稱爲分區,通過這些分區,做爲這個操作系統的管理員就可以通過使用不同的分區來執行不同的功能操作,例如:
限制應用或用戶的可用空間
允許從同一個磁盤進行不同的操作系統多重啓動
將系統文件與用戶文件進行分隔
限制磁盤空間使用-
分區方案:MBR和GPT兩種
硬盤分區符認識
注意:使用分區工具fdisk對磁盤進行操作,分區,格式化(重點)
購買設備?設備聯機?磁盤分區?磁盤格式化(創建文件系統)?磁盤掛載?正常使用
1.查看當前系統有幾個分區
[root@centos-6-1 ~]# ls /dev/sd*
/dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb
[root@centos-6-1 ~]#
/dev/sda1
dev:設備文件的目錄
sd:SCSI硬盤
a:第一塊硬盤
1:分區號
內核對不同接口的硬盤有不同的命名方式:
RHEL5 RHEL6 KVM
IDE(並口): /dev/hda /dev/hdb /dev/vda
STAT(串口): /dev/sda /dev/sdb
SCSI: /dev/sda /dev/sdb
存儲的方式:本地存儲 外部存儲(通過FC nfs)
硬盤的分區方式:
硬盤大小 分區工具 分區表格式 分區數限制
2TB以下的硬盤 fdisk 、parted、cfdisk MSDOS(mbr) 支持4個分區或者3個主分區和1個邏輯分區
2TB以上的硬盤 parted 、gdisk GPT 沒有限制
注意:分區表格式轉換,會導致數據丟失;從msdos轉換到GPT,反之亦然
使用fdisk管理分區
在虛擬機新添加硬盤後,兩種方法可以發現硬盤:
1、 重啓服務器
2、 通過命令-A echo "- - -" > /sys/class/scsi_host/host0/scan
3、 通過命令-B for i in {1..32} ;do echo "- - -" > /sys/class/scsi_host/host$i/scan;done
fdisk:磁盤分區,是Linux發行版本中最常用的分區工具
用法:fdisk [選項] device
[root@centos-6-1 ~]# fdisk /dev/sda
Command (m for help): m #查看幫助
Command action
a toggle a bootable flag #設定硬盤啓動分區
b edit bsd disklabel #編輯一個BSD格式的分區
c toggle the dos compatibility flag #編輯一個DOS兼容分區
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)
Command (m for help):
驗證一下:如果看不到新劃分的分區,可以通過命令partprobe /dev/sdb 刷新讀取一下分區表就OK了
常用的選項 : -l 查看硬盤分區表
1.查看硬盤分區表
[root@centos-6-1 ~]# fdisk -l
Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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: 0x00035f2c
Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 12794 102554624 83 Linux
/dev/sda3 12794 13055 2097152 82 Linux swap / Solaris
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: 0x00000000
[root@centos-6-1 ~]#
例如:給/dev/sdb分區
2.給/dev/sdb分區
[root@centos-6-1 ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x4f3837a9.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
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):
3.查看/dev/sda 硬盤下一共有幾個分區
Command (m for help): p
Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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: 0x00035f2c
Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 12794 102554624 83 Linux
/dev/sda3 12794 13055 2097152 82 Linux swap / Solaris
Command (m for help):
4.給/dev/sdb進行分區 分1個10G的主分區
[root@centos-6-1 ~]# fdisk /dev/sdb #對/dev/sdb進行分區
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x191a16e6.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
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): n #創建一個新的分區
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1 #創建第一個主分區
First cylinder (1-6527, default 1): #默認從1開始,按下回車即可
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-6527, default 6527): +10G #給第一塊主分區從1-10G的大小
Command (m for help): m #查看幫助
Command action
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)
Command (m for help): p #打印分區信息
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: 0x191a16e6
Device Boot Start End Blocks Id System
/dev/sdb1 1 1306 10490413+ 83 Linux
Command (m for help):
Command (m for help): w #保存並退出,如果不保存的話剛纔做的分區都將清空
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos-6-1 ~]#
cfdisk分區劃分工具
如果不指定劃分那個硬盤分區,默認是第一個如:/dev/sda
cfdisk (util-linux-ng 2.17.2)
Disk Drive: /dev/sda
Size: 107374182400 bytes, 107.3 GB
Heads: 255 Sectors per Track: 63 Cylinders: 13054
Name Flags Part Type FS Type [Label] Size (MB)
------------------------------------------------------------------------------------------------------
Pri/Log Free Space 1.05 *
sda1 Boot Primary Linux ext3 209.72 *
sda2 Primary Linux ext3 105015.94 *
sda3 Primary Linux swap / Solaris 2147.49 *
[ Help ] [ New ] [ Print ] [ Quit ] [ Units ] [ Write ]
Write partition table to disk (this might destroy data)
mkfs工具
首先需要對分區進行格式化。格式化分區非常重要,它是用來創建文件系統的,其命令爲mkfs,意爲make file system。
例如:
[root@panda ~]# mkfs.xfs /dev/sdb1
[root@panda ~]# mkfs.ext4 /dev/sdb2
剛纔有一個實戰是關於分區的,接着用上面的例子往下做
1.把/dev/sdb1分區格式化成ext4文件系統格式的
[root@centos-6-1 ~]#
[root@centos-6-1 ~]# ls /dev/sd* #查看/dev/sdb1分區
/dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb /dev/sdb1
[root@centos-6-1 ~]# mkfs.ext4
mkfs.ext4 mkfs.ext4dev
[root@centos-6-1 ~]# mkfs.ext4 /dev/sdb1 #給/dev/sdb1分區進行格式化,格式化成ext4格式的
mke2fs 1.41.12 (17-May-2010)
文件系統標籤=
操作系統:Linux
塊大小=4096 (log=2)
分塊大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655776 inodes, 2622603 blocks
131130 blocks (5.00%) reserved for the super user
第一個數據塊=0
Maximum filesystem blocks=2688548864
81 block groups
32768 blocks per group, 32768 fragments per group
8096 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
正在寫入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@centos-6-1 ~]# echo $?
0
[root@centos-6-1 ~]# fdisk -l #查看所有分區
Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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: 0x00035f2c
Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 12794 102554624 83 Linux
/dev/sda3 12794 13055 2097152 82 Linux swap / Solaris
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: 0xc29d702e
Device Boot Start End Blocks Id System
/dev/sdb1 1 1306 10490413+ 83 Linux
[root@centos-6-1 ~]#
手工進行掛載
掛載的限制:
? 根目錄是必須掛載的,而且一定要先於其他mount point被掛載。因爲mount是所有目錄的根目錄,其他目錄都是由根目錄 / 衍生出來的。
? 掛載點必須是已經存在的目錄,系統不會自己創建
? 掛載點的目錄可以任意指定,但必須遵守必要的系統目錄架構原則
? 所有掛載點在同一時間只能被掛載一次
? 所有分區在同一時間只能掛在一次
? 若進行卸載,必須將工作目錄退出掛載點(及其子目錄),也就先umount之後纔可以被卸載,一個掛載點可以被多個分區設備掛載
第一步:先創建一個目錄做爲添加硬盤設備的掛載點
[root@xuegod72 ~]# mkdir /sdb1
第二步:進行掛載,用到命令mount
mount 設備絕對路徑(中間有個空格) 你建立的掛載點目錄
第三步:如何進行卸載
1) umount 掛載點
2) umount 設備路徑
注意:在卸載前,你要保證你不在當前掛載目錄裏面,保證這個目錄是沒有被佔用的,退出你當前的掛載點
可以通過下面這種方式查看:
1.將/dev/sdb目錄掛載到 /adb目錄下,在使用umount卸載
[root@centos-6-1 ~]# mkdir /adb #創建掛載目錄
[root@centos-6-1 ~]# mount /dev/sdb1 /adb/ #掛載到到/adb/目錄下
[root@centos-6-1 ~]# 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)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sdb1 on /adb type ext4 (rw)
[root@centos-6-1 ~]#
[root@centos-6-1 ~]#
[root@centos-6-1 ~]# vi /adb/aaa #編輯一個文件,ctrl+z將此命令放入後臺
[1]+ Stopped vi /adb/aaa
[root@centos-6-1 ~]# jobs #查看後臺任務
[1]+ Stopped vi /adb/aaa
[root@centos-6-1 ~]# umount /adb/ #卸載 /adb 目錄
umount: /adb: device is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
[root@centos-6-1 ~]# lsof /adb/ #查看adb目錄所佔用的進程或程序等
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
vi 1411 root 3u REG 8,17 4096 12 /adb/.aaa.swp
[root@centos-6-1 ~]#
[root@centos-6-1 ~]# fg #切換到後臺的第一個進程
vi /adb/aaa
[root@centos-6-1 ~]#
[root@centos-6-1 ~]#
[root@centos-6-1 ~]# lsof /adb/ #查看/adb目錄是否還在被佔用
[root@centos-6-1 ~]# umount /adb/ #卸載/adb
[root@centos-6-1 ~]# echo $? #驗證上一個命令是否執行成功0爲真,除0以外都爲假
0
[root@centos-6-1 ~]# 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)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
[root@centos-6-1 ~]#
自動掛載
/etc/fstab 是自動掛載文件,開機後會讀取/etc/fstab文件進行掛載,一般所有掛載點等信息都寫到/etc/fstab文件中
最好指定UUID值,指定後不會導致分區混亂,因爲UUID是設備唯一編號
要掛載的分區設備 掛載點 文件系統類型 掛載選項 是否備份 是否檢測
/dev/sdb1 /sdb1 ext4 defaults 0 0
其中第四列:parameters-文件系統的參數
Async/sync 設置是否爲同步方式運行,默認爲async
auto/noauto 當下載mount -a 的命令時,此文件系統是否被主動掛載。默認爲auto
rw/ro 是否以以只讀或者讀寫模式掛載
exec/noexe 限制此文件系統內是否能夠進行"執行"的操作
user/nouser 是否允許用戶使用mount命令掛載
suid/nosuid 是否允許SUID的存在
Usrquota 啓動文件系統支持磁盤配額模式
Grpquota 啓動文件系統對羣組磁盤配額模式的支持
Defaults 同時具有rw,suid,dev,exec,auto,nouser,async等默認參數的設置
第五列:是否進行備份。通常這個參數的值爲0或者1
0 代表不要做備份
1 代表要每天進行操作
2 代表不定日期的進行操作
第六列:是否檢驗扇區:開機的過程中,系統默認會以fsck檢驗我們系統是否爲完整
0 不要檢驗
1 最早檢驗(一般根目錄會選擇)
2 1級別檢驗完成之後進行檢驗
掛載方式有下面幾種:
1、 通過UUID,是設備的唯一編號
2、 通過卷標 label=zhangsan /sdb1 xfs defaults 0 0 不推薦用這種方式進行掛載
3、 通過設備的主從設備號 mknod
轉換分區類型:
command acton (m for help):t #轉換分區類型
partition number (1-4):2 #選擇第二個主分區
hex code (type L to list codes):82 #按L可列出分區類型所對應的編碼
可以使用 “partprobe” 命令,重新探測磁盤中分區清空, #partprobe /dev/sdb
格式化與掛載: (掛載目錄可以自行創建也可指定存在的空目錄)
mksf.ext3 /dev/sdb1 把第二塊硬盤的第一個主分區格式化爲ext3
mkswap /dev/sdb2 初始化swap區,此區不可格式化。
mount /dev/sdb1 /mnt/d #將第一個分區掛載到d這個目錄
重啓後自動掛載:vi /etc/fstab
添加:/dev/sdb1 /mnt/d ext3 default 0 0
1.查看/etc/fstab文件內容
[root@centos-6-1 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Jun 7 18:00:58 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=bafc5fcc-f32b-493b-93fd-304bce7ad569 / ext4 defaults 1 1
UUID=8d5f402e-a564-4088-ba5d-6274cfe39179 /boot ext4 defaults 1 2
UUID=c8371ba7-8706-41bf-a8a4-925e19f4c81c 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
[root@centos-6-1 ~]#
使用UUID實現開機自動掛載
UUID是一個標識你係統中的存儲設備的字符串,其目的是幫助使用者唯一的確定系統中的所有存儲設備,全球唯一,不管它們是什麼類型的。它可以標識DVD驅動器,USB存儲設備以及你係統中的硬盤設備等。
1.將/dev/sdb1分區UUID等寫入到/etc/fstab文件中
獲取設備的UUID
[root@centos-6-1 ~]# blkid #獲取UUID
/dev/sda1: UUID="8d5f402e-a564-4088-ba5d-6274cfe39179" TYPE="ext4"
/dev/sda2: UUID="bafc5fcc-f32b-493b-93fd-304bce7ad569" TYPE="ext4"
/dev/sda3: UUID="c8371ba7-8706-41bf-a8a4-925e19f4c81c" TYPE="swap"
/dev/sdb1: UUID="25831e3b-0d39-4ea7-a2e4-9663b4edbb54" TYPE="ext4"
[root@centos-6-1 ~]#
進行掛載
[root@centos-6-1 ~]# echo "UUID="25831e3b-0d39-4ea7-a2e4-9663b4edbb54" /adb ext4 defaults 0 0" >> /etc/fstab
[root@centos-6-1 ~]# echo $?
0
[root@centos-6-1 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Jun 7 18:00:58 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=bafc5fcc-f32b-493b-93fd-304bce7ad569 / ext4 defaults 1 1
UUID=8d5f402e-a564-4088-ba5d-6274cfe39179 /boot ext4 defaults 1 2
UUID=c8371ba7-8706-41bf-a8a4-925e19f4c81c 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
UUID=25831e3b-0d39-4ea7-a2e4-9663b4edbb54 /adb ext4 defaults 0 0
[root@centos-6-1 ~]# mount -a
[root@centos-6-1 ~]# 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)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sdb1 on /adb type ext4 (rw)
[root@centos-6-1 ~]#
3.更改/dev/sdb1分區類型,更改成LVM
[root@centos-6-1 ~]# fdisk /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): t
Selected partition 1
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-
4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx
5 Extended 42 SFS 86 NTFS volume set da Non-FS data
6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility
8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt
9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access
a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary
16 Hidden FAT16 64 Novell Netware af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT
1e Hidden W95 FAT1
Hex code (type L to list codes): 8e
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos-6-1 ~]# echo $?
0
[root@centos-6-1 ~]# fdisk -l
Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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: 0x00035f2c
Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 12794 102554624 83 Linux
/dev/sda3 12794 13055 2097152 82 Linux swap / Solaris
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: 0xc29d702e
Device Boot Start End Blocks Id System
/dev/sdb1 1 1306 10490413+ 8e Linux LVM
[root@centos-6-1 ~]# mkfs
mkfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.ext4dev
[root@centos-6-1 ~]# mount /dev/sdb1 /adb/
[root@centos-6-1 ~]# echo $?
0
[root@centos-6-1 ~]# cd /adb/
[root@centos-6-1 adb]# ls
lost+found
[root@centos-6-1 adb]#
Parted 磁盤分區工具
parted是Linux中磁盤分區管理的高級工具,對GPT分區方案,也可以使用gdisk工具進行
1.查看所有的磁盤狀態
[root@centos-6-1 ~]# parted -l
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system 標誌
1 1049kB 211MB 210MB primary ext4 啓動
2 211MB 105GB 105GB primary ext4
3 105GB 107GB 2147MB primary linux-swap(v1)
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system 標誌
1 32.3kB 10.7GB 10.7GB primary ext4 lvm
錯誤: /dev/sdc: unrecognised disk label
[root@centos-6-1 ~]#
2.對/dev/sdc硬盤進行分區格式化
[root@centos-6-1 ~]# parted /dev/sdc #對/dev/sdc硬盤進行分區
GNU Parted 2.1
使用 /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt #更改成GPT格式
(parted) mkpart primary 0 100 #本次分區大小爲0-100MB 默認是(MB)
警告: The resulting partition is not properly aligned for best performance.
忽略/Ignore/放棄/Cancel? Ignore #同意(Ignore)更改並保存
(parted) print #查看分區表
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdc: 2199GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name 標誌
1 17.4kB 100MB 100MB primary
(parted) q #退出
信息: You may need to update /etc/fstab.
[root@centos-6-1 ~]#
[root@centos-6-1 ~]# mkfs -t ext4 /dev/sdc1 #格式化成ext4格式的文件系統
mke2fs 1.41.12 (17-May-2010)
文件系統標籤=
操作系統:Linux
塊大小=1024 (log=0)
分塊大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
24480 inodes, 97636 blocks
4881 blocks (5.00%) reserved for the super user
第一個數據塊=1
Maximum filesystem blocks=67371008
12 block groups
8192 blocks per group, 8192 fragments per group
2040 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
正在寫入inode表: 完成
Creating journal (4096 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@centos-6-1 ~]# echo $? #驗證上一個命令是否執行成功0爲真,除0以外都爲假
0
[root@centos-6-1 ~]# mkdir /sdc1 #創建掛載目錄
[root@centos-6-1 ~]# mount /dev/sdc1 /sdc1/ #掛載到/sdc1/目錄下
[root@centos-6-1 ~]#
[root@centos-6-1 ~]#
[root@centos-6-1 ~]# df | tail -l #查看所有分區
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 100813224 1210924 94474572 2% /
tmpfs 509956 0 509956 0% /dev/shm
/dev/sda1 194241 30723 153278 17% /boot
/dev/sdc1 90443 1550 84012 2% /sdc1
[root@centos-6-1 ~]#
參數解釋:
Number:這個就是分區的號舉例來說,1號代表的是 /dev/sdb1 的意思;
Start:起始的磁柱位置;
End:結束的磁柱位置;
Size: 分區有多少容量;
Type:就是分區的類型,有primary, extended, logical等類型;
File system:就如同 fdisk 的 System ID 之意。
擴展swap交換分區
Swap分區在系統的物理內存不夠用的時候,把硬盤空間中的一部分空間釋放出來,以供當前運行的程序使用。那些被釋放的空間可能來自一些很長時間沒有什麼操作的程序,這些被釋放的空間被臨時保存到Swap分區中,等到那些程序要運行時,再從Swap分區中恢復保存的數據到內存中
SWAP 分區在一些情況下可以提升 Linux 操作系統性能,但在某些情況下又會成爲系統運行的瓶頸,下面我們就來詳細介紹下 Linux 中的 SWAP 交換分區
SWAP 交換分區是用來存放內存溢出來的數據。
我們可以將內存想像成一個盆子,運行程序時的數據都被作爲水放在內存這個盆子當中。
如果你運行太多的應用程序,導致水過多而(內存這個)盆子已經裝不下時,就會將多於部分的水裝到 SWAP 交換分區當中。
優點:
內存用完時,提供更多運行空間
mkswap /devices (格式化成swap格式)
swapon /swap (激活/swap,加入到swap分區中)
vim /etc/fstab (開機自啓動新添加的swap分區)
—>添加
/devices swap swap defaults 0 0
如果不想使用需要刪除,只需要執行
#swapoff /swap
1.服務器內存溢出,裝系統時新建的swap分區不夠用,想在添加兩塊swap分區分別爲100MB並測試
[root@centos-6-1 ~]# fdisk /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): p
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: 0xc29d702e
Device Boot Start End Blocks Id System
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): +100M
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (15-6527, default 15):
Using default value 15
Last cylinder, +cylinders or +size{K,M,G} (15-6527, default 6527): +100M
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos-6-1 ~]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 112416 KiB
no label, UUID=e09b5c1f-118f-4510-bd9c-1ad891104dbb
[root@centos-6-1 ~]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 112448 KiB
no label, UUID=66e11e3c-7a19-469c-b53b-8d0ae1ba2f25
[root@centos-6-1 ~]# ll /dev/sdb*
brw-rw----. 1 root disk 8, 16 6月 26 23:27 /dev/sdb
brw-rw----. 1 root disk 8, 17 6月 26 23:27 /dev/sdb1
brw-rw----. 1 root disk 8, 18 6月 26 23:27 /dev/sdb2
[root@centos-6-1 ~]#
[root@centos-6-1 ~]#
[root@centos-6-1 ~]# free -m
total used free shared buffers cached
Mem: 996 262 733 0 8 156
-/+ buffers/cache: 97 898
Swap: 2047 0 2047
[root@centos-6-1 ~]# swapon /dev/sdb1
[root@centos-6-1 ~]# free -m
total used free shared buffers cached
Mem: 996 262 733 0 8 156
-/+ buffers/cache: 97 898
Swap: 2157 0 2157
[root@centos-6-1 ~]# swapon /dev/sdb2
[root@centos-6-1 ~]# free -m
total used free shared buffers cached
Mem: 996 262 733 0 8 156
-/+ buffers/cache: 97 898
Swap: 2267 0 2267
[root@centos-6-1 ~]# swapoff /dev/sdb1;swapoff /dev/sdb2
[root@centos-6-1 ~]# free -m
total used free shared buffers cached
Mem: 996 262 733 0 8 156
-/+ buffers/cache: 97 898
Swap: 2047 0 2047
[root@centos-6-1 ~]#
開機自動掛載
/dev/sdb1 swap swap defaults 0 0
通過文件增加SWAP空間
1.新建1個100MB的文件名字爲swap_file,並添加到swap交換分區上
[root@centos-6-1 ~]# dd if=/dev/zero of=swap_file bs=1M count=100
記錄了100+0 的讀入
記錄了100+0 的寫出
104857600字節(105 MB)已複製,0.0943323 秒,1.1 GB/秒
[root@centos-6-1 ~]# du -sh swap_file
100M swap_file
[root@centos-6-1 ~]# mkswap -f swap_file
Setting up swapspace version 1, size = 102396 KiB
no label, UUID=6ebd6614-15fd-4390-89df-94715a7d74a6
[root@centos-6-1 ~]# free -m
total used free shared buffers cached
Mem: 996 365 630 0 8 256
-/+ buffers/cache: 100 895
Swap: 2047 0 2047
[root@centos-6-1 ~]# chmod 600 swap_file
[root@centos-6-1 ~]# swapon swap_file
[root@centos-6-1 ~]# free -m
total used free shared buffers cached
Mem: 996 366 629 0 8 256
-/+ buffers/cache: 100 895
Swap: 2147 0 2147
[root@centos-6-1 ~]#