slitaz-modular annotate initramfs/lib/livekitlib @ rev 177

mkiso.sh: more filenames support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Mar 30 09:46:37 2016 +0200 (2016-03-30)
parents
children
rev   line source
slaxemulator@175 1 #!/bin/sh
slaxemulator@175 2
slaxemulator@175 3 # Functions library :: for Linux Live Kit scripts
slaxemulator@175 4 # Author: Tomas M. <http://www.linux-live.org>
slaxemulator@175 5 #
slaxemulator@175 6
slaxemulator@175 7 # =================================================================
slaxemulator@175 8 # debug and output functions
slaxemulator@175 9 # =================================================================
slaxemulator@175 10
slaxemulator@175 11 debug_start()
slaxemulator@175 12 {
slaxemulator@175 13 if grep -q debug /proc/cmdline; then
slaxemulator@175 14 DEBUG_IS_ENABLED=1
slaxemulator@175 15 else
slaxemulator@175 16 DEBUG_IS_ENABLED=
slaxemulator@175 17 fi
slaxemulator@175 18 }
slaxemulator@175 19
slaxemulator@175 20 debug_log()
slaxemulator@175 21 {
slaxemulator@175 22 if [ "$DEBUG_IS_ENABLED" ]; then
slaxemulator@175 23 echo "- debug: $*" >&2
slaxemulator@175 24 log "- debug: $*"
slaxemulator@175 25 fi
slaxemulator@175 26 }
slaxemulator@175 27
slaxemulator@175 28 # header
slaxemulator@175 29 # $1 = text to show
slaxemulator@175 30 #
slaxemulator@175 31 header()
slaxemulator@175 32 {
slaxemulator@175 33 echo """$@"""
slaxemulator@175 34 }
slaxemulator@175 35
slaxemulator@175 36
slaxemulator@175 37 # echo green star
slaxemulator@175 38 #
slaxemulator@175 39 echo_green_star()
slaxemulator@175 40 {
slaxemulator@175 41 echo -ne """* """
slaxemulator@175 42 }
slaxemulator@175 43
slaxemulator@175 44 # log - store given text in /var/log/livedbg
slaxemulator@175 45 log()
slaxemulator@175 46 {
slaxemulator@175 47 echo "$@" 2>/dev/null >>/var/log/livedbg
slaxemulator@175 48 }
slaxemulator@175 49
slaxemulator@175 50 echolog()
slaxemulator@175 51 {
slaxemulator@175 52 echo "$@"
slaxemulator@175 53 log "$@"
slaxemulator@175 54 }
slaxemulator@175 55
slaxemulator@175 56 # show information about the debug shell
slaxemulator@175 57 show_debug_banner()
slaxemulator@175 58 {
slaxemulator@175 59 echo
slaxemulator@175 60 echo "====="
slaxemulator@175 61 echo ": Debugging started. Here is the root shell for you."
slaxemulator@175 62 echo ": Type your desired commands or hit Ctrl+D to continue booting."
slaxemulator@175 63 echo
slaxemulator@175 64 }
slaxemulator@175 65
slaxemulator@175 66 # debug_shell
slaxemulator@175 67 # executed when debug boot parameter is present
slaxemulator@175 68 #
slaxemulator@175 69 debug_shell()
slaxemulator@175 70 {
slaxemulator@175 71 if [ "$DEBUG_IS_ENABLED" ]; then
slaxemulator@175 72 show_debug_banner
slaxemulator@175 73 setsid sh -c 'exec sh < /dev/tty1 >/dev/tty1 2>&1'
slaxemulator@175 74 echo
slaxemulator@175 75 fi
slaxemulator@175 76 }
slaxemulator@175 77
slaxemulator@175 78 fatal()
slaxemulator@175 79 {
slaxemulator@175 80 echolog
slaxemulator@175 81 header "Fatal error occured - $1"
slaxemulator@175 82 echolog "Something went wrong and we can't continue. This should never happen."
slaxemulator@175 83 echolog "Please reboot your computer with Ctrl+Alt+Delete ..."
slaxemulator@175 84 echolog
slaxemulator@175 85 setsid sh -c 'exec sh < /dev/tty1 >/dev/tty1 2>&1'
slaxemulator@175 86 }
slaxemulator@175 87
slaxemulator@175 88 # get value of commandline parameter $1
slaxemulator@175 89 # $1 = parameter to search for
slaxemulator@175 90 #
slaxemulator@175 91 cmdline_value()
slaxemulator@175 92 {
slaxemulator@175 93 cat /proc/cmdline | egrep -o "(^|[[:space:]])$1=[^[:space:]]+" | tr -d " " | cut -d "=" -f 2- | tail -n 1
slaxemulator@175 94 }
slaxemulator@175 95
slaxemulator@175 96
slaxemulator@175 97 # test if the script is started by root user. If not, exit
slaxemulator@175 98 #
slaxemulator@175 99 allow_only_root()
slaxemulator@175 100 {
slaxemulator@175 101 if [ "0$UID" -ne 0 ]; then
slaxemulator@175 102 echo "Only root can run $(basename $0)"; exit 1
slaxemulator@175 103 fi
slaxemulator@175 104 }
slaxemulator@175 105
slaxemulator@175 106 # Create bundle
slaxemulator@175 107 # call mksquashfs with apropriate arguments
slaxemulator@175 108 # $1 = directory which will be compressed to squashfs bundle
slaxemulator@175 109 # $2 = output file
slaxemulator@175 110 # $3..$9 = optional arguments like -keep-as-directory or -b 123456789
slaxemulator@175 111 #
slaxemulator@175 112 create_bundle()
slaxemulator@175 113 {
slaxemulator@175 114 debug_log "create_module" "$*"
slaxemulator@175 115 rm -f "$2" # overwrite, never append to existing file
slaxemulator@175 116 mksquashfs "$1" "$2" -comp xz -b 512K $3 $4 $5 $6 $7 $8 $9>/dev/null
slaxemulator@175 117 }
slaxemulator@175 118
slaxemulator@175 119
slaxemulator@175 120 # Move entire initramfs tree to tmpfs mount.
slaxemulator@175 121 # It's a bit tricky but is necessray to enable pivot_root
slaxemulator@175 122 # even for initramfs boot image
slaxemulator@175 123 #
slaxemulator@175 124 transfer_initramfs()
slaxemulator@175 125 {
slaxemulator@175 126 if [ ! -r /lib/initramfs_escaped ]; then
slaxemulator@175 127 echo "switch root from initramfs to ramfs"
slaxemulator@175 128 SWITCH=/m # one letter directory
slaxemulator@175 129 mkdir -p $SWITCH
slaxemulator@175 130 mount -t tmpfs -o size="100%" tmpfs $SWITCH
slaxemulator@175 131 cp -a /??* $SWITCH 2>/dev/null # only copy two-and-more-letter directories
slaxemulator@175 132 cd $SWITCH
slaxemulator@175 133 echo "This file indicates that we successfully escaped initramfs" > $SWITCH/lib/initramfs_escaped
slaxemulator@175 134 exec switch_root -c /dev/console . $0
slaxemulator@175 135 fi
slaxemulator@175 136 }
slaxemulator@175 137
slaxemulator@175 138 copy_initramfs()
slaxemulator@175 139 {
slaxemulator@175 140 SWITCH=$1 # one letter directory
slaxemulator@175 141 mkdir -p $SWITCH
slaxemulator@175 142 SYS_DIR="dev bin etc boot lib sbin home root run usr var"
slaxemulator@175 143 for dir in $SYS_DIR; do
slaxemulator@175 144 cp -a /$dir /$SWITCH
slaxemulator@175 145 done
slaxemulator@175 146
slaxemulator@175 147 }
slaxemulator@175 148
slaxemulator@175 149 # mount virtual filesystems like proc etc
slaxemulator@175 150 #
slaxemulator@175 151 init_proc_sysfs()
slaxemulator@175 152 {
slaxemulator@175 153 debug_log "init_proc_sysfs" "$*"
slaxemulator@175 154 mkdir -p /proc /sys /etc $MEMORY
slaxemulator@175 155 mount -n -t proc proc /proc
slaxemulator@175 156 echo "0" >/proc/sys/kernel/printk
slaxemulator@175 157 mount -n -t sysfs sysfs /sys
slaxemulator@175 158 mount -n -o remount,rw rootfs /
slaxemulator@175 159 ln -sf /proc/mounts /etc/mtab
slaxemulator@175 160 }
slaxemulator@175 161
slaxemulator@175 162 # make sure some devices are there
slaxemulator@175 163 init_devs()
slaxemulator@175 164 {
slaxemulator@175 165 debug_log "init_devs" "$*"
slaxemulator@175 166 echo /sbin/mdev > /proc/sys/kernel/hotplug
slaxemulator@175 167 mdev -s
slaxemulator@175 168 modprobe zram 2>/dev/null
slaxemulator@175 169 modprobe loop 2>/dev/null
slaxemulator@175 170 modprobe squashfs 2>/dev/null
slaxemulator@175 171 modprobe fuse 2>/dev/null
slaxemulator@175 172 }
slaxemulator@175 173
slaxemulator@175 174 # Activate zram (auto-compression of RAM)
slaxemulator@175 175 # Compressed RAM consumes 1/2 or even 1/4 of original size
slaxemulator@175 176 # Setup static size of 500MB
slaxemulator@175 177 #
slaxemulator@175 178 init_zram()
slaxemulator@175 179 {
slaxemulator@175 180 debug_log "init_zram" "$*"
slaxemulator@175 181 echo_green_star
slaxemulator@175 182 echo "Setting dynamic RAM compression using ZRAM"
slaxemulator@175 183 echo 536870912 > /sys/block/zram0/disksize # 512MB
slaxemulator@175 184 mkswap /dev/zram0 >/dev/null
slaxemulator@175 185 swapon /dev/zram0 -p 32767
slaxemulator@175 186 echo 100 > /proc/sys/vm/swappiness
slaxemulator@175 187 }
slaxemulator@175 188
slaxemulator@175 189 # load the AUFS kernel module if needed
slaxemulator@175 190 #
slaxemulator@175 191 init_aufs()
slaxemulator@175 192 {
slaxemulator@175 193 debug_log "init_aufs" "$*"
slaxemulator@175 194 # TODO maybe check here if aufs support is working at all
slaxemulator@175 195 # and procude useful error message if user has no aufs
slaxemulator@175 196 modprobe aufs brs=1 2>/dev/null
slaxemulator@175 197 }
slaxemulator@175 198
slaxemulator@175 199 # Setup empty union
slaxemulator@175 200 # $1 = changes directory (ramfs or persistent changes)
slaxemulator@175 201 # $2 = union directory where to mount the union
slaxemulator@175 202 #
slaxemulator@175 203 init_union()
slaxemulator@175 204 {
slaxemulator@175 205 debug_log "init_union" "$*"
slaxemulator@175 206
slaxemulator@175 207 echo_green_star
slaxemulator@175 208 echo "Setting up union using AUFS 3"
slaxemulator@175 209 mkdir -p "$1"
slaxemulator@175 210 mkdir -p "$2"
slaxemulator@175 211 mount -t aufs -o nowarn_perm,xino="/.xino",trunc_xino,br="$1" aufs "$2"
slaxemulator@175 212 if [ $? -ne 0 ]; then dmesg | tail -n 1; fatal "can't setup union (aufs)"; fi
slaxemulator@175 213 }
slaxemulator@175 214
slaxemulator@175 215 # Return device mounted for given directory
slaxemulator@175 216 # $1 = directory
slaxemulator@175 217 #
slaxemulator@175 218 mounted_device()
slaxemulator@175 219 {
slaxemulator@175 220 debug_log "mounted_device" "$*"
slaxemulator@175 221
slaxemulator@175 222 local MNT TARGET
slaxemulator@175 223 MNT="$1"
slaxemulator@175 224 while [ "$MNT" != "/" -a "$MNT" != "." -a "$MNT" != "" ]; do
slaxemulator@175 225 TARGET="$(grep -F " $MNT " /proc/mounts | cut -d " " -f 1)"
slaxemulator@175 226 if [ "$TARGET" != "" ]; then
slaxemulator@175 227 echo "$TARGET"
slaxemulator@175 228 return
slaxemulator@175 229 fi
slaxemulator@175 230 MNT="$(dirname "$MNT")"
slaxemulator@175 231 done
slaxemulator@175 232 }
slaxemulator@175 233
slaxemulator@175 234 # Return mounted dir for given directory
slaxemulator@175 235 # $1 = directory
slaxemulator@175 236 #
slaxemulator@175 237 mounted_dir()
slaxemulator@175 238 {
slaxemulator@175 239 debug_log "mounted_dir" "$*"
slaxemulator@175 240
slaxemulator@175 241 local MNT
slaxemulator@175 242 MNT="$1"
slaxemulator@175 243 while [ "$MNT" != "/" -a "$MNT" != "." -a "$MNT" != "" ]; do
slaxemulator@175 244 if mountpoint -q "$MNT" 2>/dev/null; then
slaxemulator@175 245 echo "$MNT"
slaxemulator@175 246 return
slaxemulator@175 247 fi
slaxemulator@175 248 MNT="$(dirname "$MNT")"
slaxemulator@175 249 done
slaxemulator@175 250 }
slaxemulator@175 251
slaxemulator@175 252 # Make sure to mount FAT12/16/32 using vfat
slaxemulator@175 253 # in order to support long filenames
slaxemulator@175 254 # $1 = device
slaxemulator@175 255 #
slaxemulator@175 256 device_bestfs()
slaxemulator@175 257 {
slaxemulator@175 258 debug_log "device_bestfs" "$*"
slaxemulator@175 259 local FS
slaxemulator@175 260
slaxemulator@175 261 FS="$(blkid "$1" | sed -r "s/.* TYPE=//" | sed -r 's/ .*//' | tr -d '"' | tr [A-Z] [a-z])"
slaxemulator@175 262 if [ "$FS" = "msdos" -o "$FS" = "fat" -o "$FS" = "vfat" ]; then
slaxemulator@175 263 FS="vfat"
slaxemulator@175 264 elif [ "$FS" = "ntfs" ]; then
slaxemulator@175 265 FS="ntfs-3g"
slaxemulator@175 266 elif [ "$1" = "iso9660" -o "$1" = "udf" ]; then
slaxemulator@175 267 echo -n ",ro"
slaxemulator@175 268 else
slaxemulator@175 269 echo "-t $FS"
slaxemulator@175 270 fi
slaxemulator@175 271 }
slaxemulator@175 272
slaxemulator@175 273 # Filesystem options for mount
slaxemulator@175 274 # $1 = filesystem or '-t filesystem'
slaxemulator@175 275 #
slaxemulator@175 276 fs_options()
slaxemulator@175 277 {
slaxemulator@175 278 debug_log "fs_options" "$*"
slaxemulator@175 279
slaxemulator@175 280 if [ "$1" = "-t" ]; then
slaxemulator@175 281 shift
slaxemulator@175 282 fi
slaxemulator@175 283 if [ "$1" = "vfat" ]; then
slaxemulator@175 284 echo "-o check=s,shortname=mixed,iocharset=utf8"
slaxemulator@175 285 fi
slaxemulator@175 286 }
slaxemulator@175 287
slaxemulator@175 288
slaxemulator@175 289 # Modprobe network kernel modules until a working driver is found.
slaxemulator@175 290 # These drivers are (or used to be) probed in Slackware's initrd.
slaxemulator@175 291 # The function returns the first device found, yet it doesn't have
slaxemulator@175 292 # to be a working one, eg. if the computer has two network interfaces
slaxemulator@175 293 # and ethernet cable is plugged only to one of them.
slaxemulator@175 294 #
slaxemulator@175 295 init_network_dev()
slaxemulator@175 296 {
slaxemulator@175 297 debug_log "init_network_dev" "$*"
slaxemulator@175 298 local MODULE ETH
slaxemulator@175 299
slaxemulator@175 300 for MODULE in 3c59x acenic de4x5 e1000 e1000e e100 epic100 hp100 \
slaxemulator@175 301 ne2k-pci pcnet32 8139too 8139cp tulip via-rhine r8169 atl1e yellowfin \
slaxemulator@175 302 tg3 dl2k ns83820 atl1 b44 bnx2 skge sky2 tulip depca 3c501 3c503 \
slaxemulator@175 303 3c505 3c507 3c509 3c515 ac3200 at1700 cosa cs89x0 de600 de620 e2100 \
slaxemulator@175 304 eepro eexpress eth16i ewrk3 forcedeth hostess_sv11 hp-plus hp ni52 \
slaxemulator@175 305 ni65 sb1000 sealevel smc-ultra sis900 smc9194 wd; do
slaxemulator@175 306 modprobe $MODULE 2>/dev/null
slaxemulator@175 307 ETH="$(cat /proc/net/dev | grep : | grep -v lo: | cut -d : -f 1 | tr -d " " | head -n 1)"
slaxemulator@175 308 if [ "$ETH" != "" ]; then
slaxemulator@175 309 echo $ETH
slaxemulator@175 310 return 0
slaxemulator@175 311 fi
slaxemulator@175 312 rmmod $MODULE 2>/dev/null
slaxemulator@175 313 done
slaxemulator@175 314
slaxemulator@175 315 # If we are here, none of the above specified modules worked.
slaxemulator@175 316 # As a last chance, try to modprobe everything.
slaxemulator@175 317 find /lib/modules/ | xargs -n 1 modprobe
slaxemulator@175 318 cat /proc/net/dev | grep : | grep -v lo: | cut -d : -f 1 | tr -d " " | head -n 1
slaxemulator@175 319 }
slaxemulator@175 320
slaxemulator@175 321
slaxemulator@175 322 # Download data from tftp
slaxemulator@175 323 # $1 = target (store downloaded files there)
slaxemulator@175 324 #
slaxemulator@175 325 download_data_pxe()
slaxemulator@175 326 {
slaxemulator@175 327 debug_log "download_data_pxe" "$*"
slaxemulator@175 328 local CMD CLIENT SERVER GW MASK PORT ETH PROTOCOL
slaxemulator@175 329
slaxemulator@175 330 mkdir -p "$1/$LIVEKITNAME"
slaxemulator@175 331
slaxemulator@175 332 cmdline_value ip | while IFS=":" read CLIENT SERVER GW MASK PORT; do
slaxemulator@175 333 echo_green_star >&2
slaxemulator@175 334 echo "Downloading files from $SERVER ..." >&2
slaxemulator@175 335
slaxemulator@175 336 ETH=$(init_network_dev)
slaxemulator@175 337 if [ "$PORT" = "" ]; then PORT="7529"; fi
slaxemulator@175 338
slaxemulator@175 339 # set IP address as given by boot paramter
slaxemulator@175 340 if [ "$CLIENT" != "" -a "$MASK" != "" ]; then
slaxemulator@175 341 ifconfig $ETH "$CLIENT" netmask "$MASK"
slaxemulator@175 342 route add default gw "$GW"
slaxemulator@175 343 else
slaxemulator@175 344 # if client ip is unknown, try to get a DHCP lease
slaxemulator@175 345 udhcpc -i $ETH -f -q
slaxemulator@175 346 fi
slaxemulator@175 347
slaxemulator@175 348 # well known IP address of Google public DNS service
slaxemulator@175 349 echo nameserver 8.8.8.8 >> /etc/resolv.conf
slaxemulator@175 350
slaxemulator@175 351 PROTOCOL=http
slaxemulator@175 352 wget -q -O "$1/PXEFILELIST" "http://$SERVER:$PORT/PXEFILELIST?$(uname -r):$(uname -m)"
slaxemulator@175 353 if [ $? -ne 0 ]; then
slaxemulator@175 354 echo "Error downloading from http://$SERVER:$PORT, trying TFTP" >&2
slaxemulator@175 355 PROTOCOL=tftp
slaxemulator@175 356 tftp -g -r PXEFILELIST -l "$1/PXEFILELIST" $SERVER
slaxemulator@175 357 fi
slaxemulator@175 358
slaxemulator@175 359 cat "$1/PXEFILELIST" | while read FILE; do
slaxemulator@175 360 if [ "$PROTOCOL" = "http" ]; then
slaxemulator@175 361 wget -O "$1/$LIVEKITNAME/$FILE" "http://$SERVER:$PORT/$FILE"
slaxemulator@175 362 else
slaxemulator@175 363 echo "* $FILE ..." >&2
slaxemulator@175 364 tftp -g -r $FILE -l "$1/$LIVEKITNAME/$FILE" $SERVER
slaxemulator@175 365 fi
slaxemulator@175 366 done
slaxemulator@175 367 done
slaxemulator@175 368
slaxemulator@175 369 echo "$1/$LIVEKITNAME"
slaxemulator@175 370 }
slaxemulator@175 371
slaxemulator@175 372 # Find LIVEKIT data by mounting all devices
slaxemulator@175 373 # If found, keep mounted, else unmount
slaxemulator@175 374 # $1 = data directory target (mount here)
slaxemulator@175 375 # $2 = data directory which contains compressed bundles
slaxemulator@175 376 #
slaxemulator@175 377 find_data_try()
slaxemulator@175 378 {
slaxemulator@175 379 debug_log "find_data_try" "$*"
slaxemulator@175 380
slaxemulator@175 381 local DEVICE FS FROM OPTIONS
slaxemulator@175 382
slaxemulator@175 383 mkdir -p "$1"
slaxemulator@175 384 blkid | sort | cut -d: -f 1 | grep -E -v "/loop|/ram|/zram" | while read DEVICE; do
slaxemulator@175 385 FROM="$2"
slaxemulator@175 386 FS="$(device_bestfs "$DEVICE")"
slaxemulator@175 387 OPTIONS="$(fs_options $FS)"
slaxemulator@175 388 mount -r "$DEVICE" "$1" $FS $OPTIONS 2>/dev/null
slaxemulator@175 389
slaxemulator@175 390 # if the FROM parameter is actual file, mount it again as loop (eg. iso)
slaxemulator@175 391 if [ -f "$1/$FROM" ]; then
slaxemulator@175 392 mount -o remount,rw "$DEVICE" "$1" 2>/dev/null
slaxemulator@175 393 mkdir -p "$1/../iso"
slaxemulator@175 394 mount -o loop,ro "$1/$FROM" "$1/../iso" 2>/dev/null
slaxemulator@175 395 FROM="../iso/$LIVEKITNAME"
slaxemulator@175 396 fi
slaxemulator@175 397
slaxemulator@175 398 # search for bundles in the mounted directory
slaxemulator@175 399 if [ "$(find "$1/$FROM" -maxdepth 2 -name "*.$BEXT" 2>/dev/null)" != "" ]; then
slaxemulator@175 400 # we found at least one bundle/module here
slaxemulator@175 401 mount -o remount,rw "$DEVICE" "$1" 2>/dev/null
slaxemulator@175 402 echo "$1/$FROM" | tr -s "/" | sed -r "s:/[^/]+/[.][.]/:/:g"
slaxemulator@175 403 return
slaxemulator@175 404 fi
slaxemulator@175 405
slaxemulator@175 406 # unmount twice, since there could be mounted ISO as loop too. If not, it doesn't hurt
slaxemulator@175 407 umount "$1" 2>/dev/null
slaxemulator@175 408 umount "$1" 2>/dev/null
slaxemulator@175 409 done
slaxemulator@175 410 }
slaxemulator@175 411
slaxemulator@175 412 # Retry finding LIVEKIT data several times,
slaxemulator@175 413 # until timeouted or until data is found
slaxemulator@175 414 # $1 = timeout
slaxemulator@175 415 # $2 = data directory target (mount here)
slaxemulator@175 416 #
slaxemulator@175 417 find_data()
slaxemulator@175 418 {
slaxemulator@175 419 debug_log "find_data" "$*"
slaxemulator@175 420
slaxemulator@175 421 local DATA FROM
slaxemulator@175 422
slaxemulator@175 423 if [ "$(cmdline_value ip)" != "" ]; then
slaxemulator@175 424 download_data_pxe "$2"
slaxemulator@175 425 return
slaxemulator@175 426 fi
slaxemulator@175 427
slaxemulator@175 428 FROM="$(cmdline_value from)"
slaxemulator@175 429 if [ "$FROM" = "" ]; then FROM="$LIVEKITNAME"; fi
slaxemulator@175 430
slaxemulator@175 431 echo_green_star >&2
slaxemulator@175 432 echo -n "Looking for $LIVEKITNAME data in /$FROM .." | tr -s "/" >&2
slaxemulator@175 433 for timeout in $(seq 1 $1); do
slaxemulator@175 434 echo -n "." >&2
slaxemulator@175 435 DATA="$(find_data_try "$2" "$FROM")"
slaxemulator@175 436 if [ "$DATA" != "" ]; then
slaxemulator@175 437 echo "" >&2
slaxemulator@175 438 echo "* Found on $(mounted_device "$2")" >&2
slaxemulator@175 439 echo "$DATA"
slaxemulator@175 440 return
slaxemulator@175 441 fi
slaxemulator@175 442 sleep 1
slaxemulator@175 443 done
slaxemulator@175 444 echo "" >&2
slaxemulator@175 445
slaxemulator@175 446 if [ "$DATA" = "" ]; then
slaxemulator@175 447 fatal "$LIVEKITNAME data not found"
slaxemulator@175 448 fi
slaxemulator@175 449
slaxemulator@175 450 }
slaxemulator@175 451
slaxemulator@175 452 # Activate persistent changes
slaxemulator@175 453 # $1 = data directory
slaxemulator@175 454 # $2 = target changes directory
slaxemulator@175 455 #
slaxemulator@175 456 persistent_changes()
slaxemulator@175 457 {
slaxemulator@175 458 debug_log "persistent_changes" "$*"
slaxemulator@175 459
slaxemulator@175 460 local CHANGES T1 T2
slaxemulator@175 461
slaxemulator@175 462 CHANGES="$1/$(basename "$2")"
slaxemulator@175 463 T1="$CHANGES/.empty"
slaxemulator@175 464 T2="$T1"2
slaxemulator@175 465
slaxemulator@175 466 # Setup the directory anyway, it will be used in all cases
slaxemulator@175 467 mkdir -p "$2"
slaxemulator@175 468
slaxemulator@175 469 # If persistent changes are not requested, end here
slaxemulator@175 470 if grep -vq perch /proc/cmdline; then
slaxemulator@175 471 return
slaxemulator@175 472 fi
slaxemulator@175 473
slaxemulator@175 474 # check if changes directory exists and is writable
slaxemulator@175 475 touch "$T1" 2>/dev/null && rm -f "$T1" 2>/dev/null
slaxemulator@175 476
slaxemulator@175 477 # if not, simply return back
slaxemulator@175 478 if [ $? -ne 0 ]; then
slaxemulator@175 479 echo "* Persistent changes not writable or not used"
slaxemulator@175 480 return
slaxemulator@175 481 fi
slaxemulator@175 482
slaxemulator@175 483 echo_green_star
slaxemulator@175 484 echo "Testing persistent changes for posix compatibility"
slaxemulator@175 485 touch "$T1" && ln -sf "$T1" "$T2" 2>/dev/null && \
slaxemulator@175 486 chmod +x "$T1" 2>/dev/null && test -x "$T1" && \
slaxemulator@175 487 chmod -x "$T1" 2>/dev/null && test ! -x "$T1" && \
slaxemulator@175 488 rm "$T1" "$T2" 2>/dev/null
slaxemulator@175 489
slaxemulator@175 490 if [ $? -ne 0 ]; then
slaxemulator@175 491 echo "* Activating dynamic sized storage for persistent changes"
slaxemulator@175 492 rm "$T1" "$T2" 2>/dev/null
slaxemulator@175 493
slaxemulator@175 494 mount.dynfilefs "$CHANGES/changes.dat" 4000 "$2"
slaxemulator@175 495 if [ "$(device_bestfs "$2/loop.fs" | tr -d " ")" = "-t" ]; then
slaxemulator@175 496 mke2fs -F "$2/loop.fs" >/dev/null
slaxemulator@175 497 fi
slaxemulator@175 498 mount -o loop,sync "$2/loop.fs" "$2"
slaxemulator@175 499 rmdir "$2/lost+found" 2>/dev/null
slaxemulator@175 500 else
slaxemulator@175 501 echo "* Activating native persistent changes"
slaxemulator@175 502 mount --bind "$CHANGES" "$2"
slaxemulator@175 503 fi
slaxemulator@175 504 }
slaxemulator@175 505
slaxemulator@175 506 # Copy content of rootcopy directory to union
slaxemulator@175 507 # $1 = data directory
slaxemulator@175 508 # $2 = union directory
slaxemulator@175 509 copy_rootcopy_content()
slaxemulator@175 510 {
slaxemulator@175 511 debug_log "copy_rootcopy_content" "$*"
slaxemulator@175 512
slaxemulator@175 513 if [ "$(ls -1 "$1/rootcopy/" 2>/dev/null)" != "" ]; then
slaxemulator@175 514 echo_green_star
slaxemulator@175 515 echo "Copying content of rootcopy directory..."
slaxemulator@175 516 cp -a "$1"/rootcopy/* "$2"
slaxemulator@175 517 fi
slaxemulator@175 518 }
slaxemulator@175 519
slaxemulator@175 520 # Copy data to RAM if requested
slaxemulator@175 521 # $1 = live data directory
slaxemulator@175 522 # $2 = changes directory
slaxemulator@175 523 #
slaxemulator@175 524 copy_to_ram()
slaxemulator@175 525 {
slaxemulator@175 526 debug_log "copy_to_ram" "$*"
slaxemulator@175 527
slaxemulator@175 528 local MDIR MDEV RAM CHANGES
slaxemulator@175 529
slaxemulator@175 530 if grep -vq toram /proc/cmdline; then
slaxemulator@175 531 echo "$1"
slaxemulator@175 532 return
slaxemulator@175 533 fi
slaxemulator@175 534
slaxemulator@175 535 echo "* Copying $LIVEKITNAME data to RAM..." >&2
slaxemulator@175 536 RAM="$(dirname "$2")"/toram
slaxemulator@175 537 mkdir -p "$RAM"
slaxemulator@175 538 cp -a "$1"/* "$RAM"
slaxemulator@175 539 echo "$RAM"
slaxemulator@175 540
slaxemulator@175 541 MDIR="$(mounted_dir "$1")"
slaxemulator@175 542 MDEV="$(mounted_device "$1")"
slaxemulator@175 543 MDEV="$(losetup $MDEV 2>/dev/null | cut -d " " -f 3)"
slaxemulator@175 544 umount "$MDIR" 2>/dev/null
slaxemulator@175 545
slaxemulator@175 546 if [ "$MDEV" ]; then # iso was mounted here, try to unmount the FS it resides on too
slaxemulator@175 547 MDEV="$(mounted_device "$MDEV")"
slaxemulator@175 548 umount "$MDEV" 2>/dev/null
slaxemulator@175 549 fi
slaxemulator@175 550 }
slaxemulator@175 551
slaxemulator@175 552 # load filter
slaxemulator@175 553 #
slaxemulator@175 554 filter_load()
slaxemulator@175 555 {
slaxemulator@175 556 local FILTER
slaxemulator@175 557 FILTER=$(cmdline_value load)
slaxemulator@175 558 if [ "$FILTER" = "" ]; then
slaxemulator@175 559 cat -
slaxemulator@175 560 else
slaxemulator@175 561 cat - | egrep "$FILTER"
slaxemulator@175 562 fi
slaxemulator@175 563 }
slaxemulator@175 564
slaxemulator@175 565 # noload filter
slaxemulator@175 566 #
slaxemulator@175 567 filter_noload()
slaxemulator@175 568 {
slaxemulator@175 569 local FILTER
slaxemulator@175 570 FILTER=$(cmdline_value load)
slaxemulator@175 571 if [ "$FILTER" = "" ]; then
slaxemulator@175 572 cat -
slaxemulator@175 573 else
slaxemulator@175 574 cat - | egrep -v "$FILTER"
slaxemulator@175 575 fi
slaxemulator@175 576 }
slaxemulator@175 577
slaxemulator@175 578 # sort modules by number even if they are in subdirectory
slaxemulator@175 579 #
slaxemulator@175 580 sortmod()
slaxemulator@175 581 {
slaxemulator@175 582 cat - | sed -r "s,(.*/(.*)),\\2:\\1," | sort -n | cut -d : -f 2-
slaxemulator@175 583 }
slaxemulator@175 584
slaxemulator@175 585 # Mount squashfs filesystem bundles
slaxemulator@175 586 # and add them to union
slaxemulator@175 587 # $1 = directory where to search for bundles
slaxemulator@175 588 # $2 = directory where to mount bundles
slaxemulator@175 589 # $3 = directory where union is mounted
slaxemulator@175 590 #
slaxemulator@175 591 union_append_bundles()
slaxemulator@175 592 {
slaxemulator@175 593 debug_log "union_append_bundles" "$*"
slaxemulator@175 594
slaxemulator@175 595 local BUN
slaxemulator@175 596
slaxemulator@175 597 echo_green_star
slaxemulator@175 598 echo "Adding bundles to union"
slaxemulator@175 599 ( ls -1 "$1" | sort -n ; cd "$1" ; find base/ 2>/dev/null ; find modules/ 2>/dev/null | sortmod | filter_load) | grep '[.]'$BEXT'$' | filter_noload | while read BUNDLE; do
slaxemulator@175 600 echo "* $BUNDLE"
slaxemulator@175 601 BUN="$(basename "$BUNDLE")"
slaxemulator@175 602 mkdir -p "$2/$BUN"
slaxemulator@175 603 mount -o loop -t squashfs "$1/$BUNDLE" "$2/$BUN"
slaxemulator@175 604 mount -n -o remount,add:1:$2/$BUN=rr aufs $3
slaxemulator@175 605 done
slaxemulator@175 606 }
slaxemulator@175 607
slaxemulator@175 608 # Create empty fstab properly
slaxemulator@175 609 # $1 = root directory
slaxemulator@175 610 #
slaxemulator@175 611 fstab_create()
slaxemulator@175 612 {
slaxemulator@175 613 debug_log "fstab_create" "$*"
slaxemulator@175 614
slaxemulator@175 615 local FSTAB
slaxemulator@175 616 FSTAB="$1/etc/fstab"
slaxemulator@175 617 echo aufs / aufs defaults 0 0 > $FSTAB
slaxemulator@175 618 echo proc /proc proc defaults 0 0 >> $FSTAB
slaxemulator@175 619 echo sysfs /sys sysfs defaults 0 0 >> $FSTAB
slaxemulator@175 620 echo devpts /dev/pts devpts gid=5,mode=620 0 0 >> $FSTAB
slaxemulator@175 621 echo tmpfs /dev/shm tmpfs defaults 0 0 >> $FSTAB
slaxemulator@175 622 }
slaxemulator@175 623
slaxemulator@175 624
slaxemulator@175 625 # Change root and execute init
slaxemulator@175 626 # $1 = where to change root
slaxemulator@175 627 #
slaxemulator@175 628 change_root()
slaxemulator@175 629 {
slaxemulator@175 630 debug_log "change_root" "$*"
slaxemulator@175 631
slaxemulator@175 632 umount /proc
slaxemulator@175 633 umount /sys
slaxemulator@175 634
slaxemulator@175 635 cd "$1"
slaxemulator@175 636
slaxemulator@175 637 # make sure important device files and directories are in union
slaxemulator@175 638 mkdir -p boot dev proc sys tmp mnt run
slaxemulator@175 639 chmod 1777 tmp
slaxemulator@175 640 if [ ! -e dev/console ]; then mknod dev/console c 5 1; fi
slaxemulator@175 641 if [ ! -e dev/tty ]; then mknod dev/tty c 5 0; fi
slaxemulator@175 642 if [ ! -e dev/tty0 ]; then mknod dev/tty0 c 4 0; fi
slaxemulator@175 643 if [ ! -e dev/tty1 ]; then mknod dev/tty1 c 4 1; fi
slaxemulator@175 644 if [ ! -e dev/null ]; then mknod dev/null c 1 3; fi
slaxemulator@175 645 if [ ! -e sbin/fsck.aufs ]; then ln -s /bin/true sbin/fsck.aufs; fi
slaxemulator@175 646
slaxemulator@175 647 # find chroot and init
slaxemulator@175 648 if [ -x bin/chroot ]; then CHROOT=bin/chroot; fi
slaxemulator@175 649 if [ -x sbin/chroot ]; then CHROOT=sbin/chroot; fi
slaxemulator@175 650 if [ -x usr/bin/chroot ]; then CHROOT=usr/bin/chroot; fi
slaxemulator@175 651 if [ -x usr/sbin/chroot ]; then CHROOT=usr/sbin/chroot; fi
slaxemulator@175 652 if [ "$CHROOT" = "" ]; then fatal "Can't find executable chroot command"; fi
slaxemulator@175 653
slaxemulator@175 654 if [ -x bin/init ]; then INIT=bin/init; fi
slaxemulator@175 655 if [ -x sbin/init ]; then INIT=sbin/init; fi
slaxemulator@175 656 if [ "$INIT" = "" ]; then fatal "Can't find executable init command"; fi
slaxemulator@175 657
slaxemulator@175 658 mkdir -p mnt/live
slaxemulator@175 659 mount -n -o remount,ro aufs .
slaxemulator@175 660 pivot_root . mnt/live
slaxemulator@175 661 exec $CHROOT . $INIT < dev/console > dev/console 2>&1
slaxemulator@175 662 }