-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcreate_fdisk_cmds.py
executable file
·81 lines (65 loc) · 2.08 KB
/
create_fdisk_cmds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python3
import subprocess
import sys
import math
def get_used_space():
result = subprocess.run(['df', '-k', '/'], capture_output=True, text=True)
for line in result.stdout.splitlines():
if '/dev/mmcblk0p2' in line or 'root' in line:
used_space_kb = int(line.split()[2])
return used_space_kb
raise Exception("Couldn't determine the used space on the root partition.")
def get_start_sector_partition2():
result = subprocess.run(['fdisk', '-l', '/dev/mmcblk0'], capture_output=True, text=True)
for line in result.stdout.splitlines():
if '/dev/mmcblk0p2' in line:
return int(line.split()[1])
raise Exception("Couldn't determine the start sector for partition 2.")
if len(sys.argv) != 3:
print("Usage: script_name <Extra GBs for rootfs (advise 2 or more)> <GBs for swap (advise 4)>")
sys.exit(1)
extra_gb_partition2 = int(sys.argv[1])
gb_partition3 = int(sys.argv[2])
# Get the starting sector for partition 2
start_block_partition2 = get_start_sector_partition2()
# Convert GB to KB
extra_kb_partition2 = extra_gb_partition2 * 1024 * 1024
kb_partition3 = gb_partition3 * 1024 * 1024
# Determine current used space and calculate total required KB for partition 2
used_space_kb = get_used_space()
total_required_kb_partition2 = used_space_kb + extra_kb_partition2
# Round up the total required KB of partition 2 to the nearest GB and convert to sectors
rounded_gb_partition2 = math.ceil(total_required_kb_partition2 / (1024 * 1024))
sectors_required_partition2 = rounded_gb_partition2 * 1024 * 1024 * 2
# Calculate sectors required for partition 3
sectors_required_partition3 = kb_partition3 * 2
# Calculate end block for partition 2
end_block_partition2 = start_block_partition2 + sectors_required_partition2 - 1
start_block_partition3 = end_block_partition2 + 1
end_block_partition3 = start_block_partition3 + sectors_required_partition3 - 1
print(f"""d
2
n
p
2
{start_block_partition2}
{end_block_partition2}
N
n
p
3
{start_block_partition3}
{end_block_partition3}
n
e
{end_block_partition3 + 1}
n
+512M
Y
n
Y
t
3
82
w
q""")