File: create-ubuntu-live

package info (click to toggle)
clonezilla 5.14.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,168 kB
  • sloc: sh: 41,203; perl: 193; python: 59; makefile: 26
file content (628 lines) | stat: -rwxr-xr-x 27,983 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
#!/bin/bash
# Author: Steven Shiau <steven _at_ clonezilla org>
# License: GPL
# Description: This script will create a Ubuntu live CD iso which is used as a template for clonezilla image with restoration function.

#
set -e

#
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

# ubuntu_mirror_url_def, ubuntu_mirror_security_url_def, DRBL_REPOSITORY_URL_def and DRBL_GPG_KEY_URL are loaded from drbl-ocs.conf

# debian_type can be minimal (about 67 MB for Etch)/minimal-net (about 85 MB for Etch).
# 
# Since from live-build 3.0~a55 "minimal" for debootstrap was removed. We use the option --variant of "debootstrap", i.e. "minbase" for minimal.
debian_type="minbase"
#debian_type="standard"
DEBIAN_DIST_DEF="resolute"
pkgs="$PKG_FROM_DBN_WHICH_OCS_LIVE_NEED drbl $PKG_FROM_DRBL_FOR_CLONEZILLA_LIVE"
categories_default="main restricted universe multiverse"
cpu_flavor_default="amd64"
bootstrap_default="mmdebstrap"
workdir="ubuntu-live"
# RAM as the working dir. Speed up the live system creation, but need RAM space.
use_ramfs="no"
ramfs_size_def="7516192768"  # Unit: bytes. Use for mount -t tmpfs if use_ramfs=yes

# The files in dir $ocs_live_script_dir/ will be copied to the dir /live-hook-dir in dir chroot. The file "ocs-live-hook" is in $ocs_live_script_dir
# We put some files in dir ocs_minimal_hook/ to do some jobs, like clean unnecessary files, set locales...
ocs_live_script_dir="$DRBL_SCRIPT_PATH/setup/files/ocs/live-hook"
# The script inside $ocs_live_script_dir/ will be run when chroot. There are many files in $ocs_live_script_dir/, we will just run one here.
run_hook_script="ocs-live-hook"
# This hook is for binary_local-hooks, not for chroot hook
run_binary_hook_script="efi-misc-binary-hook"
# The option to create a corresponding source image.
gen_source_tarball="no"
kernel_pkg=""
kernel_extra_pkg=""

#
check_if_root
#
prog="$(basename $0)"

# functions
USAGE() {
    echo "Usage:"
    echo "To create a Ubuntu live CD which is used a template for Clonezilla live:"
    echo "$prog [OPTION]"
    echo "OPTION:"
    echo "-a, --packages PKG     Specify to add PKG, e.g. firmware-bnx2"
    echo "-b, --branch [s|stable|t|testing|u|unstable|e|experimental]  Specify the DRBL branch to be used in Live CD. Default is stable."
    echo "-bt, --bootstrap BOOTSTRAP  Specify the bootsrap type as BOOTSTRAP (debootstrap or mmdebstrap). If not specified, $bootstrap_default will be used."
    echo "-c, --categories CAT   Sepcify the category, e.g. 'main', 'main universe', default is \'$categories_default\' if not specified."
    echo "-d, --ubuntu-dist [jammy|questing|resolute]  Assign Ubuntu dist, the default is $DEBIAN_DIST_DEF if not assigned."
    echo "-e, --ramfs-size  SIZE  Use SIZE (bytes) as the size of RAM for working dir. Default is $ramfs_size_def."
    echo "-f, --arch-flavor ARCH  Assign the CPU architecture flavor as ARCH, e.g. 386, amd64, arm64 or armhf. If it's not assigned, $cpu_flavor_default will be used."
    echo "-g, --drbl-repo-url URL  Assign the DRBL repository URL instead of default one $DRBL_REPOSITORY_URL_def."
    echo "-i, --assign-version-no NO  Assign the version no as NO instead of date."
    echo "-k, --live-kernel-pkg KERNEL_VER Assign kernel version as KERNEL_VER (KERNEL VER package must exist in repository. Ex. if KERNEL_VER is 2.6.20-1-486, then linux-image-2.6.20-1-486, squashfs-modules-2.6.20-1-486, and unionfs-modules-2.6.20-1-486 will be used."
    echo "-l, --drbl-live-ubuntu-branch [s|stable|t|testing|u|unstable|e|experimental]  specifies the DRBL live-ubuntu branch to be used in Live CD. Default is stable."
    echo "-m, --mirror-url URL  Assign the Ubuntu repository URL instead of default one $ubuntu_mirror_url_def. "
    echo "-r, --enable-ramfs    Use RAM as the working dir. This will need $ramfs_size_def bytes RAM space by default."
    echo "-s, --mirror-security-url URL  Assign the Ubuntu security repository URL instead of default one $ubuntu_mirror_security_url_def."
    echo "-o, --create-source-tarball  Create a corresponding source image to the binary image.  By default such an source image will not be created since this would require to download quite a few source packages."
    echo "-t, --batch     Run in batch mode"
    echo "-v, --verbose   Run live build in verbose mode"
}
#
build_it(){
  mkdir ${workdir}
  if [ "$use_ramfs" = "yes" ]; then
    ramfs_cmd="mount -t tmpfs -o "size=${ramfs_size}" tmpfs ${workdir}"
    eval $ramfs_cmd
    rc_ram="$?"
    if [ "$rc_ram" -ne 0 ]; then
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "Failed to mount RAM as working dir by this command:"
      echo "$ramfs_cmd"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      echo "$msg_program_stop!"
      exit 1
    fi
  fi
  cd ${workdir}
  
  # Force to load debian mode, or it will use ubuntu mode, which might fail in some cases.
  # E.g. tar (child): /usr/share/gfxboot-theme-ubuntu/bootlogo.tar.gz: Cannot open: No such file or directory
  $pref lb config --distribution $debian_dist --parent-distribution $debian_dist --archive-areas "$categories" --parent-archive-areas "$categories"
  $pref lb config --mode ubuntu
  
  # 2020/Mar/09 No more required this:
  # It's important to make the url for --parent-mirror-binary is different from that of --parent-mirror-chroot so that in /usr/lib/live/build/chroot_archives the "lb config --cache-packages false" and "lb config --apt-indices false" can work and those /var/cache/apt/{pkgcache.bin,srcpkgcache.bin} and those /var/lib/apt/lists/{*_Packages,*Release*,*_Sources} won't be cached.
  $pref lb config --parent-mirror-bootstrap $mirror_url --parent-mirror-binary $mirror_url --parent-mirror-chroot $mirror_url --parent-mirror-chroot-security $mirror_security_url --parent-mirror-binary-security $mirror_security_url --parent-mirror-debian-installer $mirror_url
  $pref lb config --updates true
  $pref lb config --mirror-debian-installer $mirror_url
  $pref lb config --mirror-binary $mirror_url --mirror-binary-security $mirror_security_url 
  $pref lb config --mirror-bootstrap $mirror_url
  $pref lb config --mirror-chroot $mirror_url --mirror-chroot-security $mirror_security_url
  # From live-build 3.0~a58-1, no --mirror-chroot-volatile option.
  #$pref lb config --mirror-chroot-volatile "none"
  
  # 2023/04/13 Since Ubuntu >= 23.04 removed v86d, we will add v86d in runtime, not here. This is not done in Debian-based as v86d is still in Debian repository.
  ## Since with squeeze or sid, we can use uvesafb to replace vesafb, we need v86d. Check https://bugs.launchpad.net/ubuntu/+source/v86d/+bug/189621 for more details.
  #case "$cpu_flavor" in
  #  386|amd64) pkgs="$pkgs v86d";;
  #  arm64) true;;
  #esac
  # Adding libc6-i386 for AMD64 arch. This will allow 32-bit program to run on AMD64 arch.
  if [ "$cpu_flavor" = "amd64" ]; then
    pkgs="$pkgs libc6-i386"
  fi
  
  # Disable the volatile repository. No such repository for Ubuntu.
  $pref lb config --initramfs live-boot
  # Refer to /usr/share/live/build/functions/defaults.sh for the original boot append parameters.
  # Don't be confused. It won't be appended. Therefore we have to put all required ones.
  $pref lb config --bootappend-live "boot=live union=overlay config username=user"
  # Force to add --bootappend-live-failsafe although we do not use it. This will overwrite the default values gotten
  # from the mode of Ubuntu. Otherwise it will set as "boot=casper memtest noapic noapm nodma nomce nolapic..." and 
  # the function get_live_boot_param in ocs-functions will parse wrong value of boot as boot=casper.
  $pref lb config --bootappend-live-failsafe "boot=live union=overlay config memtest noapic noapm nodma nomce nolapic nomodeset nosmp nosplash vga=normal"
  
  # Due to a bug, we have to include some essential packages otherwise it will failed in debian_type=minimal. The size difference between minimal and standard for intrepid is about 8 MB. Ref: https://bugs.launchpad.net/ubuntu/+source/cdebootstrap/+bug/189474
  if [ "$debian_type" = "minimal" -o  "$debian_type" = "minbase" ]; then
    DEBOOTSTRAP_OPTIONS="$DEBOOTSTRAP_OPTIONS --include=apt,ubuntu-keyring,gnupg,initramfs-tools"
  fi
  
  if [ "$debian_dist" = "jammy"  -o \
       "$debian_dist" = "questing"  -o \
       "$debian_dist" = "resolute" ]
  then
    # From live-build 3.0~a58-1, no --bootstrap-flavour option. We use debootstrap options now.
    # $pref lb config --bootstrap-flavour $debian_type 
    
    # If patched to support mmdebstrap, assigned it. Otherwise do not use "lb config --bootstrap" since "--bootstrap" does not exist.
    if [ "$bootstrap" = "mmdebstrap" ]; then
      $pref lb config --bootstrap mmdebstrap
    fi

    $pref lb config --debootstrap-options="${DEBOOTSTRAP_OPTIONS} --variant=$debian_type"
    # Since we might assign the package version, e.g. live-boot=3.0.1-1.drbl4.
    # If we do not adding "--no-upgrade", then 
    # in /usr/lib/live/build/chroot_live-packages it will run
    # "apt-get install live-boot" then it will be upgraded.
    $pref lb config --apt apt --apt-options "--yes --no-upgrade --allow-downgrades" --apt-recommends false
    # From live-build 3.0~a59-1, no --volatile option.
    # $pref lb config --volatile false
    # Disable the firmware packages automatically inclusion
    $pref lb config --firmware-binary false --firmware-chroot false
    $pref lb config --security true

    # This decide_MKSQUASHFS_OPTIONS function will output "export MKSQUASHFS_OPTIONS"
    decide_MKSQUASHFS_OPTIONS

    # Options for kernel package version name and flavor, take "linux-image-5.4.0-4-amd64" as an example
    # "linux-image-5.4.0-4" is assigned by "--linux-packages"
    # "amd64" is assigned by "--linux-flavours"
    $pref lb config --linux-packages "${kernel_pkg}"
    # For Uubntu 12.10+, linux-image has spilt as linux-image and linux-image-extra
    # Ref: https://lists.ubuntu.com/archives/kernel-team/2012-May/020448.html
    pkgs="$pkgs ${kernel_extra_pkg}"
  else
    echo "This distribution \"$debian_dist\" is not supported!"
    echo "Program terminated!"
    exit 1
  fi
  
  # For OS arch, we can build amd64 Ubuntu on i386 Ubuntu or vice versus.
  case "$cpu_flavor" in
    386)   os_arch="i386"
	   lnx_flavor="generic"
	   ;;
    amd64) os_arch="amd64"
	   lnx_flavor="generic"
	   ;;
    arm64) os_arch="arm64"
	   lnx_flavor="generic"
	   ;;
    armhf) os_arch="armhf"
	   # Used for kernel like: linux-image-5.4.0-9-generic-lpae_5.4.0-9.12_armhf.deb
	   lnx_flavor="generic-lpae"
	   ;;
  esac
  $pref lb config --architecture $os_arch
  
  # We force to use the generic Linux kernel, because Ubuntu's kernel package name is like:
  # linux-image-5.0.0-40-generic_5.0.0-40.44_i386.deb
  # linux-image-5.0.0-40-generic_5.0.0-40.44_armhf.deb
  # linux-image-5.4.0-9-generic-lpae_5.4.0-9.12_armhf.deb
  $pref lb config --linux-flavours $lnx_flavor
  
  # No memtest from debian, we will use the one from drbl since it's newer.
  $pref lb config --memtest none
  
  $pref lb config --debian-installer none
  $pref lb config --win32-loader false
  
  # Disable zsync, it might fail due to download files after /etc/resolv.conf is deconfigured.
  $pref lb config --zsync false
  
  # Enable cache-indices, by doing this, "apt-get upgrade" won't be run in lb chroot_sources after hook since we might assign older package version when building.
  # This is important especially when there is newer live-boot in Sid. E.g. on 2014/Apr/1 live-boot 4.0 alpha is aviailable, but we still want to keep using 3.0.1-1.drbl4. If --cache-indices is set true, live-boot is upgraded, and 4.0 is installed on the system after filesystem.squashfs is created. Then the initrd.img is re-created again, with live-boot 4.x in initrd.img. It will be inconsistent with what we want.
  $pref lb config --cache-indices true
  $pref lb config --cache-packages false
  $pref lb config --apt-indices false
  $pref lb config --apt-source-archives true
  
  # Disable the firmware included automatically. It will be too large. We will do that in hook.
  $pref lb config --firmware-binary false --firmware-chroot false
  
  #
  case "$cpu_flavor" in
    386) 
           # Default bootloaders in live-build is syslinux,grub-efi (for i386/amd64)
           # No need to run "$pref lb config --bootloaders syslinux,grub-efi"
           # //NOTE// This option "--binary-images" is for template iso only, not for Clonezilla live iso.
           $pref lb config --binary-images iso-hybrid
	   # Add those x86 only packages
	   pkgs="$pkgs $PKG_FROM_DBN_WHICH_OCS_LIVE_NEED_X86_ONLY"
	   ;;
    amd64) 
           # Default bootloaders in live-build is syslinux,grub-efi (for i386/amd64)
           # No need to run "$pref lb config --bootloaders syslinux,grub-efi"
           # //NOTE// This option "--binary-images" is for template iso only, not for Clonezilla live iso.
           $pref lb config --binary-images iso-hybrid
           $pref lb config --uefi-secure-boot enable
	   # Add those x86 only packages
	   pkgs="$pkgs $PKG_FROM_DBN_WHICH_OCS_LIVE_NEED_X86_64_ONLY"
	   ;;
    arm64) 
           $pref lb config --bootloaders grub-efi
           $pref lb config --binary-images iso
           $pref lb config --uefi-secure-boot enable
	   # Add those arm64 only packages
	   pkgs="$pkgs $PKG_FROM_DBN_WHICH_OCS_LIVE_NEED_ARM64_ONLY"
	   ;;
    armhf) 
           $pref lb config --bootloaders grub-efi
           $pref lb config --binary-images iso
	   # Add those armhf only packages
	   pkgs="$pkgs $PKG_FROM_DBN_WHICH_OCS_LIVE_NEED_ARMHF_ONLY"
	   ;;
  esac
  
  
  # Disable the firmware packages automatically inclusion
  $pref lb config --firmware-binary false --firmware-chroot false
  
  # Create a source tarball or not.
  if [ "$gen_source_tarball" = "yes" ]; then
    $pref lb config --source true
    $pref lb config --source-images tar
    # Since most of the debian packages are compressed (xz or gzip), we just use none, not the default one gzip.
    $pref lb config --compression none
  else
    $pref lb config --source false
  fi
  
  # Force to set the theme as live-build, so that /usr/lib/live/build/lb_binary_syslinux will copy /usr/share/live/build/bootloaders/isolinux/* to binary/isolinux/, which will trigger moving /live/vmlinuz-3.5.0-10-generic as /live/vmlinuz, and live/initrd.img-3.5.0-10-generic as live/initrd.img, for example. Then isolinux could boot since isolinux only work for shorter file name.
  #$pref lb config --syslinux-theme "live-build"
  
  # Ubuntu <= 14.10 uses upstart instead of sysvinit, while >=15.04 uses systemd.
  $pref lb config --initsystem systemd
  
  # Put files to be included in the chroot hook
  mkdir -p config/includes.chroot/live-hook-dir
  for i in $ocs_live_script_dir; do
    cp -pr $i/* config/includes.chroot/live-hook-dir/
  done
  cp -a /etc/drbl/{drbl.conf,drbl-ocs.conf} config/includes.chroot/live-hook-dir
  
  # Put the mirror url and settings, which we might need.
  cat <<-HOOK_APPEND_END >> config/includes.chroot/live-hook-dir/ocs-live-hook.conf
# The following settings were added before running hook" >> config/includes.chroot/live-hook-dir/ocs-live-hook.conf
debian_dist="$debian_dist"
mirror_url="$mirror_url"
drbl_ocs_live_ver="clonezilla-live-${version_no}"
HOOK_APPEND_END
  
  # Put packages list in config/package-lists/. //NOTE// Do not use file name like clonezilla-packages.list. Make it like clonezilla-packages.list.binary (for lb_binary) or clonezilla-packages.list.chroot (for lb_chroot). Otherwise lb_binary_package-lists will generate deb repository (/pool). 
  # Ref: http://lists.debian.org/debian-live/2012/07/msg00119.html
  echo "$pkgs" > config/package-lists/clonezilla-packages.list.chroot
  
  # Put hook file to be run in chroot
  cp $ocs_live_script_dir/${run_hook_script} config/hooks/live/${run_hook_script}.chroot
  
  # Put hook file to be run in the binary_local-hooks
  cp $ocs_live_script_dir/$run_binary_hook_script config/hooks/live/${run_binary_hook_script}.binary
  
  # prepare drbl source list
  # //NOTE// Use drbl-repository.list instead of drbl-repository.list.chroot because 
  # with ".chroot" it means only for chroot and it will be removed from the squashfs.
  cat << AddDRBLRepository > config/archives/drbl-repository.list
deb $DRBL_REPOSITORY_URL drbl $LIVE_REPOSITORY_SECTIONS_drbl
deb-src $DRBL_REPOSITORY_URL drbl $LIVE_REPOSITORY_SECTIONS_drbl
AddDRBLRepository
  
  # prepare drbl key
  LC_ALL=C wget -O config/archives/drbl-gpg.key $DRBL_GPG_KEY_URL
  
  # Disable apt languages and translations when creating DRBL live. This could reduce apt repository issue.
  # disable_apt_lang_translation chroot/etc/apt/apt.conf.d/99lang
  
  # Build it.
  $pref lb build
} # end of build_it

batch_mode="off"
# Parse command-line options
while [ $# -gt 0 ]; do
  case "$1" in
    -a|--packages)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              extra_pkgs="$1"
              shift
            fi
	    [ -z "$extra_pkgs" ] && USAGE && exit 1
            ;;
    -b|--branch)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              drbl_branch="$1"
              shift
            fi
	    [ -z "$drbl_branch" ] && USAGE && exit 1
            ;;
    -bt|--bootstrap)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case
              bootstrap="$1"
              shift
            fi
	    [ -z "$bootstrap" ] && USAGE && exit 1
            ;;
    -c|--categories)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              categories="$1"
              shift
            fi
	    [ -z "$categories" ] && USAGE && exit 1
            ;;
    -d|--debian-dist)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              debian_dist="$1"
              shift
            fi
	    [ -z "$debian_dist" ] && 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
            ;;
    -k|--live-kernel)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              live_kernel_ver="$1"
              shift
            fi
	    [ -z "$live_kernel_ver" ] && USAGE && exit 1
            ;;
    -l|--drbl-live-branch)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              drbl_live_branch="$1"
              shift
            fi
	    [ -z "$drbl_live_branch" ] && USAGE && exit 1
            ;;
    -f|--arch-flavor)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              cpu_flavor="$1"
              shift
            fi
	    [ -z "$cpu_flavor" ] && USAGE && exit 1
            ;;
    -g|--drbl-repo-url)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              DRBL_REPOSITORY_URL="$1"
              shift
            fi
	    [ -z "$DRBL_REPOSITORY_URL" ] && USAGE && exit 1
            ;;
    -m|--mirror-url)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              mirror_url="$1"
              shift
            fi
	    [ -z "$mirror_url" ] && USAGE && exit 1
            ;;
    -s|--mirror-security-url)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              mirror_security_url="$1"
              shift
            fi
	    [ -z "$mirror_security_url" ] && USAGE && exit 1
            ;;
    -o|--create-source-tarball)
	    gen_source_tarball="yes"
            shift ;;
    -r|--enable-ramfs)
	    use_ramfs="yes"
            shift ;;
    -e|--ramfs-size)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              ramfs_size="$1"
              shift
            fi
	    [ -z "$ramfs_size" ] && USAGE && exit 1
            ;;
    -t|--batch)
	    batch_mode="on"
            shift ;;
    -v|--verbose)
	    verbose="on"
            shift ;;
    -*)     echo "${0}: ${1}: invalid option" >&2
            USAGE >& 2
            exit 2 ;;
    *)      break ;;
  esac
done

[ -z "$ramfs_size" ] && ramfs_size="$ramfs_size_def"
if [ "$use_ramfs" = "yes" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "Using RAM disk with size $ramfs_size bytes for live build working dir."
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
fi

if ! type lb &>/dev/null; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "This program only works in Ubuntu 7.10 or later!"
  echo "If you are running Ubuntu 7.10 or later, use 'apt-get install live-build' to install the live-build (version $lh_ver_required or later), then run $0 again."
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  exit 1
fi

[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "///NOTE///"
echo "0. This program works on Debian or Ubuntu with the live-build (version $lh_ver_required or later) installed."
echo "1. Package debootstrap (version $debootstrap_ver_required or later) should work."
echo "2. If you want to create Ubuntu vivid live on Debian Wheezy, you have to make sure the file /usr/share/debootstrap/scripts/vivid exists, if not you can try to link gutsy as vivid by:"
echo "-------------------------------------"
echo "cd /usr/share/debootstrap/scripts/; ln -fs gutsy vivid"
echo "-------------------------------------"
echo "3. If you want to run this on Debian system, remember to import Ubuntu keyring. It's available on any Ubuntu mirror repository. e.g.: 'wget http://archive.ubuntu.com/ubuntu/pool/main/u/ubuntu-keyring/ubuntu-keyring_2012.05.19_all.deb' and 'dpkg -i ubuntu-keyring_2012.05.19_all.deb'"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
if [ "$batch_mode" = "no" ]; then
  echo -n "Press enter to continue... "
  read
fi

ask_and_load_lang_set en_US.UTF-8
rm -rf ${workdir}/.stage/

# Apply default settings if not assigned
[ -z "$debian_dist" ] && debian_dist="$DEBIAN_DIST_DEF"
[ -z "$categories" ] && categories="$categories_default"
[ -z "$DRBL_REPOSITORY_URL" ] && DRBL_REPOSITORY_URL="$DRBL_REPOSITORY_URL_def"
[ -z "$mirror_url" ] && mirror_url="$ubuntu_mirror_url_def"
[ -z "$mirror_security_url" ] && mirror_security_url="$ubuntu_mirror_security_url_def"
[ -z "$cpu_flavor" ] && cpu_flavor="$cpu_flavor_default"
[ -z "$bootstrap" ] && bootstrap=$bootstrap_default

# Append the extra packages
[ -n "$extra_pkgs" ] && pkgs="$pkgs $extra_pkgs"

# Kernel package
if [ "$debian_dist" = "jammy"  -o \
     "$debian_dist" = "plucky" ]; then
   case "$cpu_flavor" in
     amd64|arm64) 
	    # Ubuntu linux (18.04-25.04) use linux-modules-extra, 
	    # and only for i386, amd64 and arm64
	    # For that >= 25.10, no linux-modules-extra package anymore.
            # Ref: https://discourse.ubuntu.com/t/kernel-development-release-cadence-and-deprecation-of-linux-modules-extra/65176
            kernel_extra_pkg_prefix="linux-modules-extra"
	    ;;
     386|armhf) true;;
   esac
fi
if [ -n "$live_kernel_ver" ]; then
  kernel_pkg="linux-image-${live_kernel_ver}"
  if [ -n "$kernel_extra_pkg_prefix" ]; then
    # Use regex to selelct it like: linux-modules-extra-6.5.0-10 or linux-modules-extra-6.5.0-10-generic
    # Skip those linux-modules-extra-6.5.0-1010-oracle, linux-modules-extra-6.5.0-1007-gcp...
    # Hence depending packages like linux-modules-6.5.0-1010-oracle won't be included, either.
    kernel_extra_pkg="${kernel_extra_pkg_prefix}-${live_kernel_ver}(-|$)"
  fi
else
  kernel_pkg="linux-image"
  if [ -n "$kernel_extra_pkg_prefix" ]; then
    kernel_extra_pkg="${kernel_extra_pkg_prefix}"
  fi
fi

# If version_no is not assigned, use date (Ex. 20070409)
[ -z "$version_no" ] && version_no="$(date +%Y%m%d)"
target_iso="ubuntu-live-for-ocs-${version_no}.iso"
target_src_tarball="clonezilla-live-src-${version_no}.ubuntu.tar"
target_src_tarball_list="clonezilla-live-src-${version_no}.ubuntu.contents"
target_src_debian_live_tarball="clonezilla-live-src-${version_no}.debian-live.tar"
target_src_debian_live_tarball_list="clonezilla-live-src-${version_no}.debian-live.contents"

[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "Creating an Ubuntu live cd iso which is used for clonezilla image with restoration function. The created iso will be in $target_iso" 
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
#
echo "Using Ubuntu repository from: $mirror_url"
echo "Using Ubuntu security repository from: $mirror_security_url"
echo "Using DRBL repository from: $DRBL_REPOSITORY_URL"

#
case "$drbl_branch" in
  t|testing)
     echo "Using DRBL testing branch..."
     LIVE_REPOSITORY_SECTIONS_drbl="testing"
     ;;
  u|unstable)
     echo "Using DRBL unstable branch..."
     LIVE_REPOSITORY_SECTIONS_drbl="unstable"
     ;;
  e|experimental)
     echo "Using DRBL experimental branch..."
     LIVE_REPOSITORY_SECTIONS_drbl="experimental"
     ;;
  *)
     echo "Using DRBL stable branch..."
     LIVE_REPOSITORY_SECTIONS_drbl="stable"
     ;;
esac
case "$drbl_live_branch" in
  t|testing)
     echo "Using DRBL Live Ubuntu testing branch..."
     LIVE_REPOSITORY_SECTIONS_drbl="$LIVE_REPOSITORY_SECTIONS_drbl live-ubuntu-testing"
     ;;
  u|unstable)
     echo "Using DRBL Live Ubuntu unstable branch..."
     LIVE_REPOSITORY_SECTIONS_drbl="$LIVE_REPOSITORY_SECTIONS_drbl live-ubuntu-unstable"
     ;;
  e|experimental)
     echo "Using DRBL Live Ubuntu experimental branch..."
     LIVE_REPOSITORY_SECTIONS_drbl="$LIVE_REPOSITORY_SECTIONS_drbl live-ubuntu-experimental"
     ;;
  *)
     echo "Using DRBL live Ubuntu stable branch..."
     LIVE_REPOSITORY_SECTIONS_drbl="$LIVE_REPOSITORY_SECTIONS_drbl live-ubuntu-stable"
     ;;
esac

#
if [ "$verbose" = "on" ]; then
  pref="bash -x"
  export DEBOOTSTRAP_OPTIONS="$DEBOOTSTRAP_OPTIONS --verbose"
fi

if [ -d "${workdir}" ]; then
  echo "Found dir ${workdir}, clean stale files..."
  chroot ${workdir}/chroot umount /dev/pts &>/dev/null || true
  chroot ${workdir}/chroot umount /proc &>/dev/null || true
  chroot ${workdir}/chroot umount /sys &>/dev/null || true
  ( cd ${workdir}/; lb clean )
  if mountpoint ${workdir}; then
    umount ${workdir}
  fi
  [ -d "${workdir}" -a -n "$(echo ${workdir} | grep "ubuntu-live")" ] && rm -rf ${workdir}
fi

# Use sub shell to keep the working dir
(build_it)

lb_ver="$(LC_ALL=C lb --version)"  # e.g. 3.0.5-1
if [ "$lb_ver" \> 3 ] && [ "$lb_ver" \< 4 ]; then
  # Live build version 3 
  mv -f ${workdir}/binary*.iso $target_iso
  if [ "$gen_source_tarball" = "yes" ]; then
    mv -f ${workdir}/source.debian.tar $target_src_tarball
    mv -f ${workdir}/source.debian.contents $target_src_tarball_list
    mv -f ${workdir}/source.debian-live.tar $target_src_debian_live_tarball
    mv -f ${workdir}/source.debian-live.contents $target_src_debian_live_tarball_list
  fi
else
  # Live build version 4
  mv -f ${workdir}/live-image*.iso $target_iso
  if [ "$gen_source_tarball" = "yes" ]; then
    mv -f ${workdir}/live-image-source.debian.contents $target_src_tarball_list
    mv -f ${workdir}/live-image-source.debian.tar $target_src_tarball
    mv -f ${workdir}/live-image-source.live.contents $target_src_debian_live_tarball_list
    mv -f ${workdir}/live-image-source.live.tar $target_src_debian_live_tarball
  fi
fi

# Unmount the RAM disk.
if [ "$use_ramfs" = "yes" ]; then
  if mountpoint ${workdir}; then
    umount ${workdir}
  fi
fi