File: ocs-live-dev

package info (click to toggle)
clonezilla 5.13.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 5,176 kB
  • sloc: sh: 41,198; perl: 193; python: 59; makefile: 26
file content (1121 lines) | stat: -rwxr-xr-x 51,608 bytes parent folder | download | duplicates (2)
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
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
#!/bin/bash
# Author: Steven Shiau <steven _at_ clonezilla org>
# License: GPL
# Description: This program will put Debian Live minimal + DRBL/Clonezilla program into a bootable device, such as pendrive/usb stick.
# Some notes and programs about make bootable USB device in MS windows are modified from http://www.pendrivelinux.com/, Thanks to PDLA.

#
DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"

. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
. /etc/drbl/drbl-ocs.conf
. $DRBL_SCRIPT_PATH/sbin/ocs-functions

# other default settings
# If insert_mode=prog_only, only copy DRBL/Clonezilla programs only, no ocs images.
insert_mode="prog_and_img"
img_size_sum=0
# default is to create a bootable device instead of a released zip file.
# Two options: boot_dev or release_file
target_mode="boot_dev"

# If we need to download Clonezilla live iso from repository as template iso, the branch and CPU arch for the file, e.g.
# http://downloads.sourceforge.net/clonezilla/$clonezilla_iso_branch/clonezilla-live-${cz_ver}${clonezilla_iso_arch_tag}.iso
# Ref to the command get-latest-ocs-live-ver
# For Debian-based Clonezilla live, i686-pae in the stable branch, you can set: clonezilla_iso_branch="stable", and clonezilla_iso_arch_tag="-i686-pae"
# For Ubuntu-based Clonezilla live, you can set: clonezilla_iso_branch="alternative", and clonezilla_iso_arch_tag="-i386"
clonezilla_iso_branch="alternative"
clonezilla_iso_arch_tag="-i386"
# Where the MD5SUM could be found for the above iso file. This has to match $clonezilla_iso_branch and $clonezilla_iso_arch_tag
DEBIAN_ISO_ETC_PATH="http://free.nchc.org.tw/clonezilla-live/alternative/stable/"
#
live_boot_menu_opt_raw=""
live_boot_menu_opt_timeout=""
# To use image exists when creating recovery device (e.g. /dev/sdg1). the image might already exist (copied or created on the device before). For other mode, image is copied or linked in this program.
use_existing_img_on_dev="no"
# Batch mode for run Clonezilla live in restoring/recovery mode.
ocs_live_batch="no"
# Batch mode for running this program.
batch_mode="no"
custom_ocs=""
normal_menu_with_insert_image="no"
template_mode=""
prefer_archive="zip"

#
prog="$(basename $0)"
prog_option="$*"

# functions
USAGE() {
    echo "Usage:"
    echo "To put clonezilla image into a bootable device (such as pendrive/usb stick):"
    echo "$prog [OPTION] DEV CLONEZILLA_IMAGE_NAME"
    echo "CLONEZILLA_IMAGE_NAME  The image exists in the system"
    echo "OPTION:"
    language_help_prompt_by_idx_no
    echo "-a, --boot-loader [grub|syslinux]  Force to use grub or syslinux as boot loader in target device"
    echo "-b, --bg-mode  [text|graphic]  Assign the background of boot menu. Default is graphic"
    echo "-c, --create-release  Create a USB package release file, this will not create a bootable device, instead it will create a zip file contain all necessary files to make it boot and run clonezilla live."
    echo "-d, --dev DEV      Write the output to DEV (such as /dev/sdg1)"
    echo "-e, --extra-param  PARAM  Assign extra parameter PARAM for clonezilla live to run, PARAM will be appended when run in ocs-live-restore or ocs."
    echo "-f, --batch-mode   Run $prog in batch, unattended mode."
    echo "--file-name-prefix NAME    Assign the output file name as NAME.zip or NAME.tar. $0 will auto append '.zip' or '.tar' in the end of filename."
    echo "-g, --ocs-live-language LANGUAGE Assign the language when using clonezilla live, available languages are en_US.UTF-8, zh_TW.UTF-8 "
    echo "-i, --assign-version-no NO  Assign the version no as NO instead of date. This only works when using with option -s and -c."
    echo "-j, --debian-iso ISO_FILE  Assign Debian live template iso file name as ISO_FILE to be used to create Clonezilla live. By default the ISO_FILE is \"$DEBIAN_ISO_DEF\"."
    echo "-k, --ocs-live-keymap KEYBOARD_LAYOUT Assign the keyboard layout when using clonezilla live. You can find the keyboard layout in /usr/share/X11/xkb/rules/base.lst. e.g. use '-k fr' for French keyboard layout. If 'NONE' is used, the default one (US keyboard) will be use. For more info, please check the live manual on Debian Live website."
    echo "-m, --custom-ocs  PATH/custom-ocs  Use the customized ocs program 'custom-ocs' instead of the default one. Note! PATH should be assigned so that it can be found. This is advanced mode."
    echo "-n, --ocs-live-boot-menu-option EXTRA_OPTION Assign an extra option for ocs-live-boot-menu. //NOTE// Do not put '-' in this EXTRA_OPTION, $0 will add that automatically. e.g. if you want to add -s1 for ocs-live-boot-menu to run, use 's1' only."
    echo "-o, --normal-menu  When a clonezilla image is inserted, by default only restore menu will be shown in the created ISO file. If you want to show normal menu, i.e. with save and restore menu, use this one."
    echo "-p, --image-path   Assign the clonezilla image source path where CLONEZILLA_IMAGE_NAME exists in this server.  Default = $ocsroot"
    echo "-q, --existing-img To use image exists on the destination device when creating recovery device (e.g. /dev/sdg1). the image might already exist (copied or created on the device before). For other mode, image is copied or linked in this program."
    echo "-r, --ocs-live-boot-menu-timeout PARAM  Set live boot menu timeout argument for ocs-live-boot-menu to PARAM."
    echo "-s, --skip-image   Do not include any clonezilla image. The is used to created a live CD or USB flash drive with DRBL/Clonezilla programs only."
    echo "-t, --ocs-live-batch  Set clonezilla live to run in batch mode, usually it's for restoring. If this mode is set, some dialog question will be ignored."
    echo "-u, --include-dir DIR   Include a dir in the target zip file."
    echo "-x, --extra-boot-param  EXTRA_PARAM  Assign extra boot parameter EXTRA_PARAM for clonezilla live kernel to read. These parameters are the same with that from live-boot or live-config. Ex. \"noeject\" can be use to not prompt to eject the CD on reboot."
    echo "-y, --syslinux-ver VER  Assign the syslinux version as VER. E.g. 6.02, 6.03-pre1"
    echo "-z, --prefer-archive  PROG  Assign the archive program as PROG (tar or zip). Default program is $prefer_archive."
    echo "$prog will download a template Debian live CD for clonezilla iso file if ncecessary. You can also download it by yourself, and put it in the working directory when you run $prog. If you want to create that template iso file in Debian Etch, run create-debian-live."
    echo "NOTE! You have to prepare the target partition first, and the filesystem should be ready (FAT or ext2/3)."
    echo "Ex:"
    echo "To put clonezilla image squeeze-ocs (located in /home/partimag in clonezilla server) to USB device /dev/sdg1, you can run:"
    echo "  $prog -d /dev/sdg1 squeeze-ocs"
    echo "To put more images, just append them, such as:"
    echo "  $prog -d /dev/sdg1 squeeze-ocs etch-ocs"
    echo "To create a bootable, recovery USB device (e.g. /dev/sdg1, VFAT file system) with image \"precise-x86-20140206\" included:"
    echo "  $prog -g en_US.UTF-8 -t -k NONE -d /dev/sdg1 -e \"-g auto -e1 auto -e2 -c -r -j2 -p choose restoredisk precise-x86-20140206 sda\" precise-x86-20140206"
    echo "To create a Live USB device with DRBL/Clonezilla programs, which can be used to save image:"
    echo "  $prog -s -d /dev/sdg1"
    echo "To create a zip file of general purpose Clonezilla live. Later it can be put in an USB flash drive or similar device:"
    echo "  $prog -s -c"
    echo "To create a zip file for restoring with clonezilla image squeeze-r5 builtin, and make it boot then restore the image squeeze-r5 to sda in unattended mode (Only confirmation in the beginning), you can run:"
    echo "  $prog -c -g en_US.UTF-8 -t -k NONE -e \"-b -c restoredisk squeeze-r5 sda\" squeeze-r5"
    echo "To create a zip file to run your own custom-ocs program:"
    echo "  $prog -g en_US.UTF-8 -k NONE -s -c -m ./custom-ocs"
}
#
clean_tmp_dir() {
  if [ -d "$DEBIAN_ISO_TMP" -a -n "$DEBIAN_ISO_TMP" ]; then
    # Force to unmount,	althouth it might have beed unmounted, or maybe not (ctrl-c interrupt).
    umount $DEBIAN_ISO_TMP &>/dev/null
    rmdir $DEBIAN_ISO_TMP &>/dev/null
  fi
  if [ -d "$USB_TMP" -a -n "$USB_TMP" ]; then
    if umount $USB_TMP &>/dev/null; then
      rmdir $USB_TMP
    fi
  fi
  if [ -d "$md5_tmp" -a -n "$(echo $md5_tmp | grep "ocs_isomd5_tmp")" ]; then
    rm -rf $md5_tmp
  fi
} # End of clean_tmp_dir

#
check_if_root

# Parse command-line options
while [ $# -gt 0 ]; do
  case "$1" in
    --file-name-prefix)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              target_zip_prefix="$1"
              shift
            fi
	    [ -z "$target_zip_prefix" ] && USAGE && exit 1
            ;;
    -l|--language)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              specified_lang="$1"
              shift
            fi
	    [ -z "$specified_lang" ] && USAGE && exit 1
            ;;
    -a|--boot-loader)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              boot_loader="$1"
              shift
            fi
	    [ -z "$boot_loader" ] && USAGE && exit 1
            ;;
    -b|--bg-mode)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              bg_mode="$1"
              shift
            fi
	    [ -z "$bg_mode" ] && USAGE && exit 1
            ;;
    -e|--extra-param)
            shift
	    # extra param might begin with -, i.e. Ex. -b -p true. Therefore we should not skip this.
            ocs_live_extra_param="$1"
            shift
	    [ -z "$ocs_live_extra_param" ] && USAGE && exit 1
            ;;
    -i|--assign-version-no)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              version_no="$1"
              shift
            fi
	    [ -z "$version_no" ] && USAGE && exit 1
            ;;
    -f|--batch-mode)
            shift
            batch_mode="yes"
            ;;
    -g|--ocs-live-language)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              ocs_lang="$1"
              shift
            fi
	    [ -z "$ocs_lang" ] && USAGE && exit 1
            ;;
    -k|--ocs-live-keymap)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              ocs_live_keymap="$1"
              shift
            fi
	    [ -z "$ocs_live_keymap" ] && USAGE && exit 1
            ;;
    -m|--custom-ocs)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              custom_ocs="$1"
              shift
            fi
	    [ -z "$custom_ocs" ] && USAGE && exit 1
            ;;
    -p|--image-path)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              image_path="$1"
              shift
            fi
	    [ -z "$image_path" ] && USAGE && exit 1
            ;;
    -s|--skip-image)
            insert_mode="prog_only"
            shift ;;
    -t|--ocs-live-batch)
            ocs_live_batch="yes"
            shift ;;
    -o|--normal-menu)
            normal_menu_with_insert_image="yes"
            shift ;;
    -d|--dev)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              output_dev="$1"
              shift
            fi
	    [ -z "$output_dev" ] && USAGE && exit 1
            ;;
    -c|--create-release)
            target_mode="release_file"
            shift ;;
    -q|--existing-img)
            use_existing_img_on_dev="yes"
            shift ;;
    -j|--debian-iso)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              DEBIAN_ISO="$1"
	      template_mode="from-downloaded-live-media"
              shift
            fi
	    [ -z "$DEBIAN_ISO" ] && USAGE && exit 1
            ;;
    -x|--extra-boot-param)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              live_extra_boot_param="$1"
              shift
            fi
	    [ -z "$live_extra_boot_param" ] && USAGE && exit 1
            ;;
    -n|--ocs-live-boot-menu-option)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              live_boot_menu_opt_raw="$live_boot_menu_opt_raw $1"
              shift
            fi
	    [ -z "$live_boot_menu_opt_raw" ] && USAGE && exit 1
            ;;
    -r|--ocs-live-boot-menu-timeout)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              live_boot_menu_opt_timeout="$1"
              shift
            fi
	    [ -z "$live_boot_menu_opt_timeout" ] && USAGE && exit 1
            ;;
    -u|--include-dir)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              include_dir="$1"
              shift
            fi
	    [ -z "$include_dir" ] && USAGE && exit 1
            ;;
    -y|--syslinux-ver)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              isolinux_ver="$1"
              shift
            fi
	    [ -z "$isolinux_ver" ] && USAGE && exit 1
            ;;
    -z|--prefer-archive)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
            	# skip the -xx option, in case 
            	prefer_archive="$1"
            	shift
            fi
            [ -z "$prefer_archive" ] && USAGE && exit 1
            ;;
    -*)     echo "${0}: ${1}: invalid option" >&2
            USAGE >& 2
            exit 2 ;;
    *)      break ;;
  esac
done
ocs_image="$*"
# strip the / to avoid problem
ocs_image="$(echo $ocs_image | sed -e "s|/||g")"
      
if [ -z "$ocs_image" ]; then
  [ "$insert_mode" = "prog_and_img" -a "$target_mode" = "boot_dev" ] && USAGE && exit 1
fi
[ -z "$image_path" ] && image_path=$ocsroot
[ -z "$bg_mode" ] && bg_mode="$BG_MODE_DEF"
[ -z "$output_dev" -a "$target_mode" = "boot_dev" ] && USAGE && exit 1
[ -z "$DEBIAN_ISO" ] && DEBIAN_ISO="$DEBIAN_ISO_DEF"
if [ -n "$live_boot_menu_opt_raw" ]; then
   for i in $live_boot_menu_opt_raw; do
     ocs_live_boot_menu_option="$ocs_live_boot_menu_option -$i"
   done
fi
if [ -n "$live_boot_menu_opt_timeout" ]; then
   ocs_live_boot_menu_timeout_option="--timeout $live_boot_menu_opt_timeout"
fi
md5_file_url="$DEBIAN_ISO_ETC_PATH/$md5_file"

#
ask_and_load_lang_set $specified_lang

#
if [ -n "$custom_ocs" -a ! -e "$custom_ocs" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "File $custom_ocs not found!"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_program_stop"
  exit 1
fi

# Format the lang variable. This is for clonezilla live running, not for ocs-iso.
case "$ocs_lang" in 
  zh_TW.BIG5|zh_TW.big5|tw.BIG5)
   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
   echo "For Traditional Chinese locale, Clonezilla live only supports unicode (zh_TW.UTF-8), not Big5 encoding (zh_TW.BIG5). Force to use UTF-8 for Traditional Chinese in Clonezilla live."
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   ocs_lang="zh_TW.UTF-8" ;;
  zh_TW.UTF-8|zh_TW.utf8|tw.UTF-8|tw.utf8)
   ocs_lang="zh_TW.UTF-8" ;;
esac

# we need zip to create the release file when target_mode is release_file
if ! type zip &>/dev/null && [ "$target_mode" = "release_file" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "Command zip not found!"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_program_stop"
  exit 1
fi

if [ "$target_mode" = "boot_dev" ]; then
  # Prepare output dev, name, grub dev name...
  # /dev/sda1 -> /dev/sda, sda1 (output_dev_hd, output_dev_name)
  output_dev_hd="$(echo $output_dev | sed -e "s/[[:digit:]]*$//g")"
  output_dev_name="$(basename $output_dev)"
  
  # check if the target exists
  # Todo: if it's devfs ?
  if [ -z "$(grep -Ew "$output_dev_name" /proc/partitions)" ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "$output_dev $msg_NOT_found!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    echo "$msg_program_stop"
    exit 1
  fi

  # check if the target is busy or not
  if [ -n "$(grep -Ew "^$output_dev" /proc/mounts)" ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "$msg_is_mounted_u_must_unmount_it: $output_dev"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    echo "$msg_this_is_disk_usage_status:"
    df -h
    echo "$msg_program_stop"
    exit 1
  fi
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_create_live_device_warning: $output_dev"
  echo "$msg_this_is_disk_usage_status:"
  parted -s $output_dev_hd print
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  if [ "$batch_mode" = "no" ]; then
    echo "$msg_are_u_sure_u_want_to_continue"
    echo -n "[y/N] "
    read cont_ans
    case "$cont_ans" in
      y|Y|[yY][eE][sS])
         echo $msg_ok_let_do_it
         ;;
      *)
         echo "Abort!"
         exit 2
    esac
  fi
fi

# Decide where is the $LIVE_MEDIA
get_live_media_mnt_point &>/dev/null

#
mkdir -p $ocsroot
USB_TMP="$(mktemp -d $ocsroot/ocs-usb-tmp.XXXXXX)"

img_cp_type=""
echo "Starting creating Clonezilla live USB files..."
if [ -n "$output_dev" ]; then
  # output_dev, e.g. /dev/sdc1 is set, therefore we have to copy files, not using symbolic.
  img_cp_type="copy"
fi
if [ -z "$img_cp_type" ]; then
  # First, we test if the file system on USB_TMP supports symbolic link
  tmpf="$(mktemp $USB_TMP/linktest.XXXXXX)"
  if ln -fs $tmpf $tmpf.link 2>/dev/null; then
    echo "File system on $USB_TMP/ supports symbolic link. The image file will be linked instead of being copied."
    img_cp_type="link"
  else
    echo "File system on $USB_TMP/ does _NOT_ supports symbolic link. The image file will be copied instead of being linked."
    img_cp_type="copy"
  fi
  # Clean the test files
  [ -L "$tmpf.link" ] && rm -f $tmpf.link   # remove the linked file before the real file.
  [ -e "$tmpf" ] && rm -f $tmpf
fi

#
trap clean_tmp_dir HUP INT QUIT TERM EXIT

# Try to find if it is running on live cd/usb already
if [ -z "$template_mode" ]; then
  if [ -n "$LIVE_MEDIA" ]; then
    # It's running from Clonezilla live media, use the existing resource
    template_mode="from-booting-live-media"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
    echo "Found a Clonezilla live media... Will use that as a template..."
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    check_if_in_netboot_env $LIVE_MEDIA
  else
    template_mode="from-downloaded-live-media"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
    echo "No Clonezilla live media was found... Will use the downloaded template from Clonezilla repository..."
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  fi 
fi 

if [ "$template_mode" = "from-booting-live-media" ]; then
  DEBIAN_ISO_TMP="$LIVE_MEDIA"
else
  DEBIAN_ISO_TMP="$(mktemp -d /tmp/ocs-iso.XXXXXX)"
  mount -o loop,ro $DEBIAN_ISO $DEBIAN_ISO_TMP
fi

# Check if EFI boot could be supported.
# Check bootx64.efi, bootia32.efi, bootaa64.efi, bootarm.efi or bootriscv64.efi.
if [ -e "$DEBIAN_ISO_TMP/EFI/boot/bootx64.efi" -o \
     -e "$DEBIAN_ISO_TMP/EFI/boot/bootia32.efi" ]; then
   cpu_arch_type="amd64"
elif [ -e "$DEBIAN_ISO_TMP/EFI/boot/bootaa64.efi"  ]; then
   cpu_arch_type="arm64"
elif [ -e "$DEBIAN_ISO_TMP/EFI/boot/bootarm.efi"  ]; then
   cpu_arch_type="armhf"
elif [ -e "$DEBIAN_ISO_TMP/EFI/boot/bootriscv64.efi"  ]; then
   cpu_arch_type="riscv64"
else
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "/EFI/boot/bootx64.efi, /EFI/boot/bootia32.efi, /EFI/boot/bootaa64.efi, /EFI/boot/bootarm.efi or /EFI/boot/bootriscv64.efi was not found in template Clonezilla live!"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_program_stop"
  exit 1
fi

if [ "$template_mode" = "from-downloaded-live-media" ]; then
  if [ ! -f "$DEBIAN_ISO" ]; then
    echo "Searching for the latest Clonezilla live iso..."
    cz_ver="$(LC_ALL=C get-latest-ocs-live-ver $clonezilla_iso_branch)"
    clonezilla_iso_fname="clonezilla-live-${cz_ver}${clonezilla_iso_arch_tag}.iso"
    iso_url_for_pxe_ocs_live="http://downloads.sourceforge.net/clonezilla/$clonezilla_iso_fname"
    echo "We need Clonnezilla live iso to create such a recovery cd."
    echo "Downloadling the iso file from $iso_url_for_pxe_ocs_live..."
    wget $iso_url_for_pxe_ocs_live
    get_iso_rc=$?
    # validate it
    if [ "$get_iso_rc" -eq 0 ]; then
      echo -n "Validating $clonezilla_iso_fname... "
      md5_tmp="$(mktemp -d /tmp/isomd5.XXXXXX)"
      #  wget http://free.nchc.org.tw/clonezilla-live/stable/MD5SUMS
      echo -n "Downloading $md5_file_url... "
      wget $wget_opt -P "$md5_tmp" $md5_file_url
      if ! grep -w "$clonezilla_iso_fname" $md5_tmp/$md5_file | md5sum -c; then
        [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
        echo "$DEBIAN_ISO is broken! Try to remove $clonezilla_iso_fname and run this program again."
        [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
        echo "$msg_program_stop"
        exit 1
      fi
      # Rename it to the template iso file name (fixed name)
      mv -f $clonezilla_iso_fname $DEBIAN_ISO
    else
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "Unable to download $iso_url_for_pxe_ocs_live!"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      echo "$msg_program_stop"
      exit 1
    fi
  else
    echo "A template iso ($DEBIAN_ISO) was found!"
  fi
fi

if [ "$insert_mode" = "prog_and_img" ]; then
  # 2 cases:
  # (1) normal_menu_with_insert_image=no, i.e. only restore menu. ocs-live-restore is the main program.
  # If it's not batch mode for clonezilla live (especially for restoring), by default we have to append "-x --restore-only", and especially before any other parameters, so that ocs-sr will run like:
  # ocs-sr -l $ocs_lang -p true -x --restore-only -b restoredisk squeeze_image
  # (2) normal_menu_with_insert_image=yes, i.e. normal menu. ocs-live-general is the main program. Threfore no extra param.
  if [ "$ocs_live_batch" = "no" ]; then
    case "$normal_menu_with_insert_image" in
      no) ocs_live_extra_param="-x --restore-only $ocs_live_extra_param" ;;
      yes) ocs_live_extra_param="" ;;
    esac
  fi

  echo "Creating clonezilla live with image(s) $ocs_image from $image_path..."
  # Use the 1st image name + CPU arch tag as the zip file name
  zip_label_tag="$(echo $ocs_image | awk -F" " '{print $1}')"-"${cpu_arch_type}"
  if [ "$use_existing_img_on_dev" = "no" ]; then
    # Image will be copied or linked from $ocsroot.
    for im in $ocs_image; do
      if [ ! -d "$image_path/$im" ]; then
        [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
        echo "$image_path/$im $msg_NOT_found!"
        [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
        echo "$msg_program_stop"
        exit 1
      fi
      convert_ocs_format_from_1.5_to_2.0_or_newer $image_path/$im/
      img_size="$(du -ms $image_path/$im/ | awk -F" " '{print $1}')" # unit: MB
      img_size_sum="$((img_size_sum+img_size))"
    done
  fi
else
  echo "Creating Clonezilla Live without any clonezilla image embedded..."
  # If version_no is not set, use date (Ex. 20070409)
  [ -z "$version_no" ] && version_no="$(date +%Y%m%d)"
  zip_label_tag="${version_no}"
fi
boot_menu_opt="--version-no $zip_label_tag"

#
WD="$(pwd)"
if [ "$target_mode" = "release_file" ]; then
  # assume boot loader as syslinux for release_file mode.
  boot_loader="syslinux"
else
  # mount the target boot device
  mount $output_dev $USB_TMP
  rc=$?
  if [ "$rc" -gt 0 ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "$msg_unable_to_mount_this_dev $output_dev!"
    echo "$msg_format_as_FAT_16_32:"
    echo "mkfs.vfat -F 16 $output_dev"
    echo "$msg_or"
    echo "mkfs.vfat -F 32 $output_dev"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    umount $DEBIAN_ISO_TMP
    exit 1
  fi
fi

#
# Possible kernel/initrd paths are /casper (created by casper) or /live (created by live-initramfs)
# Find the kernel and initrd in $DEBIAN_ISO_TMP/casper or $DEBIAN_ISO_TMP/live
# Ex: $DEBIAN_ISO_TMP/casper/vmlinuz1, /$DEBIAN_ISO_TMP/casper/initrd1.img
# For riscv64, the Linux kernel is vmlinux-6.11.5-riscv64 (not vmlinuz-*).
# $live_sys_files_dir_list is from drbl-ocs.conf.
sys_files_dir=""
for i in $live_sys_files_dir_list; do
  krnfile="$(find $DEBIAN_ISO_TMP/$i/ -maxdepth 1 -name "vmlinu[xz]*" -print 2>/dev/null)"
  if [ -n "$krnfile" ]; then
    krnfile="$(basename $krnfile)"
    sys_files_dir="$i"
    irdfile="$(find $DEBIAN_ISO_TMP/$i/ -maxdepth 1 -name "initrd*" -print)"
    irdfile="$(basename $irdfile)"
    break
  fi
done

if [ -z "$sys_files_dir" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "No system files from template live iso are found!"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_program_stop"
  exit 1
fi

if [ -z "$krnfile" -o -z "$irdfile" ]; then
   [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
   echo "Kernel and initrd files NOT found in path $DEBIAN_ISO_TMP/$sys_files_dir/!"
   echo "$msg_program_stop"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   exit 1
fi

#
template_iso_size=0
if [ "$template_mode" = "from-booting-live-media" ]; then
  # From boot media (clonezilla live), DEBIAN_ISO_TMP is LIVE_MEDIA
  # The dirs/files in Clonezilla live (amd64):
  # For arm64/armhf/riscv64, only: EFI live
  # -r--r--r--  Clonezilla-Live-Version
  # -r--r--r--  GPL
  # dr-xr-xr-x  .disk/
  # dr-xr-xr-x  EFI/
  # dr-xr-xr-x  live/
  # dr-xr-xr-x  syslinux/
  # dr-xr-xr-x  utils/
  # We need all of them.
  f_dir_chklist="Clonezilla-Live-Version GPL .disk EFI $sys_files_dir syslinux utils"
  for i in $f_dir_chklist; do 
    if [ -e "$LIVE_MEDIA/$i" ]; then
       template_iso_size="$(($template_iso_size + $(du -Lmsc "$LIVE_MEDIA/$i" | tail -n 1 | awk -F" " '{print $1}')))"
    fi
  done
else
  # template_mode is "from-downloaded-live-media"
  # From iso file (debian-live-for-ocs.iso)
  # The dirs/files in Debian live for OCS (amd64):
  # dr-xr-xr-x 1 root root 2.0K Nov 22 12:08 boot/
  # dr-xr-xr-x 1 root root 2.0K Nov 22 12:10 efi/
  # dr-xr-xr-x 1 root root 2.0K Nov 22 12:10 EFI/
  # -r--r--r-- 1 root root 320K Nov 22 12:10 efi.img
  # dr-xr-xr-x 1 root root 4.0K Nov 22 12:10 isolinux/
  # dr-xr-xr-x 1 root root 2.0K Nov 22 12:10 live/
  # -r--r--r-- 1 root root  36K Nov 22 12:10 md5sum.txt
  # We only need dirs: boot EFI isolinux live
  # For arm64/armhf/riscv64, only: EFI live
  f_dir_chklist="boot EFI isolinux $sys_files_dir"
  for i in $f_dir_chklist; do 
    if [ -e "$DEBIAN_ISO_TMP/$i" ]; then
       template_iso_size="$(($template_iso_size + $(du -Lmsc "$DEBIAN_ISO_TMP/$i" | tail -n 1 | awk -F" " '{print $1}')))"
    fi
  done
fi
target_files_size="$(($template_iso_size + $img_size_sum))"

echo "Estimated necessary space size in target dev: $target_files_size MB"

# 2020/Aug/17 Newer zip can create zip file larger than 2 GB. Comment this.
# if [ "$target_files_size" -gt 2000 ]; then
#   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
#   echo "Because currently 'zip' does not support file larger than 2 GB very well, we switch to use 'tar' to create the image."
#   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
#   if [ "$batch_mode" = "no" ]; then
#     echo -n "$msg_press_enter_to_continue..."
#     read
#   fi
#   prefer_archive="tar"
# fi

#
if [ "$target_mode" = "release_file" ]; then
  if [ -n "$target_zip_prefix" ]; then
    output_filename="${target_zip_prefix}.${prefer_archive}"
  else
    output_filename="clonezilla-live-${zip_label_tag}.${prefer_archive}"
  fi
  echo "The output file name is: $output_filename"
  # Clean the stale zip file
  [ -f "$output_filename" ] && rm -f $output_filename
fi

echo -n "Preparing the system files to working dir... This might take a few minutes... "
# //NOTE// Not all the files in $DEBIAN_ISO_TMP/ will be copied. Only the required files. Otherwise other existing files on the USB flash drive will be copied.
# Instead of copying files, we use link to save time and space
case "$img_cp_type" in
  copy) echo "Copying dir $DEBIAN_ISO_TMP/$sys_files_dir to $USB_TMP/"
        rsync -a $DEBIAN_ISO_TMP/$sys_files_dir $USB_TMP/
        chmod 755 $USB_TMP/$sys_files_dir
        ;;
  link) echo "Linking files from $DEBIAN_ISO_TMP/$sys_files_dir/ to $USB_TMP/$sys_files_dir..."
        mkdir -p $USB_TMP/$sys_files_dir 
        ( cd $USB_TMP/$sys_files_dir
          for i in $DEBIAN_ISO_TMP/$sys_files_dir/*; do
           [ ! -e "$i" ] && continue 
           ln -fs $i "$(basename $i)"
          done
        )
        ;;
esac
# More dir or files. Dirs need to be modified or small files, just copy them.
rsync -a $DEBIAN_ISO_TMP/boot $DEBIAN_ISO_TMP/.disk $USB_TMP/
# Remove unnecessary files in $USB_TMP/boot/
rm -rf $USB_TMP/boot/grub/{live-theme,loopback.cfg,grub.cfg,splash.png}
echo "done!"

cp -af $DRBL_SCRIPT_PATH/doc/GPL $USB_TMP/

if [ "$cpu_arch_type" = "amd64" ]; then
  # replace the original syslinux and related files.
  mkdir -p $USB_TMP/syslinux/
  # For syslinux
  syslinux_related_files="$isolinux_file $pxelinux_simple_vesamenu $pxelinux_simple_menu $pxelinux_memdisk_file $pxelinux_bg_img $ocs_logo_img_png $pxelinux_chain_file"
  # If it's in Clonezilla live environment, we have those files. Use that first so the version mismatch can be avoided.
  for i in $syslinux_related_files; do
    if [ -e "$DEBIAN_ISO_TMP/syslinux/$(basename $i)" ]; then
      cp -af "$DEBIAN_ISO_TMP/syslinux/$(basename $i)" $USB_TMP/syslinux/
    else
      cp -af $i $USB_TMP/syslinux/
    fi
  done
  # For Syslinux >= 5, new .c32 are required: ldlinux.c32, libcom32.c32, libutil.c32
  for i in $sys_pxelinux_v5p_required_c32; do
    if [ -e "$DEBIAN_ISO_TMP/syslinux/$i" ]; then
      cp -af "$DEBIAN_ISO_TMP/syslinux/$i" $USB_TMP/syslinux/
    elif [ -e "$pxelinux_binsrc_dir/$i" ]; then
      # syslinux <= 5 
      cp -af $pxelinux_binsrc_dir/$i $USB_TMP/syslinux/
    elif [ -e "$pxelinux_binsrc_dir/bios/$i" ]; then
      # syslinux >= 6, different path
      cp -af $pxelinux_binsrc_dir/bios/$i $USB_TMP/syslinux/
    fi
  done
fi

# 2. For EFI machine
mkdir -p $USB_TMP/EFI/boot/
# Copy the EFI boot files
if [ "$live_efi_boot_loader" = "grub" ]; then
  if [ -e "$DEBIAN_ISO_TMP/EFI/boot/bootia32.efi" -o -e "$DEBIAN_ISO_TMP/EFI/boot/bootx64.efi" -o \
       -e "$DEBIAN_ISO_TMP/EFI/boot/bootaa64.efi" -o -e "$DEBIAN_ISO_TMP/EFI/boot/bootarm.efi" -o \
       -e "$DEBIAN_ISO_TMP/EFI/boot/bootriscv64.efi" ]; then
    cp -af $DEBIAN_ISO_TMP/EFI $USB_TMP/
    cp -af $ocs_logo_grub2_img_png $USB_TMP/boot/grub/
  else
    [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
    echo "Warning! Missing /EFI/boot/bootia32.efi, /EFI/boot/bootx64.efi, /EFI/boot/bootaa64.efi, /EFI/boot/bootarm.efi or /EFI/boot/bootriscv64.efi in the template iso!"
    echo "uEFI booting won't work!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  fi
elif [ "$live_efi_boot_loader" = "syslinux" ]; then
   # Those files are prepared in /usr/share/drbl/pkg/syslinux/efi64/ by drbl-prepare-pxelinux
   cp -af $pxelinux_binsrc_dir/efi64/syslinux.efi $USB_TMP/EFI/boot/bootx64.efi 
   cp -af $pxelinux_binsrc_dir/efi64/ldlinux.e64 $USB_TMP/EFI/boot/
   cp -af $pxelinux_binsrc_dir/efi64/*.c32 $USB_TMP/EFI/boot/
   cp -af $ocs_logo_grub2_img_png $USB_TMP/boot/grub/
fi
# $USB_TMP/EFI and its sub dirs might be not in the mode of 755 since they are copied from the files from iso. We chmod to 755 so that it's easier to remove them by "rm -rf" when it's not used anymore.
# This especially for zip file, not iso file, because iso file won't only mount it, not unzip it.
chmod 755 $USB_TMP/EFI
find $USB_TMP/EFI -type d -exec chmod 755 {} \;

if [ -n "$include_dir" ]; then
  cp -af $include_dir $USB_TMP/
fi

#
etherboot_zlilo="$($query_pkglist_cmd drbl-etherboot 2>/dev/null | grep -E "eb-.*-etherboot-pci.zlilo$")"
# we have to force it name as etherboot.zdsk, since isolinux only uses the "plain" ISO 9660 filenames, i.e. it does not support Rock Ridge or Joliet filenames.
# ref: http://syslinux.zytor.com/archives/2006-October/007440.html
# "-" will be regards as "_" if you want to use "-" for isolinux.
# In syslinux on vfat, etherboot.zlilo is too long, make it ever shorter as eb.zli
if [ -n "$etherboot_zlilo" ]; then
  # This is run in DRBL server
  cp -af $etherboot_zlilo $USB_TMP/$sys_files_dir/eb.zli
else
  # This is run in Clonezilla live with casper or live mechanism
  for i in $live_sys_files_dir_list; do
    if [ -e "$LIVE_MEDIA/$i/eb.zli" ] && [ ! -e "$USB_TMP/$sys_files_dir/eb.zli" ]; then
      cp -af $LIVE_MEDIA/$i/eb.zli $USB_TMP/$sys_files_dir/eb.zli
      break
    fi
  done
fi
# Same reason, we have to use different name in syslinux
# gPXE is deprecated, we will use iPXE. We keep this gpxe part for ref only.
gpxe_lkn="$($query_pkglist_cmd gpxe 2>/dev/null | grep -E "gpxe.lkrn$")"
if [ -n "$gpxe_lkn" ]; then
  # This is run in DRBL server
  cp -af $gpxe_lkn $USB_TMP/$sys_files_dir/gpxe.lkn
else
  # This is run in Clonezilla live with casper or live mechanism
  for i in $live_sys_files_dir_list; do
    if [ -e "$LIVE_MEDIA/$i/gpxe.lkn" ] && [ ! -e "$USB_TMP/$sys_files_dir/gpxe.lkn" ]; then
      cp -af $LIVE_MEDIA/$i/gpxe.lkn $USB_TMP/$sys_files_dir/gpxe.lkn
      break
    fi
  done
fi
# Same reason, we have to use different name in syslinux
# For ipxe from Debian, there are 2 "ipxe.lkrn", and one of them is symbolic file:
# $ dpkg -c ipxe_1.0.0+git-20150424.a25a16d-1_all.deb | grep ipxe.lkrn
# -rw-r--r-- root/root    279677 2015-04-30 03:04 ./boot/ipxe.lkrn
# lrwxrwxrwx root/root         0 2015-04-30 03:04 ./usr/lib/ipxe/ipxe.lkrn -> /boot/ipxe.lkrn
# Therefore we force to use "-L" option in cp command
ipxe_lkn="$(LC_ALL=C $query_pkglist_cmd ipxe 2>/dev/null | grep -Ew "ipxe.lkrn$" | tail -n 1)"
if [ -n "$ipxe_lkn" ]; then
  # This is run in DRBL server
  cp -afL $ipxe_lkn $USB_TMP/$sys_files_dir/ipxe.lkn
else
  # This is run in Clonezilla live with casper or live mechanism
  for i in $live_sys_files_dir_list; do
    if [ -e "$LIVE_MEDIA/$i/ipxe.lkn" ] && [ ! -e "$USB_TMP/$sys_files_dir/ipxe.lkn" ]; then
      cp -afL $LIVE_MEDIA/$i/ipxe.lkn $USB_TMP/$sys_files_dir/ipxe.lkn
      break
    fi
  done
fi
# For ipxe from Debian, there are 2 "ipxe.efi", and one of them is symbolic file:
# $ dpkg -c ipxe_1.0.0+git-20150424.a25a16d-1_all.deb | grep ipxe.efi
# -rw-r--r-- root/root    754752 2015-04-30 03:04 ./boot/ipxe.efi
# lrwxrwxrwx root/root         0 2015-04-30 03:04 ./usr/lib/ipxe/ipxe.efi -> /boot/ipxe.efi
# Therefore we force to use "-L" option in cp command
ipxe_efi="$(LC_ALL=C $query_pkglist_cmd ipxe 2>/dev/null | grep -Ew "ipxe.efi$" | tail -n 1)"
if [ -n "$ipxe_efi" ]; then
  # This is run in DRBL server
  cp -afL $ipxe_efi $USB_TMP/$sys_files_dir/ipxe.efi
else
  # This is run in Clonezilla live with casper or live mechanism
  for i in $live_sys_files_dir_list; do
    if [ -e "$LIVE_MEDIA/$i/ipxe.efi" ] && [ ! -e "$USB_TMP/$sys_files_dir/ipxe.efi" ]; then
      cp -afL $LIVE_MEDIA/$i/ipxe.efi $USB_TMP/$sys_files_dir/ipxe.efi
      break
    fi
  done
fi
# same reason, we have to use different name in syslinux
if [ -e "$fdos_img_src" ] ; then
  cp -af $fdos_img_src $USB_TMP/$sys_files_dir/freedos.img
else
  # This is run in Clonezilla live with casper or live mechanism
  for i in $live_sys_files_dir_list; do
    if [ -e "$LIVE_MEDIA/$i/freedos.img" ] && [ ! -e "$USB_TMP/$sys_files_dir/freedos.img" ]; then
      cp -af $LIVE_MEDIA/$i/freedos.img $USB_TMP/$sys_files_dir/freedos.img
      break
    fi
  done
fi
# $memtest86_file (memtest86) is 9 characters, will go wrong when it's FAT (usb flash drive). We use memtest to overwrite the one comes from Debian live.
if [ -n "$(ls -l $memtest86_dir/mt86+x* 2>/dev/null)" ]; then
  cp -af $memtest86_dir/mt86+x* $USB_TMP/$sys_files_dir/
else
  # This is run in Clonezilla live with casper or live mechanism
  for i in $live_sys_files_dir_list; do
    if [ -e "$LIVE_MEDIA/$i/mt86+x*" ]; then
      cp -af $LIVE_MEDIA/$i/mt86+x* $USB_TMP/$sys_files_dir/
      break
    fi
  done
fi

# Decide ocs_live_run
# $DRBL_SCRIPT_PATH/sbin/custom-ocs is copied from $LIVE_MEDIA/pkg/custom-ocs by /etc/ocs/ocs-live.d/S03prep-drbl-clonezilla when booting
if [ -n "$custom_ocs" ]; then
    # If $custom_ocs is found, put it in pkg/ as custom-ocs
    mkdir -p $USB_TMP/pkg
    cp -af $custom_ocs $USB_TMP/pkg/custom-ocs
    ocs_live_run="custom-ocs"
else
  if [ "$insert_mode" = "prog_and_img" ]; then
    case "$normal_menu_with_insert_image" in
      no) ocs_live_run="ocs-live-restore" ;;
      yes) ocs_live_run="ocs-live-general" ;;
    esac
  else # prog_only mode
    ocs_live_run="ocs-live-general"
  fi
fi

# create dir $ocsroot in target dev
mkdir -p $USB_TMP/$ocsroot
if [ -n "$ocs_image" ]; then
  if [ "$use_existing_img_on_dev" = "no" ]; then
    # Image will be copied or linked from $ocsroot.
    # When image is included, we force not to compress otherwise it will take a lot of time.
    zip_compress_opt="-0"
    echo -n "Copying clonezilla image to working dir... "
    for im in $ocs_image; do
      echo -n "$im "
      # we just copy images to $ocsroot in usb debice. since $ocsroot in drbl.conf will be read when ocs-live-save-*/ocs-live-restore is run
      # If the file system of $USB_TMP/$ocsroot/ supports link, we will link the image, and when zip or tar, follow the link. Actually zip will follow the link automatically since it does not support archiving link file. For tar we have to use  option "h".
      case "$img_cp_type" in
        copy)
             # Since we will always use /home/partimag as the image root in Clonezilla live, here the path in the created iso is /home/partimag/
    	 mkdir -p $USB_TMP/home/partimag
             cp -rfL $image_path/$im $USB_TMP/home/partimag/
             ;;
        link)
             # Since we will always use /home/partimag as the image root in Clonezilla live, here the path in the created iso is /home/partimag/
    	 mkdir -p $USB_TMP/home/partimag
             ( cd $USB_TMP/home/partimag/
               ln -fs $image_path/$im
             )
             ;;
      esac
    done
    echo "done!"
  else
    echo "Use the existing image \"$ocs_image\" on the device $output_dev under path $ocsroot."
  fi
fi

# put boot loader in MBR if not assign
[ -z "$boot_loader" ] && set_boot_loader $output_dev
# check & format boot_loader
case "$boot_loader" in
  grub|GRUB)         boot_loader="grub" ;;
  syslinux|SYSLINUX) boot_loader="syslinux" ;;
  *) 
     [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
     echo "Unknown boot loader $boot_loader!"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
     echo "$msg_program_stop"
     exit 1
esac

echo "Copying kernel, initrd, etc..."
mkdir -p $USB_TMP/$sys_files_dir
case "$boot_loader" in
  grub)
      # For grub, we force to put the kernel and initrd in /boot of device.
      mkdir -p $USB_TMP/boot/grub
      cp -a $DEBIAN_ISO_TMP/$sys_files_dir/{$krnfile,$irdfile} $USB_TMP/boot/
      cp -af $ocs_logo_img_xpm $USB_TMP/boot/grub
      kernel_on_dev="/boot/$krnfile"
      initrd_on_dev="/boot/$irdfile"
      ;;
  syslinux)
      case "$img_cp_type" in
        copy) echo "Copying dir $DEBIAN_ISO_TMP/$sys_files_dir to $USB_TMP/"
              cp -a $DEBIAN_ISO_TMP/$sys_files_dir/{$krnfile,$irdfile} $USB_TMP/$sys_files_dir/
              ;;
        link)
              if [ ! -e "$USB_TMP/$sys_files_dir/$krnfile" ]; then
                ln -fs $DEBIAN_ISO_TMP/$sys_files_dir/$krnfile $USB_TMP/$sys_files_dir/$krnfile
              fi
              if [ ! -e "$USB_TMP/$sys_files_dir/$irdfile" ]; then
                ln -fs $DEBIAN_ISO_TMP/$sys_files_dir/$irdfile $USB_TMP/$sys_files_dir/$irdfile
              fi
              ;;
      esac
      kernel_on_dev="/$sys_files_dir/$krnfile"  # e.g. /live/vmlinuz1
      initrd_on_dev="/$sys_files_dir/$irdfile"
      ;;
esac
# put version tag
# The content is like clonezilla-live-20070308
echo "clonezilla-live-${zip_label_tag}" > $USB_TMP/Clonezilla-Live-Version
echo "This Clonezilla live zip file was created by this command:" >> $USB_TMP/Clonezilla-Live-Version
echo "$prog $prog_option" >> $USB_TMP/Clonezilla-Live-Version
if [ ! -e $USB_TMP/$sys_files_dir/Clonezilla-Live-Version ]; then
  # If it's not linked yet (For the mode $img_cp_type=copy)
  # Copy another one in the live dir. It will be used for toram=live mode.
  cp -a $USB_TMP/Clonezilla-Live-Version $USB_TMP/$sys_files_dir/
fi
create_cpu_arch_tag_in_live $USB_TMP Clonezilla

# Find the boot param $boot_param, also $splash_opt and $quiet_opt will be gotten
if [ "$template_mode" = "from-booting-live-media" ]; then
  # From boot media (clonezilla live), DEBIAN_ISO_TMP is LIVE_MEDIA
  # Jan/19/2014 We have unified syslinux and isolinux to one dir "syslinux"
  if [ -d "$DEBIAN_ISO_TMP/syslinux/" ]; then
    get_live_boot_param $DEBIAN_ISO_TMP/syslinux/  # Got boot_param
  elif [ -d "$DEBIAN_ISO_TMP/boot/grub/" ]; then
    get_live_boot_param $DEBIAN_ISO_TMP/boot/grub/ # Got boot_param
  fi
else
  # From iso file (debian-live-for-ocs.iso)
  if [ -d "$DEBIAN_ISO_TMP/syslinux/" ]; then
    get_live_boot_param $DEBIAN_ISO_TMP/isolinux/  # Got boot_param
  elif [ -d "$DEBIAN_ISO_TMP/boot/grub/" ]; then
    get_live_boot_param $DEBIAN_ISO_TMP/boot/grub/ # Got boot_param
  fi
fi

if [ "$splash_opt" = "splash" ]; then
  splash_switch="-sp"
else
  splash_switch=""
fi
if [ "$quiet_opt" = "quiet" ]; then
  quiet_switch="-q"
else
  quiet_switch=""
fi

# Prepare the sort file for xorriso. Although we do not use xorriso here, however, we'd like the file created by ocs-live-dev is the same with that of ocs-iso. So it's easier to convert zip file to iso file.
# Therefore we still put the file iso_sort.txt here.
if [ -d "$USB_TMP/syslinux/" -o -d "$USB_TMP/isolinux/" ]; then
  gen_iso_sort_file $USB_TMP/syslinux/iso_sort.txt
fi

# Make sure no extra white space in the end of ocs_live_run and ocs_live_extra_param
# Otherwise it might cause the boot parameters to be wrongly parsed, i.e. the boot parameters:
# ocs_live_run="clonezilla -l en_US.UTF-8 -p reboot -k  " net.ifnames=0
# is wrongly parsed by Linux kernel 4.1 so that net.ifnames=0 won't take effect.
ocs_live_run="$(LC_ALL=C echo "$ocs_live_run" | sed -r -e "s/[[:space:]]*$//g")"
ocs_live_extra_param="$(LC_ALL=C echo "$ocs_live_extra_param" | sed -r -e "s/[[:space:]]*$//g")"

#
if [ -n "$ocs_image" ]; then
  # Image is included
  # For syslinux
  if [ "$cpu_arch_type" = "amd64" ]; then
    # Dir syslinux only for x86 CPU arch
    ocs-live-boot-menu -j -vb $splash_switch $quiet_switch $ocs_live_boot_menu_timeout_option $ocs_live_boot_menu_option -l $lang_answer -f $VGA_MODE_DEF -b $bg_mode -k $kernel_on_dev -i $initrd_on_dev -m $ocs_logo_img_png $boot_menu_opt --boot-param "$boot_param $live_extra_boot_param $supp_boot_param_ocs_live_dev locales=$ocs_lang keyboard-layouts=$ocs_live_keymap ocs_live_run=\"$ocs_live_run\" ocs_live_extra_param=\"$ocs_live_extra_param\" ocs_live_batch=\"$ocs_live_batch\"" --title "clonezilla live with img $ocs_image" --toram-mode live -r "* Boot menu for BIOS machine" syslinux $USB_TMP/syslinux/
    # For isolinux
    ocs-live-boot-menu -j -vb $splash_switch $quiet_switch $ocs_live_boot_menu_timeout_option $ocs_live_boot_menu_option -l $lang_answer -f $VGA_MODE_DEF -b $bg_mode -k $kernel_on_dev -i $initrd_on_dev -m $ocs_logo_img_png $boot_menu_opt --boot-param "$boot_param $live_extra_boot_param $supp_boot_param_ocs_live_iso locales=$ocs_lang keyboard-layouts=$ocs_live_keymap ocs_live_run=\"$ocs_live_run\" ocs_live_extra_param=\"$ocs_live_extra_param\" ocs_live_batch=\"$ocs_live_batch\"" --title "clonezilla live with img $ocs_image" --toram-mode live -r "* Boot menu for BIOS machine" isolinux $USB_TMP/syslinux/
  fi
  # Dir /EFI/boot/ is for amd64, arm64, armhf or riscv64 CPU arch
  # For EFI, grub2 efi or syslinux efi
  if [ "$live_efi_boot_loader" = "grub" ]; then
    if [ -e "$USB_TMP/EFI/boot/bootia32.efi" -o -e "$USB_TMP/EFI/boot/bootx64.efi" -o \
	 -e "$USB_TMP/EFI/boot/bootaa64.efi" -o -e "$USB_TMP/EFI/boot/bootarm.efi" -o \
	 -e "$USB_TMP/EFI/boot/bootriscv64.efi" ]; then
      ocs-live-boot-menu -j -vb $splash_switch $quiet_switch $ocs_live_boot_menu_timeout_option $ocs_live_boot_menu_option -l $lang_answer -f $VGA_MODE_DEF -b $bg_mode -k $kernel_on_dev -i $initrd_on_dev -m $ocs_logo_grub2_img_png $boot_menu_opt --boot-param "$boot_param $live_extra_boot_param $supp_boot_param_ocs_live_dev locales=$ocs_lang keyboard-layouts=$ocs_live_keymap ocs_live_run=\"$ocs_live_run\" ocs_live_extra_param=\"$ocs_live_extra_param\" ocs_live_batch=\"$ocs_live_batch\"" --title "clonezilla live with img $ocs_image" --toram-mode live -r "* Boot menu for EFI machine" grub2-efi $USB_TMP/boot/grub/
    fi
  elif [ "$live_efi_boot_loader" = "syslinux" ]; then
    ocs-live-boot-menu -j -vb $splash_switch $quiet_switch $ocs_live_boot_menu_timeout_option $ocs_live_boot_menu_option -l $lang_answer -f $VGA_MODE_DEF -b $bg_mode -k $kernel_on_dev -i $initrd_on_dev -m $ocs_logo_img_png $boot_menu_opt --boot-param "$boot_param $live_extra_boot_param $supp_boot_param_ocs_live_dev locales=$ocs_lang keyboard-layouts=$ocs_live_keymap ocs_live_run=\"$ocs_live_run\" ocs_live_extra_param=\"$ocs_live_extra_param\" ocs_live_batch=\"$ocs_live_batch\"" --title "clonezilla live with img $ocs_image" --toram-mode live -r "* Boot menu for EFI machine" syslinux $USB_TMP/boot/grub/
  fi
else
  # No image is included
  # For syslinux
  if [ "$cpu_arch_type" = "amd64" ]; then
    # Dir syslinux only for x86 CPU arch
    ocs-live-boot-menu -j -vb $splash_switch $quiet_switch $ocs_live_boot_menu_timeout_option $ocs_live_boot_menu_option -l $lang_answer -f $VGA_MODE_DEF -b $bg_mode -k $kernel_on_dev -i $initrd_on_dev -m $ocs_logo_img_png $boot_menu_opt --boot-param "$boot_param $live_extra_boot_param $supp_boot_param_ocs_live_dev locales=$ocs_lang keyboard-layouts=$ocs_live_keymap ocs_live_run=\"$ocs_live_run\" ocs_live_extra_param=\"$ocs_live_extra_param\" ocs_live_batch=\"$ocs_live_batch\"" --toram-mode live -r "* Boot menu for BIOS machine" syslinux $USB_TMP/syslinux/
    # For isolinux
    ocs-live-boot-menu -j -vb $splash_switch $quiet_switch $ocs_live_boot_menu_timeout_option $ocs_live_boot_menu_option -l $lang_answer -f $VGA_MODE_DEF -b $bg_mode -k $kernel_on_dev -i $initrd_on_dev -m $ocs_logo_img_png $boot_menu_opt --boot-param "$boot_param $live_extra_boot_param $supp_boot_param_ocs_live_iso locales=$ocs_lang keyboard-layouts=$ocs_live_keymap ocs_live_run=\"$ocs_live_run\" ocs_live_extra_param=\"$ocs_live_extra_param\" ocs_live_batch=\"$ocs_live_batch\"" --toram-mode live -r "* Boot menu for BIOS machine" isolinux $USB_TMP/syslinux/
  fi
  # Dir /EFI/boot/ is for amd64, arm64, armhf or riscv64 CPU arch
  # For EFI, grub2 efi or syslinux efi
  if [ "$live_efi_boot_loader" = "grub" ]; then
    if [ -e "$USB_TMP/EFI/boot/bootia32.efi" -o -e "$USB_TMP/EFI/boot/bootx64.efi" -o \
	 -e "$USB_TMP/EFI/boot/bootaa64.efi" -o -e "$USB_TMP/EFI/boot/bootarm.efi" -o\
	 -e "$USB_TMP/EFI/boot/bootriscv64.efi" ]; then
      ocs-live-boot-menu -j -vb $splash_switch $quiet_switch $ocs_live_boot_menu_timeout_option $ocs_live_boot_menu_option -l $lang_answer -f $VGA_MODE_DEF -b $bg_mode -k $kernel_on_dev -i $initrd_on_dev -m $ocs_logo_grub2_img_png $boot_menu_opt --boot-param "$boot_param $live_extra_boot_param $supp_boot_param_ocs_live_dev locales=$ocs_lang keyboard-layouts=$ocs_live_keymap ocs_live_run=\"$ocs_live_run\" ocs_live_extra_param=\"$ocs_live_extra_param\" ocs_live_batch=\"$ocs_live_batch\"" --toram-mode live -r "* Boot menu for EFI machine" grub2-efi $USB_TMP/boot/grub/
    fi
  elif [ "$live_efi_boot_loader" = "syslinux" ]; then
    ocs-live-boot-menu -j -vb $splash_switch $quiet_switch $ocs_live_boot_menu_timeout_option $ocs_live_boot_menu_option -l $lang_answer -f $VGA_MODE_DEF -b $bg_mode -k $kernel_on_dev -i $initrd_on_dev -m $ocs_logo_img_png $boot_menu_opt --boot-param "$boot_param $live_extra_boot_param $supp_boot_param_ocs_live_dev locales=$ocs_lang keyboard-layouts=$ocs_live_keymap ocs_live_run=\"$ocs_live_run\" ocs_live_extra_param=\"$ocs_live_extra_param\" ocs_live_batch=\"$ocs_live_batch\"" --toram-mode live -r "* Boot menu for EFI machine" syslinux $USB_TMP/boot/grub/
  fi
fi

if [ "$cpu_arch_type" = "amd64" ]; then
  echo "Preparing syslinux, syslinux.exe, makeboot.sh, and makeboot.bat in dir utils... "
  # If it's in Clonezilla live environment, we have those files
  if [ -e "$LIVE_MEDIA/utils/linux/x64/syslinux" -a \
       -e "$LIVE_MEDIA/utils/linux/x86/syslinux" -a \
       -e "$LIVE_MEDIA/utils/mbr/mbr.bin" -a \
       -e "$LIVE_MEDIA/utils/win64/syslinux64.exe" -a \
       -e "$LIVE_MEDIA/utils/win32/syslinux.exe" ]; then
    cp -af $LIVE_MEDIA/utils $USB_TMP
    if [ -d "$LIVE_MEDIA/syslinux" ]; then
      # Since syslinux (for GNU/Linux) and syslinux*.exe are copied, to make the version consistent,
      # the other related files should be copied, too.
      cp -af $LIVE_MEDIA/syslinux/{*.c32,*.bin,memdisk} $USB_TMP/syslinux/
    fi
  else
    # Since we can not judge the version from any files in $USB_TMP/syslinux, we use $USB_TMP/isolinux/isolinux.bin.
    if [ -z "$isolinux_ver" ]; then
      isolinux_ver="$(LC_ALL=C strings $USB_TMP/syslinux/isolinux.bin 2>/dev/null | grep "^ISOLINUX" | awk -F" " '{print $2}')"
    fi
    if [ -n "$isolinux_ver" ]; then
      put_syslinux_makeboot_for_usb_flash $USB_TMP $isolinux_ver
    fi
  fi
fi

#
if [ "$target_mode" = "boot_dev" ]; then
  mkbt_opt=""
  if [ "$batch_mode" = "yes" ]; then
    mkbt_opt="-b"
  fi
  echo "Making $output_dev_hd bootable..."
  bash $USB_TMP/utils/linux/makeboot.sh $mkbt_opt $output_dev
  sleep 1
  umount $USB_TMP
else
  # just store it. since big files, like squash flie and opt_drbl.tgz are compressed, it's not necessary to compress it again.
  # Before packing it, make sure all the modes are writable so that the uncompressed files can be removed easily with rm -rf.
  chmod -R u+w $USB_TMP
  (cd $USB_TMP
   echo "Packing the output file $output_filename..."
   if [ "$prefer_archive" = "tar" ]; then
     tar --dereference -cvf $WD/$output_filename ./
   else
     zip $zip_compress_opt -r $WD/$output_filename ./
   fi
  )
  echo "The created release file is $output_filename. You can extract all the files into your pendrive, and use utils/linux/makeboot.sh on GNU/Linux or use utils/win32/makeboot.bat from pendrive on MS windows."
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "Warning: DO NOT RUN makeboot.bat from your local hard drive on MS Windows!!! It is intended to be run from your USB device."
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
fi

# unmount all iso file
umount $DEBIAN_ISO_TMP &>/dev/null

# Clean the tmp working directory
echo "Cleaning tmp dirs..."
if [ -d "$DEBIAN_ISO_TMP" -a -n "$DEBIAN_ISO_TMP" -a \
	"$template_mode" = "from-downloaded-live-media" ]; then
  rmdir $DEBIAN_ISO_TMP
fi
[ -d "$USB_TMP" -a -n "$(echo $USB_TMP | grep "ocs-usb-tmp")" ] && rm -rf $USB_TMP

echo "Done!"
if [ "$target_mode" = "boot_dev" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_boot_clonezilla_live_dev: $output_dev_hd"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
fi

exit 0