관리 API 서버의 kubeconfig 파일 경로를 사용하여 이 안내의 MANAGEMENT_API_SERVER를 바꿉니다.
권한 및 액세스 요청
이 페이지에 나열된 작업을 수행하려면 프로젝트 VirtualMachine 관리자 역할이 있어야 합니다. 액세스 권한을 확인하거나 프로젝트 IAM 관리자가 VM이 있는 프로젝트의 네임스페이스에서 프로젝트 VirtualMachine 관리자 (project-vm-admin) 역할을 할당하도록 단계를 따르세요.
시작 스크립트 전달
bash 또는 비bash 스크립트를 시작 스크립트로 사용할 수 있습니다. 이렇게 하려면 스크립트 인터프리터를 나타내기 위해 스크립트 시작 부분에 #!/bin/…을 포함합니다.
예를 들어 Python 3 시작 스크립트를 사용하려면 스크립트 시작 부분에 #! /usr/bin/python3를 추가합니다.
Google Distributed Cloud (GDC) 에어 갭은 각 시작 스크립트의 이름을 기준으로 시작 스크립트를 알파벳순으로 실행합니다.
다음 표에는 스크립트 크기에 따라 사용할 스크립트 형식이 나와 있습니다.
스크립트 크기
스크립트 형식
최대 2,048바이트 스크립트
텍스트 삭제
2,048바이트를 초과하는 스크립트
Kubernetes 보안 비밀
시작 스크립트 정의
시작 스크립트를 사용하려면 VM spec 필드에 startupScripts 필드를 추가해야 합니다. 이 필드 내에서 여러 시작 스크립트를 일반 텍스트 또는 Kubernetes 보안 비밀로 지정할 수 있습니다.
다음 예시에서는 시작 스크립트를 일반 텍스트와 Kubernetes 보안 비밀로 지정합니다.
부팅 디스크의 크기입니다(예: 20G).
이 값은 항상 부팅 디스크 이미지의 minimumDiskSize보다 크거나 같아야 합니다.
BOOT_DISK_AUTO_DELETE
true 또는 false입니다. VM 인스턴스가 삭제될 때 부팅 디스크가 자동으로 삭제되는지 여부를 나타냅니다.
MACHINE_TYPE
새 VM의 사전 정의된 머신 유형입니다. 사용 가능한 머신 유형을 선택하려면 다음 명령어를 실행하세요.
kubectl --kubeconfig MANAGEMENT_API_SERVER get virtualmachinetype.virtualmachine.gdc.goog --namespace vm-system
시작 스크립트로 기존 VM 업데이트
시작 스크립트로 기존 VM을 업데이트할 수도 있습니다. 업데이트를 실행하기 전에 VM을 종료해야 합니다.
VM 속성을 업데이트하는 단계를 따라 실행할 시작 스크립트로 spec 필드를 업데이트합니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-09-04(UTC)"],[[["\u003cp\u003eStartup scripts in a virtual machine (VM) are used to perform tasks during the VM's start-up process, and can be either bash or non-bash scripts by including the \u003ccode\u003e#!/bin/...\u003c/code\u003e script interpreter indicator.\u003c/p\u003e\n"],["\u003cp\u003eStartup scripts can be defined as clear text for scripts up to 2048 bytes, or as a Kubernetes secret for larger scripts, which requires matching \u003ccode\u003escriptSecretRef\u003c/code\u003e names with the \u003ccode\u003emetadata.name\u003c/code\u003e field within the Kubernetes secret.\u003c/p\u003e\n"],["\u003cp\u003eTo utilize startup scripts, the \u003ccode\u003estartupScripts\u003c/code\u003e field must be added to the VM's \u003ccode\u003espec\u003c/code\u003e field, allowing for the specification of multiple scripts as either clear text or Kubernetes secrets.\u003c/p\u003e\n"],["\u003cp\u003eTo create a VM with a startup script, you must have the Project VirtualMachine Admin role, create the script (as clear text or a Kubernetes secret), and then add it to the \u003ccode\u003espec\u003c/code\u003e field when creating or updating the VM.\u003c/p\u003e\n"],["\u003cp\u003eStartup script logs can be viewed inside the guest VM by running the \u003ccode\u003esudo journalctl -u cloud-final\u003c/code\u003e command.\u003c/p\u003e\n"]]],[],null,["# Use a startup script on Linux VMs\n\nA startup script performs tasks during the start-up process for a virtual machine\n(VM). This page provides you with the steps to follow for using startup scripts\non VM instances.\n\n\nBefore you begin\n----------------\n\nTo use `gdcloud` command-line interface (CLI) commands, ensure that you have downloaded, installed,\nand configured the [`gdcloud` CLI](/distributed-cloud/hosted/docs/latest/gdch/resources/gdcloud-overview).\nAll commands for Distributed Cloud use the `gdcloud` or\n`kubectl` CLI, and require an operating system (OS) environment.\n\n### Get the kubeconfig file path\n\nTo run commands against the Management API server, ensure you have the following\nresources:\n\n1. [Sign in and generate](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/iam/sign-in#cli) the\n kubeconfig file for the Management API server if you don't have one.\n\n2. Use the path to the kubeconfig file of the Management API server to replace\n \u003cvar translate=\"no\"\u003eMANAGEMENT_API_SERVER\u003c/var\u003e in these instructions.\n\n### Request permissions and access\n\nTo perform the tasks listed in this page, you must have the Project\nVirtualMachine Admin role. Follow the steps to either\n[verify](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vms/preparation#verify-user-access)\nyour access or have your Project IAM Admin\n[assign](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vms/preparation)\nyou the Project VirtualMachine Admin (`project-vm-admin`) role in the namespace\nof the project where the VM resides.\n\nPass a startup script\n---------------------\n\nYou can use either bash or non-bash scripts as startup scripts. To do so,\ninclude `#!/bin/...` at the beginning of the script to indicate the script interpreter.\nFor example, to use a Python 3 startup script, add `#! /usr/bin/python3` to the\nbeginning of the script.\n\nGoogle Distributed Cloud (GDC) air-gapped runs startup scripts in alphabetical order,\nbased on the name of each startup script.\n\nThe following table shows the script format to use based on your script size:\n\n### Define a startup script\n\nTo use a startup script you must add the `startupScripts` field to the VM `spec`\nfield. Within this field you can specify multiple startup scripts as clear\ntext or as a Kubernetes secret.\n\nThe following example specifies the startup scripts as clear text and a\nKubernetes secret: \n\n apiVersion: virtualmachine.gdc.goog/v1\n kind: VirtualMachine\n metadata:\n name: \"my-vm\"\n spec:\n ...\n startupScripts:\n - name: hello-world\n script: |\n #!/bin/bash\n echo hello\n - name: add-user\n scriptSecretRef:\n name: add-user\n ---\n\n apiVersion: v1\n kind: Secret\n type: Opaque\n metadata:\n name: add-user\n data: \n script:\n IyEvYmluL2Jhc2gKYWRkdXNlciB1c2VyCg==\n\nReview the following considerations:\n\n- The startup script runs on *every boot*.\n- The startup script has default root privileges.\n- In the Kubernetes secret, the name of the `scriptSecretRef` in the VM `spec` must match the `metadata.name` field.\n- In the Kubernetes secret, specify the startup script content by adding a key `script` to the `data` field.\n\nCreate a new VM with a startup script\n-------------------------------------\n\nThese instructions apply regardless of what you use as the image to create your\nVM. Complete the following:\n\n1. To create a startup script as a Kubernetes secret, run:\n\n cat \u003c\u003cEOF \u003e\u003e\u003cvar translate=\"no\"\u003eFILE_NAME\u003c/var\u003e\n \u003cvar translate=\"no\"\u003eSTARTUP_SCRIPT_CONTENT\u003c/var\u003e\n EOF\n\n kubectl --kubeconfig \u003cvar translate=\"no\"\u003eMANAGEMENT_API_SERVER\u003c/var\u003e create secret -n \u003cvar translate=\"no\"\u003ePROJECT\u003c/var\u003e generic \u003cvar translate=\"no\"\u003eSECRET_NAME\u003c/var\u003e --from-file=script=\u003cvar translate=\"no\"\u003eFILE_NAME\u003c/var\u003e\n\n rm \u003cvar translate=\"no\"\u003eFILE_NAME\u003c/var\u003e\n\n2. Reference the steps to create a VM as described on the\n [create a VM](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vms/create-a-vm/create-and-start) page.\n At [step one](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vms/create-a-vm/create-and-start#create-vm-from-provided-image) add the startup script or scripts to the `spec` field before you run the command to\n create a VM.\n\n This example defines a startup script with both clear text and a Kubernetes\n secret: \n\n apiVersion: virtualmachine.gdc.goog/v1\n kind: VirtualMachine\n metadata:\n name: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003eVM_NAME\u003c/span\u003e\u003c/var\u003e\n namespace: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003ePROJECT\u003c/span\u003e\u003c/var\u003e\n spec:\n ...\n startupScripts:\n - name: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003eCLEAR_TEXT_SCRIPT_NAME\u003c/span\u003e\u003c/var\u003e\n script: |\n #!/bin/bash\n \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-no\"\u003eCLEAR_TEXT_SCRIPT\u003c/span\u003e\u003c/var\u003e\n - name: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003eSECRET_SCRIPT_NAME\u003c/span\u003e\u003c/var\u003e\n scriptSecretRef:\n name: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003eSECRET_NAME\u003c/span\u003e\u003c/var\u003e\n\n These variables are defined as follows:\n\n3. Proceed with the following steps to create a VM.\n\n The following is an example of how to create a VM with startup scripts\n that adds a new user using a Kubernetes secret and clear text. \n\n kubectl --kubeconfig \u003cvar translate=\"no\"\u003eMANAGEMENT_API_SERVER\u003c/var\u003e \\\n apply -n \u003cvar translate=\"no\"\u003ePROJECT\u003c/var\u003e -f - \u003c\u003cEOF\n apiVersion: virtualmachine.gdc.goog/v1\n kind: VirtualMachineDisk\n metadata:\n name: \u003cvar translate=\"no\"\u003eVM_BOOT_DISK_NAME\u003c/var\u003e\n spec:\n source:\n image:\n name: \u003cvar translate=\"no\"\u003eBOOT_DISK_IMAGE_NAME\u003c/var\u003e\n namespace: vm-system\n size: \u003cvar translate=\"no\"\u003eBOOT_DISK_SIZE\u003c/var\u003e\n ---\n apiVersion: v1\n kind: Secret\n type: Opaque\n metadata:\n name: add-user\n data: \n script:\n IyEvYmluL2Jhc2gKYWRkdXNlciB1c2VyCg==\n ---\n apiVersion: virtualmachine.gdc.goog/v1\n kind: VirtualMachine\n metadata:\n name: \u003cvar translate=\"no\"\u003eVM_NAME\u003c/var\u003e\n spec:\n compute:\n virtualMachineType: \u003cvar translate=\"no\"\u003eMACHINE_TYPE\u003c/var\u003e\n disks:\n - virtualMachineDiskRef:\n name: \u003cvar translate=\"no\"\u003eVM_BOOT_DISK_NAME\u003c/var\u003e\n boot: true\n autoDelete: \u003cvar translate=\"no\"\u003eBOOT_DISK_AUTO_DELETE\u003c/var\u003e\n startupScripts:\n - name: add-user\n scriptSecretRef:\n name: add-user\n - name: add-to-sudoers\n script: |\n #!/bin/bash\n usermod -aG sudo user\n EOF\n\n In the example the variables are defined as follows.\n\nUpdate an existing VM with a startup script\n-------------------------------------------\n\nYou can also update an existing VM with a startup script. The VM must be shut\ndown before you perform the update.\n\nFollow the steps to [update VM properties](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vms/manage-vms/operations-and-lifecycle/update-vm-properties#update-properties)\nand update the `spec` field with the startup script that you would like to run.\n\nView the output of a startup script\n-----------------------------------\n\n1. Follow the steps to [connect to a VM](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vms/connect-to-vm/connect-to-a-vm).\n2. Run the following command inside the guest VM to get the logs of the\n startup script that you ran:\n\n sudo journalctl -u cloud-final\n\n The startup script logs begin with the following: \n\n Started to run the command: /var/lib/google/startup-scripts/\u003cscript-name\u003e ..."]]