File: ocs-live-hook-functions

package info (click to toggle)
clonezilla 5.12.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,580 kB
  • sloc: sh: 41,014; perl: 193; python: 59; makefile: 26
file content (990 lines) | stat: -rwxr-xr-x 47,266 bytes parent folder | download
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
#!/bin/bash
# Author: Steven Shiau <steven _at_ clonezilla org>
# License: GPL
# Program to create Clonezilla live.

# functions
set_localepurge(){
  # Create en_US.UTF-8, the dialog in clonezilla need that in console.
  # Otherwise dialog will be distorted.
  if [ -z "$(localedef --list-archive | grep -iw "en_US.utf8")" ] && \
     [ -z "$(unalias ls 2>/dev/null; ls /usr/lib/locale | grep -iw "en_US.utf8")" ] 
  then
    localedef -f UTF-8 -i en_US en_US.UTF-8
  fi
  # Keep some locales.
  echo "Preseeding nonepurge locales before install localepurge..."
  # Ref: http://bugs.debian.org/724491
  debconf-set-selections <<EOF
localepurge     localepurge/dontbothernew       boolean false
localepurge     localepurge/mandelete   boolean true
localepurge     localepurge/none_selected       boolean false
localepurge     localepurge/nopurge     multiselect     $locale_to_keep
localepurge     localepurge/quickndirtycalc     boolean true
localepurge     localepurge/remove_no   boolean false
localepurge     localepurge/use-dpkg-feature    boolean false
localepurge     localepurge/showfreedspace      boolean true
localepurge     localepurge/verbose     boolean false
EOF
  # Install localepurge
  echo "Installing localepurge..."
  LC_ALL=C apt-get -y install localepurge
  echo "The content of /etc/locale.nopurge:"
  cat /etc/locale.nopurge
  # Run it
  echo "Purging locale files..."
  LC_ALL=C localepurge
} # end of set_localepurge
#
locales_gen() {
  local lang_gen_conf_file locale_pkg locale_to_keep_wo_comma
  # First we put a copy of en_US locale setting in the prebuild environment, so that if "nolocales" is used in boot param, the basic environment will be ok.
  # The settings can be overwritten by 14locales in live-initramfs if "nolocales" is not used in boot param. 
  printf 'LANG="%s"\n' "en_US.UTF-8" >> /etc/default/locale
  # The locale_to_keep is like: "C.UTF-8, en_US.UTF-8, zh_TW, zh_TW.UTF-8, fr_FR.UTF-8, ja_JP.UTF-8". We do not need "," here
  locale_to_keep_wo_comma="$(echo $locale_to_keep | sed -e "s/,//g")"
  # There are 2 locales package: 
  # (1) locales 
  # (2) belocs-locales-bin/belocs-locales-data (belocs.alioth.debian.org)
  # Decide the config file
  locale_pkg="$(LANG=C dpkg -S `command -v locale-gen`)"
  if [ -n "$(echo $locale_pkg | grep -E "^belocs-locales")" ]; then
    lang_gen_conf_file="/var/lib/locales/supported.d/local"
  else
    lang_gen_conf_file="/etc/locale.gen"
  fi
  for i in $locale_to_keep_wo_comma; do
    # Skip those C, zh_TW..., we only deal with those with UTF-8
    [ "$i" = "C.UTF-8" ] && continue
    [ -z "$(echo $i | grep -iE "UTF-8")" ] && continue
    if [ -z "$(grep -iE "^[[:space:]]*$i" $lang_gen_conf_file 2>/dev/null)" ]; then
     echo -n "Appending $i to $lang_gen_conf_file... "
     echo "$i UTF-8" >> $lang_gen_conf_file
     echo "done!"
    fi
  done
  # This is a workaround for Ubuntu jaunty, which changes back to use locales but still respect /var/lib/locales/supported.d/locale (without honoring /etc/locales.gen) for backward compatibility.
  if [ -e /etc/locale.gen -a -d /var/lib/locales/supported.d ]; then
    (cd /var/lib/locales/supported.d; ln -fs /etc/locale.gen local)
  fi
  locale-gen
} # end of locales_gen

# set root passwd, I do not like root without passwd.
set_root_passwd() {
  local passwd=""
  case "$root_passwd_opt" in
    "random")
      # Note! Do not use -y (--symbols, Include at least one special character in the password) with pwgen, since it might contain ' or ", which will cause problem when run in shell and pipe to chpasswd
      passwd="$(pwgen -s -c $random_passwd_length 1)"
      echo "Using random password \"$passwd\" with length $random_passwd_length for account root..."
      ;;
    "none")
      passwd=""
      echo "No password for account root..."
      ;;
    *)
      passwd="$root_passwd_def"
      echo "Using password \"$passwd\" for account root..."
      ;;
  esac
  if [ -n "$passwd" ]; then
    echo "root:$passwd" | chpasswd
  fi
}

#
preseed_autologin_account() {
  # add the account user with UID=1000 first, otherwise if we add accounts later, for example, when Debian live boots, scripts/casper-bottom/10adduser will kill casper account first (if it's not UID=1000, 10adduser can remove that successfully), then when it try to add it, it will fail. Then there is no casper account at all. This will result client's /sbin/casper-getty fails in /etc/inittab. Therefore we can not login in console 1-6.
  # The password (live) of auto login (say casper) is creaetd runtime when Debian Live boots, 
  # in /usr/share/initramfs-tools/scripts/casper-bottom/10adduser (casper) or
  # /usr/share/initramfs-tools/scripts/live-bottom/10adduser (live-initramfs), 
  # there is:
  # user_crypted="8Ab05sVQ4LLps" # as in `echo "live" | mkpasswd -s`
  # Therefore it's useless to assign password here. Skip setting password.
  useradd -m -u $autologin_account_uid -s /bin/bash $autologin_account
}

# Put the clonezilla live script in rc2.d
# In the past the reason we put start scripts in rcS.d instead of rc2.d is because the upstart in Ubuntu will enter command line prompt in rc2.d.
# Now since there is no such problem, we put them in rc2.d.
cp_ocs_live_startup_to_rc.d() {
  if is_systemd_init; then
    # Systemd
    # (1) Prepare the clonezilla live starting service
    # (2) Prepare autologin for tty*, serial console [Ref: https://wiki.archlinux.org/index.php/automatic_login_to_virtual_console]
    cp -af /live-hook-dir/systemd/ocs-live/start-ocs-live.service /lib/systemd/system/; chown root:root /lib/systemd/system/start-ocs-live.service
    ( cd /etc/systemd/system/getty.target.wants/; ln -fs /lib/systemd/system/start-ocs-live.service .)
    # Disable the booting status, otherwise the messages might be shown on the dialog menu (of keyboard/language for Clonezilla/GParted live) which is annoying.
    perl -pi -e "s/^[#]ShowStatus=.*/ShowStatus=no/g" /etc/systemd/system.conf
    for i in `seq 1 6`; do
      # tty1-6
      mkdir /etc/systemd/system/getty\@tty${i}.service.d/
      cp -af /live-hook-dir/systemd/ocs-live/tty-autologin-override.conf /etc/systemd/system/getty\@tty${i}.service.d/override.conf
      chown root:root /etc/systemd/system/getty\@tty${i}.service.d/override.conf
    done
    # Serial console
    for i in `seq 0 3`; do
      # ttyS0-3
      mkdir /etc/systemd/system/serial-getty\@ttyS${i}.service.d/
      cp -af /live-hook-dir/systemd/ocs-live/serial-console-autologin.conf /etc/systemd/system/serial-getty\@ttyS${i}.service.d/autologin.conf
      chown root:root /etc/systemd/system/serial-getty\@ttyS${i}.service.d/autologin.conf
      # ttyAMA0-3 (ARM64)
      mkdir /etc/systemd/system/serial-getty\@ttyAMA${i}.service.d/
      cp -af /live-hook-dir/systemd/ocs-live/serial-console-autologin.conf /etc/systemd/system/serial-getty\@ttyAMA${i}.service.d/autologin.conf
      chown root:root /etc/systemd/system/serial-getty\@ttyAMA${i}.service.d/autologin.conf
    done
    # Put the script "start-ocs-live" and "stop-ocs-live" to /etc/ocs/
    cp -af /live-hook-dir/start-ocs-live /live-hook-dir/stop-ocs-live /etc/ocs/
  elif [ -d "/etc/init" ] && dpkg -L upstart &>/dev/null; then
    # Ubuntu's upstart. We use the compatibility mode of sysv-init in upstart
    cp -af /live-hook-dir/start-ocs-live /etc/rc2.d/S99start-ocs-live; chown root:root /etc/rc2.d/S99start-ocs-live
    cp -af /live-hook-dir/stop-ocs-live /etc/rc0.d/K19stop-ocs-live; chown root:root /etc/rc0.d/K19stop-ocs-live
    cp -af /live-hook-dir/stop-ocs-live /etc/rc6.d/K19stop-ocs-live; chown root:root /etc/rc6.d/K19stop-ocs-live
  elif type insserv &>/dev/null; then
    # This has to be 2nd (i.e. in elif, since ubuntu also has insserv, but it is not enabled)
    install -o root -g root -m 755 /live-hook-dir/start-ocs-live /etc/init.d/
    install -o root -g root -m 755 /live-hook-dir/stop-ocs-live /etc/init.d/
    insserv start-ocs-live
    insserv stop-ocs-live
  else
    cp -af /live-hook-dir/start-ocs-live /etc/rc2.d/S99start-ocs-live; chown root:root /etc/rc2.d/S99start-ocs-live
    cp -af /live-hook-dir/stop-ocs-live /etc/rc0.d/K19stop-ocs-live; chown root:root /etc/rc0.d/K19stop-ocs-live
    cp -af /live-hook-dir/stop-ocs-live /etc/rc6.d/K19stop-ocs-live; chown root:root /etc/rc6.d/K19stop-ocs-live
  fi
} # end of cp_ocs_live_startup_to_rc.d

# Put the drbl live script in rcS.d
# The reason we put in rcS.d instead of rc2.d is because it's easier to do so before gdm is started.
cp_drbl_live_startup_to_rc.d() {
  if is_systemd_init; then
    # Systemd
    # (1) Prepare the drbl live starting service
    # (2) Prepare autologin for tty*, serial console [Ref: https://wiki.archlinux.org/index.php/automatic_login_to_virtual_console]
    cp -af /live-hook-dir/systemd/drbl-live/start-drbl-live.service /lib/systemd/system/; chown root:root /lib/systemd/system/start-drbl-live.service
    ( cd /etc/systemd/system/getty.target.wants/; ln -fs /lib/systemd/system/start-drbl-live.service .)
    # Disable the booting status, otherwise the messages might be shown on the dialog menu (of keyboard/language for Clonezilla/GParted live) which is annoying.
    perl -pi -e "s/^[#]ShowStatus=.*/ShowStatus=no/g" /etc/systemd/system.conf
    for i in `seq 1 6`; do
      # tty1-6
      mkdir /etc/systemd/system/getty\@tty${i}.service.d/
      cp -af /live-hook-dir/systemd/drbl-live/tty-autologin-override.conf /etc/systemd/system/getty\@tty${i}.service.d/override.conf
      chown root:root /etc/systemd/system/getty\@tty${i}.service.d/override.conf
    done
    for i in `seq 0 3`; do
      # ttyS0-3
      mkdir /etc/systemd/system/serial-getty\@ttyS${i}.service.d/
      cp -af /live-hook-dir/systemd/drbl-live/serial-console-autologin.conf /etc/systemd/system/serial-getty\@ttyS${i}.service.d/autologin.conf
      chown root:root /etc/systemd/system/serial-getty\@ttyS${i}.service.d/autologin.conf
    done
    # Put the script "start-drbl-live" and "stop-drbl-live" to /etc/drbl/
    cp -af /live-hook-dir/start-drbl-live /live-hook-dir/stop-drbl-live /etc/drbl/
  elif [ -d "/etc/init" ] && dpkg -L upstart &>/dev/null; then
    # Ubuntu's upstart. We use the compatibility mode of sysv-init in upstart
    cp -af /live-hook-dir/start-drbl-live /etc/rcS.d/S97start-drbl-live; chown root:root /etc/rcS.d/S97start-drbl-live
    cp -af /live-hook-dir/stop-drbl-live /etc/rc0.d/K19stop-drbl-live; chown root:root /etc/rc0.d/K19stop-drbl-live
    cp -af /live-hook-dir/stop-drbl-live /etc/rc6.d/K19stop-drbl-live; chown root:root /etc/rc6.d/K19stop-drbl-live
  elif type insserv &>/dev/null; then
    # This has to be 2nd (i.e. in elif, since ubuntu also has insserv, but it is not enabled)
    install -o root -g root -m 755 /live-hook-dir/start-drbl-live /etc/init.d/
    install -o root -g root -m 755 /live-hook-dir/stop-drbl-live /etc/init.d/
    insserv start-drbl-live
    insserv stop-drbl-live
  else
    cp -af /live-hook-dir/start-drbl-live /etc/rcS.d/S97start-drbl-live; chown root:root /etc/rcS.d/S97start-drbl-live
    cp -af /live-hook-dir/stop-drbl-live /etc/rc0.d/K19stop-drbl-live; chown root:root /etc/rc0.d/K19stop-drbl-live
    cp -af /live-hook-dir/stop-drbl-live /etc/rc6.d/K19stop-drbl-live; chown root:root /etc/rc6.d/K19stop-drbl-live
  fi
}

# clean unnecessary backup file to save space
clean_unnecessary_backup_file_in_boot() {
  local orphan_deb
  # (1)
  rm -f /boot/initrd*.bak
  # (2)
  # On AMD64 arch, if libc6-i386 is installed, it will be listed. While we need that so that 32-bit program could run on it. Therefore exclude it.
  # orphan_deb="$(deborphan -n -e libc6-i386)"
  # orphan_deb="$(echo $orphan_deb)"  # put it in one line
  # if [ -n "$orphan_deb" ]; then
  #   echo "Removing orphan packages..."
  #   apt-get --yes --purge remove $orphan_deb
  # fi
  # (3)
  # Thess files in dir /var/lib/apt/lists/ are like:
  # free.nchc.org.tw_debian_dists_etch_main_binary-i386_Packages
  # free.nchc.org.tw_debian-security_dists_etch_updates_main_binary-i386_Packages
  # free.nchc.org.tw_debian_dists_squeeze_main_i18n_Translation-zh
  # free.nchc.org.tw_debian_dists_squeeze_main_binary-i386_Packages
  # free.nchc.org.tw_debian_dists_squeeze_main_source_Sources
  # free.nchc.org.tw_debian_dists_squeeze_non-free_binary-i386_Packages
  # free.nchc.org.tw_debian_dists_squeeze_non-free_source_Sources
  # free.nchc.org.tw_debian_dists_squeeze_Release
  # free.nchc.org.tw_debian_dists_squeeze_Release.gpg
  # free.nchc.org.tw_debian_dists_squeeze-updates_main_binary-i386_Packages
  # free.nchc.org.tw_drbl-core_dists_drbl_stable_binary-i386_Packages
  # free.nchc.org.tw_drbl-core_dists_drbl_testing_binary-i386_Packages
  # free.nchc.org.tw_drbl-core_dists_drbl_unstable_binary-i386_Packages
  rm -f /var/lib/apt/lists/*_Packages*
  rm -f /var/lib/apt/lists/*_Source*
  rm -f /var/lib/apt/lists/*_Release*
  rm -f /var/lib/apt/lists/*_Translation*
  # (4) lock file. Thanks to Louie Chen.
  rm -f /var/lib/apt/lists/lock
}

# since ssh services is on, and account is known for the whole world, we have to block it.
block_all_clients_by_tcpwrapper() {
  echo 'ALL: ALL EXCEPT localhost' >> /etc/hosts.deny
}

# force to load fuse in live CD to avoid if sshfs is used, no /dev/fuse.
append_mod_in_etc_modules() {
  for imod in $mod_loaded_at_startup; do
    echo "$imod" >> /etc/modules
  done
}

#
clean_ocs_hook_files_in_chroot() {
  # all the file name including ocs and in the / in chroot will be removed.
  echo "Removing DRBL/Clonezilla live related hook files in chroot..."
  find ./ -maxdepth 1 -name "*ocs*" -type f -exec rm -fv {} \;
  find ./ -maxdepth 1 -name "*-live-hook*" -type f -exec rm -fv {} \;
  # drbl.conf is special, remove manually
  [ -f /drbl.conf ] && rm -fv /drbl.conf
}
#
append_start_clonezilla_in_user_bash_profile(){
  local auto_login_id_home
  auto_login_id_home="$(bash -c "echo ~$autologin_account")"
  cat <<-PROFILE_END >> $auto_login_id_home/.bash_profile
# added by Clonezilla live
clear
# start clonezilla
sudo [ -x /sbin/ocs-live-run-menu ] && sudo /sbin/ocs-live-run-menu
PROFILE_END
  chown ${autologin_account}:${autologin_account} $auto_login_id_home/.bash_profile
}
#
remove_start_stop_daemon_diverts(){
  # we have to use the real start-stop-daemon, therefore remove cdebootstrap-helper-diverts
  # live:~# ls -alF /sbin/start-stop-daemon*
  # -rwxr-xr-x 1 root root    10 2006-10-20 16:07 /sbin/start-stop-daemon*
  # -rwxr-xr-x 1 root root 18504 2007-01-01 23:02 /sbin/start-stop-daemon.REAL*
  # By doing apt-get --purge remove cdebootstrap-helper-diverts, it will
  # Removing `diversion of /sbin/start-stop-daemon to /sbin/start-stop-daemon.REAL by cdebootstrap-helper-diverts'
  # Removing `diversion of /usr/sbin/invoke-rc.d to /usr/sbin/invoke-rc.d.REAL by cdebootstrap-helper-diverts'
  if [ -e /usr/sbin/start-stop-daemon.REAL ]; then
    apt-get -y --purge remove cdebootstrap-helper-diverts
  fi
  # For live-build v2.x, the file name is /sbin/start-stop-daemon.orig, and no more cdebootstrap-helper-diverts in lb_chroot_dpkg. We just restore it. Otherwise the file start-stop-daemon copied to /tftpboot/node_root/sbin/ will be the temp fake one.
  if [ -e /usr/sbin/start-stop-daemon.orig ]; then
    mv -v /usr/sbin/start-stop-daemon.orig /sbin/start-stop-daemon
  fi
  # For live-build v3.x, the file name is renamed as start-stop-daemon.distrib by
  # dpkg-divert in /usr/lib/live/build/chroot_dpkg:
  # dpkg-divert --rename --add /sbin/start-stop-daemon
  # Adding 'local diversion of /sbin/start-stop-daemon to /sbin/start-stop-daemon.distrib'
  # root@debian:/sbin# ls -alFh start-stop-da*
  # -rwxr-xr-x 1 root root 27K Mar 18 06:16 start-stop-daemon.distrib*
  lb_3_start_stop_daemon_revert_flag="false"
  if [ -e /usr/sbin/start-stop-daemon.distrib ]; then
    # Remove the existing text exec script file, otherwise dpkg-divert won't revert.
    # Then dpkg-divert will rename /sbin/start-stop-daemon.distrib as /sbin/start-stop-daemon
    # //NOTE// For live-build v3.x, after drblpush, we have to revert the status to fake, temp one, so the rest of chroot_dpkg command won't remove the real /sbin/start-stop-daemon.
    rm -f /usr/sbin/start-stop-daemon
    dpkg-divert --rename --remove /usr/sbin/start-stop-daemon
    lb_3_start_stop_daemon_revert_flag="true"
  fi
  # begin-remove-after: released:trixie
  # The diversion may be duplicated by live-build due to the target having been
  # moved to /usr/sbin. See DEP17 P3 M18.
  lb_3_start_stop_daemon_revert_aliased_flag="false"
  if [ -e /sbin/start-stop-daemon.distrib.usr-is-merged ]; then
    rm -f /sbin/start-stop-daemon
    dpkg-divert --rename --remove /sbin/start-stop-daemon
    lb_3_start_stop_daemon_revert_aliased_flag="true"
  fi
  # end-remove-after
}
#
set_start_stop_daemon_diverts(){
  # For live-build v3.x, after drblpush, we have to revert the status to fake, temp one, so the rest of chroot_dpkg command won't remove the real /sbin/start-stop-daemon.
  # Ref: /usr/lib/live/build/chroot_dpkg
  # begin-remove-after: released:trixie
  if [ "$lb_3_start_stop_daemon_revert_aliased_flag" = "true" ]; then
    if dpkg -D /sbin/start-stop-daemon >/dev/null 2>&1; then
      dpkg-divert --rename --add --divert /sbin/start-stop-daemon.distrib.usr-is-merged /sbin/start-stop-daemon
    else
      dpkg-divert --no-rename --add --divert /sbin/start-stop-daemon.distrib.usr-is-merged /sbin/start-stop-daemon
    fi
  fi
  # end-remove-after
  if [ "$lb_3_start_stop_daemon_revert_flag" = "true" ]; then
    dpkg-divert --rename --add /usr/sbin/start-stop-daemon
    cat > /usr/sbin/start-stop-daemon << EOF
#!/bin/sh

exit 0
EOF
    chmod 755 /usr/sbin/start-stop-daemon
  fi
  # begin-remove-after: released:trixie
  if [ "$lb_3_start_stop_daemon_revert_aliased_flag" = "true" ] && [ "$lb_3_start_stop_daemon_revert_flag" != "true" ]; then
    cat > /sbin/start-stop-daemon << EOF
#!/bin/sh

exit 0
EOF
    chmod 755 /sbin/start-stop-daemon
  fi
  # end-remove-after
}
#
remove_service_in_system() {
  local srv="$1"
  if [ -z "$srv" ]; then
    echo "No service is assigned! Function remove_service_in_system terminated!"
    return 1
  fi
  # Get the "$DISTRIB_ID"
  [ -e /etc/lsb-release ] && . /etc/lsb-release
  # //NOTE// Both /etc/init/$srv.conf and /etc/init.d/$srv might exist, e.g. for openssh-server 1:6.2p2-6 on Debian Jessie, both /etc/init/ssh.conf and /etc/init.d/ssh exist. Therefore we have to disable /etc/init/ssh.conf and /etc/rc2.d/*ssh
  # For upstart (>1.3) service, we can use override file (Ref: http://upstart.ubuntu.com/cookbook/#override-files)
  if [ -e "/etc/init/$srv.conf" ]; then
    echo "Disabling auto start service $srv in /etc/init/..."
    echo "manual" > /etc/init/$srv.override
  fi
  if is_systemd_init; then
    # For systemd service
    systemctl disable $srv
  elif [ -e "/etc/init.d/$srv" ]; then
    # For sysV service
    if [ "$DISTRIB_ID" = "Ubuntu" ]; then
      # Ubuntu
      # For Ubuntu system (even 11.04), we keep using update-rc.d. Otherwise if we use insserv it will confuse upstart and init.d system. E.g. The "halt" in rc0.d might be S03, and others in S04, like:
      # K01gpm            README                S03networking    S03umountroot
      # K01live-config    S01cryptdisks-early  S03sendsigs      S04live-boot
      # K01pcscd          S02cryptdisks        S03umountfs      S04urandom
      # K19stop-ocs-live*  S03halt              S03umountnfs.sh
      update-rc.d -f $srv remove
    else
      # Debian
      echo "Removing service $srv by \"insserv -r $srv\"..."
      if type insserv &>/dev/null; then
        insserv -r $srv
      else
        update-rc.d -f $srv remove
      fi
    fi
  else
    echo "Service $srv not found! Skip removing service $srv."
  fi
} # end of remove_service_in_system
#
fix_ubuntu_upstart_tty1_6_distorted_if_necessary() {
  # Ref: https://bugs.launchpad.net/ubuntu/+source/upstart/+bug/65230
  # For ubuntu 6.10-9.04 upstart problem (upstart < 0.6.3, for >=0.6.3, no such bug).
  # Change the contents of /etc/event.d/tty[1-6] from:
  # start on runlevel 2
  # ->
  # start on stopped rc2
  # Here we assume upstart <= 0.6.3, /etc/event.d/rc-default exists, and
  # upstart >= 0.6.3, /etc/init/rc-sysinit.conf exists
  [ ! -e /etc/event.d/rc-default ] && return 9
  for i in /etc/event.d/tty[0-9]; do
    perl -pi -e "s/^start on runlevel /start on stopped rc/g" $i
  done
}
#
copy_all_dev_id_prog_from_udev_lib() {
  # This function is for newer Ubuntu or Debian (udev > version 146), which does not copy cdrom_id, i.e. the update-initramfs udev hook doesn not copy all /live/udev/*_id. For live CD, We need cdrom_id.
  # Ref: https://bugs.launchpad.net/ubuntu/+source/live-initramfs/+bug/254360
  # update_initramfs_flag is global variable
  if ! grep -Eq "^for program in /lib/udev/\*_id; do" /usr/share/initramfs-tools/hooks/udev; then
    cat <<-UDEV_END >> /usr/share/initramfs-tools/hooks/udev
  
# Appended by Clonezilla live
# Ref: https://bugs.launchpad.net/ubuntu/+source/live-initramfs/+bug/254360
for program in /lib/udev/*_id; do
  copy_exec \$program /lib/udev
done
UDEV_END
    update_initramfs_flag="yes"
  fi
  # end-remove-after
}
#
append_framebuffer_modules_if_necessary() {
  # For Ubuntu 7.10 using live-initramfs, if no fbcon and vesafb modules, the /scripts/init-top/framebuffer will fail to modprobe them and during the booting, the screen will be blank if vga=xxx is assigned in kernel param.
  [ -e /etc/lsb-release ] && . /etc/lsb-release
  if [ "$DISTRIB_ID" = "Ubuntu" -a "$DISTRIB_RELEASE" = "7.10" ]; then
    if [ -f "/etc/initramfs-tools/modules" ]; then
      echo "# The following framebuffer related modules are added by Clonezilla:" >> /etc/initramfs-tools/modules
      mod_2_be_inserted="fbcon vesafb uvesafb"
      for i in $mod_2_be_inserted; do
        echo "$i" >> /etc/initramfs-tools/modules
      done
    fi
  fi
  # A workaround to fix the framebuffer not working in Ubuntu 7.10, i.e.
  # Do not let vesafb in the blacklist-framebuffer, otherwise /script/init-top/framebuffer won't be able to modprobe it because it uses "modprobe -Qb vesafb" (-b will honor /etc/modprobe.d/blacklist-framebuffer, and if vesafb is in the list, it won't load it)
  # Ref: https://bugs.launchpad.net/ubuntu/+source/initramfs-tools/+bug/129910
  if [ -e "/etc/modprobe.d/blacklist-framebuffer" ]; then
    perl -pi -e "s|^blacklist vesafb|# blacklist vesafb # Commented by Clonezilla|g" /etc/modprobe.d/blacklist-framebuffer
  fi
  # A workaround to fix the uvesafb module not working in Ubuntu 10.04 (alpha1). i.e. vga16fb is always loaded. We want uvesafb, not vga16fb.
  # Ref: https://bugs.launchpad.net/ubuntu/+source/linux/+bug/494062
  if [ -e "/etc/modprobe.d/blacklist-framebuffer.conf" ]; then
    if [ -z "$(grep -iE "^blacklist vga16fb" /etc/modprobe.d/blacklist-framebuffer.conf)" ]; then
     echo "blacklist vga16fb" >> /etc/modprobe.d/blacklist-framebuffer.conf
    fi
  fi

  # It's better to run update-initramfs here. However, actually live helper will run it after hook scripts are run.
}
#
update_build_system_in_etc_live_conf() {
  local SYSTEM
  [ ! /etc/live.conf ] && return 1
  [ -e /etc/lsb-release ] && . /etc/lsb-release
  if [ "$DISTRIB_ID" = "Ubuntu" ]; then
    SYSTEM="Ubuntu"
  else
    SYSTEM="Debian"
  fi
  if grep -q "^[[:space:]]*BUILD_SYSTEM=" /etc/live.conf 2>/dev/null; then
    perl -pi -e "s/BUILD_SYSTEM=.*/BUILD_SYSTEM=$SYSTEM/g" /etc/live.conf
  else
    echo "BUILD_SYSTEM=$SYSTEM" >> /etc/live.conf
  fi
} # end of update_build_system_in_etc_live_conf
#
remove_lib32_on_amd64_live() {
  if [ "$(LC_ALL=C uname -m)" = "x86_64" ] && \
     [ -d /lib32 ] && \
     [ -d /usr/lib32 ]; then
     rm -rf /lib32 /usr/lib32
  fi
} # end of remove_lib32_on_amd64_live
#
dirty_hacking_rm_files_for_ocs_live() {
  local unnecessary_packages unnecessary_x_packages
  # This is dirty hacking...
  echo "Starting dirty hacking to remove files..."
  unnecessary_packages="man-db manpages info"
  echo "Force to remove some packages ($unnecessary_packages) if installed..."
  for i in $unnecessary_packages; do
   if dpkg -L $i &>/dev/null; then	   
     echo "Force to remove $i..."
     apt-get --yes --force-yes --purge remove $i
   fi
  done

  # Clean package by autoremove
  if LANG=C apt-get --help 2>&1 | grep -q -- autoremove; then
    apt-get -y autoremove
  fi

  # About jfbterm depends program, especially for Lenny. Since in Etch, jfbterm requires unifont, and unifont does not depend on any. In Lenny, jfbterm requires unifont, and unifonts depends on xfonts-unifont, which depends on some big X programs. We remove them, only keep /usr/share/fonts/X11/misc/unifont.pcf.gz. jfbterm only need /usr/share/fonts/X11/misc/unifont.pcf.gz.
  # /usr/share/fonts/X11/misc/unifont.pcf.gz is in package unifont in Etch, but it is in package xfonts-unifont in Lenny.
  # 10/20/2010 x11-common was not removed since startpar will try to start it, but it will failed and show error message. Therefore we keep it.
  # 08/09/2020 Keep ttf-unifont, unifont, xfonts-unifont, xfonts-utils to avoid unifont.pcf.gz being removed.
  echo -n "Removing unnecessary X programs except unifont.pcf.gz which is required for jfbterm/fbterm... Now try to clean if exists: "
  unnecessary_x_packages="libfontenc1 xfonts-encodings libcairo2 ttf-dejavu-core"
  for i in $unnecessary_x_packages; do
    echo -n "$i... " 
    for j in `dpkg -L $i 2>/dev/null`; do
      [ "$(basename $j)" = "unifont.pcf.gz" ] && continue
      [ -d "$j" ] && continue
      rm -f $j
    done
  done
  echo

  # List for the file or dir to be removed:
  # /usr/share/
  share_dir_2_be_rm="info man doc-base doc"
  # Keep the parameters.txt, avoid it to be removed later.
  # live-initramfs want to copy parameters.txt to live cd
  [ -e /usr/share/doc/live-initramfs/parameters.txt ] && {
     cp -p /usr/share/doc/live-initramfs/parameters.txt /
  }
  # Remove them
  for i in $share_dir_2_be_rm; do
    # Here we remove files only, but keep dirs so that later if user want to install packages, postrun won't exit because no /usr/share/man/man1, for example.
    # rm -rf /usr/share/$i/*
    find /usr/share/$i/ -type f -exec rm -f {} \;
  done
  # put it back
  [ -e /parameters.txt ] && {
    mkdir -p /usr/share/doc/live-initramfs/
    mv -f /parameters.txt /usr/share/doc/live-initramfs/
  }

  # Just clean them
  # Log, clean them, but keep the file name. i.e. make the size 0, otherwise if we clean them, some daemon will complain.
  find /var/log/ -type f | xargs -I % bash -c "echo -n '' > %"
  # CPP
  [ -d /usr/lib/gcc ] && find /usr/lib/gcc/ -name "cc1" -exec rm {} \;
  find /usr/bin/ -name "cpp*" -exec rm {} \;
  # Forget about removing zoneinfo.... This might break live-config.
  # if [ -e /usr/share/zoneinfo/UTC ]; then
  #  rm -f /etc/localtime # The original is a soft link file to UTC
  #  cp -af /usr/share/zoneinfo/UTC /etc/localtime
  #  rm -rf /usr/share/zoneinfo/*
  #  # Restore UTC only to avoind live-initramfs giving error
  #  cp -af /etc/localtime /usr/share/zoneinfo/UTC 
  # fi
  # We'd better not to remove /usr/lib/gconv/ since if we want to create debian live from this generated ISO/ZIP, In /usr/bin/lh_binary_syslinux it will run: iconv -f utf-8 -t latin1. Without /usr/lib/gconv/ lh_binary_syslinux will fail.
  # [ -d /usr/lib/gconv/ ] && rm -rf /usr/lib/gconv/*
  [ -d /var/backups ] && rm -rf /var/backups/*
  # For amd64 version, we just need a pure x86-64 env to make the generated file smaller. Therefore remove those lib32 lib (smaller about 30 MB in filesystem.squashfs for Debian Squeeze.
  # 2014/02/17 Forget about this. Otherwise some 32-bit program might not work.
  # remove_lib32_on_amd64_live

  # For Ubuntu 10.04, some update motd programs to avoid lsb-release not found error. (If we install lsb-release package, 5 more MB will be added in the created filesystem.squashfs since python and more are required.)
  [ -d /etc/update-motd.d ] && rm -f /etc/update-motd.d/*
} # end of dirty_hacking_rm_files_for_ocs_live
#
append_numlock_on_setting() {
  # //NOTE// We do not use this function for clonezilla live any more, but do use this function for drbl live and gparted live (maybe in the future). For clonezilla live, The numlock or capslock now are processed in S03prep-drbl-clonezilla with command setleds.
  local kbd_cfg=$1
  # /etc/console-tools/config (for console-tools) or /etc/kbd/config (for kbd)
  [ -z "$kbd_cfg" ] && echo "No kbd_cfg!!!" && exit 1
  echo "Appending numlock on setting in $kbd_cfg..."
  cat <<-NUMLOCK_END >> $kbd_cfg

# Turn on numlock by default by DRBL/Clonezilla/GParted.
LEDS=+num
NUMLOCK_END
} # end of append_numlock_on_setting
#
turn_on_numlock_in_booting(){
  # Here we force to append setting for console-tools AND kbd, since sometimes if console-tools is installed first, and then kbd is installed again, the setting of console-tools might still exist (i.e. packkage is removed, but setting still remains).
  if [ -e "/etc/console-tools/config" ]; then
    # for console-tools
    append_numlock_on_setting /etc/console-tools/config
  fi
  if [ -e "/etc/kbd/config" ]; then
    # for kbd
    append_numlock_on_setting /etc/kbd/config
  fi
} # end of turn_on_numlock_in_booting
#
exclude_umount_live_mnt_point_in_umountfs() {
  # 2010/12/25 Steven Shiau: This function is obsolete. We let live-config to do this.
  # Ref: http://lists.debian.org/debian-live/2010/12/msg00191.html
  # Since now we support only live helper, no more live package, so only exclude /live/*.
  perl -pi -e 's@(/sys\|/lib/init/rw)@$1|/live|/live/image|/live/cow|/cow|/filesystem.squashfs|/sys/fs/fuse/connections\) # Modified by Clonezilla@' /etc/init.d/umountfs
}
#
append_drbl_clonezilla_PATH_in_system() {
  # For bash, appending PATH in /etc/profile is enough, do not append in /etc/bash.bashrc again. Otherwise it will be appended twice.
  echo "export PATH=\$PATH:/sbin:/usr/sbin:$DRBL_SCRIPT_PATH/sbin:$DRBL_SCRIPT_PATH/bin:" >> /etc/profile
  # echo "export PATH=\$PATH:/sbin:/usr/sbin:$DRBL_SCRIPT_PATH/sbin:$DRBL_SCRIPT_PATH/bin:" >> /etc/bash.bashrc
} # end of append_drbl_clonezilla_PATH_in_system
#
query_and_install_PKG_TO_QUERY() {
  # For DRBL live
  # PKG_TO_QUERY is listed in drbl-ocs.conf. Here we query if it exists in the repository. If so, install.
  # The reason we do not install these packages by live-config option is because
  # we do not know if it exists in the repository or not. If it's not, apt will just quit. Therefore we have to install in the hook mechanism.
  local pkg_2_inst
  echo "Searching if $PKG_TO_QUERY available... "
  for ipkg in $PKG_TO_QUERY; do
    if ! chk_deb_installed $ipkg; then
      # Only when it's not installed we will check it. Otherwise maybe we have force to
      # use older version and if this package is installed again, the newer version will be installed.
      if [ -n "$(LC_ALL=C apt-cache show $ipkg 2>/dev/null)" ]; then
        echo "Package $ipkg exists in repository."
        pkg_2_inst="$pkg_2_inst $ipkg"
      fi
    fi
  done
  echo "Installing packages: $pkg_2_inst"
  LC_ALL=C apt-get -y install $pkg_2_inst
} # end of query_and_install_PKG_TO_QUERY
#
install_debian_extra_modules() {
  local ker_list pkg_list
  # extra_module_list is from ocs-live-hook.conf
  ker_list=''
  for i in /boot/vmlinuz-*; do
     ker_list="$(LC_ALL=C basename "$i" | sed -e 's/vmlinuz-//g')"
  done
  pkg_list=''
  for i in $ker_list; do
    for j in $extra_module_list; do
     if apt-cache show ${j}-modules-${i} &>/dev/null; then
       pkg_list="$pkg_list ${j}-modules-${i}"
     fi
    done
  done
  echo "Installing available Debian extra modules: $pkg_list"
  LC_ALL=C apt-get -y install $pkg_list
} # end of install_debian_extra_modules
#
clean_udev_persistent_net_rules() {
  # Although live helper will do it, but we here force to do it just in case.
  rm -f /etc/udev/rules.d/*persistent-net.rules
} # end of clean_udev_persistent_net_rules
#
disable_kexec() {
  local kexec_cfg="/etc/default/kexec"
  if [ -e "$kexec_cfg" ]; then
    perl -pi -e "s/^LOAD_KEXEC=.*/LOAD_KEXEC=false/g" $kexec_cfg
  fi
} # end of disable_kexec
#
disable_xfce_startup_tips_and_tricks() {
  local tip_auto_cfg="/etc/xdg/autostart/xfce4-tips-autostart.desktop"
  if [ -e "$tip_auto_cfg" ]; then
    perl -pi -e "s/^Hidden=.*/Hidden=true/g" $tip_auto_cfg
  fi
} # end of disable_xfce_startup_tips_and_tricks
#
remove_some_xfce_startup() {
  for i in $startup_2_be_removed_for_drbl_live; do
    rm -f /etc/xdg/autostart/$i
  done
} # end of remove_some_xfce_startup
#
download_grub_1_2_deb_for_later_use() {
  local grub1_pkg_name dest_dir pkg_name
  local grub_common_pkg grub2_common_pkg grub_pc_bin
  # grub-common is required by grub-legacy and grub2, which grub2-common is only required for grub2.
  # Ref: http://packages.debian.org/sid/grub-common
  # and  http://packages.debian.org/sid/grub2-common
  # grub-common: GRand Unified Bootloader (common files)
  # This package contains common files shared by the distinct flavours of GRUB. It is shared between GRUB Legacy and GRUB 2, although a number of files specific to GRUB 2 are here as long as they do not break GRUB Legacy.
  # grub2-common: GRand Unified Bootloader (common files for version 2)
  # This package contains common files shared by the distinct flavours of GRUB. The files in this package are specific to GRUB 2, and would break GRUB Legacy if installed on the same system. 
  if [ "$LIVE_CREATING_MODE" = "gparted" ]; then
    # For GParted, we put the grub debs in /root/
    dest_dir=/root/
  else
    # By default we put in $DRBL_SCRIPT_PATH, this is for drbl and clonezilla cases.
    dest_dir=$DRBL_SCRIPT_PATH
  fi

  # First we decide the grub 1 package name. Since in Debian Sid, grub1 is grub-legacy, while in ubuntu 9.10, grub1 is grub.
  if [ -n "$(LC_ALL=C apt-cache show grub-legacy | grep -iE "^Version:")" ]; then
   grub1_pkg_name="grub-legacy"
  elif [ -n "$(LC_ALL=C apt-cache show grub | grep -iE "^Version:")" ]; then
   grub1_pkg_name="grub"
  fi
  # If grub-common exists, download it, too.
  if [ -n "$(LC_ALL=C apt-cache show grub-common | grep -iE "^Version:")" ]; then
    grub_common_pkg="grub-common"
  fi
  apt-get clean
  if [ -n "${grub1_pkg_name}" -a -n "${grub_common_pkg}" ]; then
    LC_ALL=C apt-get -d --reinstall -y install ${grub1_pkg_name} ${grub_common_pkg}
    # E.g.: Filename: pool/main/g/grub/grub-legacy_0.97-59_i386.deb
    # full_grub1_pkg_name="$(LC_ALL=C apt-cache show grub-legacy | grep -iE "^Filename:" | awk -F":" '{print $2}' | xargs basename)"
    mkdir -p $dest_dir/pkg/grub/grub1/
    mv /var/cache/apt/archives/grub*.deb $dest_dir/pkg/grub/grub1/
    rm -f $dest_dir/pkg/grub/grub1-pkgs-list.txt
    for i in $dest_dir/pkg/grub/grub1/grub*.deb; do
     pkg_name=""
     if [ -f "$i" ]; then
       pkg_name="$(LC_ALL=C dpkg --info $i | grep -E "^ Package:" | awk -F":" '{print $2}' | sed -r -e "s/^[[:space:]]*//g")"
       echo "$pkg_name: $(basename $i)" >> $dest_dir/pkg/grub/grub1-pkgs-list.txt
     fi
    done
  fi

  # Grub 2
  # If grub-common and grub-pc-bin exist, download them, too.
  if [ -n "$(LC_ALL=C apt-cache show grub2-common | grep -iE "^Version:")" ]; then
    grub2_common_pkg="grub2-common"
  fi
  if [ -n "$(LC_ALL=C apt-cache show grub-pc-bin | grep -iE "^Version:")" ]; then
    grub_pc_bin="grub-pc-bin"
  fi
  # grub2 package name is grub-pc for debian sid and ubuntu 9.10.
  apt-get clean
  if [ -n "${grub_pc_bin}" -a -n "${grub2_common_pkg}" -a -n "${grub_common_pkg}" ]; then
    # //NOTE// grub-common is also required by grub2
    LC_ALL=C apt-get -d --reinstall -y install grub-pc $grub2_common_pkg $grub_pc_bin $grub_common_pkg
    mkdir -p $dest_dir/pkg/grub/grub2/
    mv /var/cache/apt/archives/grub*.deb $dest_dir/pkg/grub/grub2/
    rm -f $dest_dir/pkg/grub/grub2-pkgs-list.txt
    for i in $dest_dir/pkg/grub/grub2/grub*.deb; do
     pkg_name=""
     if [ -f "$i" ]; then 
       pkg_name="$(LC_ALL=C dpkg --info $i | grep -E "^ Package:" | awk -F":" '{print $2}' | sed -r -e "s/^[[:space:]]*//g")"
       echo "$pkg_name: $(basename $i)" >> $dest_dir/pkg/grub/grub2-pkgs-list.txt
     fi
    done
  fi
} # end of download_grub_1_2_deb_for_later_use
#
assing_default_dns_server() {
  cat <<-RESOLV_END > /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
RESOLV_END
} # end of assing_default_dns_server
#
disable_parallel_start_during_booting() {
  LC_ALL=C perl -pi -e "s|^CONCURRENCY=.*|CONCURRENCY=none # Modified by DRBL|g" /etc/init.d/rc
} # end of disable_parallel_start_during_booting
#
get_unifont_bgf() {
  # Ref: http://apt.bzzware.org/debian-edu/html/BuildMirror.html
  # $mirror_url and $debian_dist are from hook
  local target_dir="$1"
  local tmp_wd bterm_unifont_udeb
  [ -z "$target_dir" ] && return 1
  tmp_wd="$(mktemp -d /tmp/unifont.XXXXXX)"
  pkg_url="$tmp_wd $mirror_url/dists/$debian_dist/main/debian-installer/binary-${darch}/Packages.xz"
  wget -P $pkg_url
  bterm_unifont_udeb="$(LC_ALL=C xzcat $tmp_wd/Packages.xz | sed -ne 's/^Filename: //p' | grep -Ew bterm-unifont)"
  if [ -n "$bterm_unifont_udeb" ]; then
    # result like: pool/main/b/bterm-unifont/bterm-unifont_1.2_i386.udeb
    wget -P $tmp_wd $mirror_url/$bterm_unifont_udeb
    dpkg-deb --extract $tmp_wd/$(basename $bterm_unifont_udeb) $tmp_wd
    find $tmp_wd -name "unifont.bgf" -exec cp -a {} $target_dir \;
  else
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "Package bterm-unifont was not found in $pkg_url"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  fi
  if [ -d "$tmp_wd" -a -n "$(echo $tmp_wd | grep -E unifont)" ]; then
    rm -rf $tmp_wd
  fi
} # end of get_unifont_bgf
#
decide_if_use_xz_compression_for_initrd() {
  # If kernel support XZ initrd, and xz command exists, use xz to compress the initrd. We'd like to have smaller Clonezilla live.
  # update_initramfs_flag is gloable variable.
  local ker_list
  ker_list=''
  for i in /boot/vmlinuz-*; do
     ker_list="$(LC_ALL=C basename "$i" | sed -e 's/vmlinuz-//g')"
  done
  for i in $ker_list; do
    if [ -n "$(LC_ALL=C grep -E "^CONFIG_RD_XZ=y" /boot/config-${i})" ]; then
      if type xz &>/dev/null; then
        perl -pi -e "s/^COMPRESS=.*/COMPRESS=xz/g" /etc/initramfs-tools/initramfs.conf
        update_initramfs_flag="yes"
      fi
    fi
  done
} # end of decide_if_use_xz_compression_for_initrd
#
disable_apt_lang_translation() {
  cat <<-APT_END > /etc/apt/apt.conf.d/99lang
Acquire
{
  Retries "0";
        Languages "none";
        Translation "none";
};
APT_END
} # end of disable_apt_lang_translation
#
remove_grpck_opt_p() {
  # This function is a workaround to remove the "shadowconfig on" error because the debian maintainer hasn't fix it for more than 2 months.
  # Ref: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=638263
  perl -pi -e 's/^([[:space:]]*)grpck -p/$1grpck/g' /sbin/shadowconfig
} # end of remove_grpck_opt_p
#
get_non_free_net_firmware_for_ubuntu() {
  # This function is used to get non-free network firmware from ubuntu.
  # //NOTE// "linux-firmware" only exists on Ubuntu, while for Debian there are separate packages for those non-free firmware, e.g.
  # firmware-atheros, firmware-brcm80211, firmware-bnx2, firmware-bnx2x, firmware-ipw2x00, firmware-ipw3945, firmware-iwlwifi
  # $mirror_url and $debian_dist are from hook
  local target_dir="$1"
  local DISTRIB_ID tmp_wd f inst_cmd
  if [ -z "$target_dir" ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "XXXXXXXXX! No target_dir in function get_non_free_net_firmware!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    return 1
  fi
  [ -e /etc/lsb-release ] && . /etc/lsb-release
  if [ "$DISTRIB_ID" != "Ubuntu" ]; then
    echo "No need to download non-free firmware for non-ubuntu distribution."
    return 0
  fi
  tmp_wd="$(mktemp -d /tmp/nic_firmware.XXXXXX)"
  # >= Ubuntu 21.04, nic-firmware is not available anymore in udeb format.
  # Hence we have to extract them from linux-firmware.
  # Find the license doc and firmware then put them.
  apt-get -d --reinstall install linux-firmware
  rc=$?
  if [ "$rc" -gt 0 ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "XXXXXXXXX! Unable to download linux-firmware!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    return 1
  fi
  echo "Extracting non-free firmware doc and puting it in Clonezilla live..."
  mkdir -p $tmp_wd/firm_tmp/
  dpkg --extract /var/cache/apt/archives/linux-firmware_*.deb $tmp_wd/firm_tmp/
  mkdir -p $target_dir/usr/share/doc/linux-firmware/
  cp -a $tmp_wd/firm_tmp/usr/share/doc/linux-firmware/* $target_dir/usr/share/doc/linux-firmware/
  while read -r f; do
    [ -n "$(echo "$f" | grep -E "^[[:space:]]*#")" ] && continue
    if [ -e "$tmp_wd/firm_tmp/lib/firmware/$f.zst" ]; then
      # With .zst compression
      inst_cmd="install -m644 -D $tmp_wd/firm_tmp/lib/firmware/$f.zst $target_dir/lib/firmware/$f.zst"
      echo "Running: $inst_cmd"
      eval $inst_cmd
    elif [ -e "$tmp_wd/firm_tmp/lib/firmware/$f" ]; then
      # Without .zst compression
      inst_cmd="install -m644 -D $tmp_wd/firm_tmp/lib/firmware/$f $target_dir/lib/firmware/$f"
      echo "Running: $inst_cmd"
      eval $inst_cmd
    else
      [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
      echo "File $tmp_wd/firm_tmp/$f not found. Skip putting it."
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    fi
  done <<< "$(cat /usr/share/drbl/setup/files/ocs/live-hook/nic-firmware.lst)"

  # Clean unnecessary files
  apt-get clean
  if [ -d "$tmp_wd" -a -n "$(echo $tmp_wd | grep -E nic_firmware)" ]; then
    rm -rf $tmp_wd
  fi
} # end of get_non_free_net_firmware_for_ubuntu
#
blacklist_module() {
  local b_mod="$1"
  for i in $b_mod; do
    if [ -z "$(grep -Ew "^blacklist[[:space:]]+$i" /etc/modprobe.d/ocs-live-blacklist.conf 2>/dev/null)" ]; then
      echo "blacklist $i" >> /etc/modprobe.d/ocs-live-blacklist.conf
    fi
  done
} # end of blacklist_module
#
enable_vim_syntax_and_dark_bg() {
  # This will only work when corresponding program exist. For Clonezilla live, we only have basic vi, not vim installed. So it won't be enbaled, but for DRBL live it will.
  perl -pi -e "s|^\"syntax on|syntax on|g" /etc/vim/vimrc
  perl -pi -e "s|^\"set background=.*|set background=dark|g" /etc/vim/vimrc
  echo "let skip_defaults_vim=1" >> /etc/vim/vimrc.local
} # end of enable_vim_syntax_and_dark_bg
#
rm_ifup_d_ntpdate() {
  if [ -e "/etc/network/if-up.d/ntpdate" ]; then
    rm -f /etc/network/if-up.d/ntpdate
  fi
} # end of rm_ifup_d_ntpdate
#
set_HandleLidSwitch_ignore() {
  # Function to avoid laptop LID trigger sleep.
  # Ref: https://sourceforge.net/p/clonezilla/discussion/Clonezilla_live/thread/ee859ae7
  perl -pi -e 's|^[#]*HandleLidSwitch=.*|HandleLidSwitch=ignore|g' /etc/systemd/logind.conf
} # end of set_HandleLidSwitch_ignore
#
append_live_boot_config() {
  cat <<-LB_END >> /etc/live/boot.conf
# Added by Clonezilla live
DISABLE_NTFS=false
LB_END
} # end of append_live_boot_config
#
get_debian_pkg_arch() {
  local uarch_ 
  # darch is a global variable
  uarch_="$(LC_ALL=C uname -m 2>/dev/null)"
  case "$uarch_" in
    x86_64)  darch="amd64";;
    x86)     darch="i386";;
    aarch64) darch="arm64";;
    arm*)    darch="armhf";;
    riscv64) darch="riscv64";;
  esac
} #end of get_debian_pkg_arch
#
set_ntp_off() {
  echo "Running: timedatectl set-ntp false..."
  timedatectl set-ntp false
} # end of set_ntp_off
#
disable_sleep_hibernate() {
  echo "Disable sleep and hibernation..."
  systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
} # end of disable_sleep_hibernate
#
replace_jfbterm_terminfo() {
  # The terminfo for jfbterm from ncurses-term is not really complete. It's linked to kon.
  # Not a good one for CJK environment. Hence patch here.
  # Ref: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=994433  
  if [ "$(LC_ALL=C readlink -f /usr/share/terminfo/j/jfbterm)" = "/usr/share/terminfo/k/kon" ] && \
     [ -e /usr/share/terminfo/j/jfbterm.genuine ] ; then
    rm -f /usr/share/terminfo/j/jfbterm
    ln -fsr /usr/share/terminfo/j/jfbterm.genuine /usr/share/terminfo/j/jfbterm
  fi
} # end of replace_jfbterm_terminfo
#
disable_sudo_use_pty() {
  # Do not let sudo to spawn pseudo-terminal when running a job. Otherwise
  # ocs-live-run-menu will be run twice, and it will make the console weird.
  # Ref: https://groups.google.com/g/ocs-clonezilla/c/tB93Vjz9CVw
  # Check sudoers's manual, and look for "use_pty".
  #
  # With use_pty (as Clonezilla live 3.0.0-26 didi, by default Debian put use_pty in /etc/sudoers)
  # root       50427   50424  0 07:44 tty1     00:00:00 sudo SUDO_TTY=/dev/tty1 ocs-live-run-menu
  # root       50428   50427  0 07:44 pts/0    00:00:00 sudo SUDO_TTY=/dev/tty1 ocs-live-run-menu  <------
  # root       50429   50428  0 07:44 pts/0    00:00:00 /bin/bash /usr/sbin/ocs-live-run-menu
  # root       50596   50429  0 07:44 pts/0    00:00:00 jfbterm -q -e /tmp/ocs_live_run_tmp.jPwvrt
  # root       50599   50596  0 07:44 pts/4    00:00:00 /bin/bash /tmp/ocs_live_run_tmp.jPwvrt
  # root       50600   50599  0 07:44 pts/4    00:00:00 /bin/bash /usr/sbin/ocs-live-general
  # 
  # without use_pty (as clonezilla live 2.8.0-27-amd64 did,Debian by default Debian does not put use_pty in /etc/sudoers)
  # root        2900    2897  0 06:45 tty1     00:00:00 sudo -i ocs-live-run-menu
  # root        2901    2900  0 06:45 tty1     00:00:00 /bin/bash /usr/sbin/ocs-live-run-menu <------
  # root        3071    2901  0 06:45 tty1     00:00:00 jfbterm -q -e /tmp/ocs_live_run_tmp.G6ycSu
  # root        3074    3071  0 06:45 pts/1    00:00:00 /bin/bash /tmp/ocs_live_run_tmp.G6ycSu
  # root        3075    3074  0 06:45 pts/1    00:00:00 /bin/bash /usr/sbin/ocs-live-general
  #
  # /etc/sudoers:
  # Defaults        env_reset
  # Defaults        mail_badpass
  # Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  # Defaults        use_pty -> Defaults        !use_pty  <-----

  chmod u+w /etc/sudoers
  perl -pi -e 's/^(Defaults[[:space:]])+use_pty/$1!use_pty # Modified by DRBL/g' /etc/sudoers
  chmod u-w /etc/sudoers
} # end of disable_sudo_use_pty
#
enlarge_screen_scrollback() {
  # define a bigger scrollback so that it's easier to debug.
  perl -pi -e "s|^defscrollback.*|defscrollback 102400|g" /etc/screenrc
} # end of enlarge_screen_scrollback
#
add_cpu_offline_online_in_initrd() {
  install -o root -g root -m 755 /live-hook-dir/udev/cpu_offline /etc/initramfs-tools/scripts/init-top/01_cpu_offline
  install -o root -g root -m 755 /live-hook-dir/udev/cpu_online /etc/initramfs-tools/scripts/init-premount/01_cpu_online
} # end of add_cpu_offline_online_in_initrd
#
enable_bitmaps_fonts_for_fontconfig(){
  cat <<-FNT_END >/etc/fonts/conf.avail/70-force-bitmaps.conf
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
  <match target="font">
    <edit name="embeddedbitmap" mode="assign">
      <bool>true</bool>
    </edit>
  </match>
  <selectfont>
    <acceptfont>
      <pattern>
        <patelt name="scalable"><bool>false</bool></patelt>
      </pattern>
    </acceptfont>
  </selectfont>
</fontconfig>
FNT_END

  ln -s /etc/fonts/conf.avail/70-force-bitmaps.conf /etc/fonts/conf.d/70-force-bitmaps.conf
  fc-cache -fv
} # end of enable_bitmaps_fonts_for_fontconfig