summaryrefslogtreecommitdiffstats
path: root/liveinit
blob: a0067f87ae8a5b86ee0a72bd9354bac016145c83 (about) (plain) (blame)
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
#!/bin/ash
#
# Copyright 2004  Slackware Linux, Inc., Concord, CA, USA
# Copyright 2007, 2008, 2009, 2010, 2012  Patrick J. Volkerding, Sebeka, MN, USA
# Copyright 2015, 2016  Eric Hameleers, Eindhoven, NL
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
##################################################################################
# Changelog
# 10-Dec-2012 <mozes@slackware.com>
#  * Added support for the official Kernel parameters to select root filesystem
#    type ('rootfstype') and pause before attempting to mount the root filesystem
#    ('rootdelay').  The original parameters may continue to be used.
# 23-Oct-2015 <alien@slackware.com>
#  * Modified for booting as a Live filesystem.
##################################################################################

# The ISO creation script will create a filesystem with this label.
# Nevertheless, the user may have copied the ISO content to a different device.
MEDIALABEL="@MEDIALABEL@"

LIVEMAIN="@LIVEMAIN@"
PERSISTENCE="@PERSISTENCE@"

LIVEMEDIA=""

# By default, let the media determine if we can write persistent changes:
VIRGIN=0

# Used for debugging the init; set to '1' to enable explicit pauses>
DEBUG=0

# Masochists can copy the live environment into RAM:
TORAM=0

INITRD=$(cat /initrd-name)
WAIT=$(cat /wait-for-root)
KEYMAP=$(cat /keymap)
LUKSVOL=$(cat /luksdev)
INIT=/sbin/init

PATH="/sbin:/bin:/usr/sbin:/usr/bin"

# Mount /proc and /sys:
mount -n proc /proc -t proc
mount -n sysfs /sys -t sysfs
mount -n tmpfs /run -t tmpfs -o mode=0755

if grep devtmpfs /proc/filesystems 1>/dev/null 2>/dev/null ; then
  DEVTMPFS=1
  mount -n devtmpfs /dev -t devtmpfs
fi	

# Parse command line
for ARG in $(cat /proc/cmdline); do
  case $ARG in
    0|1|2|3|4|5|6|S|s|single)
      RUNLEVEL=$ARG
    ;;
    debug)
      DEBUG=1
    ;;
    debug=*)
      DEBUG=$(echo $ARG | cut -f2 -d=)
    ;;
    hostname=*)
      LIVE_HOSTNAME=$(echo $ARG | cut -f2 -d=)
    ;;
    init=*)
      INIT=$(echo $ARG | cut -f2 -d=)
    ;;
    kbd=*)
      KEYMAP=$(echo $ARG | cut -f2 -d=)
    ;;
    livemain=*)
      LIVEMAIN=$(echo $ARG | cut -f2 -d=)
    ;;
    livemedia=*)
      LIVEMEDIA=$(echo $ARG | cut -f2 -d=)
    ;;
    livepw=*)
      LIVEPW=$(echo $ARG | cut -f2 -d=)
    ;;
    load=*)
      LOAD=$(echo $ARG | cut -f2 -d=)
    ;;
    locale=*)
      LOCALE=$(echo $ARG | cut -f2 -d=)
    ;;
    luksvol=*)
      # Format: luksvol=file1[:/mountpoint1][,file1[:/mountpoint2],...]
      LUKSVOL=$(echo $ARG | cut -f2 -d=)
    ;;
    noload=*)
      NOLOAD=$(echo $ARG | cut -f2 -d=)
    ;;
    nop)
      VIRGIN=1
    ;;
    persistence=*)
      PERSISTENCE=$(echo $ARG | cut -f2 -d=)
    ;;
    rescue)
      RESCUE=1
    ;;
    swap)
      USE_SWAP=1
    ;;
    rootpw=*)
      ROOTPW=$(echo $ARG | cut -f2 -d=)
    ;;
    toram)
      TORAM=1
    ;;
    tz=*)
      TZ=$(echo $ARG | cut -f2 -d=)
    ;;
    waitforroot=*|rootdelay=*)
      WAIT=$(echo $ARG | cut -f2 -d=)
    ;;
    xkb=*)
      XKB=$(echo $ARG | cut -f2 -d=)
    ;;
  esac
done

[ $DEBUG -ge 2 ] && set -x

debugit () {
  [ $DEBUG -eq 0 ] && return
  echo "DEBUG>> -- blkid info -- :"
  blkid | while read LINE ; do echo "DEBUG>> $LINE" ; done
  echo "DEBUG>> -- mount info -- :"
  mount | while read LINE ; do echo "DEBUG>> $LINE" ; done
  echo "DEBUG>> -- Press ENTER to continue -- : "
  read JUNK
  return
}

# If udevd is available, use it to generate block devices
# else use mdev to read sysfs and generate the needed devices 
if [ -x /sbin/udevd -a -x /sbin/udevadm ]; then
  /sbin/udevd --daemon --resolve-names=never
  /sbin/udevadm trigger --subsystem-match=block --action=add
  /sbin/udevadm settle --timeout=10
else
  [ "$DEVTMPFS" != "1" ] && mdev -s
fi

# Load kernel modules (ideally this was already done by udev):
if [ ! -d /lib/modules/$(uname -r) ]; then
  echo "No kernel modules found for Linux $(uname -r)."
elif [ -x ./load_kernel_modules ]; then # use load_kernel_modules script:
  echo "${INITRD}:  Loading kernel modules from initrd image:"
  . ./load_kernel_modules 1>/dev/null 2>/dev/null
else # load modules (if any) in order:
  if ls /lib/modules/$(uname -r)/*.*o 1> /dev/null 2> /dev/null ; then
    echo "${INITRD}:  Loading kernel modules from initrd image:"
    for module in /lib/modules/$(uname -r)/*.*o ; do
      /sbin/modprobe $module 1>/dev/null 2>/dev/null
    done
    unset module
  fi
fi

# Sometimes the devices need extra time to be available.
# A root filesystem on USB is a good example of that.
sleep $WAIT
# Fire at least one blkid:
blkid 1>/dev/null 2>/dev/null

# Load a custom keyboard mapping:
if [ -n "$KEYMAP" ]; then
  echo "${INITRD}:  Loading '$KEYMAP' keyboard mapping:"
  tar xzOf /etc/keymaps.tar.gz ${KEYMAP}.bmap | loadkmap
fi

if [ "$RESCUE" = "" ]; then 
  # Initialize RAID:
  if [ -x /sbin/mdadm ]; then
    # If /etc/mdadm.conf is present, udev should DTRT on its own;
    # If not, we'll make one and go from there:
    if [ ! -r /etc/mdadm.conf ]; then
      /sbin/mdadm -E -s >/etc/mdadm.conf
      /sbin/mdadm -S -s
      /sbin/mdadm -A -s
      # This seems to make the kernel see partitions more reliably:
      fdisk -l /dev/md* 1> /dev/null 2> /dev/null
    fi
  fi

  # Scan for btrfs multi-device filesystems:
  if [ -x /sbin/btrfs ]; then
    /sbin/btrfs device scan
  fi

  # --------------------------------------------------------------------- #
  #                     SLACKWARE LIVE - START                            #
  # --------------------------------------------------------------------- #

  ## Support functions ##

  find_loop() {
    # The losetup of busybox is different from the real losetup - watch out!
    lodev=$(losetup -f)
    if [ -z "$lodev" ]; then
      # We exhausted the available loop devices, so create the block device:
      for NOD in $(seq 0 64); do
        if [ ! -b /dev/loop${NOD} ]; then
          mknod -m660 /dev/loop${NOD} b 7 ${NOD}
          break
        fi
      done
      lodev=/dev/loop${NOD}
    elif [ ! -b $lodev ]; then
      # We exhausted the available loop devices, so create the block device:
      mknod -m660 $lodev b 7 $(echo $lodev |sed %/dev/loop%%)
    fi
    echo "$lodev"
  }

  find_modloc() {
    MY_LOC="$1"

    if [ $TORAM -ne 0 ]; then
      # If we need to copy the module to RAM, we need a place for that:
      mkdir -p /mnt/live/toram
      # Copy the module to RAM before mounting it:
      MODNAME="$(basename ${MY_LOC})"
      cp ${MY_LOC} /mnt/live/toram
      MY_LOC=/mnt/live/toram/${MODNAME}
    fi

    echo "${MY_LOC}"
  }

  load_modules() {
    # SUBSYS can be 'system', 'addons', 'optional':
    SUBSYS="$1"

    if ls /mnt/media/${LIVEMAIN}/${SUBSYS}/*.sxz 1>/dev/null 2>/dev/null ; then
      for MODULE in /mnt/media/${LIVEMAIN}/${SUBSYS}/*.sxz ; do
        MODBASE="$(basename ${MODULE} .sxz)"
        if [ "$SUBSYS" = "optional" ]; then
          # Load one or more optionals by using boot parameter 'load':
          # load=mod1[,mod2[,mod3]]
          if [ -z "$LOAD" -o -z '$(echo ",${LOAD}," |grep -i ",$(echo $MODBASE |cut -d- -f2),")' ]; then
            continue
          fi
        elif [ "$SUBSYS" = "addons" ]; then
          # Skip loading one or more addons by using boot parameter 'noload':
          # noload=mod1[,mod2[,mod3]]
          if [ -n "$NOLOAD" -a -n '$(echo ",${NOLOAD}," |grep -i ",$(echo $MODBASE |cut -d- -f2),")' ]; then
            echo "$MODBASE" >> /mnt/live/modules/skipped
            continue
          fi
        fi
        MODLOC=$(find_modloc ${MODULE})
        mkdir /mnt/live/modules/${MODBASE}
        mount -t squashfs -o loop ${MODLOC} /mnt/live/modules/${MODBASE}
        RODIRS=":/mnt/live/modules/${MODBASE}${RODIRS}"
      done
    fi
  }

  ## End support functions ##

  # We need a mounted filesystem here to be able to do a switch_root later,
  # so we create one in RAM:
  mount -t tmpfs -o defaults none /mnt

  # Find the Slackware Live media.
  # TIP: Increase WAIT to give USB devices a chance to be seen by the kernel.
  mkdir /mnt/media
  if [ -z "$LIVEMEDIA" ]; then
    # LIVEMEDIA not specified on the boot commandline using "livemedia="
    # Filter out the block devices, only look at partitions at first:
    LIVEALL=$(blkid |grep LABEL="\"$MEDIALABEL\"" |cut -d: -f1 |grep "[0-9]$")
    LIVEMEDIA=$(blkid |grep LABEL="\"$MEDIALABEL\"" |cut -d: -f1 |grep "[0-9]$" |head -1)
    if [ ! -z "$LIVEMEDIA" ]; then
      # That was easy... we found the media straight away.
      # Determine filesystem type ('iso9660' means we found a CDROM/DVD)
      LIVEFS=$(blkid $LIVEMEDIA |rev |cut -d'"' -f2 |rev)
      mount -t $LIVEFS -o ro $LIVEMEDIA /mnt/media
    else
      LIVEALL=$(blkid |grep LABEL="\"$MEDIALABEL\"" |cut -d: -f1 |grep -v "[0-9]$")
      LIVEMEDIA=$(blkid |grep LABEL="\"$MEDIALABEL\"" |cut -d: -f1 |grep -v "[0-9]$" |head -1)
      if [ ! -z "$LIVEMEDIA" ]; then
        # We found a block device with the correct label (non-UEFI media).
        # Determine filesystem type ('iso9660' means we found a CDROM/DVD)
        LIVEFS=$(blkid $LIVEMEDIA |rev |cut -d'"' -f2 |rev)
        mount -t $LIVEFS -o ro $LIVEMEDIA /mnt/media
      else
        # Bummer... label not found; the ISO was extracted to a different device.
        # Separate partitions from block devices, look at partitions first:
        for SLDEVICE in $(blkid |cut -d: -f1 |grep "[0-9]$") $(blkid |cut -d: -f1 |grep -v "[0-9]$") ; do
          SLFS=$(blkid $SLDEVICE |rev |cut -d'"' -f2 |rev)
          mount -t $SLFS -o ro $SLDEVICE /mnt/media
          if [ -d /mnt/media/${LIVEMAIN} ]; then
            # Found our media!
            LIVEALL=$SLDEVICE
            LIVEMEDIA=$SLDEVICE
            LIVEFS=$(blkid $LIVEMEDIA |rev |cut -d'"' -f2 |rev)
            break
          else
            umount $SLDEVICE
            unset SLDEVICE
          fi
        done
      fi
    fi
    if [ -n "$LIVEMEDIA" ]; then
      # Gotcha!
      break
    fi
    sleep 1
  else
    # LIVEMEDIA was spcified on the boot commandline using "livemedia="
    LIVEALL="$LIVEMEDIA"
    LIVEFS=$(blkid $LIVEMEDIA |rev |cut -d'"' -f2 |rev)
    mount -t $LIVEFS -o ro $LIVEMEDIA /mnt/media
  fi

  # Finished determining the media availability, it should be mounted now.

  if [ ! -z "$LIVEMEDIA" ]; then
    echo "${INITRD}:  Live media found at ${LIVEMEDIA}."
    if [ ! -d /mnt/media/${LIVEMAIN} ]; then
      echo "${INITRD}:  However, live media was not mounted... trouble ahead."
    fi
    if [ "$LIVEMEDIA" != "$LIVEALL" ]; then
      echo "${INITRD}:  NOTE: Multiple partitions with '$MEDIALABEL' label were found ($(echo $LIVEALL))... success not guaranteed."
    fi
  else
    echo "${INITRD}:  No live media found... trouble ahead."
    echo "${INITRD}:  Try adding \"rootdelay=20\" to the boot command."
  fi

  debugit

  # Start assembling our live system components below /mnt/live :
  mkdir /mnt/live

  # Mount our squashed modules (.sxz extension).
  mkdir /mnt/live/modules

  if [ $TORAM -ne 0 ]; then
    echo "${INITRD}:  Copying Live modules to RAM, please be patient."
  fi

  # Modules were created in specific order and will be mounted in that order.
  # In the lowerdirs parameter for the overlay, the module with the highest
  # number (i.e. created last) will be leftmost in a colon-separated list:
  RODIRS=""

  # First, the base Slackware system components:
  load_modules system

  # Next, the add-on (3rd party etc) components, if any:
  # Remember, module name must adhere to convention: "NNNN-modname-*.sxz"
  # where 'N' is a digit and 'modname' must not contain a dash '-'.
  load_modules addons

  # And finally any explicitly requested optionals (like nvidia drivers):
  # Remember, module name must adhere to convention: "NNNN-modname-*.sxz"
  # where 'N' is a digit and 'modname' must not contain a dash '-'.
  load_modules optional

  # Get rid of the starting colon:
  RODIRS=$(echo $RODIRS |cut -c2-)

  if [ $TORAM -ne 0 ]; then
    echo "${INITRD}:  Live OS copied to RAM, you can remove the Live medium."
  fi

  # Setup persistence in case our media is writable, *and* the user
  # has created a directory "persistence" in the root of the media.
  # otherwise we let the block changes accumulate in RAM only.

  # Create the mount point for the writable upper directory of the overlay:
  # Assume the default to be a readonly media - we write to RAM:
  UPPERDIR=/mnt/live/changes
  OVLWORK=/mnt/live/.ovlwork
  if [ "$VIRGIN" = "0" ]; then
    if [ "LIVEFS" != "iso9660" -a -d /mnt/media/${PERSISTENCE} ]; then
      # Looks OK, but we need to remount the media in order to write
      # to the persistence directory:
      mount -o remount,rw /mnt/media
      # Try a write... just to be dead sure:
      if touch /mnt/media/${PERSISTENCE}/.rwtest 2>/dev/null && rm /mnt/media/${PERSISTENCE}/.rwtest 2>/dev/null ; then
        # Writable media and we are allowed to write to it.
        echo "${INITRD}:  Writing persistent changes to media directory '/${PERSISTENCE}'."
        UPPERDIR=/mnt/media/${PERSISTENCE}
        OVLWORK=/mnt/media/.ovlwork
      fi
    elif [ "LIVEFS" != "iso9660" -a -f /mnt/media/${PERSISTENCE}.img ]; then
      # Use a container file; the filesystem needs to be writable:
      mount -o remount,rw /mnt/media
      # Find a free loop device to mount the persistence container file:
      prdev=$(find_loop)
      prdir=${PERSISTENCE}_$(od -An -N1 -tu1 /dev/urandom |tr -d ' ')
      mkdir -p /mnt/live/${prdir}
      losetup $prdev /mnt/media/${PERSISTENCE}.img
      prfs=$(blkid $prdev |rev |cut -d'"' -f2 |rev)
      mount -t $prfs $prdev /mnt/live/${prdir}
      if [ $? -ne 0 ]; then
        echo "${INITRD}:  Failed to mount persistence file '/${PERSISTENCE}.img'."
        echo "${INITRD}:  Falling back to RAM."
      else
        echo "${INITRD}:  Writing persistent changes to file '/${PERSISTENCE}.img'."
        UPPERDIR=/mnt/live/${prdir}/${PERSISTENCE}
        OVLWORK=/mnt/live/${prdir}/.ovlwork
      fi
    fi
  elif [ ! -z "$LUKSVOL" ]; then
    # Even without persistence, we need to be able to write to the partition
    # if we are using a LUKS container file:
    mount -o remount,rw /mnt/media
  fi

  # Create the writable upper directory, plus the workdir which is required
  # for overlay to function (the two must be in the same POSIX filesystem):
  [ ! -d ${UPPERDIR} ] && mkdir -p ${UPPERDIR}
  [ ! -d ${OVLWORK} ] && mkdir -p ${OVLWORK}

  # Create the overlay of readonly and writable directories:
  mkdir -p /mnt/overlay
  mount -t overlay -o workdir=${OVLWORK},upperdir=${UPPERDIR},lowerdir=${RODIRS} overlay /mnt/overlay
  if [ $? -ne 0 -a "$VIRGIN" = "0" ]; then
    # Failed to create the persistent overlay - try without persistence:
    echo "${INITRD}:  Failed to create persistent overlay, attempting to continue in RAM."
    UPPERDIR=/mnt/live/changes
    OVLWORK=/mnt/live/.ovlwork
    mkdir -p ${UPPERDIR}
    mkdir -p ${OVLWORK}
    mount -t overlay -o workdir=${OVLWORK},upperdir=${UPPERDIR},lowerdir=${RODIRS} overlay /mnt/overlay
  fi

  debugit

  # Make the underpinning RAM fs accessible in the live system (for fun):
  mkdir -p /mnt/overlay/mnt/live
  mount --bind /mnt/live /mnt/overlay/mnt/live

  # Same for the Linux filesystem on the USB stick:
  mkdir -p /mnt/overlay/mnt/livemedia
  mount --bind /mnt/media /mnt/overlay/mnt/livemedia

  if [ ! -z "$USE_SWAP" ]; then
    # Use any available swap device:
    for SWAPD in $(blkid |grep TYPE="\"swap\"" |cut -d: -f1) ; do
      echo "${INITRD}:  Enabling swapping to '$SWAPD'"
      echo "$SWAPD  swap  swap defaults  0  0" >> /mnt/overlay/etc/fstab
    done
  fi

  if [ ! -z "$KEYMAP" ]; then
    # Configure custom keyboard mapping in console and X:
    echo "${INITRD}:  Switching live console to '$KEYMAP' keyboard"
    cat <<EOT > /mnt/overlay/etc/rc.d/rc.keymap
#!/bin/sh
# Load the keyboard map.  More maps are in /usr/share/kbd/keymaps.
if [ -x /usr/bin/loadkeys ]; then
 /usr/bin/loadkeys ${KEYMAP}
fi
EOT
    chmod 755 /mnt/overlay/etc/rc.d/rc.keymap
  fi
  if [ ! -z "$KEYMAP" -o ! -z "$XKB" ]; then
    # Set a keyboard mapping in X.Org, derived from the console map if needed:
    # Variable XKB can be set to "XkbLayout,XkbVariant", like "xkb=ch,fr"
    # You can set just the XkbVariant by adding something like "kbd=ch xkb=,fr"
    XKBLAYOUT=$(echo $XKB |cut -d, -f1)
    XKBVARIANT=$(echo $XKB |cut -d, -f2)
    XKBOPTIONS="compose:ralt"
    # Ensure that XKBLAYOUT gets a value; XKBVARIANT is allowed to be empty.
    if [ -z "$XKBLAYOUT" ]; then
      if [ -z "$KEYMAP" ]; then
        XKBLAYOUT="us"
      else
        XKBLAYOUT="$(echo $KEYMAP |cut -c1-2)"
      fi
    fi
  else
    XKBLAYOUT="us"
  fi
  echo "${INITRD}:  Switching live X desktop to '$XKBLAYOUT' keyboard"
  # If the layout is not 'us' then add 'us' as a secondary nevertheless:
  if [ "$XKBLAYOUT" != "us" ]; then
    XKBLAYOUT="$XKBLAYOUT,us"
    XKBVARIANT="$XKBVARIANT,"
    XKBOPTIONS="grp:alt_shift_toggle,$XKBOPTIONS"
  fi
  mkdir -p /mnt/overlay/etc/X11/xorg.conf.d
  cat <<EOT > /mnt/overlay/etc/X11/xorg.conf.d/30-keyboard.conf
Section "InputClass"
  Identifier "keyboard-all"
  Driver "evdev"
  Option "XkbLayout" "$XKBLAYOUT"
  Option "XkbVariant" "$XKBVARIANT"
  Option "XkbOptions" "$XKBOPTIONS"
  MatchIsKeyboard "on"
EndSection
EOT

  if [ ! -z "$LOCALE" ]; then
    # Configure custom locale:
    echo "${INITRD}:  Switching to '$LOCALE' locale"
    sed -i -e "s/^ *export LANG=.*/export LANG=${LOCALE}/" /mnt/overlay/etc/profile.d/lang.sh
  fi

  if [ ! -z "$TZ" -a -f /mnt/overlay/usr/share/zoneinfo/${TZ} ]; then
    # Configure custom timezone:
    echo "${INITRD}:  Configuring timezone '$TZ'"
    cp /mnt/overlay/usr/share/zoneinfo/${TZ} /mnt/overlay/etc/localtime
    cp /mnt/overlay/usr/share/zoneinfo/${TZ} /mnt/overlay/etc/localtime
    rm /mnt/overlay/etc/localtime-copied-from
    ln -s /usr/share/zoneinfo/${TZ} /mnt/overlay/etc/localtime-copied-from
    # Configure the hardware clock to be interpreted as localtime and not UTC:
    cat <<EOT > /mnt/overlay/etc/hardwareclock
# /etc/hardwareclock
#
# Tells how the hardware clock time is stored.
# You should run timeconfig to edit this file.
localtime
EOT
  fi

  if [ ! -z "$LIVEPW" ]; then
    # User entered a custom live password on the boot commandline:
    echo "${INITRD}:  Changing password for user 'live'."
    chroot /mnt/overlay /usr/sbin/chpasswd <<EOPW
live:${LIVEPW}
EOPW
  fi

  if [ ! -z "$ROOTPW" ]; then
    # User entered a custom root password on the boot commandline:
    echo "${INITRD}:  Changing password for user 'root'."
    chroot /mnt/overlay /usr/sbin/chpasswd <<EOPW
root:${ROOTPW}
EOPW
  fi

  if [ ! -z "$LIVE_HOSTNAME" ]; then
    # User entered a custom hostname on the boot commandline:
    echo "${INITRD}:  Changing hostname to '$LIVE_HOSTNAME'."
    echo "${LIVE_HOSTNAME}.example.net" > /mnt/overlay/etc/HOSTNAME
    if [ -f /mnt/overlay/etc/NetworkManager/NetworkManager.conf ]; then
      sed -i -e "s/^hostname=.*/hostname=${LIVE_HOSTNAME}/" \
        /mnt/overlay/etc/NetworkManager/NetworkManager.conf
    fi
    sed -i -e "s/^\(127.0.0.1\t*\)@DARKSTAR@.*/\1${LIVE_HOSTNAME}.example.net ${LIVE_HOSTNAME}/" /mnt/overlay/etc/hosts
  fi

  # Delete ALSA state file, the Live OS may be booted on different computers:
  rm -f /mnt/overlay/var/lib/alsa/asound.state

  # Copy contents of rootcopy directory (may be empty) to overlay:
  cp -af /mnt/media/${LIVEMAIN}/rootcopy/* /mnt/overlay/ 2>/dev/null

  # Bind any LUKS container into the Live filesystem:
  if [ ! -z "$LUKSVOL" ]; then
    for luksvol in $(echo $LUKSVOL |tr ',' ' '); do
      luksfil="$(echo $luksvol |cut -d: -f1)"
      luksmnt="$(echo $luksvol |cut -d: -f2)"
      luksnam="$(echo $(basename $luksfil) |tr '.' '_')"
      if [ "$luksmnt" = "$luksfil" ]; then
        # No optional mount point specified, so we use the default: /home/
        luksmnt="/home"
      fi

      # Find a free loop device:
      lodev=$(find_loop)

      losetup $lodev /mnt/media/$luksfil
      echo "Unlocking LUKS encrypted container '$luksfil' at mount point '$luksmnt'"
      cryptsetup luksOpen $lodev $luksnam </dev/tty0 >/dev/tty0 2>&1
      if [ $? -ne 0 ]; then
        echo "${INITRD}:  Failed to unlock LUKS container '$luksfil'... trouble ahead."
      else
        # Create the mount directory if it does not exist (unlikely):
        mkdir -p /mnt/overlay/$luksmnt

        # Let Slackware mount the unlocked container:
        luksfs=$(blkid /dev/mapper/$luksnam |rev |cut -d'"' -f2 |rev)
        if ! grep -q /dev/mapper/$luksnam /mnt/overlay/etc/fstab ; then
          echo "/dev/mapper/$luksnam  $luksmnt  $luksfs  defaults  1  1" >> /mnt/overlay/etc/fstab
        fi
        # On shutdown, ensure that the container gets locked again:
        if ! grep -q "$luksnam $luksmnt" /mnt/overlay/etc/crypttab ; then
          echo "$luksnam $luksmnt" >> /mnt/overlay/etc/crypttab
        fi
      fi
    done
  fi

  # --------------------------------------------------------------------- #
  #                     SLACKWARE LIVE - !END!                            #
  # --------------------------------------------------------------------- #

  # Minimal changes to the original Slackware init follow:

  # Switch to real root partition:
  /sbin/udevadm settle --timeout=10
  echo 0x0100 > /proc/sys/kernel/real-root-dev
  
  if [ ! -r /mnt/overlay/${INIT} ]; then
    echo "ERROR:  No ${INIT} found on rootdev (or not mounted).  Trouble ahead."
    echo "        You can try to fix it. Type 'exit' when things are done." 
    echo
    /bin/sh
  fi
else
  echo
  echo "RESCUE mode"
  echo
  echo "        You can try to fix or rescue your system now. If you want"
  echo "        to boot into your fixed system, mount your root filesystem"
  echo "        read-only under /mnt:"
  echo
  echo "            # mount -o ro -t filesystem root_device /mnt"
  echo
  echo "        Type 'exit' when things are done."
  echo
  /bin/sh
fi

# Need to make sure OPTIONS+="db_persist" exists for all dm devices
# That should be handled in /sbin/mkinitrd now
/sbin/udevadm info --cleanup-db
/sbin/udevadm control --exit

unset ERR
umount /proc
umount /sys
umount /run
# We disable the filesystem checking code in /etc/rc.d/rc.S,
# so we need to keep the fs writable here.
#mount -o ro,remount /mnt/overlay 2>/dev/null
echo "${INITRD}:  Slackware Live system is ready."

echo "${INITRD}:  exiting"
exec switch_root /mnt/overlay $INIT $RUNLEVEL