OSDN Git Service

b62dabbed2611cb8b44f6c5bf35aef1659eec2fe
[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 urxvt; then
48             urxvt -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                 echo "> alternative options
84 select another partition
85 cancel partition selection" | imenu -l " " >/tmp/homecancel
86                 if grep 'cancel' /tmp/homecancel; then
87                     touch /tmp/loopaskdisk
88                     rm /tmp/homecancel
89                     iroot r manualpartitioning
90                     exit
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         choosepart "choose swap partition> " >/root/instantARCH/config/partswap
133         ;;
134
135     esac
136 }
137
138 # choose root partition for programs etc
139 chooseroot() {
140     while [ -z "$ROOTCONFIRM" ]; do
141         PARTROOT="$(choosepart 'choose root partition (required) ')"
142         if imenu -c "This will erase all data on that partition. Continue?"; then
143             ROOTCONFIRM="true"
144             echo "instantOS will be installed on $PARTROOT"
145         fi
146         echo "$PARTROOT" | iroot i partroot
147     done
148 }
149
150 # choose wether to install grub and where to install it
151 choosegrub() {
152
153     while [ -z "$BOOTLOADERCONFIRM" ]; do
154         if ! imenu -c "install bootloader (grub) ? (recommended)"; then
155             if imenu -c "are you sure? This could make the system unbootable. "; then
156                 iroot nobootloader 1
157                 return
158             fi
159         else
160             BOOTLOADERCONFIRM="true"
161         fi
162     done
163
164     if efibootmgr; then
165
166         while [ -z "$EFICONFIRM" ]; do
167             choosepart 'select efi partition' | iroot i partefi
168             if imenu -c "this will erase all data on $(cat /root/instantARCH/config/partefi)"; then
169                 EFICONFIRM="true"
170             else
171                 rm /root/instantARCH/config/partefi
172             fi
173         done
174
175     else
176         GRUBDISK=$(fdisk -l | grep -i '^Disk /.*:' | imenu -l "select disk for grub > " | grep -o '/dev/[^:]*')
177         echo "$GRUBDISK"
178         iroot grubdisk "$GRUBDISK"
179     fi
180 }
181
182 startchoice
183 iroot manualpartitioning 1