{"id":13339108,"url":"https://github.com/hekmon/DiskTools","last_synced_at":"2025-03-11T12:31:16.771Z","repository":{"id":99435637,"uuid":"58070248","full_name":"hekmon/DiskTools","owner":"hekmon","description":"A set of sysadmin tools to better handle disk management","archived":false,"fork":false,"pushed_at":"2020-02-24T18:01:26.000Z","size":9,"stargazers_count":3,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-28T22:52:09.867Z","etag":null,"topics":["bash","boot","disk","gpt","msdos","ops","partitions-aligned","uefi"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hekmon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-05-04T17:19:06.000Z","updated_at":"2023-06-05T08:16:13.000Z","dependencies_parsed_at":null,"dependency_job_id":"93827f8c-424c-43a3-95be-3a5218eb0077","html_url":"https://github.com/hekmon/DiskTools","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hekmon%2FDiskTools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hekmon%2FDiskTools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hekmon%2FDiskTools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hekmon%2FDiskTools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hekmon","download_url":"https://codeload.github.com/hekmon/DiskTools/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243034834,"owners_count":20225413,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","boot","disk","gpt","msdos","ops","partitions-aligned","uefi"],"created_at":"2024-07-29T19:19:01.312Z","updated_at":"2025-03-11T12:31:16.724Z","avatar_url":"https://github.com/hekmon.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DiskTools\n\nDiskTools is a small collection of bash scripts I wrote for tasks I was tired of doing manually.\n\nIt is for now composed of :\n* [GPTinitialize](https://github.com/Hekmon/DiskTools#gptinitialize)\n* [optimalAlignment](https://github.com/Hekmon/DiskTools#optimalalignment)\n\n\n## GPTinitialize\n\nGPTinitialize prepare a disk with a GPT partitonning schema for a dual compatibility between BIOS and UEFI. Once initialized, the disk can be used in both boot mode as long as the 2 specials partitions are still on the disk.\n\nIt :\n* Initializes the disk as GPT (and yes that means __loss of data__)\n* Creates a special partition for BIOS boot compatibility (in GPT there is no more unused MBR free space and boot loaders need one)\n* Creates a UEFI partition with the right formating (FAT32) and flags (mainly boot) in order to be used in a UEFI boot mode\n\n### Requirements\n\nIn order to work properly, the following binaries should be available on your system/PATH :\n\n* `bash` obviously  :)\n* `parted` for printing partition tables, partitioning and tagging\n* `partprobe` to update udev in order to properly see the partitions as block devices files just after being created\n* `dd` to \"format\" the BIOS partition as it should be in RAW format (zeroed)\n* `mkfs.fat` to format the UEFI partition as it should be in FAT32\n\n\n### Example\n\nLet's initialize a disk seen as `/dev/sdb`. At the beginning it has a classical `msdos` partionning schema, and a single 500 GB size partition. I did backup the data I wanted to keep, and now I am ready to reinitialize it using `GPTinitialize.sh` :\n\n```\nDiskTools\u003e sudo ./GPTinitialize.sh /dev/sdb\n\n Table partition for : /dev/sdb\n\nModel: ASMT 2105 (scsi)\nDisk /dev/sdb: 500GB\nSector size (logical/physical): 512B/512B\nPartition Table: msdos\nDisk Flags: \n\nNumber  Start   End     Size    Type     File system  Flags\n        32,3kB  1049kB  1016kB           Free Space\n 1      1049kB  500GB   500GB   primary\n        500GB   500GB   24,6kB           Free Space\n\n\n\tYou are about to FORMAT and INITIALIZE as GPT the following block device :\n\t\t/dev/sdb\n\nPlease type 'yes' to proceed : yes\n\n\n * Initializing disk with GPT partitionning\n\n * Writing BIOS compatibility partition\n\tcreating partition\n\tsetting bios compatibility flag\n    formating partition                                               \n\n * Writing UEFI partition\n\tcreating partition\n\tsetting uefi partition flag\n\tformating partition\n\n * Verifying partitions alignement\n\tBIOS partition : OK\n\tUEFI partition : OK\n\n * Done !\n\n\n Final table partition for /dev/sdb :\n\nModel: ASMT 2105 (scsi)\nDisk /dev/sdb: 500GB\nSector size (logical/physical): 512B/512B\nPartition Table: gpt\nDisk Flags: \n\nNumber  Start   End     Size    File system  Name       Flags\n        17,4kB  2097kB  2080kB  Free Space\n 1      2097kB  4194kB  2097kB  ext4         bios_boot  bios_grub\n 2      4194kB  541MB   537MB   fat32        uefi_boot  boot, esp\n        541MB   500GB   500GB   Free Space\n\n\nDiskTools\u003e\n```\n\nAs you can seen in the end, disk is in `GPT` and has abilities to be used as a boot disk in both `msdos` or `GPT` mode. You are now free to add whatever partitions you need, but I recommand you check [optimalAlignment](https://github.com/Hekmon/DiskTools#optimalalignment) in order to do so ;)\n\n### Limitations\n\nTo keep the partitions aligned in most cases, I had to set some hard values in the script. It will work as long as your disk has `sector size \u003e= 512B`. If not, you will have a nice error message.\n\nBut using [optimalAlignment](https://github.com/Hekmon/DiskTools#optimalalignment) and with little calculations, you should be able to adapt it to your needs as you only need to change two variables : `bios_part_sectors_start` and `uefi_part_sectors_start`.\n\nHo, and if you are wondering how to check your disk sector size, just keep on reading.\n\n## optimalAlignment\n\noptimalAlignment helps you keep your partitions aligned depending on your disk sector size.\n\nIt gives you the sector multiple to use for every partition start. It can also compute the next sector you should use for a new partition by using the last sector used (check example 2).\n\n### Example 1\n\nIf you are just interested by your disk metadata and/or the the multiple you should use, just pass a block device as first parameter :\n\n```\nDiskTools\u003e ./optimalAlignment.sh /dev/sdb\n\n  Results for device :\t/dev/sdb\n    Optimal IO Size\t\t0\n    Minimum IO Size\t\t512\n    Alignment Offset\t0\n    Physical Block Size\t512\n\n\tYou should start your partition with a multiple of 2048 sectors\n\nDiskTools\u003e\n```\n\n### Example 2\n\nYou can also pass a second argument to optimalAlignment : the sector number of your last partition in order to compute the sector number to use for your next partition in order to make it aligned.\n\nLet's use the disk we initialized in the [GPTinitialize](https://github.com/Hekmon/DiskTools#gptinitialize) part :\n\n```\nDiskTools\u003e sudo parted /dev/sdb u s print free\nModel: ASMT 2105 (scsi)\nDisk /dev/sdb: 976773168s\nSector size (logical/physical): 512B/512B\nPartition Table: gpt\nDisk Flags: \n\nNumber  Start     End         Size        File system  Name       Flags\n        34s       4095s       4062s       Free Space\n 1      4096s     8191s       4096s       ext4         bios_boot  bios_grub\n 2      8192s     1056767s    1048576s    fat32        uefi_boot  boot, esp\n        1056768s  976773134s  975716367s  Free Space\n\nDiskTools\u003e\n```\n\n\nAs you can see, the last used sector is `1056767`. Let's pass it as a second parameter :\n\n```\nDiskTools\u003e ./optimalAlignment.sh /dev/sdb 1056767\n\n  Results for device :\t/dev/sdb\n    Optimal IO Size\t\t0\n    Minimum IO Size\t\t512\n    Alignment Offset\t0\n    Physical Block Size\t512\n\n\tYou should start your partition with a multiple of 2048 sectors\n\n\tStarting sector of your next partition should be 1056768 as the last sector used is 1056767\n\nDiskTools\u003e\n```\n\nThe script will compute the next sector safe to be used for a new aligned partition by taking into account your disk particularities : `1056768`.\n\nIn this case it is the sector just after the last one used, but this is because I made sure that [GPTinitialize](https://github.com/Hekmon/DiskTools#gptinitialize) creates very precise partitions bounderies and wastes no space (at least for 512/4096 scenarios) : it won't always be the case for other partitions so don't assume a simple `+1` for every cases !\n\n\n## License\n\nMIT licensed. See the LICENSE file for details.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhekmon%2FDiskTools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhekmon%2FDiskTools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhekmon%2FDiskTools/lists"}