OSDN Git Service

fix flatpak opener
[instantos/instantARCH.git] / askdisk.sh
1 #!/bin/bash
2
3 #########################################################
4 ## Allow manual partitioning when installing instantOS ##
5 ## Supports editing partitions and using existing ones ##
6 #########################################################
7
8 # todo: warning and confirmation messages
9
10 source /root/instantARCH/askutils.sh
11
12 # first displayed menu
13 startchoice() {
14     STARTCHOICE="$(echo 'edit partitions
15 choose partitions' | imenu -l)"
16
17     case "$STARTCHOICE" in
18     edit*)
19         editparts
20         ;;
21     choose*)
22         chooseparts
23         ;;
24     esac
25 }
26
27 # cfdisk wrapper to modify partition table during installation
28 editparts() {
29     echo 'instantOS requires the following paritions: 
30  - a root partition, all data on it will be erased
31  - an optional home partition.
32     If not specified, the same partition as root will be used. 
33     Gives you the option to keep existing data on the partition
34  - an optional swap partition. 
35     If not specified a swap file will be used. 
36 The Bootloader requires
37
38  - an EFI partition on uefi systems
39  - a disk to install it to on legacy-bios systems
40 ' | imenu -M
41
42     EDITDISK="$(fdisk -l | grep -i '^Disk /.*:' | imenu -l 'choose disk to edit> ' | grep -o '/dev/[^:]*')"
43     echo "editing disk $EDITDISK"
44     if guimode; then
45         if command -v st; then
46             st -e bash -c "cfdisk $EDITDISK"
47         elif command -v st; then
48             st -e bash -c "cfdisk $EDITDISK"
49         else
50             xterm -e bash -c "cfdisk $EDITDISK"
51         fi
52     else
53         cfdisk "$EDITDISK"
54     fi
55
56     iroot disk "$EDITDISK"
57     startchoice
58 }
59
60 # choose all partitions
61 chooseparts() {
62     chooseroot
63     choosehome
64     chooseswap
65     choosegrub
66 }
67
68 # menu that allows choosing a partition and put it in stdout
69 choosepart() {
70     unset RETURNPART
71     while [ -z "$RETURNPART" ]; do
72         fdisk -l | grep '^/dev' | sed 's/\*/ b /g' | imenu -l "$1" | grep -o '^[^ ]*' >/tmp/diskchoice
73         RETURNPART="$(cat /tmp/diskchoice)"
74         if ! [ -e "$RETURNPART" ]; then
75             imenu -m "$RETURNPART does not exist" &>/dev/null
76             unset RETURNPART
77         fi
78
79         for i in /root/instantARCH/config/part*; do
80             if grep "^$RETURNPART$" "$i"; then
81                 echo "partition $RETURNPART already taken"
82                 imenu -m "partition $RETURNPART is already selected for $i"
83                 CANCELOPTION="$(echo '> alternative options
84 select another partition
85 cancel partition selection' | imenu -l ' ')"
86                 if grep -q 'cancel' <<<"$CANCELOPTION"; then
87                     touch /tmp/loopaskdisk
88                     rm /tmp/homecancel
89                     iroot r manualpartitioning
90                     exit 1
91                 fi
92                 unset RETURNPART
93             fi
94         done
95
96     done
97     echo "$RETURNPART"
98 }
99
100 # choose home partition, allow using existing content or reformatting
101 choosehome() {
102     if ! imenu -c "do you want to use a seperate home partition?"; then
103         return
104     fi
105
106     HOMEPART="$(choosepart 'choose home partition >')"
107     case "$(echo 'keep current home data
108 erase partition to start fresh' | imenu -l)" in
109     keep*)
110         echo "keeping"
111         ;;
112     erase*)
113         echo "erasing"
114         iroot erasehome 1
115         ;;
116     esac
117     iroot parthome "$HOMEPART"
118     echo "$HOMEPART" >/root/instantARCH/config/parthome
119
120 }
121
122 # choose swap partition or swap file
123 chooseswap() {
124     case "$(echo 'use a swap file
125 use a swap partition' | imenu -l)" in
126
127     *file)
128         echo "using a swap file"
129         ;;
130     *partition)
131         echo "using a swap partition"
132         while [ -z "$SWAPCONFIRM" ]; do
133             PARTSWAP="$(choosepart 'choose swap partition> ')"
134             if imenu -c "This will erase all data on that partition. It should also be on a fast drive. Continue?"; then
135                 SWAPCONFIRM="true"
136                 echo "$PARTSWAP will be used as swap"
137                 echo "$PARTSWAP" | iroot i partswap
138             fi
139         done
140         ;;
141     esac
142
143 }
144
145 # choose root partition for programs etc
146 chooseroot() {
147     while [ -z "$ROOTCONFIRM" ]; do
148         PARTROOT="$(choosepart 'choose root partition (required) ')"
149         if imenu -c "This will erase all data on that partition. Continue?"; then
150             ROOTCONFIRM="true"
151             echo "instantOS will be installed on $PARTROOT"
152         fi
153         echo "$PARTROOT" | iroot i partroot
154     done
155 }
156
157 # choose wether to install grub and where to install it
158 choosegrub() {
159
160     while [ -z "$BOOTLOADERCONFIRM" ]; do
161         if ! imenu -c "install bootloader (grub) ? (recommended)"; then
162             if imenu -c "are you sure? This could make the system unbootable. "; then
163                 iroot nobootloader 1
164                 return
165             fi
166         else
167             BOOTLOADERCONFIRM="true"
168         fi
169     done
170
171     if efibootmgr; then
172
173         while [ -z "$EFICONFIRM" ]; do
174             choosepart 'select efi partition' | iroot i partefi
175             if imenu -c "this will erase all data on $(iroot partefi). Existing operating systems will be detected and added back into the bootloader"; then
176                 EFICONFIRM="true"
177             else
178                 rm /root/instantARCH/config/partefi
179             fi
180         done
181
182     else
183         GRUBDISK=$(fdisk -l | grep -i '^Disk /.*:' | imenu -l "select disk for grub " | grep -o '/dev/[^:]*')
184         echo "$GRUBDISK"
185         iroot grubdisk "$GRUBDISK"
186     fi
187 }
188
189 startchoice
190 iroot manualpartitioning 1