#有时df -h查看分区信息无法满足需求#
借助Android系统自带的sgdisk可以查看详细的分区信息,类似fdisk命令,sgdisk同样可以创建分区,功能也很强大。
一、sgdisk帮助信息
127|console:/ # sgdisk
USAGE: sgdisk [OPTION...] <device>--attributes operate on partition attributes--set-alignment set sector alignment--backup backup GPT to file--change-name change partition's name--recompute-chs recompute CHS values in protective/hybrid MBR--delete delete a partition--display-alignment show number of sectors per allocation block--move-second-header move second header to end of disk--end-of-largest show end of largest free block--first-in-largest show start of the largest free block--first-aligned-in-largest show start of the largest free block, aligned--mbrtogpt convert MBR to GPT--randomize-guids randomize disk and partition GUIDs--hybrid create hybrid MBR--info show detailed information on partition--move-main-table adjust the location of the main partition table--load-backup load GPT backup from file--list-types list known partition types--gpttombr convert GPT to MBR--new create new partition--largest-new create largest possible new partition--clear clear partition table--print-mbr print MBR partition table--print print partition table--pretend make changes in memory, but don't write them--transpose transpose two partitions--replicate replicate partition table--sort sort partition table entries--resize-table resize partition table--typecode change partition type code--transform-bsd transform BSD disklabel partition to GPT--partition-guid set partition GUID--disk-guid set disk GUID--verify check partition table integrity--version display version information--zap zap (destroy) GPT (but not MBR) data structures--zap-all zap (destroy) GPT and MBR data structures
二、查看磁盘分区详情
sgdisk --print /dev/block/sdaconsole:/ # sgdisk --print /dev/block/sda
Disk /dev/block/sda: 16777216 sectors, 64.0 GiB
Sector size (logical/physical): 4096/4096 bytes
Disk identifier (GUID): 54054CFA-8B64-425D-A63F-B6B6F795C66E
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 5
First usable sector is 6, last usable sector is 16777210
Partitions will be aligned on 256-sector boundaries
Total free space is 1780981 sectors (6.8 GiB)Number Start (sector) End (sector) Size Code Name1 256 511 1024.0 KiB 8300 misc2 512 16895 64.0 MiB 8300 boot_a3 16896 33279 64.0 MiB 8300 boot_b4 33280 33535 1024.0 KiB 8300 dtb_a5 33536 33791 1024.0 KiB 8300 dtb_b6 33792 37887 16.0 MiB 8300 metadata7 37888 6067199 23.0 GiB 8300 super8 6067200 6591487 2.0 GiB 8300 kdump9 6591488 6607871 64.0 MiB 8300 persist10 6607872 14996479 32.0 GiB 8300 userdata
使用这个指令可以很方便查看RAW分区的大小,以及起始和终止簇。新的版本df -h无法查看super分区大小,也需要使用sgdisk来查看。
通过上面信息可以看到sda大小为64GB,还剩余6.8GB空间未划分。
系统df -h信息如下:
console:/ # df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 3.9G 856K 3.9G 1% /dev
tmpfs 3.9G 0 3.9G 0% /mnt
/dev/block/sda6 11M 104K 11M 1% /metadata
/dev/block/dm-0 5.9G 3.6G 2.2G 63% /
/dev/block/dm-5 8.2G 444M 7.7G 6% /mnt/scratch
overlay 8.2G 444M 7.7G 6% /vendor
overlay 8.2G 444M 7.7G 6% /product
overlay 8.2G 444M 7.7G 6% /system_ext
overlay 8.2G 444M 7.7G 6% /odm
tmpfs 3.9G 0 3.9G 0% /apex
tmpfs 3.9G 264K 3.9G 1% /linkerconfig
/dev/block/sda9 58M 24K 58M 1% /persist
/dev/block/sda10 32G 1.6G 30G 6% /data
tmpfs 3.9G 0 3.9G 0% /data_mirror
/dev/block/sda8 1.9G 24K 1.9G 1% /data/vendor/kdump
/dev/fuse 32G 1.6G 30G 6% /mnt/user/0/emulated
/dev/fuse 32G 1.6G 30G 6% /mnt/user/10/emulated
三、分区操作实操
(1)查看可以开始使用的第一个簇
console:/ # sgdisk --first-in-largest /dev/block/sda
14996480
通过上面的分区详情也可以看到userdata最后一个簇是14996479,接下来可以使用的簇就是14996480,与上面指令得到的一致。
做地址对齐后的第一个可用簇:
console:/ # sgdisk --first-aligned-in-largest /dev/block/sda
14996480
(2)查看可以使用的最后一个簇
console:/ # sgdisk --end-of-largest /dev/block/sda
16777210
如果希望将剩余所有空间都划分出去,可以使用此指令查看最后一个簇。
(3)新建一个test分区
console:/ # sgdisk --new 11:0:0 /dev/block/sda
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.
console:/ # sgdisk --print /dev/block/sda
Disk /dev/block/sda: 16777216 sectors, 64.0 GiB
Sector size (logical/physical): 4096/4096 bytes
Disk identifier (GUID): 54054CFA-8B64-425D-A63F-B6B6F795C66E
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 5
First usable sector is 6, last usable sector is 16777210
Partitions will be aligned on 256-sector boundaries
Total free space is 250 sectors (1000.0 KiB)Number Start (sector) End (sector) Size Code Name1 256 511 1024.0 KiB 8300 misc2 512 16895 64.0 MiB 8300 boot_a3 16896 33279 64.0 MiB 8300 boot_b4 33280 33535 1024.0 KiB 8300 dtb_a5 33536 33791 1024.0 KiB 8300 dtb_b6 33792 37887 16.0 MiB 8300 metadata7 37888 6067199 23.0 GiB 8300 super8 6067200 6591487 2.0 GiB 8300 kdump9 6591488 6607871 64.0 MiB 8300 persist10 6607872 14996479 32.0 GiB 8300 userdata11 14996480 16777210 6.8 GiB 8300
sgdisk --new 11:0:0 /dev/block/sda
11代表分区序号,即sda11,中间的0代表起始簇为第一个可用地址,最后的0代表结束簇为最后一个可用地址。两个都为0即把剩余空间全部划分给新分区。
修改分区名字:
console:/ # sgdisk --change-name 11:"test" /dev/block/sda
Setting name!
partNum is 10
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.
console:/ # sgdisk --print /dev/block/sda
Disk /dev/block/sda: 16777216 sectors, 64.0 GiB
Sector size (logical/physical): 4096/4096 bytes
Disk identifier (GUID): 54054CFA-8B64-425D-A63F-B6B6F795C66E
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 5
First usable sector is 6, last usable sector is 16777210
Partitions will be aligned on 256-sector boundaries
Total free space is 250 sectors (1000.0 KiB)Number Start (sector) End (sector) Size Code Name1 256 511 1024.0 KiB 8300 misc2 512 16895 64.0 MiB 8300 boot_a3 16896 33279 64.0 MiB 8300 boot_b4 33280 33535 1024.0 KiB 8300 dtb_a5 33536 33791 1024.0 KiB 8300 dtb_b6 33792 37887 16.0 MiB 8300 metadata7 37888 6067199 23.0 GiB 8300 super8 6067200 6591487 2.0 GiB 8300 kdump9 6591488 6607871 64.0 MiB 8300 persist10 6607872 14996479 32.0 GiB 8300 userdata11 14996480 16777210 6.8 GiB 8300 test
注意:重启生效
(4)新建一个指定大小的分区
新建一个2G大小分区
console:/ # sgdisk --new 11:14996480:+2G /dev/block/sda
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.
console:/ # sgdisk --print /dev/block/sda
Disk /dev/block/sda: 16777216 sectors, 64.0 GiB
Sector size (logical/physical): 4096/4096 bytes
Disk identifier (GUID): 54054CFA-8B64-425D-A63F-B6B6F795C66E
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 5
First usable sector is 6, last usable sector is 16777210
Partitions will be aligned on 256-sector boundaries
Total free space is 1256693 sectors (4.8 GiB)Number Start (sector) End (sector) Size Code Name1 256 511 1024.0 KiB 8300 misc2 512 16895 64.0 MiB 8300 boot_a3 16896 33279 64.0 MiB 8300 boot_b4 33280 33535 1024.0 KiB 8300 dtb_a5 33536 33791 1024.0 KiB 8300 dtb_b6 33792 37887 16.0 MiB 8300 metadata7 37888 6067199 23.0 GiB 8300 super8 6067200 6591487 2.0 GiB 8300 kdump9 6591488 6607871 64.0 MiB 8300 persist10 6607872 14996479 32.0 GiB 8300 userdata11 14996480 15520767 2.0 GiB 8300
(5) 查看分区详情
console:/ # sgdisk --info 11 /dev/block/sda
Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem)
Partition unique GUID: AEA8DD0F-C48F-4177-BD08-C29F28910AD8
First sector: 14996480 (at 57.2 GiB)
Last sector: 15520767 (at 59.2 GiB)
Partition size: 524288 sectors (2.0 GiB)
Attribute flags: 0000000000000000
Partition name: ''
(6)删除一个分区
console:/ # sgdisk --delete 11 /dev/block/sda
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.
11代表/dev/block/sda11这个分区,需要重启才能生效
四、结语
以上就是常用的分区操作,还有一些其他的操作可以根据help信息自行尝试。
说明:Android自带的sgdisk help信息不友好,看了后不清楚参数应该如何添加。Android的sgdisk是从linux移植而来,使用方法和Ubuntu上的sgdisk基本一样,可以参考Ubuntu的help信息。
也可以参考https://www.cnblogs.com/mountain2011/p/9622001.html
Ubuntu sgdisk使用帮助
# sgdisk --help
Usage: sgdisk [OPTION...] <device>-A, --attributes=list|[partnum:show|or|nand|xor|=|set|clear|toggle|get[:bitnum|hexbitmask]] operate on partition attributes-a, --set-alignment=value set sector alignment-b, --backup=file backup GPT to file-c, --change-name=partnum:name change partition's name-C, --recompute-chs recompute CHS values in protective/hybrid MBR-d, --delete=partnum delete a partition-D, --display-alignment show number of sectors per allocation block-e, --move-second-header move second header to end of disk-E, --end-of-largest show end of largest free block-f, --first-in-largest show start of the largest free block-F, --first-aligned-in-largest show start of the largest free block, aligned-g, --mbrtogpt convert MBR to GPT-G, --randomize-guids randomize disk and partition GUIDs-h, --hybrid=partnum[:partnum...][:EE] create hybrid MBR-i, --info=partnum show detailed information on partition-j, --move-main-table=sector adjust the location of the main partition table-l, --load-backup=file load GPT backup from file-L, --list-types list known partition types-m, --gpttombr=partnum[:partnum...] convert GPT to MBR-n, --new=partnum:start:end create new partition-N, --largest-new=partnum create largest possible new partition-o, --clear clear partition table-O, --print-mbr print MBR partition table-p, --print print partition table-P, --pretend make changes in memory, but don't write them-r, --transpose=partnum:partnum transpose two partitions-R, --replicate=device_filename replicate partition table-s, --sort sort partition table entries-S, --resize-table=numparts resize partition table-t, --typecode=partnum:{hexcode|GUID} change partition type code-T, --transform-bsd=partnum transform BSD disklabel partition to GPT-u, --partition-guid=partnum:guid set partition GUID-U, --disk-guid=guid set disk GUID-v, --verify check partition table integrity-V, --version display version information-z, --zap zap (destroy) GPT (but not MBR) data structures-Z, --zap-all zap (destroy) GPT and MBR data structuresHelp options:-?, --help Show this help message--usage Display brief usage message