-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·165 lines (142 loc) · 4.27 KB
/
install.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
#!/usr/bin/env tclsh
# --------------------------------------------------------------------------
# exec variant
# --------------------------------------------------------------------------
proc wexec { args } {
puts "+ $args"
exec {*}$args >@ stdout 2>@ stderr
}
# --------------------------------------------------------------------------
# Run a program capturing its stdout
# --------------------------------------------------------------------------
proc stdout { args } {
set fin [open "| $args" "r"]
set res [read $fin]
close $fin
return [string trim $res]
}
# --------------------------------------------------------------------------
# Exit with error message
# --------------------------------------------------------------------------
proc die {msg} {
puts $msg
exit 1
}
# --------------------------------------------------------------------------
# Assert like
# --------------------------------------------------------------------------
proc dieif { cond msg } {
if $cond {
die $msg
}
}
# --------------------------------------------------------------------------
# Custom unknown which runs its arguments
# --------------------------------------------------------------------------
proc unknown { args } {
wexec {*}$args
}
# --------------------------------------------------------------------------
# Check is a debian package is installed
# --------------------------------------------------------------------------
proc checkpkg { pkg msg } {
if { [catch {stdout dpkg -l $pkg} err] } {
die $msg
}
}
# --------------------------------------------------------------------------
# Print error message and infos about program usage then quit
# --------------------------------------------------------------------------
proc usage { msg } {
puts stderr "\n$::argv0 error: $msg"
puts stderr "\nUsage:"
puts stderr "\t$::argv0 -f <payload-format> -d <disk>"
exit 1
}
# -- Check if running as root
dieif {[stdout id -u] != 0 } "You must be root to run this script !"
# -- Check mandatory debian packages
checkpkg "extlinux" "extlinux missing !"
checkpkg "syslinux-common" "syslinux-common missing !"
# -- parse command line
set failed [catch {
array set ::PARAMS $argv
}]
if { $failed } {
usage "Failed to parse command line"
}
# -- global variables
set failed [catch {
set ::FORMAT $::PARAMS(-f)
set ::DISK $::PARAMS(-d)
}]
if { $failed } {
usage "Failed to parse command line"
}
# -- Get list of process using /old_root
puts "Listing processes using ${DISK}"
set tokill [list]
set procs [glob /proc/*/exe ]
foreach p $procs {
catch {
set plink [file readlink $p]
if { $plink eq "" } continue
lassign [file split $plink] dummy root
if { $root eq "old_root" } {
lassign [file split $p] dummy dummy pid
lappend tokill $pid
}
}
}
# -- Kill them !
puts "Killing processes using ${DISK}"
foreach p $tokill {
catch { kill -9 $p }
}
# -- Intentional second pass
foreach p $tokill {
catch { kill -9 $p }
}
if { $FMT eq "tgz" } {
# -- Erase disk
puts "Erase ${DISK}1 content"
sync
set fout [open /proc/sys/vm/drop_caches w]
puts $fout 3
close $fout
umount -l /old_root
mount ${DISK}1 /old_root
catch [list rm -rf {*}[glob /old_root/*]]
# -- untar file system
puts "Unpacking rootfs in /old_root"
fconfigure stdin -encoding binary -translation binary
tar -C /old_root/ -xzv
# -- install extlinux mbr
puts "Installing bootloader MBR"
dd bs=440 conv=notrunc count=1 if=/usr/lib/syslinux/mbr/mbr.bin of=${DISK}
# -- installation des menus extlinux
puts "Installing bootloader"
extlinux --install /old_root
set fout [open "/old_root/extlinux.conf" "w"]
puts $fout "default linux"
puts $fout "timeout 1"
puts $fout "label linux"
puts $fout "kernel /boot/vmlinuz-4.9.0-4-amd64"
puts $fout "append initrd=/boot/initrd.img-4.9.0-4-amd64 root=${DISK}1 net.ifnames=0"
close $fout
# -- syncing
sync
umount /old_root
}
if { $FMT eq "img" } {
# -- Erase disk
puts "Erase ${DISK} content"
sync
umount -l /old_root
# -- untar file system
puts "Flashing image"
fconfigure stdin -encoding binary -translation binary
dd of=${DISK} bs=4096 status=progress
}
# -- leaving
exit 0