咏月东南 2019-10-19
Linux fdisk是一个创建和维护分区表的程序,它兼容DOS类型的分区表、BSD或者SUN类型的磁盘列表。
fdisk [必要参数][选择参数]
必要参数:
-l 列出素所有分区表
-u 与"-l"搭配使用,显示分区数目
选择参数:
-s<分区编号> 指定分区
-v 版本信息
菜单操作说明
m :显示菜单和帮助信息
a :活动分区标记/引导分区
d :删除分区
l :显示分区类型
n :新建分区
p :显示分区信息
q :退出不保存
t :设置分区号
v :进行分区检查
w :保存修改
x :扩展应用,高级功能
显示当前分区情况:
# fdisk -l?Disk /dev/sda: 10.7 GB, 10737418240 bytes255 heads, 63 sectors/track, 1305 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytes? Device Boot Start End Blocks Id System/dev/sda1 * 1 13 104391 83 Linux/dev/sda2 14 1305 10377990 8e Linux LVM?Disk /dev/sdb: 5368 MB, 5368709120 bytes255 heads, 63 sectors/track, 652 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytes?Disk /dev/sdb doesn‘t contain a valid partition table
显示SCSI硬盘的每个分区情况
# fdisk -lu ?Disk /dev/sda: 10.7 GB, 10737418240 bytes255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectorsUnits = sectors of 1 * 512 = 512 bytes? Device Boot Start End Blocks Id System/dev/sda1 * 63 208844 104391 83 Linux/dev/sda2 208845 20964824 10377990 8e Linux LVM?Disk /dev/sdb: 5368 MB, 5368709120 bytes255 heads, 63 sectors/track, 652 cylinders, total 10485760 sectorsUnits = sectors of 1 * 512 = 512 bytes?Disk /dev/sdb doesn‘t contain a valid partition table
gdisk
-l /dev/sdX 查看分区信息
gdisk
(?|m) 帮助
n 建立分区
d 删除分区
c 更改分区名字
I 显示分区的详细信息
p 显示分区信息
t转换分区类型
a 将指定分区设置/取消 活动分区
l 查看分区类型
o 重建分区表
v 验证分区表,显示剩余没有被分区划分的扇区数量
q 退出不保存,不保存,所有的修改都不生效
w 退出并保存
gdisk时用来创建GPT分区的,我们可以看到,gdisk和fdisk的开头是有些不同的。
[ ~]# gdisk /dev/sdbGPT fdisk (gdisk) version 0.8.10?Partition table scan: MBR: not present BSD: not present APM: not present GPT: present?Found valid GPT with corrupt MBR; using GPT and will write newprotective MBR on save.?Command (? for help):
一个GPT分区
Command (? for help): oThis option deletes all partitions and creates a new protective MBR.Proceed? (Y/N): y?Command (? for help): pDisk /dev/sdb: 1048576000 sectors, 500.0 GiB ----------------磁盘大小,显示扇区数Logical sector size: 512 bytes ---------------------------扇区大小Disk identifier (GUID): E072194E-C4DA-449B-9CF9-E5171235E6D3 Partition table holds up to 128 entriesFirst usable sector is 34, last usable sector is 1048575966 -------------------第一个有用的扇区在34,最后一个有用的扇区在104857966Partitions will be aligned on 2048-sector boundariesTotal free space is 1048575933 sectors (500.0 GiB)?Number Start (sector) End (sector) Size Code Name?Command (? for help):
更改磁盘的类型
Command (? for help): t ------------------------------------------更改磁盘类型Using 1Current type is ‘Linux filesystem‘Hex code or GUID (L to show codes, Enter = 8300): 8e00Changed type of partition to ‘Linux LVM‘ -------------------------更改为"Linux LVM"?Command (? for help): pDisk /dev/sdb: 1048576000 sectors, 500.0 GiBLogical sector size: 512 bytesDisk identifier (GUID): E072194E-C4DA-449B-9CF9-E5171235E6D3Partition table holds up to 128 entriesFirst usable sector is 34, last usable sector is 1048575966Partitions will be aligned on 2048-sector boundariesTotal free space is 838860733 sectors (400.0 GiB)?Number Start (sector) End (sector) Size Code Name 2048 209717247 100.0 GiB 8E00 Linux LVM ------------------------ 改过了?Command (? for help):
当我们查看磁盘的时候,我们首先要看磁盘是那种分区格式的,如果是MBR分区格式,我们最好使用fdisk –l,要是为GPT分区格式的硬盘,我们就使用gdisk –l来查看。因为fdisk查看GPT格式的磁盘最大能看到2T,再多的磁盘分区就无法查看,还会出错。
parted命令是由GNU组织开发的一款功能强大的磁盘分区和分区大小调整工具,与fdisk不同,它支持调整分区的大小。作为一种设计用于Linux的工具,它没有构建成处理与fdisk关联的多种分区类型,但是,它可以处理最常见的分区格式,包括:ext2、ext3、fat16、fat32、NTFS、ReiserFS、JFS、XFS、UFS、HFS以及Linux交换分区。
parted? -h 显示帮助信息? -l 显示磁盘信息? -s 脚本模式,不提示用户? -v 显示版本号
[ ~]# parted -h Usage: parted [OPTION]... [DEVICE [COMMAND [PARAMETERS]...]...]Apply COMMANDs with PARAMETERS to DEVICE. If no COMMAND(s) are given, run ininteractive mode.?OPTIONs: -h, --help displays this help message -l, --list lists partition layout on all block devices -m, --machine displays machine parseable output -s, --script never prompts for user intervention -v, --version displays the version -a, --align=[none|cyl|min|opt] alignment for new partitions?COMMANDs: 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 ... ...
[ ~]# parted -l Model: VMware, VMware Virtual S (scsi)Disk /dev/sda: 215GBSector size (logical/physical): 512B/512BPartition Table: msdos?Number Start End Size Type File system Flags 1049kB 1075MB 1074MB primary ext4 boot 1075MB 54.8GB 53.7GB primary ext4 54.8GB 108GB 53.7GB primary ext4 108GB 215GB 106GB extended 108GB 113GB 4295MB logical linux-swap(v1) 113GB 134GB 21.5GB logical
parted /dev/sdX 分区? mklabel label_type 设置分区格式(MBR,GPT)? mkpart part-type [fs-type] start end(单位Mb)设置分区? print 查看分区信息? help [command] 查看命令[command]如何使用? rm num 删除一个分区? set num flag status 设置flag(boot,root,swap)状态(on,off)? quit 退出
[ ~]# parted /dev/sdbGNU Parted 2.1Using /dev/sdbWelcome to GNU Parted! Type ‘help‘ to view a list of commands.(parted)
(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 on partition 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(parted)
(parted) help mklabel mklabel,mktable LABEL-TYPE create a new disklabel (partition table)? LABEL-TYPE is one of: aix, amiga, bsd, dvh, gpt, mac, msdos, pc98, sun, loop --------------可以设置的类型(parted)
(parted) mklabel gpt ------------------------------设置GPT分区(parted) print Model: VMware, VMware Virtual S (scsi)Disk /dev/sdb: 537GBSector size (logical/physical): 512B/512BPartition Table: gpt?Number Start End Size File system Name Flags?(parted)
(parted) help mkpart ----------------------------------查看分区命令如何用 mkpart PART-TYPE [FS-TYPE] START END make a partition? PART-TYPE is one of: primary, logical, extended FS-TYPE is one of: ext4, ext3, ext2, fat32, fat16, hfsx, hfs+, hfs, jfs, swsusp, linux-swap(v1), linux-swap(v0), ntfs, reiserfs, hp-ufs, sun-ufs, xfs, apfs2, apfs1, asfs, amufs5, amufs4, amufs3, amufs2, amufs1, amufs0, amufs, affs7, affs6, affs5, affs4, affs3, affs2, affs1, affs0, linux-swap, linux-swap(new), linux-swap(old) START and END are disk locations, such as 4GB or 10%. Negative values count from the end of the disk. For example, -1s specifies exactly the last sector. ‘mkpart‘ makes a partition without creating a new file system on the partition. FS-TYPE may be specified to set an appropriate partition ID.(parted) mkpart primary 0 200G -------------------------------------------------设置分区Warning: The resulting partition is not properly aligned for best performance.Ignore/Cancel? ignore (parted) print -----------------------------------------------------查看分区 Model: VMware, VMware Virtual S (scsi)Disk /dev/sdb: 537GBSector size (logical/physical): 512B/512BPartition Table: gpt?Number Start End Size File system Name Flags 17.4kB 200GB 200GB primary -------------------------------分区大小?(parted) quit Information: You may need to update /etc/fstab.
这个命令是一个实时生效的命令,当我们分完一个分区后,不用结束就可以直接生效。不想fdisk和给gdisk那样需要退出后才保存生效
列出块设备
lsblk命令用于列出所有可用块设备的信息,而且还能显示他们之间的依赖关系,但是它不会列出RAM盘的信息。块设备有硬盘,闪存盘,cd-ROM等等
如:
lsblk /dev/sdb
[ ~]# lsblk /dev/sdaNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTsda 8:0 0 200G 0 disk ├─sda1 8:1 0 1G 0 part /boot├─sda2 8:2 0 50G 0 part /├─sda3 8:3 0 50G 0 part /app├─sda4 8:4 0 1K 0 part ├─sda5 8:5 0 4G 0 part [SWAP]└─sda6 8:6 0 20G 0 part
[ ~]# cat /proc/partitions major minor #blocks name 0 209715200 sda 1 1048576 sda1 2 52428800 sda2 3 52428800 sda3 4 1 sda4 5 4194304 sda5 6 20980890 sda6 16 524288000 sdb 17 195312483 sdb1
我们在原硬盘未使用的空间进行分区后,在物理设备上已经完成了,使用fdisk -l可以查看到,但是内存信息中并没有新的分区信息,此时我们需要手动更新分区表,在centOS6中使用partx,在5 或者7中可以使用partprobe(这个在6中有BUG)
partx -a /dev/sda 更新内存中的磁盘分区表信息
partx -d --nr 6 /dev/sda 删除掉在内存中的已经删除的第六块分区的信息
[ ~]# lsblk /dev/sda NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTsda 8:0 0 200G 0 disk ├─sda1 8:1 0 1G 0 part /boot├─sda2 8:2 0 50G 0 part /├─sda3 8:3 0 50G 0 part /app├─sda4 8:4 0 1K 0 part └─sda5 8:5 0 4G 0 part [SWAP]
[ ~]# fdisk -l /dev/sda | grep "^/dev/sda*"/dev/sda1 * 1 131 1048576 83 Linux/dev/sda2 131 6658 52428800 83 Linux/dev/sda3 6658 13185 52428800 83 Linux/dev/sda4 13185 26109 103808000 5 Extended/dev/sda5 13186 13708 4194304 82 Linux swap / Solaris
Command (m for help): nFirst cylinder (13185-26109, default 13185): 14000Last cylinder, +cylinders or +size{K,M,G} (14000-26109, default 26109): +10G?Command (m for help): p?Disk /dev/sda: 214.7 GB, 214748364800 bytesheads, 63 sectors/track, 26108 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x0007a903? Device Boot Start End Blocks Id System/dev/sda1 * 1 131 1048576 83 Linux/dev/sda2 131 6658 52428800 83 Linux/dev/sda3 6658 13185 52428800 83 Linux/dev/sda4 13185 26109 103808000 5 Extended/dev/sda5 13186 13708 4194304 82 Linux swap / Solaris/dev/sda6 14000 15305 10490444+ 83 Linux?Command (m for help): wThe partition table has been altered!?Calling ioctl() to re-read partition table.?WARNING: Re-reading the partition table failed with error 16: Device or resource busy.The kernel still uses the old table. The new table will be used atthe next reboot or after you run partprobe(8) or kpartx(8)Syncing disks.
[ ~]# fdisk -l /dev/sda | grep "^/dev/sda.*"/dev/sda1 * 1 131 1048576 83 Linux/dev/sda2 131 6658 52428800 83 Linux/dev/sda3 6658 13185 52428800 83 Linux/dev/sda4 13185 26109 103808000 5 Extended/dev/sda5 13186 13708 4194304 82 Linux swap / Solaris/dev/sda6 14000 15305 10490444+ 83 Linux[ ~]# lsblk /dev/sdaNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTsda 8:0 0 200G 0 disk ├─sda1 8:1 0 1G 0 part /boot├─sda2 8:2 0 50G 0 part /├─sda3 8:3 0 50G 0 part /app├─sda4 8:4 0 1K 0 part └─sda5 8:5 0 4G 0 part [SWAP]
[ ~]# partx -a /dev/sdaBLKPG: Device or resource busyerror adding partition 1BLKPG: Device or resource busyerror adding partition 2BLKPG: Device or resource busyerror adding partition 3BLKPG: Device or resource busyerror adding partition 4BLKPG: Device or resource busyerror adding partition 5[ ~]# lsblk /dev/sda NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTsda 8:0 0 200G 0 disk ├─sda1 8:1 0 1G 0 part /boot├─sda2 8:2 0 50G 0 part /├─sda3 8:3 0 50G 0 part /app├─sda4 8:4 0 1K 0 part ├─sda5 8:5 0 4G 0 part [SWAP]└─sda6 8:6 0 10G 0 part