-
Notifications
You must be signed in to change notification settings - Fork 57
/
chrx-setup-storage
172 lines (139 loc) · 5.43 KB
/
chrx-setup-storage
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/bash
#
# setup_storage
#
# This script is _sourced_ from install-chrx (not exec'ed),
# so all functions and variables from there are available.
#
# It cannot be run directly from the commandline.
#
eval_try umount -R ${CHRX_INSTALL_ROOT} 2>/dev/null ## be safe.
set_root_partition() {
pnum=$1
case "$CHRX_TARGET_DISK" in
/dev/sd*) CHRX_ROOT_PARTITION="${CHRX_TARGET_DISK}${pnum}" ;;
/dev/mmcblk*) CHRX_ROOT_PARTITION="${CHRX_TARGET_DISK}p${pnum}" ;;
/dev/nvme*) CHRX_ROOT_PARTITION="${CHRX_TARGET_DISK}p${pnum}" ;;
*) echo_fail "\nunrecognized disk device: \"$CHRX_TARGET_DISK\"!"; exit 1 ;;
esac
}
if [ "${CHRX_TARGET_DISK}" ]; then
echo_info "Installing to target disk ${ANSI_WHT}${CHRX_TARGET_DISK}${ANSI_RST}"
cat <<- EOF
${ANSI_YEL}
WARNING: All data on ${CHRX_TARGET_DISK} will be erased!
Continue at your own risk!
${ANSI_RST}
EOF
echo_warn "Press [enter] to continue or CTRL-C to quit: \c"
prompt_if_interactive
echo_info "Setting up target disk ${CHRX_TARGET_DISK} formatting..."
#CHRX_ROOT_PARTITION="${CHRX_TARGET_DISK}1"
#if [[ "${CHRX_TARGET_DISK}" =~ "mmcblk" ]]; then
# CHRX_ROOT_PARTITION="${CHRX_TARGET_DISK}p1"
#fi
set_root_partition 1
#dd if=/dev/zero of="${CHRX_ROOT_PARTITION}" bs=512 count=1
#printf ",,L,*\n" | sfdisk "${CHRX_TARGET_DISK}"
printf "g\nn\n\n\n\nw\n" | fdisk "${CHRX_TARGET_DISK}" > /dev/null
fdisk -l "${CHRX_TARGET_DISK}"
#echo debug ; read line
#blockdev --rereadpt "${CHRX_TARGET_DISK}"
echo_info "OK"
else
CHRX_TARGET_DISK="`rootdev -d -s`"
echo_info "Setting up target disk ${CHRX_TARGET_DISK} partitions..."
# Do partitioning (if we haven't already)
ckern_size="`cgpt show -i 6 -n -s -q ${CHRX_TARGET_DISK}`"
croot_size="`cgpt show -i 7 -n -s -q ${CHRX_TARGET_DISK}`"
state_size="`cgpt show -i 1 -n -s -q ${CHRX_TARGET_DISK}`"
max_os_size=$((${state_size}/1024/1024/2))
rec_os_size=$((${max_os_size} - 1))
# If KERN-C and ROOT-C are one, we partition, otherwise assume they're what they need to be...
if [ "${ckern_size}" = "1" -o "${croot_size}" = "1" ]; then
while :
do
cat <<- EOF
How much disk space should we reserve for Linux?
Choose between 5 and ${max_os_size} GB (recommended: ${rec_os_size}).
EOF
echo_info "Enter size in GB [${rec_os_size}]: \c"
prompt_if_interactive
os_size=${PROMPT_RESPONSE}
[ -z "${os_size}" ] && os_size=${rec_os_size}
os_size_test=`echo ${os_size} | grep "^[0-9][0-9]*$"`
if [ -z "${os_size_test}" ]; then
echo_fail "\nNumbers only please!"
continue
fi
if [ ${os_size} -lt 5 -o ${os_size} -gt ${max_os_size} ]; then
echo_fail "\nPlease enter a number between 5 and ${max_os_size}!"
continue
fi
break
done
# We've got our size in GB for ROOT-C so do the math...
rootc_size=$((${os_size}*1024*1024*2)) # sector size for rootc
kernc_size=32768 # kernc is always 16MB
#new stateful size with rootc and kernc subtracted from original
stateful_size=$((${state_size} - ${rootc_size} - ${kernc_size}))
#start stateful at the same spot it currently starts at
stateful_start="`cgpt show -i 1 -n -b -q ${CHRX_TARGET_DISK}`"
#start kernc at stateful start plus stateful size
kernc_start=$((${stateful_start} + ${stateful_size}))
#start rootc at kernc start plus kernc size
rootc_start=$((${kernc_start} + ${kernc_size}))
echo " stateful_start ${stateful_start}"
echo " stateful_size ${stateful_size}"
echo " kernc_start ${kernc_start}"
echo " kernc_size ${kernc_size}"
echo " rootc_start ${rootc_start}"
echo " rootc_size ${rootc_size}"
## Do the real work
echo_info "\n\nModifying partition table to make room for Linux."
umount -f /mnt/stateful_partition > /dev/null 2>&1
cgpt add -i 1 -b ${stateful_start} -s ${stateful_size} -l STATE ${CHRX_TARGET_DISK}
cgpt add -i 6 -b ${kernc_start} -s ${kernc_size} -l KERN-C -t "kernel" ${CHRX_TARGET_DISK}
cgpt add -i 7 -b ${rootc_start} -s ${rootc_size} -l ROOT-C ${CHRX_TARGET_DISK}
cat <<- EOF
${ANSI_GRN}
Your Chromebook will reboot and clear your data.
Run chrx again to install Linux.
${ANSI_RST}
EOF
echo_warn "Press [enter] to continue: \c"
prompt_if_interactive
## reboot without dropping user to shell
reboot
sleep 10
exit
fi
fi
## set targets
if [ -z "$CHRX_ROOT_PARTITION" ]; then
#CHRX_ROOT_PARTITION="${CHRX_TARGET_DISK}7"
#if [[ "${CHRX_TARGET_DISK}" =~ "mmcblk" ]]; then
# CHRX_ROOT_PARTITION="${CHRX_TARGET_DISK}p7"
#fi
set_root_partition 7
fi
echo_info "\nInstalling to ${CHRX_ROOT_PARTITION}..."
eval_try umount -R ${CHRX_ROOT_PARTITION} 2>/dev/null
eval_try umount -R ${CHRX_INSTALL_ROOT} 2>/dev/null
if [ "`mount | grep ${CHRX_ROOT_PARTITION}`" ]; then
echo_fail "Install target ${CHRX_ROOT_PARTITION} is already mounted!\n"
echo_warn "Unmount manually (sudo umount -R ${CHRX_INSTALL_ROOT}) or reboot"
echo_warn "to try again."
exit 1
fi
eval_crit mkfs.ext4 ${VERBOSITY_MKFS} ${CHRX_ROOT_PARTITION}
echo_info "Preparing installation root directory ${CHRX_INSTALL_ROOT}"
rm -rf ${CHRX_INSTALL_ROOT}
mkdir -p ${CHRX_INSTALL_ROOT}
if [ ! -d "${CHRX_INSTALL_ROOT}" ]; then
echo_fail "fatal: Unable to create installation root directory!"
exit 1
fi
eval_crit mount -t ext4 ${CHRX_ROOT_PARTITION} ${CHRX_INSTALL_ROOT}
## clear error variable, which is checked in the calling script
SETUP_STORAGE_ERROR=""