OSDN Git Service

Initial commit
[rebornos/cnchi-gnome-mac-osdn.git] / airootfs / usr / bin / auto-partition.sh
1 #!/bin/bash
2 # REQUIREMENTS: Must have only 1 free, unformatted partition on /dev/sda, that also happens to be at least 30 GB in size.
3 START()
4 {
5 if [[ $(parted /dev/sda unit B print free | grep 'Free Space' | awk '{print $4}' | wc -c) -le 14 ]]; then
6         echo "ERROR - no unformatted partitions to use"
7         echo "[ERROR] No unformatted partitions to use" >>/tmp/cnchi.log
8
9 elif [[ $(parted /dev/sda unit B print free | grep 'Free Space' | awk '{print $4}' | wc -c) -ge 16 ]]; then
10         echo "ERROR - too many unformatted partition to use (must be only 1)"
11         echo "[ERROR] Too many unformatted partitions to use (must only be 1)" >>/tmp/cnchi.log
12
13 else
14         echo "[SUCCESS]  An unformatted partition exists. Continuing to auto-partition it for you..." >>/tmp/cnchi.log
15         echo $(parted /dev/sda unit B print free | grep 'Free Space' | awk '{print $3}' | tr -d B) | awk  '{print $2}' >/partition.txt
16        echo $(sed '2q;d' /partition.txt)
17 export SIZE=$(sed '2q;d' /partition.txt)
18 TEST
19 fi
20
21 if [[ $(parted /dev/sda unit B print free | grep 'Free Space' | awk '{print $4}' | wc -c) -le 4 ]]; then
22         echo "Confirmed - no unformatted partitions to use"
23         echo "[ERROR] No unformatted partitions to use" >>/tmp/cnchi.log
24
25 elif [[ $(parted /dev/sda unit B print free | grep 'Free Space' | awk '{print $4}' | wc -c) -ge 6 ]]; then
26         echo "Confirmed - too many unformatted partition to use (must be only 1)"
27         echo "[ERROR] Too many unformatted partitions to use (must only be 1)" >>/tmp/cnchi.log
28
29 else
30         echo "SUCCESS - never mind! Found an unformatted partition to use."
31         echo "[SUCCESS]  An unformatted partition exists. Continuing to auto-partition it for you..." >>/tmp/cnchi.log
32         echo $(parted /dev/sda unit B print free | grep 'Free Space' | awk '{print $3}' | tr -d B) | awk  '{print $2}' >/partition.txt
33        echo $(sed '1q;d' /partition.txt)
34 export SIZE=$(sed '1q;d' /partition.txt)
35 TEST
36 fi
37 }
38
39 TEST(){
40 if [[ $(parted /dev/sda unit B print free | grep 'Free Space' | awk '{print $4}' | wc -c) -eq 15 ]]; then
41 export SIZE=$(sed '2q;d' /partition.txt)
42 fi 
43 if [[ $(parted /dev/sda unit B print free | grep 'Free Space' | awk '{print $4}' | wc -c) -eq 5 ]]; then
44 export SIZE=$(sed '1q;d' /partition.txt)
45 fi
46 if [ "$SIZE" -ge 30000000000 ]; then
47     echo "[SUCCESS] Partition is large enough to continue" >>/tmp/cnchi.log 
48     FIRMWARE
49 else
50     echo "[ERROR] Partition is too small to continue" >>/tmp/cnchi.log
51 fi
52 }
53
54 FIRMWARE(){
55 if [ -f "/sys/firmware/efi" ]; then
56 echo "[INFO] UEFI system detected" >>/tmp/cnchi.log
57 SIZES_UEFI
58 else
59 echo "[INFO] BIOS system detected" >>/tmp/cnchi.log
60 SIZES_BIOS
61 fi
62 }
63
64 SIZES_UEFI(){
65 export SIZE=$(sed '1q;d' /partition.txt)
66 # Root = 36%
67 echo $(awk -vn=$SIZE 'BEGIN{print(n*0.35)}') >>/tmp/rooting.txt
68 export ROOT=$(sed '1q;d' /tmp/rooting.txt) 
69 echo "[INFO] Root partition will be $ROOT bytes large" >>/tmp/cnchi.log
70 # Home = 64%
71 echo $(awk -vn=$SIZE 'BEGIN{print(n*0.63)}') >>/tmp/homing.txt
72 export HOME=$(sed '1q;d' /tmp/homing.txt) 
73 echo "[INFO] Home partition will be $HOME bytes large" >>/tmp/cnchi.log
74 # Swap = 1%
75 echo $(awk -vn=$SIZE 'BEGIN{print(n*0.01)}') >>/tmp/swapping.txt
76 export SWAP=$(sed '1q;d' /tmp/swapping.txt) 
77 echo "[INFO] Swap partition will be $SWAP bytes large" >>/tmp/cnchi.log
78 #export SWAP=512M
79 #echo "[INFO] SWAP file $SWAP large will be created" >>/tmp/cnchi.log
80 # Boot partition
81 echo $(df -h --output=source,fstype,size,used,avail,pcent,target -x tmpfs -x devtmpfs | grep "/dev/sd" | grep "/boot/efi") >/partitioning.txt
82 export BOOT=echo $(sed '1q;d' /partitioning.txt) | awk '{print $1}'
83 echo "[INFO] Boot partition to be used is $BOOT" >>/tmp/cnchi.log
84 PARTITION_ROOT
85 }
86
87 SIZES_BIOS(){
88 export SIZE=$(sed '1q;d' /partition.txt)
89 # Root = 36%
90 echo $(awk -vn=$SIZE 'BEGIN{print(n*0.35)}') >>/tmp/rooting.txt
91 export ROOT=$(sed '1q;d' /tmp/rooting.txt) 
92 echo "[INFO] Root partition will be $ROOT bytes large" >>/tmp/cnchi.log
93 # Home = 63%
94 echo $(awk -vn=$SIZE 'BEGIN{print(n*0.63)}') >>/tmp/homing.txt
95 export HOME=$(sed '1q;d' /tmp/homing.txt) 
96 echo "[INFO] Home partition will be $HOME bytes large" >>/tmp/cnchi.log
97 # Swap = 1%
98 echo $(awk -vn=$SIZE 'BEGIN{print(n*0.01)}') >>/tmp/swapping.txt
99 export SWAP=$(sed '1q;d' /tmp/swapping.txt) 
100 echo "[INFO] Swap partition will be $SWAP bytes large" >>/tmp/cnchi.log
101 # Boot = 1%
102 echo $(awk -vn=$SIZE 'BEGIN{print(n*0.01)}') >>/tmp/booting.txt
103 export SWAP=$(sed '1q;d' /tmp/booting.txt) 
104 echo "[INFO] Boot partition will be $BOOT bytes large" >>/tmp/cnchi.log
105 #export SWAP=512M
106 #echo "[INFO] SWAP file $SWAP large will be created" >>/tmp/cnchi.log
107 PARTITION_ROOT
108 }
109
110 PARTITION_ROOT(){
111 export ROOT=$(sed '1q;d' /tmp/rooting.txt) 
112 echo "[INFO] Root partition is being created..." >>/tmp/cnchi.log
113 export ROOT_START=echo $(parted /dev/sda unit B print free | grep 'Free Space' | tr -d B) | awk '{print $6}' 
114 export ROOT_END=echo $(parted /dev/sda unit B print free | grep 'Free Space' | tr -d B) | awk '{print $7}'
115 export ROOT_SIZE=$((ROOT_START+ROOT))
116 unit B mkpartfs primary ext4 $ROOT_START $ROOT_SIZE
117 # Determine what partition was just created
118 echo $(fdisk -l --bytes | grep $ROOT_SIZE) | awk '{print $1}' >/root.txt
119 export ROOT_PARTITION=$(sed '1q;d' /root.txt)
120 e2label $ROOT_PARTITION "ROOT"
121 echo "$ROOT_PARTITION / ext4 defaults,relatime,data=ordered 0 1" >>/etc/fstab
122 echo "[SUCCESS] Root partition has been created as $ROOT_PARTITION" >>/tmp/cnchi.log
123 PARTITION_HOME
124 }
125
126 PARTITION_HOME(){
127 export HOME=$(sed '1q;d' /tmp/homing.txt) 
128 echo "[INFO] Home partition is being created..." >>/tmp/cnchi.log
129 export HOME_START=echo $(parted /dev/sda unit B print free | grep 'Free Space' | tr -d B) | awk '{print $6}' 
130 export HOME_END=echo $(parted /dev/sda unit B print free | grep 'Free Space' | tr -d B) | awk '{print $7}'
131 export HOME_SIZE=$((HOME_START+HOME))
132 unit B mkpartfs primary ext4 $HOME_START $HOME_SIZE
133 # Determine what partition was just created
134 echo $(fdisk -l --bytes | grep $HOME_SIZE) | awk '{print $1}' >/home.txt
135 export HOME_PARTITION=$(sed '1q;d' /home.txt)
136 e2label $HOME_PARTITION "HOME"
137 echo "$HOME_PARTITION /home ext4 defaults,relatime,data=ordered 0 0" >>/etc/fstab
138 echo "[SUCCESS] Home partition has been created as $HOME_PARTITION" >>/tmp/cnchi.log
139 PARTITION_SWAP
140 }
141
142 PARTITION_SWAP(){
143 export SWAP=$(sed '1q;d' /tmp/swapping.txt) 
144 echo "[INFO] Swap partition is being created..." >>/tmp/cnchi.log
145 export SWAP_START=echo $(parted /dev/sda unit B print free | grep 'Free Space' | tr -d B) | awk '{print $6}' 
146 export SWAP_END=echo $(parted /dev/sda unit B print free | grep 'Free Space' | tr -d B) | awk '{print $7}'
147 export SWAP_SIZE=$((SWAP_START+SWAP))
148 unit B mkpartfs primary swap $SWAP_START $SWAP_SIZE
149 # Determine what partition was just created
150 echo $(fdisk -l --bytes | grep $SWAP_SIZE) | awk '{print $1}' >/swap.txt
151 export SWAP_PARTITION=$(sed '1q;d' /swap.txt)
152 e2label $SWAP_PARTITION "swap"
153 echo "$SWAP_PARTITION swap swap defaults 0 0" >>/etc/fstab
154 echo "[SUCCESS] Swap partition has been created as $HOME_PARTITION" >>/tmp/cnchi.log
155 swapon $SWAP_PARTITION
156 PARTITION_UEFI_BIOS
157 }
158
159 PARTITION_UEFI_BIOS(){
160 if [ -f "/sys/firmware/efi" ]; then
161 echo $(fdisk -l --bytes | grep EFI) | awk '{print $1}' >/esp.txt
162 export ESP_PARTITION=$(sed '1q;d' /esp.txt)
163 echo "$ESP_PARTITION /boot/efi vfat defaults,relatime 0 0" >>/etc/fstab
164 echo "[SUCCESS] Boot (EFI) partition has been created as $ESP_PARTITION" >>/tmp/cnchi.log
165 else
166 echo "[INFO] Boot partition is being created..." >>/tmp/cnchi.log
167 export BOOT=$(sed '1q;d' /tmp/booting.txt) 
168 export BOOT_START=echo $(parted /dev/sda unit B print free | grep 'Free Space' | tr -d B) | awk '{print $6}' 
169 export BOOT_END=echo $(parted /dev/sda unit B print free | grep 'Free Space' | tr -d B) | awk '{print $7}'
170 export BOOT_SIZE=$((HOME_START+HOME))
171 unit B mkpartfs primary ext4 $BOOT_START $BOOT_SIZE
172 # Determine what partition was just created
173 echo $(fdisk -l --bytes | grep $BOOT_SIZE) | awk '{print $1}' >/boot.txt
174 export HOME_PARTITION=$(sed '1q;d' /boot.txt)
175 e2label $BOOT_PARTITION "BOOT"
176 echo "$BOOT_PARTITION /boot ext4 defaults,relatime,data=ordered 0 0" >>/etc/fstab
177 echo "[SUCCESS] Boot partition has been created as $BOOT_PARTITION" >>/tmp/cnchi.log
178 fi
179 CLEAN
180 }
181
182 CLEAN(){
183 rm -f /partition.txt
184 rm -f /partitioning.txt
185 rm -f /root.txt
186 rm -f /home.txt
187 rm -f /swap.txt
188 rm -f /tmp/rooting.txt
189 rm -f /tmp/homing.txt
190 rm -f /tmp/swapping.txt
191 if [ -f "/sys/firmware/efi" ]; then
192 rm -f /esp.txt
193 else
194 rm -f /boot.txt
195 rm -f /tmp/booting.txt
196 fi
197 }
198 export -f START TEST FIRMWARE SIZES_BIOS SIZES_UEFI PARTITION_ROOT PARTITION_HOME PARTITION_SWAP PARTITION_UEFI_BIOS CLEAN
199 START