-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmklive.sh
executable file
·195 lines (179 loc) · 5.71 KB
/
mklive.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
#!/bin/sh
# live-tools/mklive.sh: update/create live media
[ `id -u` -ne 0 ] && asroot="sudo"
# print error message
error () {
printf "${@} \n" 1>&2
}
# mount first partition of image file
mklive_mount () {
mount -o offset=1048576 "${1}" "${2}"
}
# exclude packages in desktop install
mklive_filter_packages () {
grep -Ev 'casper|cryptsetup|debian-installer|discover|dmraid|icu|rdate|reiser|ubiquity|user-setup' "${1}"
}
# update root filesystem
mklive_update () {
${asroot} cp -aL /etc/resolv.conf "${1}/etc/resolv.conf"
${asroot} mount -t devpts devpts "${1}/dev/pts"
${asroot} chroot "${1}" << EOF || return $?
apt-get update
apt-get -y safe-upgrade
apt-get clean
EOF
${asroot} umount "${1}/dev/pts"
}
# remove apt cache
mklive_clean_apt () {
${asroot} rm -f \
"${1}/var/cache/apt/pkgcache.bin" \
"${1}/var/cache/apt/srcpkgcache.bin" \
"${1}/var/cache/apt/archives/partial/"* \
"${1}/var/lib/aptitude/pkgstates.old" \
"${1}/var/lib/apt/lists/"*_Sources \
"${1}/var/lib/apt/lists/"*_Packages \
"${1}/var/lib/apt/lists/"*_Translation-de \
"${1}/var/lib/apt/lists/"*_Translation-en \
"${1}/var/lib/apt/lists/partial/"*
}
# remove history
mklive_clean_hist () {
${asroot} rm -f \
"${1}/var/cache/debconf/"*.dat-old \
"${1}/var/lib/dpkg/"*-old \
"${1}/var/backups/"*.gz \
"${1}/var/log/"*.gz \
"${1}/var/log/upstart/"*.gz
}
# create live files
mklive_create () {
local root=${1}
local dest=${2}
[ -z "${root}" ] && error "missing chroot directory" && return 1
[ -n "${dest}" ] || dest="."
# get package selections
${asroot} chroot "${root}" dpkg-query -W --showformat='${Package} ${Version}\n' > "${dest}/filesystem.manifest" || return 1
mklive_filter_packages "${dest}/filesystem.manifest" > "${dest}/filesystem.manifest-desktop"
# clear some files
mklive_clean_apt "${root}"
mklive_clean_hist "${root}"
${asroot} rm -f "${root}/etc/udev/rules.d/"*persistent*.rules
# calculate unpacked size
printf $(${asroot} du -sx --block-size=1 "${root}" | cut -f1) > "${dest}/filesystem.size" || return $?
# update kernel and initrd
${asroot} cp -L --preserve=mode,timestamps "${root}/vmlinuz" "${root}/initrd.img" "${dest}"
# build squash filesystem
${asroot} mksquashfs "${root}" "${dest}/filesystem.squashfs" \
-comp xz -noappend -wildcards \
-e 'boot/grub' -e 'boot/initrd.*' \
-e 'initrd.img' \
-e 'tmp/*' -e 'var/tmp/*' \
|| return $?
${asroot} chmod 644 "${dest}/filesystem.squashfs"
}
# create single partition
mklive_part () {
${asroot} fdisk ${1} > /dev/null << EOF
o
n
p
1
t
c
w
EOF
}
# change file system label
mklive_label () {
case `$asroot blkid -o value -s TYPE "$1"` in
vfat) local label=fatlabel;;
exfat) local label=exfatlabel;;
ntfs) local label=ntfslabel;;
*) error 'unsupported file system'; return 1;;
esac
${asroot} "${label}" "${@}"
}
# install grub to device
mklive_grub () {
local sys='boot/grub'
[ -z "${1}" ] && error "missing target device" && return 1
[ -n "${2}" ] && sys="${2}"
# temporary file names
local early=`mktemp /tmp/grub-core_XXXXXX.cfg`
local core=`mktemp /tmp/grub-core_XXXXXX.img`
# create grub core file config
printf '%s\n%s\n' 'set root=hd0,msdos1' "set prefix=(\$root)/${sys}" > "${early}" && \
# create grub core image file
grub-mkimage -O i386-pc -c "${early}" -o "${core}" biosdisk part_msdos `blkid -o value -s TYPE "${1}1"` && \
# write grub boot code files
${asroot} dd if="/usr/lib/grub/i386-pc/boot.img" of="${1}" bs=440 count=1 && \
${asroot} dd if="${core}" of="${1}" bs=512 seek=1
rm "${early}" "${core}"
}
# copy live files
mklive_copy () {
[ $# -lt 2 ] && return 2
# make target directory
mkdir -p "${2}" || return $?
# copy new files
printf '%s' 'copying live files... '
rsync --modify-window=2 -crtL "${1}/*" "${2}" || return $?
printf '%s\n' 'finished'
}
# live media creation
mklive_install () {
# default settings
local source="/home/live"
local mpoint="/mnt"
local sysdir=".sys"
# overwrite from args
[ $# -lt 1 ] && return 2
[ -n "${2}" ] && source="${2}"
[ -n "${3}" ] && mpoint="${3}"
[ -n "${4}" ] && sysdir="${4}"
# copy live files
${asroot} mount "${1}1" "${mpoint}" || return $?
mkdir -p "/${mpoint}/${sysdir}" || return $?
mklive_copy "${source}" "/${mpoint}/${sysdir}" || return $?
# set target directory filesystem flags
local attr="`which fatattr`"
if [ -n "$attr" ]; then \
printf '%s' 'hide system directory... '
if "$attr" +hs "/${mpoint}/${sysdir}"; then
printf '%s\n' 'done'
else
printf '%s\n' 'failed'
fi
fi
# use grub2 as boot loader
printf '%s' 'write boot code... '
mklive_grub "${1}" "${sysdir}/grub"
${asroot} umount "${mpoint}"
printf '%s\n' 'finished'
}
# print available script operations
mklive_usage () {
printf "${1} usage:\n"
local prog="`basename ${1}`"
printf ' %s %s\n' "${prog}" 'new <dev>'
printf ' %s %s\n' "${prog}" 'mount <image> <root>'
printf ' %s %s\n' "${prog}" 'label <partition>'
printf ' %s %s\n' "${prog}" 'create <root> <dir>'
printf ' %s %s\n' "${prog}" 'install <dev> [<dir>] [<mountpoint>]'
printf ' %s %s\n' "${prog}" 'update <sysroot>'
}
# script command selection when called
[ -n "$-" ] || {
case "${1}" in
mount) mklive_mount "${2}" "${3}";;
update) mklive_update "${2}";;
new) mklive_part "${2}" && mkfs.vfat -n LIVE "${2}";;
create) shift; mklive_create "${@}";;
label) shift; mklive_label "${@}";;
install) shift; mklive_install "${@}";;
'') mklive_usage "${0}";;
*) mklive_usage "${0}"; return 1;;
esac
return $?
}