2019独角兽企业重金招聘Python工程师标准>>>
概览:
设备文件的创建
权限 设备名 类型 主设备号 次设备号
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".
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
磁盘管理:
I/O设备:
磁盘
网卡
文件:read, write, open, close
设备文件:特殊文件
只有inode,而没有数据
关联至一个驱动程序,进而跟对应的硬件设备打交道;
/dev: b, c
每个设备都有其设备号:
主设备号:用于标记设备类型
次设备号:用于标记同一类型中的不同设备
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:
b create a block (buffered) special file 有缓存
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
------------------------------------------------------------------
练习:写一个脚本
1、提示输入一个对其执行分区的新硬件设备文件;
2、提醒用户接下来的操作销毁所有的数据,你继续吗?
y或yes: 继续
n或no:退出
其它字符:提醒输错了,再来一次;
3、对磁盘新建分区:
主分区1:大小512M,
主分区2:大小2G
4、创建完成后显示创建的结果;
成功完成及修改次数
----------------------------------------------------------------
[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