This repository has been archived by the owner on Jul 17, 2023. It is now read-only.
forked from lzap/snap-guest
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprepare-image.pl
executable file
·144 lines (126 loc) · 4.05 KB
/
prepare-image.pl
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
#!/usr/bin/perl
use strict;
use Sys::Guestfs;
my $g = Sys::Guestfs->new ();
# For debugging.
#$g->set_trace (1);
# Add the disk and launch.
$g->add_drive_opts ($ENV{TARGET_IMG}, format => "qcow2");
$g->launch ();
# Inspect for OSes.
my @roots = $g->inspect_os ();
die "no operating system was found" if @roots == 0;
die "dual/multi-boot operating system not supported" if @roots > 1;
my $root = $roots[0];
# Get OS and version.
my $type = $g->inspect_get_type ($root);
my $distro = $g->inspect_get_distro ($root);
my $major = $g->inspect_get_major_version ($root);
my $minor = $g->inspect_get_minor_version ($root);
print "OS: $type $distro $major $minor\n";
# Mount up the operating system disks.
my %mps = $g->inspect_get_mountpoints ($root);
my @mps = sort { length $a <=> length $b } (keys %mps);
for my $mp (@mps) {
eval { $g->mount ($mps{$mp}, $mp) }
}
print "Disable fsck startup check for all volumes\n";
my $file;
my $content;
$file = "/etc/fstab";
$content = $g->read_file ($file);
$content =~ s/[0-9]$/0/g;
$g->write ($file, $content);
print "Configuring message of the day\n";
$content = "\n\nSnap-guest box from $ENV{SOURCE_NAME} on " . `date` . "\n\n";
$g->write ("/etc/motd", $content);
if ($ENV{LOOPBACK_HOSTNAME} == 1) {
print "Configuring /etc/hosts file\n";
$file = "/etc/hosts";
$content = $g->read_file ($file);
$content =~ s/$ENV{SOURCE_NAME}/localbox/g;
$content .= "\n127.0.0.1 $ENV{TARGET_HOSTNAME} $ENV{TARGET_NAME}\n";
$g->write ($file, $content);
}
if ($distro eq "debian" || $distro eq "ubuntu") {
print "Setting up for Debian\n";
# nothing
}
elsif ($distro =~ m/^(fedora|rhel|redhat-based|centos|scientificlinux)$/) {
print "Setting up for Fedora / RHEL\n";
my $device = "eth0";
$file = "/etc/sysconfig/network-scripts/ifcfg-$device";
if (!$g->exists ($file)) {
$device = "ens3";
$file = "/etc/sysconfig/network-scripts/ifcfg-$device";
}
if ($ENV{STATIC_IPADDR}) {
print "Setting static network configuration\n";
$content = "DEVICE=$device\n";
$content .= "ONBOOT=yes\n";
$content .= "BOOTPROTO=static\n";
$content .= "HWADDR=$ENV{MAC}\n";
$content .= "IPADDR=$ENV{STATIC_IPADDR}\n";
$content .= "NETMASK=$ENV{STATIC_NETMASK}\n";
$content .= "GATEWAY=$ENV{STATIC_GATEWAY}\n";
} else {
print "Setting MAC address\n";
$content = $g->read_file ($file);
$content =~ s/HWADDR=.*/HWADDR=$ENV{MAC}/g;
}
$g->write ($file, $content);
print "Setting hostname\n";
if ($major >= 18 || ($distro eq "rhel" || $distro eq "centos") && $major >= 7) {
$g->write ("/etc/hostname", $ENV{TARGET_HOSTNAME});
} else {
$file = "/etc/sysconfig/network";
my @lines = $g->read_lines ($file);
my $found_it = 0;
foreach (@lines) {
s/HOSTNAME=.*/HOSTNAME=$ENV{TARGET_HOSTNAME}/;
$found_it = 1;
}
if (!$found_it) {
push @lines, "# added by snap-guest", "HOSTNAME=$ENV{TARGET_HOSTNAME}";
}
$g->write ($file, join ("\n", @lines) . "\n");
}
}
elsif($distro eq "sles") {
print "Setting up for SLES\n";
print "Setting hostname\n";
$g->write ("/etc/hostname", $ENV{TARGET_HOSTNAME});
}
if ($ENV{SWAP}) {
print "Adding swap file\n";
$file = "/etc/fstab";
$content = $g->read_file ($file);
$content .= "\n/dev/vdb none swap swap 0 0\n";
$g->write ($file, $content);
}
if ($ENV{COMMAND}) {
print "Preparing fristboot command and tt watch progress alias\n";
$content = "pushd /root\n$ENV{COMMAND}\npopd\n";
$g->write ("/root/firstboot.sh", $content);
$file = "/etc/rc.d/rc.local";
if ($g->exists ($file)) {
$content = $g->read_file ($file);
} else {
$content = "#!/bin/bash\n";
}
$content .= "\nbash -x /root/firstboot.sh 2>&1 | /usr/bin/tee /root/firstboot.log\n";
$g->write ($file, $content);
$g->chmod (0755, $file);
$file = "/root/.bashrc";
if ($g->exists ($file)) {
$content = $g->read_file ($file);
} else {
$content = "";
}
$content .= "\n# snap-guest\nalias tt=\"tail -f -n500 /root/firstboot.log\"\n\n";
$g->write ($file, $content);
}
# Shutdown the appliance cleanly.
$g->umount_all ();
$g->shutdown ();
$g->close ();