add a new disk to your Google Cloud Platform (GCP) virtual machine (VM)

Okay, you’ve added a new disk to your Google Cloud Platform (GCP) virtual machine (VM), and now you need to format and set it up so your operating system can use it. The steps differ slightly depending on whether your VM is running Linux or Windows.

Here’s a guide for both:

For a Linux VM:

  1. Connect to your VM:

    • Use SSH to connect to your Linux instance. You can do this via the Google Cloud Console (clicking the “SSH” button next to your VM instance) or using your own SSH client.
  2. Identify the new disk:

    • Once connected, you need to find the device name for the new disk. It’s often /dev/sdb, /dev/sdc, etc., if you already have one disk (/dev/sda is usually the boot disk). You can list block devices with commands like:
      lsblk

      or

      sudo fdisk -l
    • Look for a disk that matches the size of the one you added and doesn’t have any partitions listed under it (or shows as unformatted). Let’s assume your new disk is /dev/sdb for the following commands. Be very careful to select the correct disk, as formatting the wrong one can lead to data loss.
  3. Partition the disk (Optional but Recommended for GPT):

    • While you can format the whole disk directly, it’s good practice to create a partition table. For disks larger than 2TB, GPT is recommended.
      sudo parted /dev/sdb mklabel gpt
    • Then, create a partition that uses the whole disk:
      sudo parted -a opt /dev/sdb mkpart primary ext4 0% 100%

      This creates a primary partition formatted (or rather, typed) as ext4 spanning the entire disk. The actual formatting comes next. After this, your new partition might be named /dev/sdb1. Use lsblk again to confirm. If so, use /dev/sdb1 in the subsequent steps. If you don’t partition, you’d use /dev/sdb. For simplicity, the following steps will assume /dev/DEVICE_NAME which could be /dev/sdb (if not partitioning) or /dev/sdb1 (if you created a partition).

  4. Format the disk (or partition):

    • Choose a file system. ext4 is common, or xfs is also a good choice for large disks.
    • To format with ext4:
      sudo mkfs.ext4 -m 0 -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/DEVICE_NAME
    • Or, to format with xfs:
      sudo mkfs.xfs /dev/DEVICE_NAME
    • Replace /dev/DEVICE_NAME with the actual device name (e.g., /dev/sdb or /dev/sdb1). The discard option enables TRIM for better performance on SSDs.
  5. Create a mount point:

    • This is the directory where the disk’s file system will be accessible.
      sudo mkdir -p /mnt/disks/mynewdisk
    • You can replace mynewdisk with your preferred directory name.
  6. Mount the disk:

    • Mount the formatted disk to the directory you created:
      sudo mount -o discard,defaults /dev/DEVICE_NAME /mnt/disks/mynewdisk
  7. Configure automatic mounting on boot (via /etc/fstab):

    • To ensure the disk is automatically mounted every time your VM starts, you need to add an entry to the /etc/fstab file.
    • First, get the UUID (Universally Unique Identifier) of your disk/partition:
      sudo blkid /dev/DEVICE_NAME

      This will output something like: /dev/DEVICE_NAME: UUID="your-disk-uuid" TYPE="ext4" ... Copy the UUID value.

    • It’s a good idea to back up your current fstab file before editing:
      sudo cp /etc/fstab /etc/fstab.backup
    • Open /etc/fstab with a text editor (like nano or vim):
      sudo nano /etc/fstab
    • Add a new line at the end of the file with the following format. Replace your-disk-uuid, /mnt/disks/mynewdisk, and ext4 (if you used a different file system like xfs) accordingly:
      UUID=your-disk-uuid /mnt/disks/mynewdisk ext4 discard,defaults,nofail 0 2

      The nofail option is important; it allows the system to boot even if the disk is not present or fails to mount.

    • Save and close the file.
    • You can test if the fstab entry is correct by unmounting and then mounting all entries in /etc/fstab:
      sudo umount /mnt/disks/mynewdisk
      sudo mount -a

      If there are no errors, it’s configured correctly.

  8. Set permissions (if needed):

    • If you need other users besides root to write to the disk, you might need to change the ownership or permissions of the mount point:
      sudo chmod a+w /mnt/disks/mynewdisk

      Or, change ownership to your user:

      sudo chown your_username:your_username /mnt/disks/mynewdisk

Your new disk should now be formatted, mounted, and ready to use on your Linux VM.

For a Windows VM:

  1. Connect to your VM:

    • Use RDP (Remote Desktop Protocol) to connect to your Windows instance. You can initiate this from the Google Cloud Console by clicking the “RDP” button next to your VM instance.
  2. Open Disk Management:

    • Once connected to your Windows desktop:
      • Right-click the Start button.
      • Select Disk Management.
  3. Initialize the Disk:

    • Disk Management will likely detect the new disk automatically and prompt you to initialize it.
    • You’ll be asked to choose a partition style:
      • GPT (GUID Partition Table): Recommended for disks larger than 2TB and for modern systems.
      • MBR (Master Boot Record): Older standard, generally not recommended for new disks unless you have specific compatibility reasons.
    • Select GPT (unless you have a specific reason not to) and click OK.
    • The disk will now show as “Online” but “Unallocated”.
  4. Create a New Simple Volume:

    • Right-click on the unallocated space of the new disk.
    • Select New Simple Volume…. This will open the “New Simple Volume Wizard”.
  5. Follow the New Simple Volume Wizard:

    • Specify Volume Size: By default, it will use the maximum available space. You can adjust this if you want to create multiple partitions. Click Next.
    • Assign Drive Letter or Path: Choose an available drive letter (e.g., D:, E:) or mount it to an empty NTFS folder. Click Next.
    • Format Partition:
      • File system: Choose NTFS (recommended for Windows).
      • Allocation unit size: Usually, “Default” is fine.
      • Volume label: Give your disk a descriptive name (e.g., “Data Disk”).
      • Check the box for Perform a quick format.
      • Click Next.
    • Completing the New Simple Volume Wizard: Review your settings and click Finish.
  6. Check Disk Status:

    • After the wizard completes the formatting process, the new disk will appear in Disk Management as a healthy volume with the drive letter and volume label you assigned.
    • It will also be accessible in File Explorer.

Your new disk is now formatted, mounted, and ready to use on your Windows VM.

Remember to always back up important data, regardless of where it’s stored.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *