-
Notifications
You must be signed in to change notification settings - Fork 0
/
installiso.sh
390 lines (339 loc) · 8.39 KB
/
installiso.sh
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/bin/bash
# Author: long wei
ISOPATH="*.iso"
USERNAME="linux"
PASSWORD="123456"
TIMEZONE="Asia/Shanghai"
LOCALE="zh_CN.UTF-8"
HOST="linux-pc"
ROOTPATH="/dev/sda1"
HOMEPATH=""
GRUBPATH="/dev/sda"
FORMATROOT="false"
MOUNT_ISO="/mnt/iso/$RANDOM"
SQUASHFS_DEST="/mnt/squashfs/$RANDOM"
ROOT_TARGET="/mnt/root/$RANDOM"
ROOT_UUID=""
ROOT_TYPE=""
HOME_UUID=""
HOME_TYPE=""
usage()
{
echo "
Usage:
installiso.sh [OPTION]
Options:
-i <iso> specify iso to install
-u <username> specify username for target os
-p <password> specicy password for target os
-t <timezone> specify timezone for target os
-l <locale> specify locale for target os
-n <hostname> specify hostname for target os
-r <root> specify / mount partition
-m <home> specify /home mount partition
-g <grub> specify grub install location
-f format root partition
-h show help
"
}
parse_opt()
{
while getopts "i:u:p:t:l:n:r:m:g:fh" opt
do
case "$opt" in
i)
ISOPATH=$OPTARG;;
u)
USERNAME=$OPTARG;;
p)
PASSWORD=$OPTARG;;
t)
TIMEZONE=$OPTARG;;
l)
LOCALE=$OPTARG;;
n)
HOST=$OPTARG;;
r)
ROOTPATH=$OPTARG;;
m)
HOMEPATH=$OPTARG;;
g)
GRUBPATH=$OPTARG;;
f)
FORMATROOT="true";;
h)
usage
exit 0;;
*)
usage
return 1
esac
done
}
check_requirements()
{
if [ $UID -ne 0 ]; then
echo "Need run as root"
exit 1
fi
if [ ! -f $ISOPATH ]; then
echo "Invalid isopath:" $ISOPATH
usage
exit 1
fi
if [ ! -b $ROOTPATH ]; then
echo "Invalid root path:" $ROOTPATH
usage
exit 1
fi
if [ ! -z $HOMEPATH ] && [ ! -b $HOMEPATH ] ; then
echo "Invalid home path:" $HOMEPATH
usage
exit 1
fi
if [ X`df -h |grep $ROOTPATH | awk '{print $6}'`Y == X/Y ]; then
echo "$ROOTPATH already mounted on /, please select another partition"
usage
exit 1
fi
ROOT_UUID=`blkid $ROOTPATH | awk '{print $2}'`
ROOT_TYPE=`blkid $ROOTPATH | awk '{print $3}' | awk -F'"' '{print $2}'`
if [ ! -z $HOMEPATH ]; then
HOME_UUID=`blkid $HOMEPATH | awk '{print $2}'`
HOME_TYPE=`blkid $HOMEPATH | awk '{print $3}' | awk -F'"' '{print $2}'`
fi
echo "Disk requirements satisfied"
if [ X`which unsquashfs` == X ]; then
echo "Need install squashfs-tools"
exit 1
fi
if [ $FORMATROOT == "true" ] ; then
eval "mkfs.$ROOT_TYPE $ROOTPATH"
fi
}
ensure_directory()
{
echo "Ensure directory"
if [ -d $MOUNT_ISO ] ; then
umount -l $MOUNT_ISO
rm -rf $MOUNT_ISO
fi
mkdir -p $MOUNT_ISO
if [ -d $ROOT_TARGET ]; then
rm -rf $ROOT_TARGET
fi
mkdir -p $ROOT_TARGET
if [ -d $SQUASHFS_DEST ]; then
rm -rf $SQUASHFS_DEST
fi
if [ ! -d "/mnt/squashfs" ]; then
mkdir -p /mnt/squashfs
fi
}
install_squashfs()
{
echo "Install squashfs"
mount -t iso9660 -o loop $ISOPATH $MOUNT_ISO
if [ ! -f $MOUNT_ISO/casper/filesystem.squashfs ]; then
echo "Invalid ISO: filesystem.squashfs not found"
clean_up
exit
fi
echo "mount -t $ROOT_TYPE $ROOTPATH $ROOT_TARGET"
mount -t $ROOT_TYPE $ROOTPATH $ROOT_TARGET
if [ ! -z $HOMEPATH ]; then
mount -t $HOME_TYPE $HOMEPATH $ROOT_TARGET/home
fi
echo "Begin unsquashfs $MOUNT_ISO/casper/filesystem.squashfs to $SQUASHFS_DEST"
unsquashfs -d $SQUASHFS_DEST $MOUNT_ISO/casper/filesystem.squashfs
if [ $? -ne 0 ]; then
echo "Unsquashfs failed"
clean_up
exit
fi
echo "Copy os to target, please wait......"
cp -a $SQUASHFS_DEST/* $ROOT_TARGET
echo "Copy os to target finish."
}
install_kernel()
{
echo "Install kernel"
cd_kernel=""
prefix=""
for prefix in "vmlinux" "vmlinuz"; do
for suffix in "" ".efi" ".efi.signed"; do
if [ -f $MOUNT_ISO/casper/$prefix$suffix ]; then
cd_kernel=$prefix$suffix
break 2
fi
done
done
cd_kernel_path=$MOUNT_ISO/casper/$cd_kernel
echo "cd kernel path: $cd_kernel_path"
release=`ls $ROOT_TARGET/boot/ | grep 'config-' | cut -c 8-`
target_kernel=$prefix-$release
target_kernel_path=$ROOT_TARGET/boot/$target_kernel
echo "target kernel path: $target_kernel_path"
cp $cd_kernel_path $target_kernel_path
}
write_fstab()
{
echo "Write fstab"
if [ -f $ROOT_TARGET/etc/fstab ]; then
rm -rf $ROOT_TARGET/etc/fstab
fi
cat > $ROOT_TARGET/etc/fstab <<"EOF"
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
EOF
echo "# / was on $ROOTPATH during installation" >> $ROOT_TARGET/etc/fstab
echo "$ROOT_UUID / $ROOT_TYPE errors=remount-ro 0 1" >> $ROOT_TARGET/etc/fstab
if [ ! -z $HOMEPATH ]; then
echo "# /home was on $HOMEPATH during installation" >> $ROOT_TARGET/etc/fstab
echo "$HOME_UUID /home $HOME_TYPE defaults 0 2" >> $ROOT_TARGET/etc/fstab
fi
if [ `cat /proc/swaps | wc -l` -eq 2 ]; then
local swap=`cat /proc/swaps | sed -n '2p' | awk '{print $1}'`
local swap_uuid=`blkid $swap | awk '{print $2}'`
echo "# swap was on $swap during installation" >> $ROOT_TARGET/etc/fstab
echo "$swap_uuid none swap sw 0 0" >> $ROOT_TARGET/etc/fstab
fi
}
set_hostname()
{
echo "Set hostname"
echo "$HOST" > $ROOT_TARGET/etc/hostname
echo -e "127.0.1.1 $HOST" > $ROOT_TARGET/etc/hosts
cat >> $ROOT_TARGET/etc/hosts << "EOF"
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
EOF
}
set_timezone()
{
echo "Set timezone"
echo $TIMEZONE > $ROOT_TARGET/etc/timezone
}
set_keyboard_layout()
{
echo "Set keyboard layout"
cat > $ROOT_TARGET/etc/default/keyboard << "EOF"
# Check /usr/share/doc/keyboard-configuration/README.Debian for
# documentation on what to do after having modified this file.
# The following variables describe your keyboard and can have the same
# values as the XkbModel, XkbLayout, XkbVariant and XkbOptions options
# in /etc/X11/xorg.conf.
XKBMODEL="pc105"
XKBLAYOUT="cn"
XKBVARIANT=""
XKBOPTIONS=""
EOF
}
set_locale()
{
echo "Set locale"
echo "LANG=\"$LOCALE\"" > $ROOT_TARGET/etc/default/locale
local language=`echo $LOCALE | awk -F"." '{print $1}'`
echo "LANGUAGE=\"$language\"" >> $ROOT_TARGET/etc/default/locale
#echo "LC_ALL=\"C\"" >> $ROOT_TARGET/etc/default/locale
}
do_chroot_set()
{
echo "Chroot set locale"
chroot $ROOT_TARGET /bin/bash -c "locale-gen $LOCALE"
chroot $ROOT_TARGET /bin/bash -c "update-locale"
echo "Chroot create user"
chroot $ROOT_TARGET /bin/bash -c "groupadd $USERNAME"
chroot $ROOT_TARGET /bin/bash -c "useradd -s /bin/bash -m -k /etc/skel -g $USERNAME $USERNAME"
chroot $ROOT_TARGET /bin/bash -c "echo \"$USERNAME:$PASSWORD\" | chpasswd"
allgroups=`cat $ROOT_TARGET/etc/group |awk -F':' '{print $1}'`
dgroups="adm cdrom sudo dip plugdev lpadmin sambashare"
groups=""
for ag in $allgroups; do
for dg in $dgroups; do
if [ $dg == $ag ]; then
groups=$groups" "$ag
fi
done
done
echo "add user to groups:$groups"
trimmed=`echo $groups | tr " " ,`
chroot $ROOT_TARGET /bin/bash -c "usermod -a -G $trimmed $USERNAME"
echo "Chroot set grub"
chroot $ROOT_TARGET /bin/bash -c "update-initramfs -u"
chroot $ROOT_TARGET /bin/bash -c "grub-install $GRUBPATH"
chroot $ROOT_TARGET /bin/bash -c "update-grub2"
}
clean_up()
{
echo "Clean up"
while [ `mount |grep -c $MOUNT_ISO` -ne 0 ]
do
umount -l $MOUNT_ISO
done
if [ -d $MOUNT_ISO ] ;then
rm -rf $MOUNT_ISO
fi
if [ -d $SQUASHFS_DEST ]; then
rm -rf $SQUASHFS_DEST
fi
while [ $(mount | grep -c $ROOT_TARGET/proc) != '0' ]
do
umount -l $ROOT_TARGET/proc
sleep 1
done
while [ $(mount | grep -c $ROOT_TARGET/sys) != '0' ]
do
umount -l $ROOT_TARGET/sys
sleep 1
done
while [ $(mount | grep -c $ROOT_TARGET/dev/pts) != '0' ]
do
umount -l $ROOT_TARGET/dev/pts
sleep 1
done
while [ $(mount | grep -c $ROOT_TARGET/dev) != '0' ]
do
umount -l $ROOT_TARGET/dev
sleep 1
done
while [ $(mount | grep -c $ROOT_TARGET) != '0' ]
do
umount -l $ROOT_TARGET
sleep 1
done
if [ -d $ROOT_TARGET ]; then
rm -rf $ROOT_TARGET
fi
}
main()
{
parse_opt $@
check_requirements
ensure_directory
install_squashfs
install_kernel
write_fstab
set_hostname
set_timezone
set_keyboard_layout
set_locale
mount --bind /dev $ROOT_TARGET/dev
mount -t proc none $ROOT_TARGET/proc
mount -t sysfs none $ROOT_TARGET/sys
mount -t devpts none $ROOT_TARGET/dev/pts
do_chroot_set
clean_up
echo "Install finish, please restart your system"
}
main $@