概覽:
設備文件的建立
權限 設備名 類型 主設備號 次設備號
mknod [-m MODE] NAME type [MAJOR MINOR] 建立時,先查看是否存在設備號的衝突
零磁道零扇區是MBR:Master Boot Record
主引導記錄
512Bytes: 引導啓動OS
前446bytes: 存放程序,bootloader
64bytes: 分區表,每16bytes標記一個分區,一共4個分區(主分區)
2bytes: 55AA, MBR有效性標記 標記當前mbr是否有效
fdisk是不支持GPT格式的硬盤分區,parted是能夠支持的
parted [options] [device [command [options...]...]] 直接跟設備不跟其餘會分區
-l, --list 會列出全部分區信息
lists partition layout on all block devices
command
mklabel label-type
Create a new disklabel (partition table) of label-type. label-type should be one of
"bsd", "dvh", "gpt", "loop", "mac", "msdos", "pc98", or "sun".node
mkpart part-type [fs-type] start end
Make a part-type partition with filesystem fs-type (if specified), beginning at start
and ending at end (by default in megabytes). fs-type can be one of "fat16", "fat32",
"ext2", "HFS", "linux-swap", "NTFS", "reiserfs", or "ufs". part-type should be one of
"primary", "logical", or "extended".
move partition start end
Move partition so that it begins at start and ends at end. Note: move never changes
the minor number.
rm partition
Delete partition.
print Display the partition table.
# parted /dev/sdb mkpart primary ext4 1 10G
#parted /dev/sdb rm 1
# parted /dev/sdb print
磁盤管理:linux
I/O設備:
磁盤
網卡shell
文件:read, write, open, close緩存
設備文件:特殊文件
只有inode,而沒有數據bash
關聯至一個驅動程序,進而跟對應的硬件設備打交道;app
/dev: b, cide
每一個設備都有其設備號:
主設備號:用於標記設備類型
次設備號:用於標記同一類型中的不一樣設備工具
mknod - make block or character special files
-m, --mode=MODE
set file permission bits to MODE, not a=rw - umask
mknod [OPTION]... NAME TYPE [MAJOR MINOR] 主設備號能夠相同,次設備號不能相同
TYPE may be:oop
b create a block (buffered) special file 有緩存ui
c, u create a character (unbuffered) special file 無緩存的
p create a FIFO
設備文件的命名:由ICANN:互聯網名稱與數字地址分配機構(Internet
Corporation for Assigned Names and Numbers)
[root@linux_basic tmp]#mknod -m 664 blocktest b 1 20
[root@linux_basic tmp]#ls -l
total 0
brw-rw-r--. 1 root root 1, 20 Jan 4 07:38 blocktest
[root@linux_basic tmp]#rm blocktest
rm: remove block special file `blocktest'? y
[root@localhost tmp]# mknod test c 1 30
[root@localhost tmp]# ls -l
total 0
lrwxrwxrwx. 1 root root 8 Dec 2 13:10 file1 -> softfile
-rw-r--r--. 1 root root 0 Dec 2 13:09 softfile
crw-r--r--. 1 root root 1, 30 Dec 2 13:27 test
磁盤設備文件命名:
/dev/hd:
IDE: 並口接口, 133MB/s
/dev/sd:
USB: 串行
SATA: 串行接口,6Gbps/8
SCSI: 並行,(Small Computer System Interface) 小型計算機系統接口
SAS:串行,
rpm:轉數
/dev/sd[a-z]
分區:數字
/dev/sda1
/dev/sda2
分區編號:
主+擴展分區:1-4
邏輯分區:5開始
硬盤分區:
磁道:track
扇區:sector
柱面:cylinder:
分區根據柱面劃分,在同一盤片上的兩面磁頭同時讀取,速度快
零磁道零扇區是MBR:Master Boot Record
主引導記錄
512Bytes: 引導啓動OS
前446bytes: 存放程序,bootloader
64bytes: 分區表,每16bytes標記一個分區,一共4個分區(主分區)
2bytes: 55AA, MBR有效性標記 標記當前mbr是否有效
1T: 3主+1擴展(切割1個或多個邏輯分區)
[root@linux_basic tmp]#fdisk -l | grep -A 3 "^Disk /dev/.*"
Disk /dev/sda: 48.3 GB, 48318382080 bytes
255 heads(磁頭數), 63 sectors/track每磁道多少扇區, 5874 cylinders(柱面)
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
--
Disk /dev/mapper/vg_lvm-lv1: 12.9 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
--
Disk /dev/mapper/vg_lvm-lv2: 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
/proc:
[root@linux_basic tmp]#cat /proc/partitions
major minor #blocks name
8 0 47185920 sda
8 1 204800 sda1
8 2 10485760 sda2
8 3 36494336 sda3
253 0 12582912 dm-0
253 1 10485760 dm-1
分區建立:
fdisk - Partition table manipulator for Linux
操做Linux分區表
查看:fdisk -l [DEVICE]...
fdisk工具最多能夠識別15個分區
建立分區:fdisk [DEVICE]
交互式界面,有許多子命令
p: 顯示磁盤分區表
p print the partition table
n: new,新建分區
n add a new partition
d: delete,刪除分區
d delete a partition
t: 修改分區的系統ID
t change a partition's system id
l: 列表出全部已知的系統ID
l list known partition types
83 Linux
82 Linux swap / So
8e Linux LVM
fd Linux raid auto
w: 保存並退出
w write table to disk and exit
q: 不保存退出
q quit without saving changes
選n後出現
e extended
p primary partition (1-4)
對於已經有分區處於使用狀態的磁盤來說,新建分區後須要讓內核重讀其分區表:
CentOS 5:
# partprobe [DEVICE]
CentOS 6:
partx -a [DEVICE]
partx - telling the kernel about presence and numbering of on-disk partitions.
-a add specified partitions or read disk and add all partitions
讀磁盤新增的分區
kpartx -af [DEVICE]
kpartx - Create device maps from partition tables
-a Add partition mappings
-l List partition mappings that would be added -a
-f force creation of mappings; overrides ’no_partitions’ feature
查看/proc/partitions,有顯示則建立成功
分區建立工具:sfdisk和parted
------------------------------------------------------------------
Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
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): +5G
注意這裏設定大小時的格式
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: 0x00000000
Device Boot Start End Blocks Id System
/dev/sdb1 1 654 5253223+ 5 Extended
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@lvm ~]# partx -a /dev/sdb
Warning: Disk has a valid GPT signature but invalid PMBR.
Assuming this disk is *not* a GPT disk anymore.
Use gpt kernel option to override. Use GNU Parted to correct disk.
BLKPG: Device or resource busy
error adding partition 1
[root@lvm ~]# kpartx -af /dev/sdb
Warning: Disk has a valid GPT signature but invalid PMBR.
Assuming this disk is *not* a GPT disk anymore.
Use gpt kernel option to override. Use GNU Parted to correct disk.
[root@lvm ~]# cat /proc/partitions
major minor #blocks name
8 0 52428800 sda
8 1 204800 sda1
8 2 10485760 sda2
8 16 52428800 sdb
8 17 1 sdb1 說明建立成功了
-------------------------------------------------------------------------------
死循環的一個簡單腳本
[root@lvm ~]# bash -n hello.sh
[root@lvm ~]# ./hello.sh
Hello:ld
Again hello:jhfld
Again hello:n
[root@lvm ~]# cat hello.sh
#!/bin/bash
#
read -p "Hello:" kir
while true;do
if [ "$kir" == 'n' ];then
break;
else
read -p "Again hello:" kir
fi
done
------------------------------------------------------------------
練習:寫一個腳本
一、提示輸入一個對其執行分區的新硬件設備文件;
二、提醒用戶接下來的操做銷燬全部的數據,你繼續嗎?
y或yes: 繼續
n或no:退出
其它字符:提醒輸錯了,再來一次;
三、對磁盤新建分區:
主分區1:大小512M,
主分區2:大小2G
四、建立完成後顯示建立的結果;
成功完成及修改次數
----------------------------------------------------------------
[root@lvm shells]# cat disk_part.sh
#!/bin/bash
#
read -p "Input a partition disk device path:" disk
read -p "The following operation will damage the all data on the disk.yes|no(y|n)?" flag
while true;do
if [ "$flag" == 'n' -o "$flag" == 'no' ];then
break;
elif [ "$flag" == 'y' -o "$flag" == 'yes' ];then
echo "n
p
1
+512M
n
p
2
+2G w"|fdisk $disk > /dev/null partx -a $disk kpartx -af $disk fdisk -l | grep "$disk" read -p "The following operation will damage the all data on the disk.yes|no(y|s)?" flag else echo "Input error." read -p "Again input:" flag fi done