You are not logged in.

#1 2021-03-14 20:21:28

Itzak
Member
Registered: 2021-02-03
Posts: 17

[SOLVED] Script to partition disk

I'm currently trying to make a script for personal use to partition my 500GB nvme ssd as follows using fdisk:

/dev/nvme0n1p1   2048    206847    204800   100M EFI System
/dev/nvme0n1p2 206848 976773134 976566287 465.7G Linux filesystem

I keep the swap partition in another hdd with Windows 10 on it:

/dev/sda4  1108310016 1125087231   16777216     8G Linux swap

However, I'm not sure how I can automate the simulation of option entry that fdisk requires and the assignment of disk space.

I'm new to bash scripting so any help would be appreciated. I've looked online but don't find a simple solution to this.

Last edited by Itzak (2021-03-15 22:05:53)

Offline

#2 2021-03-14 21:09:17

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: [SOLVED] Script to partition disk

I'd suggest not using fdisk for this purpose as it's designed for human interaction. Use the scripting functionality of sfdisk instead.


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#3 2021-03-15 18:01:58

Itzak
Member
Registered: 2021-02-03
Posts: 17

Re: [SOLVED] Script to partition disk

Thanks for the reply. I've looked up sfdisk but wasn't able to get much information on how to use it in a script. Can you point me on the right direction to look?

Offline

#4 2021-03-15 18:06:43

hcjl
Member
From: berlin
Registered: 2007-06-29
Posts: 330

Re: [SOLVED] Script to partition disk

man sfdisk

contains most of its secrets.

Last edited by hcjl (2021-03-15 18:07:02)

Offline

#5 2021-03-15 18:29:54

Head_on_a_Stick
Member
From: London
Registered: 2014-02-20
Posts: 7,680
Website

Re: [SOLVED] Script to partition disk

sgdisk ftw:

# sgdisk -n 1:2048:206847 -t 1:ef00 -n 2:206848:976773134 -t 2:8300 /dev/nvme0n1
# sgdisk -n 4:1108310016:1125087231 -t 4:8200 /dev/sda

EDIT: mixed up swap & Linux partition codes. Oops.

Last edited by Head_on_a_Stick (2021-03-15 18:31:35)

Offline

#6 2021-03-15 20:43:55

Itzak
Member
Registered: 2021-02-03
Posts: 17

Re: [SOLVED] Script to partition disk

I think sgdisk will do the trick but can you tell me if this tool is available in the arch ISO since I want to use the script to reinstall my system.

If I understood correctly the man page, I don't need to specify the partition table type since it automatically makes it GPT, right? So, with the commands you provided me would be more than enough to partition the disk.

Also, can you explain me what's the difference between all these tools. I know now that sfdisk is the script version of fdisk, but what about sgdisk, cfdisk, cgdisk, gdisk or parted. Is one better than the others?

Last edited by Itzak (2021-03-15 21:05:19)

Offline

#7 2021-03-15 21:05:39

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: [SOLVED] Script to partition disk

They are all closely related, I will try and explain as best as I understand...

fdisk - originally created to work with only msdos partition tables.
gdisk - originally created to work with only gpt partition tables.

However, they have both been updated over the years to work with both msdos and GPT tables so there is now no real difference except the front-end.

c{f,g}disk - versions of the above that use ncurses as the interface instead of just std{in,out}.

s{f,g}disk - versions of the above that are designed to be scriptable.

parted - a utility from different authors that has the same basic functionality.

This is just my understanding, it might all be incorrect smile
Waiting for another poster to point out my mistakes...


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#8 2021-03-15 21:10:36

Itzak
Member
Registered: 2021-02-03
Posts: 17

Re: [SOLVED] Script to partition disk

That was actually a very clear explanation, thank you. Do you know if the sgdisk utility is available in the default Arch ISO, I want to use the function as part of a script to reinstall my system.

When using sgdisk I don't need to specify the partition table type since it automatically makes it GPT, right?

Offline

#9 2021-03-15 21:17:11

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: [SOLVED] Script to partition disk

sgdisk is part of the gdisk package which is included on the ISO.

Itzak wrote:

When using sgdisk I don't need to specify the partition table type since it automatically makes it GPT, right?

No idea. Read the man page.


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#10 2021-03-15 22:04:30

Itzak
Member
Registered: 2021-02-03
Posts: 17

Re: [SOLVED] Script to partition disk

I'll read it. Thanks for the help!

Offline

Board footer

Powered by FluxBB