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
|
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
#
#
# Send the error message to the screen and to the logfile.
#
error()
{
typeset fmt="$1"
shift
printf "${MSG_PREFIX}ERROR: ${fmt}\n" "$@"
[[ -n $LOGFILE ]] && printf "[$(date)] ERROR: ${fmt}\n" "$@" >&2
}
fatal()
{
typeset fmt="$1"
shift
error "$fmt" "$@"
exit $EXIT_CODE
}
fail_fatal() {
typeset fmt="$1"
shift
error "$fmt" "$@"
exit $ZONE_SUBPROC_FATAL
}
#
# Send the provided printf()-style arguments to the screen and to the logfile.
#
log()
{
typeset fmt="$1"
shift
printf "${MSG_PREFIX}${fmt}\n" "$@"
[[ -n $LOGFILE ]] && printf "[$(date)] ${MSG_PREFIX}${fmt}\n" "$@" >&2
}
#
# Print provided text to the screen if the shell variable "OPT_V" is set.
# The text is always sent to the logfile.
#
vlog()
{
typeset fmt="$1"
shift
[[ -n $OPT_V ]] && printf "${MSG_PREFIX}${fmt}\n" "$@"
[[ -n $LOGFILE ]] && printf "[$(date)] ${MSG_PREFIX}${fmt}\n" "$@" >&2
}
#
# Validate that the directory is safe.
#
# It is possible for a malicious zone root user to modify a zone's filesystem
# so that modifications made to the zone's filesystem by administrators in the
# global zone modify the global zone's filesystem. We can prevent this by
# ensuring that all components of paths accessed by scripts are real (i.e.,
# non-symlink) directories.
#
# NOTE: The specified path should be an absolute path as would be seen from
# within the zone. Also, this function does not check parent directories.
# If, for example, you need to ensure that every component of the path
# '/foo/bar/baz' is a directory and not a symlink, then do the following:
#
# safe_dir /foo
# safe_dir /foo/bar
# safe_dir /foo/bar/baz
#
safe_dir()
{
typeset dir="$1"
if [[ -h $ZONEROOT/$dir || ! -d $ZONEROOT/$dir ]]; then
fatal "$e_baddir" "$dir"
fi
}
# Like safe_dir except the dir doesn't have to exist.
safe_opt_dir()
{
typeset dir="$1"
[[ ! -e $ZONEROOT/$dir ]] && return
if [[ -h $ZONEROOT/$dir || ! -d $ZONEROOT/$dir ]]; then
fatal "$e_baddir" "$dir"
fi
}
# Only make a copy if we haven't already done so.
safe_backup()
{
typeset src="$1"
typeset dst="$2"
if [[ ! -h $src && ! -h $dst && ! -d $dst && ! -f $dst ]]; then
/usr/bin/cp -p $src $dst || fatal "$e_badfile" "$src"
fi
}
# Make a copy even if the destination already exists.
safe_copy()
{
typeset src="$1"
typeset dst="$2"
if [[ ! -h $src && ! -h $dst && ! -d $dst ]]; then
/usr/bin/cp -p $src $dst || fatal "$e_badfile" "$src"
fi
}
# Move a file
safe_move()
{
typeset src="$1"
typeset dst="$2"
if [[ ! -h $src && ! -h $dst && ! -d $dst ]]; then
/usr/bin/mv $src $dst || fatal "$e_badfile" "$src"
fi
}
safe_rm()
{
if [[ ! -h $ZONEROOT/$1 && -f $ZONEROOT/$1 ]]; then
rm -f "$ZONEROOT/$1"
fi
}
#
# Replace the file with a wrapper pointing to the native brand code.
# However, we only do the replacement if the file hasn't already been
# replaced with our wrapper. This function expects the cwd to be the
# location of the file we're replacing.
#
# Some of the files we're replacing are hardlinks to isaexec so we need to 'rm'
# the file before we setup the wrapper while others are hardlinks to rc scripts
# that we need to maintain.
#
safe_replace()
{
typeset filename="$1"
typeset runname="$2"
typeset mode="$3"
typeset own="$4"
typeset rem="$5"
if [ -h $filename -o ! -f $filename ]; then
return
fi
egrep -s "Solaris Brand Replacement" $filename
if [ $? -eq 0 ]; then
return
fi
safe_backup $filename $filename.pre_p2v
if [ $rem = "remove" ]; then
rm -f $filename
fi
cat <<-END >$filename || exit 1
#!/bin/sh -p
#
# Solaris Brand Replacement
#
# Attention. This file has been replaced with a new version for
# use in a virtualized environment. Modification of this script is not
# supported and all changes will be lost upon reboot. The
# {name}.pre_p2v version of this file is a backup copy of the
# original and should not be deleted.
#
END
echo ". $runname \"\$@\"" >>$filename || exit 1
chmod $mode $filename
chown $own $filename
}
safe_wrap()
{
typeset filename="$1"
typeset runname="$2"
typeset mode="$3"
typeset own="$4"
if [ -f $filename ]; then
log "$e_cannot_wrap" "$filename"
exit 1
fi
cat <<-END >$filename || exit 1
#!/bin/sh
#
# Solaris Brand Wrapper
#
# Attention. This file has been created for use in a
# virtualized environment. Modification of this script
# is not supported and all changes will be lost upon reboot.
#
END
echo ". $runname \"\$@\"" >>$filename || exit 1
chmod $mode $filename
chown $own $filename
}
#
# Read zonecfg fs entries and save the relevant data, one entry per
# line.
# This assumes the properties from the zonecfg output, e.g.:
# fs:
# dir: /opt
# special: /opt
# raw not specified
# type: lofs
# options: [noexec,ro,noatime]
#
# and it assumes the order of the fs properties as above.
#
get_fs_info()
{
zonecfg -z $zonename info fs | nawk '{
if ($1 == "options:") {
# Remove brackets.
options=substr($2, 2, length($2) - 2);
printf("%s %s %s %s\n", dir, type, special, options);
} else if ($1 == "dir:") {
dir=$2;
} else if ($1 == "special:") {
special=$2;
} else if ($1 == "type:") {
type=$2
}
}' >> $fstmpfile
}
#
# Mount zonecfg fs entries into the zonepath.
#
mnt_fs()
{
if [ ! -s $fstmpfile ]; then
return;
fi
# Sort the fs entries so we can handle nested mounts.
sort $fstmpfile | nawk -v zonepath=$zonepath '{
if (NF == 4)
options="-o " $4;
else
options=""
# Create the mount point. Ignore errors since we might have
# a nested mount with a pre-existing mount point.
cmd="/usr/bin/mkdir -p " zonepath "/root" $1 " >/dev/null 2>&1"
system(cmd);
cmd="/usr/sbin/mount -F " $2 " " options " " $3 " " \
zonepath "/root" $1;
if (system(cmd) != 0) {
printf("command failed: %s\n", cmd);
exit 1;
}
}' >>$LOGFILE
}
#
# Unmount zonecfg fs entries from the zonepath.
#
umnt_fs()
{
if [ ! -s $fstmpfile ]; then
return;
fi
# Reverse sort the fs entries so we can handle nested unmounts.
sort -r $fstmpfile | nawk -v zonepath=$zonepath '{
cmd="/usr/sbin/umount " zonepath "/root" $1
if (system(cmd) != 0) {
printf("command failed: %s\n", cmd);
}
}' >>$LOGFILE
}
# Find the dataset mounted on the zonepath.
get_zonepath_ds() {
ZONEPATH_DS=`/usr/sbin/zfs list -H -t filesystem -o name,mountpoint | \
/usr/bin/nawk -v zonepath=$1 '{
if ($2 == zonepath)
print $1
}'`
if [ -z "$ZONEPATH_DS" ]; then
fail_fatal "$f_no_ds"
fi
}
#
# Perform validation and cleanup in the zoneroot after unpacking the archive.
#
post_unpack()
{
#
# Check if the image was created with a valid libc.so.1.
#
hwcap=`moe -v -32 $ZONEROOT/lib/libc.so.1 2>&1`
if (( $? != 0 )); then
vlog "$f_hwcap_info" "$hwcap"
fail_fatal "$f_sanity_hwcap"
fi
( cd "$ZONEROOT" && \
find . \( -type b -o -type c \) -exec rm -f "{}" \; )
}
#
# Determine flar compression style from identification file.
#
get_compression()
{
typeset ident=$1
typeset line=$(grep "^files_compressed_method=" $ident)
print ${line##*=}
}
#
# Determine flar archive style from identification file.
#
get_archiver()
{
typeset ident=$1
typeset line=$(grep "^files_archived_method=" $ident)
print ${line##*=}
}
#
# Unpack flar into current directory (which should be zoneroot). The flash
# archive is standard input. See flash_archive(5) man page.
#
# We can't use "flar split" since it will only unpack into a directory called
# "archive". We need to unpack in place in order to properly handle nested
# fs mounts within the zone root. This function does the unpacking into the
# current directory.
#
# This code is derived from the gen_split() function in /usr/sbin/flar so
# we keep the same style as the original.
#
install_flar()
{
typeset result
typeset archiver_command
typeset archiver_arguments
vlog "cd $ZONEROOT && $stage1 "$insrc" | install_flar"
# Read cookie
read -r input_line
if (( $? != 0 )); then
log "$not_readable" "$install_media"
return 1
fi
# The cookie has format FlAsH-aRcHiVe-m.n where m and n are integers.
if [[ ${input_line%%-[0-9]*.[0-9]*} != "FlAsH-aRcHiVe" ]]; then
log "$not_flar"
return 1
fi
while [ true ]
do
# We should always be at the start of a section here
read -r input_line
if [[ ${input_line%%=*} != "section_begin" ]]; then
log "$bad_flar"
return 1
fi
section_name=${input_line##*=}
# If we're at the archive, we're done skipping sections.
if [[ "$section_name" == "archive" ]]; then
break
fi
#
# Save identification section to a file so we can determine
# how to unpack the archive.
#
if [[ "$section_name" == "identification" ]]; then
/usr/bin/rm -f identification
while read -r input_line
do
if [[ ${input_line%%=*} == \
"section_begin" ]]; then
/usr/bin/rm -f identification
log "$bad_flar"
return 1
fi
if [[ $input_line == \
"section_end=$section_name" ]]; then
break;
fi
echo $input_line >> identification
done
continue
fi
#
# Otherwise skip past this section; read lines until detecting
# section_end. According to flash_archive(5) we can have
# an arbitrary number of sections but the archive section
# must be last.
#
success=0
while read -r input_line
do
if [[ $input_line == "section_end=$section_name" ]];
then
success=1
break
fi
# Fail if we miss the end of the section
if [[ ${input_line%%=*} == "section_begin" ]]; then
/usr/bin/rm -f identification
log "$bad_flar"
return 1
fi
done
if (( $success == 0 )); then
#
# If we get here we read to the end of the file before
# seeing the end of the section we were reading.
#
/usr/bin/rm -f identification
log "$bad_flar"
return 1
fi
done
# Check for an archive made from a ZFS root pool.
egrep -s "^rootpool=" identification
if (( $? == 0 )); then
/usr/bin/rm -f identification
log "$bad_zfs_flar"
return 1
fi
# Get the information needed to unpack the archive.
archiver=$(get_archiver identification)
if [[ $archiver == "pax" ]]; then
# pax archiver specified
archiver_command="/usr/bin/pax"
if [[ -s $fspaxfile ]]; then
archiver_arguments="-r -p e -c \
$(/usr/bin/cat $fspaxfile)"
else
archiver_arguments="-r -p e"
fi
elif [[ $archiver == "cpio" || -z $archiver ]]; then
# cpio archived specified OR no archiver specified - use default
archiver_command="/usr/bin/cpio"
archiver_arguments="-icdumfE $fscpiofile"
else
# unknown archiver specified
log "$unknown_archiver" $archiver
return 1
fi
if [[ ! -x $archiver_command ]]; then
/usr/bin/rm -f identification
log "$cmd_not_exec" $archiver_command
return 1
fi
compression=$(get_compression identification)
# We're done with the identification file
/usr/bin/rm -f identification
# Extract archive
if [[ $compression == "compress" ]]; then
/usr/bin/zcat | \
$archiver_command $archiver_arguments 2>/dev/null
else
$archiver_command $archiver_arguments 2>/dev/null
fi
result=$?
post_unpack
(( $result != 0 )) && return 1
return 0
}
#
# Get the archive base.
#
# We must unpack the archive in the right place within the zonepath so
# that files are installed into the various mounted filesystems that are set
# up in the zone's configuration. These are already mounted for us by the
# mntfs function.
#
# Archives can be made of either a physical host's root file system or a
# zone's zonepath. For a physical system, if the archive is made using an
# absolute path (/...) we can't use it. For a zone the admin can make the
# archive from a variety of locations;
#
# a) zonepath itself: This will be a single dir, probably named with the
# zone name, it will contain a root dir and under the root we'll see all
# the top level dirs; etc, var, usr... We must be above the ZONEPATH
# when we unpack the archive but this will only work if the the archive's
# top-level dir name matches the ZONEPATH base-level dir name. If not,
# this is an error.
#
# b) inside the zonepath: We'll see root and it will contain all the top
# level dirs; etc, var, usr.... We must be in the ZONEPATH when we unpack
# the archive.
#
# c) inside the zonepath root: We'll see all the top level dirs, ./etc,
# ./var, ./usr.... This is also the case we see when we get an archive
# of a physical system. We must be in ZONEROOT when we unpack the archive.
#
# Note that there can be a directory named "root" under the ZONEPATH/root
# directory.
#
# This function handles the above possibilities so that we reject absolute
# path archives and figure out where in the file system we need to be to
# properly unpack the archive into the zone. It sets the ARCHIVE_BASE
# variable to the location where the achive should be unpacked.
#
get_archive_base()
{
stage1=$1
archive=$2
stage2=$3
vlog "$m_analyse_archive"
base=`$stage1 $archive | $stage2 2>/dev/null | nawk -F/ '{
# Check for an absolute path archive
if (substr($0, 1, 1) == "/")
exit 1
if ($1 != ".")
dirs[$1] = 1
else
dirs[$2] = 1
}
END {
for (d in dirs) {
cnt++
if (d == "bin") sawbin = 1
if (d == "etc") sawetc = 1
if (d == "root") sawroot = 1
if (d == "var") sawvar = 1
}
if (cnt == 1) {
# If only one top-level dir named root, we are in the
# zonepath, otherwise this must be an archive *of*
# the zonepath so print the top-level dir name.
if (sawroot)
print "*zonepath*"
else
for (d in dirs) print d
} else {
# We are either in the zonepath or in the zonepath/root
# (or at the top level of a full system archive which
# looks like the zonepath/root case). Figure out which
# one.
if (sawroot && !sawbin && !sawetc && !sawvar)
print "*zonepath*"
else
print "*zoneroot*"
}
}'`
if (( $? != 0 )); then
umnt_fs
fatal "$e_absolute_archive"
fi
if [[ "$base" == "*zoneroot*" ]]; then
ARCHIVE_BASE=$ZONEROOT
elif [[ "$base" == "*zonepath*" ]]; then
ARCHIVE_BASE=$ZONEPATH
else
# We need to be in the dir above the ZONEPATH but we need to
# validate that $base matches the final component of ZONEPATH.
bname=`basename $ZONEPATH`
if [[ "$bname" != "$base" ]]; then
umnt_fs
fatal "$e_mismatch_archive" "$base" "$bname"
fi
ARCHIVE_BASE=`dirname $ZONEPATH`
fi
}
#
# Unpack cpio archive into zoneroot.
#
install_cpio()
{
stage1=$1
archive=$2
get_archive_base "$stage1" "$archive" "cpio -it"
cpioopts="-idmfE $fscpiofile"
vlog "cd \"$ARCHIVE_BASE\" && $stage1 \"$archive\" | cpio $cpioopts"
# Ignore errors from cpio since we expect some errors depending on
# how the archive was made.
( cd "$ARCHIVE_BASE" && $stage1 "$archive" | cpio $cpioopts )
post_unpack
return 0
}
#
# Unpack pax archive into zoneroot.
#
install_pax()
{
archive=$1
get_archive_base "cat" "$archive" "pax"
if [[ -s $fspaxfile ]]; then
filtopt="-c $(/usr/bin/cat $fspaxfile)"
fi
vlog "cd \"$ARCHIVE_BASE\" && pax -r -f \"$archive\" $filtopt"
# Ignore errors from pax since we expect some errors depending on
# how the archive was made.
( cd "$ARCHIVE_BASE" && pax -r -f "$archive" $filtopt )
post_unpack
return 0
}
#
# Unpack UFS dump into zoneroot.
#
install_ufsdump()
{
archive=$1
vlog "cd \"$ZONEROOT\" && ufsrestore rf \"$archive\""
#
# ufsrestore goes interactive if you ^C it. To prevent that,
# we make sure its stdin is not a terminal.
#
( cd "$ZONEROOT" && ufsrestore rf "$archive" < /dev/null )
result=$?
post_unpack
return $result
}
#
# Copy directory hierarchy into zoneroot.
#
install_dir()
{
source_dir=$1
cpioopts="-pdm"
first=1
filt=$(for i in $(cat $fspaxfile)
do
echo $i | egrep -s "/" && continue
if [[ $first == 1 ]]; then
printf "^%s" $i
first=0
else
printf "|^%s" $i
fi
done)
list=$(cd "$source_dir" && ls -d * | egrep -v "$filt")
flist=$(for i in $list
do
printf "%s " "$i"
done)
findopts="-xdev ( -type d -o -type f -o -type l ) -print"
vlog "cd \"$source_dir\" && find $flist $findopts | "
vlog "cpio $cpioopts \"$ZONEROOT\""
# Ignore errors from cpio since we expect some errors depending on
# how the archive was made.
( cd "$source_dir" && find $flist $findopts | \
cpio $cpioopts "$ZONEROOT" )
post_unpack
return 0
}
#
# This is a common function for laying down a zone image from a variety of
# different sources. This can be used to either install a fresh zone or as
# part of zone migration during attach.
#
# The first argument specifies the type of image: archive, directory or stdin.
# The second argument specifies the image itself. In the case of stdin, the
# second argument specifies the format of the stream (cpio, flar, etc.).
# Any validation or post-processing on the image is done elsewhere.
#
# This function calls a 'sanity_check' function which must be provided by
# the script which includes this code.
#
install_image()
{
intype=$1
insrc=$2
if [[ -z "$intype" || -z "$insrc" ]]; then
return 1
fi
filetype="unknown"
filetypename="unknown"
stage1="cat"
if [[ "$intype" == "directory" ]]; then
if [[ "$insrc" == "-" ]]; then
# Indicates that the existing zonepath is prepopulated.
filetype="existing"
filetypename="existing"
else
if [[ "$(echo $insrc | cut -c 1)" != "/" ]]; then
fatal "$e_path_abs" "$insrc"
fi
if [[ ! -e "$insrc" ]]; then
log "$e_not_found" "$insrc"
fatal "$e_install_abort"
fi
if [[ ! -r "$insrc" ]]; then
log "$e_not_readable" "$insrc"
fatal "$e_install_abort"
fi
if [[ ! -d "$insrc" ]]; then
log "$e_not_dir"
fatal "$e_install_abort"
fi
sanity_check $insrc
filetype="directory"
filetypename="directory"
fi
else
# Common code for both archive and stdin stream.
if [[ "$intype" == "archive" ]]; then
if [[ ! -f "$insrc" ]]; then
log "$e_unknown_archive"
fatal "$e_install_abort"
fi
ftype="$(LC_ALL=C file $insrc | cut -d: -f 2)"
else
# For intype == stdin, the insrc parameter specifies
# the stream format coming on stdin.
ftype="$insrc"
insrc="-"
fi
# Setup vars for the archive type we have.
case "$ftype" in
*cpio*) filetype="cpio"
filetypename="cpio archive"
;;
*bzip2*) filetype="bzip2"
filetypename="bzipped cpio archive"
;;
*gzip*) filetype="gzip"
filetypename="gzipped cpio archive"
;;
*ufsdump*) filetype="ufsdump"
filetypename="ufsdump archive"
;;
"flar")
filetype="flar"
filetypename="flash archive"
;;
"flash")
filetype="flar"
filetypename="flash archive"
;;
*Flash\ Archive*)
filetype="flar"
filetypename="flash archive"
;;
"tar")
filetype="tar"
filetypename="tar archive"
;;
*USTAR\ tar\ archive)
filetype="tar"
filetypename="tar archive"
;;
"pax")
filetype="xustar"
filetypename="pax (xustar) archive"
;;
*USTAR\ tar\ archive\ extended\ format*)
filetype="xustar"
filetypename="pax (xustar) archive"
;;
"zfs")
filetype="zfs"
filetypename="ZFS send stream"
;;
*ZFS\ snapshot\ stream*)
filetype="zfs"
filetypename="ZFS send stream"
;;
*) log "$e_unknown_archive"
fatal "$e_install_abort"
;;
esac
fi
vlog "$filetypename"
# Check for a non-empty root if no '-d -' option.
if [[ "$filetype" != "existing" ]]; then
cnt=$(ls $ZONEROOT | wc -l)
if (( $cnt != 0 )); then
fatal "$e_root_full" "$ZONEROOT"
fi
fi
fstmpfile=$(/usr/bin/mktemp -t -p /var/tmp)
if [[ -z "$fstmpfile" ]]; then
fatal "$e_tmpfile"
fi
# Make sure we always have the files holding the directories to filter
# out when extracting from a CPIO or PAX archive. We'll add the fs
# entries to these files in get_fs_info()
fscpiofile=$(/usr/bin/mktemp -t -p /var/tmp fs.cpio.XXXXXX)
if [[ -z "$fscpiofile" ]]; then
rm -f $fstmpfile
fatal "$e_tmpfile"
fi
# Filter out these directories.
echo 'dev/*' >>$fscpiofile
echo 'devices/*' >>$fscpiofile
echo 'devices' >>$fscpiofile
echo 'proc/*' >>$fscpiofile
echo 'tmp/*' >>$fscpiofile
echo 'var/run/*' >>$fscpiofile
echo 'system/contract/*' >>$fscpiofile
echo 'system/object/*' >>$fscpiofile
fspaxfile=$(/usr/bin/mktemp -t -p /var/tmp fs.pax.XXXXXX)
if [[ -z "$fspaxfile" ]]; then
rm -f $fstmpfile $fscpiofile
fatal "$e_tmpfile"
fi
printf "%s " \
"dev devices proc tmp var/run system/contract system/object" \
>>$fspaxfile
# Set up any fs mounts so the archive will install into the correct
# locations.
get_fs_info
mnt_fs
if (( $? != 0 )); then
umnt_fs >/dev/null 2>&1
rm -f $fstmpfile $fscpiofile $fspaxfile
fatal "$mount_failed"
fi
if [[ "$filetype" == "existing" ]]; then
log "$no_installing"
else
log "$installing"
fi
#
# Install the image into the zonepath.
#
unpack_result=0
stage1="cat"
if [[ "$filetype" == "gzip" ]]; then
stage1="gzcat"
filetype="cpio"
elif [[ "$filetype" == "bzip2" ]]; then
stage1="bzcat"
filetype="cpio"
fi
if [[ "$filetype" == "cpio" ]]; then
install_cpio "$stage1" "$insrc"
unpack_result=$?
elif [[ "$filetype" == "flar" ]]; then
( cd "$ZONEROOT" && $stage1 $insrc | install_flar )
unpack_result=$?
elif [[ "$filetype" == "xustar" ]]; then
install_pax "$insrc"
unpack_result=$?
elif [[ "$filetype" = "tar" ]]; then
vlog "cd \"$ZONEROOT\" && tar -xf \"$insrc\""
# Ignore errors from tar since we expect some errors depending
# on how the archive was made.
( cd "$ZONEROOT" && tar -xf "$insrc" )
unpack_result=0
post_unpack
elif [[ "$filetype" == "ufsdump" ]]; then
install_ufsdump "$insrc"
unpack_result=$?
elif [[ "$filetype" == "directory" ]]; then
install_dir "$insrc"
unpack_result=$?
elif [[ "$filetype" == "zfs" ]]; then
#
# Given a 'zfs send' stream file, receive the snapshot into
# the zone's dataset. We're getting the original system's
# zonepath dataset. Destroy the existing dataset created
# above since this recreates it.
#
if [[ -z "$DATASET" ]]; then
fatal "$f_nodataset"
fi
/usr/sbin/zfs destroy "$DATASET"
if (( $? != 0 )); then
log "$f_zfsdestroy" "$DATASET"
fi
vlog "$stage1 $insrc | zfs receive -F $DATASET"
( $stage1 $insrc | /usr/sbin/zfs receive -F $DATASET )
unpack_result=$?
fi
# Clean up any fs mounts used during unpacking.
umnt_fs
rm -f $fstmpfile $fscpiofile $fspaxfile
chmod 700 $zonepath
(( $unpack_result != 0 )) && fatal "$f_unpack_failed"
# Verify this is a valid image.
sanity_check $ZONEROOT
return 0
}
# Setup i18n output
TEXTDOMAIN="SUNW_OST_OSCMD"
export TEXTDOMAIN
e_cannot_wrap=$(gettext "%s: error: wrapper file already exists")
e_baddir=$(gettext "Invalid '%s' directory within the zone")
e_badfile=$(gettext "Invalid '%s' file within the zone")
e_path_abs=$(gettext "Pathname specified to -a '%s' must be absolute.")
e_not_found=$(gettext "%s: error: file or directory not found.")
e_install_abort=$(gettext "Installation aborted.")
e_not_readable=$(gettext "Cannot read directory '%s'")
e_not_dir=$(gettext "Error: must be a directory")
e_unknown_archive=$(gettext "Error: Unknown archive format. Must be a flash archive, a cpio archive (can also be gzipped or bzipped), a pax XUSTAR archive, or a level 0 ufsdump archive.")
e_absolute_archive=$(gettext "Error: archive contains absolute paths instead of relative paths.")
e_mismatch_archive=$(gettext "Error: the archive top-level directory (%s) does not match the zonepath (%s).")
e_tmpfile=$(gettext "Unable to create temporary file")
e_root_full=$(gettext "Zonepath root %s exists and contains data; remove or move aside prior to install.")
f_mkdir=$(gettext "Unable to create directory %s.")
f_chmod=$(gettext "Unable to chmod directory %s.")
f_chown=$(gettext "Unable to chown directory %s.")
f_hwcap_info=$(gettext "HWCAP: %s\n")
f_sanity_hwcap=$(gettext \
"The image was created with an incompatible libc.so.1 hwcap lofs mount.\n"\
" The zone will not boot on this platform. See the zone's\n"\
" documentation for the recommended way to create the archive.")
m_analyse_archive=$(gettext "Analysing the archive")
not_readable=$(gettext "Cannot read file '%s'")
not_flar=$(gettext "Input is not a flash archive")
bad_flar=$(gettext "Flash archive is a corrupt")
bad_zfs_flar=$(gettext "Flash archive contains a ZFS send stream.\n\tRecreate the flar using the -L option with cpio or pax.")
f_unpack_failed=$(gettext "Unpacking the archive failed")
unknown_archiver=$(gettext "Archiver %s is not supported")
cmd_not_exec=$(gettext "Required command '%s' not executable!")
#
# Exit values used by the script, as #defined in <sys/zone.h>
#
# ZONE_SUBPROC_OK
# ===============
# Installation was successful
#
# ZONE_SUBPROC_USAGE
# ==================
# Improper arguments were passed, so print a usage message before exiting
#
# ZONE_SUBPROC_NOTCOMPLETE
# ========================
# Installation did not complete, but another installation attempt can be
# made without an uninstall
#
# ZONE_SUBPROC_FATAL
# ==================
# Installation failed and an uninstall will be required before another
# install can be attempted
#
ZONE_SUBPROC_OK=0
ZONE_SUBPROC_USAGE=253
ZONE_SUBPROC_NOTCOMPLETE=254
ZONE_SUBPROC_FATAL=255
|