OSDN Git Service

dc4e924c8dc4ff7fbc77041778b112f076e353c7
[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
16 continue installation' | imenu -l)"
17
18     case "$STARTCHOICE" in
19
20     edit*)
21         editparts
22         ;;
23     choose*)
24         chooseparts
25         ;;
26     continue*)
27         exit
28         ;;
29     esac
30 }
31
32 # cfdisk wrapper to modify partition table during installation
33 editparts() {
34     echo 'instantOS requires the following paritions: 
35  - a root partition, all data on it will be erased
36  - an optional home partition.
37     If not specified, the same partition as root will be used. 
38     Gives you the option to keep existing data on the partition
39  - an optional swap partition. 
40     If not specified a swap file will be used. 
41 The Bootloader requires
42
43  - an EFI partition on uefi systems
44  - a disk to install it to on legacy-bios systems
45 ' | imenu -M
46
47     EDITDISK="$(fdisk -l | grep -i '^Disk /.*:' | imenu -l 'choose disk to edit> ' | grep -o '/dev/[^:]*')"
48     echo "editing disk $EDITDISK"
49     sleep 2
50     if guimode; then
51         if command -v st; then
52             st -e bash -c "cfdisk $EDITDISK"
53         elif command -v urxvt; then
54             urxvt -e bash -c "cfdisk $EDITDISK"
55         else
56             xterm -e bash -c "cfdisk $EDITDISK"
57         fi
58     else
59         cfdisk "$EDITDISK"
60     fi
61
62     iroot disk "$EDITDISK"
63     startchoice
64 }
65
66 # choose all partitions
67 chooseparts() {
68     choosehome
69     chooseroot
70     chooseswap
71     choosegrub
72 }
73
74 # menu that allows choosing a partition and put it in stdout
75 choosepart() {
76     unset RETURNPART
77     while [ -z "$RETURNPART" ]; do
78         fdisk -l | grep '^/dev' | sed 's/\*/ * /g' | imenu -l "$1" | grep -o '^[^ ]*' >/tmp/diskchoice
79         RETURNPART="$(cat /tmp/diskchoice)"
80         if ! [ -e "$RETURNPART" ]; then
81             imenu -m "$RETURNPART does not exist" &>/dev/null
82             unset RETURNPART
83         fi
84     done
85     echo "$RETURNPART"
86 }
87
88 # choose home partition, allow using existing content or reformatting
89 choosehome() {
90     if ! imenu -c "do you want to use a seperate home partition?"; then
91         return
92     fi
93
94     HOMEPART="$(choosepart 'choose home partition >')"
95     case "$(echo 'keep current home data
96 erase partition to start fresh' | imenu -l)" in
97     keep*)
98         echo "keeping"
99         ;;
100     erase*)
101         echo "erasing"
102         iroot erasehome 1
103         ;;
104     esac
105     iroot parthome "$HOMEPART"
106     echo "$HOMEPART" >/root/instantARCH/config/parthome
107
108 }
109
110 # choose swap partition or swap file
111 chooseswap() {
112     case "$(echo 'use a swap file
113 use a swap partition' | imenu -l)" in
114
115     *file)
116         echo "using a swap file"
117         ;;
118     *partition)
119         echo "using a swap partition"
120         choosepart "choose swap partition> " >/root/instantARCH/config/partswap
121         ;;
122
123     esac
124 }
125
126 # choose root partition for programs etc
127 chooseroot() {
128     while [ -z "$ROOTCONFIRM" ]; do
129         PARTROOT="$(choosepart 'choose root partition')"
130         if imenu -c "This will erase all data on that partition. Continue?"; then
131             ROOTCONFIRM="true"
132             echo "instantOS will be installed on $PARTROOT"
133             sleep 1
134         fi
135         echo "$PARTROOT" >/root/instantARCH/config/partroot
136     done
137 }
138
139 # choose wether to install grub and where to install it
140 choosegrub() {
141
142     while [ -z $BOOTLOADERCONFIRM ]; do
143         if ! confirm -c "install bootloader (grub) ?"; then
144             if confirm -c "are you sure? This could make the system unbootable. "; then
145                 touch /root/instantARCH/config/nobootloader
146                 return
147             fi
148         else
149             BOOTLOADERCONFIRM="true"
150         fi
151     done
152
153     if efibootmgr; then
154
155         while [ -z "$EFICONFIRM" ]; do
156             choosepart 'select efi partition' >/root/instantARCH/config/partefi
157             if imenu -c "this will erase all data on $(cat /root/instantARCH/config/partefi)"; then
158                 EFICONFIRM="true"
159             else
160                 rm /root/instantARCH/config/partefi
161             fi
162         done
163
164     else
165         GRUBDISK=$(fdisk -l | grep -i '^Disk /.*:' | imenu -l "select disk for grub > " | grep -o '/dev/[^:]*')
166         echo "$GRUBDISK"
167     fi
168 }
169
170 startchoice