|
18 | 18 | # |
19 | 19 | # This scripts before ssh.service but after cloud-early-config |
20 | 20 |
|
| 21 | +. /lib/lsb/init-functions |
| 22 | + |
21 | 23 | log_it() { |
22 | 24 | echo "$(date) $@" >> /var/log/cloud.log |
23 | 25 | log_action_msg "$@" |
|
47 | 49 |
|
48 | 50 | CMDLINE=/var/cache/cloud/cmdline |
49 | 51 | TYPE=$(grep -Po 'type=\K[a-zA-Z]*' $CMDLINE) |
| 52 | + |
| 53 | +# Execute cloud-init if user data is present |
| 54 | +run_cloud_init() { |
| 55 | + if [ ! -f "$CMDLINE" ]; then |
| 56 | + log_it "No cmdline file found, skipping cloud-init execution" |
| 57 | + return 0 |
| 58 | + fi |
| 59 | + |
| 60 | + local encoded_userdata=$(grep -Po 'userdata=\K[^[:space:]]*' "$CMDLINE" || true) |
| 61 | + if [ -z "$encoded_userdata" ]; then |
| 62 | + log_it "No user data found in cmdline, skipping cloud-init execution" |
| 63 | + return 0 |
| 64 | + fi |
| 65 | + |
| 66 | + log_it "User data detected, setting up and running cloud-init" |
| 67 | + |
| 68 | + # Update cloud-init config to use NoCloud datasource |
| 69 | + cat <<EOF > /etc/cloud/cloud.cfg.d/cloudstack.cfg |
| 70 | +#cloud-config |
| 71 | +datasource_list: ['NoCloud'] |
| 72 | +network: |
| 73 | + config: disabled |
| 74 | +manage_etc_hosts: false |
| 75 | +manage_resolv_conf: false |
| 76 | +users: [] |
| 77 | +disable_root: false |
| 78 | +ssh_pwauth: false |
| 79 | +cloud_init_modules: |
| 80 | + - migrator |
| 81 | + - seed_random |
| 82 | + - bootcmd |
| 83 | + - write-files |
| 84 | + - growpart |
| 85 | + - resizefs |
| 86 | + - disk_setup |
| 87 | + - mounts |
| 88 | + - rsyslog |
| 89 | +cloud_config_modules: |
| 90 | + - locale |
| 91 | + - timezone |
| 92 | + - runcmd |
| 93 | +cloud_final_modules: |
| 94 | + - scripts-per-once |
| 95 | + - scripts-per-boot |
| 96 | + - scripts-per-instance |
| 97 | + - scripts-user |
| 98 | + - final-message |
| 99 | + - power-state-change |
| 100 | +EOF |
| 101 | + |
| 102 | + # Set up user data files (reuse the function from init.sh) |
| 103 | + mkdir -p /var/lib/cloud/seed/nocloud |
| 104 | + |
| 105 | + # Decode and potentially decompress user data |
| 106 | + local decoded_userdata |
| 107 | + decoded_userdata=$(echo "$encoded_userdata" | base64 -d 2>/dev/null) |
| 108 | + if [ $? -ne 0 ] || [ -z "$decoded_userdata" ]; then |
| 109 | + log_it "ERROR: Failed to decode base64 user data" |
| 110 | + return 1 |
| 111 | + fi |
| 112 | + |
| 113 | + # Write user data |
| 114 | + echo "$decoded_userdata" > /var/lib/cloud/seed/nocloud/user-data |
| 115 | + chmod 600 /var/lib/cloud/seed/nocloud/user-data |
| 116 | + |
| 117 | + # Create meta-data |
| 118 | + local instance_name=$(grep -Po 'name=\K[^[:space:]]*' "$CMDLINE" || hostname) |
| 119 | + cat > /var/lib/cloud/seed/nocloud/meta-data << EOF |
| 120 | +instance-id: $instance_name |
| 121 | +local-hostname: $instance_name |
| 122 | +EOF |
| 123 | + chmod 644 /var/lib/cloud/seed/nocloud/meta-data |
| 124 | + |
| 125 | + log_it "User data files created, executing cloud-init..." |
| 126 | + |
| 127 | + # Clean any previous cloud-init state |
| 128 | + cloud-init clean --logs |
| 129 | + |
| 130 | + # Run cloud-init stages manually |
| 131 | + cloud-init init --local && \ |
| 132 | + cloud-init init && \ |
| 133 | + cloud-init modules --mode=config && \ |
| 134 | + cloud-init modules --mode=final |
| 135 | + |
| 136 | + local cloud_init_result=$? |
| 137 | + if [ $cloud_init_result -eq 0 ]; then |
| 138 | + log_it "Cloud-init executed successfully" |
| 139 | + else |
| 140 | + log_it "ERROR: Cloud-init execution failed with exit code: $cloud_init_result" |
| 141 | + fi |
| 142 | + |
| 143 | + return $cloud_init_result |
| 144 | +} |
| 145 | + |
50 | 146 | if [ "$TYPE" == "router" ] || [ "$TYPE" == "vpcrouter" ] || [ "$TYPE" == "dhcpsrvr" ] |
51 | 147 | then |
52 | 148 | if [ -x /opt/cloud/bin/update_config.py ] |
|
71 | 167 | systemctl disable --now --no-block $svc |
72 | 168 | done |
73 | 169 |
|
| 170 | +run_cloud_init |
| 171 | + |
74 | 172 | date > /var/cache/cloud/boot_up_done |
0 commit comments