NixOS / nixops

NixOps is a tool for deploying to NixOS machines in a network or cloud.
https://nixos.org/nixops
GNU Lesser General Public License v3.0
1.84k stars 363 forks source link

Ability to resize virtualbox "root" disk #100

Open zefhemel opened 11 years ago

zefhemel commented 11 years ago

The default partition in virtualbox runs out of space quite quickly, so I have to destroy and recreate it. It would be great to have a deployment.virtualbox attribute for this.

alexmingoia commented 9 years ago

There is an option to specify the base image size here: https://github.com/NixOS/nixpkgs/blob/0761f81da71fc6a940c7f51129b6c7717db78e87/nixos/modules/virtualisation/virtualbox-image.nix#L11-L22 but I'm not exactly sure how to set that option in the nixops configs...

Did you ever figure this out?

expipiplus1 commented 8 years ago

A command to get the path to the .vdi file could be used in conjunction with VBoxManage modifyhd --resize

Warbo commented 8 years ago

I've used the following to successfully resize some nixops VDIs, in case the commands are helpful:

#!/usr/bin/env bash                                                                                                                                            
set -e
set -o pipefail

STOPPED=0
for MACHINE in hydra slave1
do
  echo "Looking for '$MACHINE' VM" 1>&2

  VMUUID=$(vboxmanage list vms | grep -o "nixops-[^ ]*-$MACHINE")

  echo "Found VM ID: $VMUUID" 1>&2

  echo "Looking for disk" 1>&2

  DISK=$(vboxmanage showvminfo "$VMUUID" | grep "^SATA" | grep -o "/.*\.vdi")

  echo "Found disk '$DISK'" 1>&2

  SIZE=$(vboxmanage showhdinfo "$DISK" | grep "^Capacity:" | grep -o "[0-9]* MBytes" | grep -o "[0-9]*")

  echo "Has size '$SIZE'" 1>&2

  DESIRED=100000

  if [[ "$SIZE" -lt "$DESIRED" ]]
  then
    echo "Size is too small, resizing" 1>&2

    if [[ "$STOPPED" -eq 0 ]]
    then
      echo "Stopping hydra deployment" 1>&2
      STOPPED=1
      nixops stop
    fi

    echo "Resizing" 1>&2
    vboxmanage modifyhd "$DISK" --resize "$DESIRED"
  else
    echo "Disk doesn't need resizing" 1>&2
  fi
done

if [[ "$STOPPED" -eq 1 ]]
then
    echo "Redeploying" 1>&2
    ./deploy.sh
fi

However, this only affects the disk size; I then had to resize the partition and grow the filesystem (I did this with GParted on a live CD). As long as NixOps is stopped, it doesn't seem to mind (i.e. it's happy to use the resized drive without complaining)

expipiplus1 commented 8 years ago

To complement @Warbo's instructions here's what I wrote for our hydra server's manual

Resizing the disk

If more space is needed, one can resize the disk.

Assuming the deployment is called hydra

resize the physical disk

Find the disk file.

export DISK_FILE=$(nixops export -d hydra | nix-shell -p jq --command "jq -r '..|.\"virtualbox.disks\"?|select(.!=null)' | jq -r .disk1.path")
nixops stop -d hydra
VBoxManage modifyhd --resize 64000 $DISK_FILE
nixops start -d hydra

Resize the partition

nixops ssh -d hydra hydra -- fdisk -u /dev/sda

Send these instructions to fdisk

Load the new partition table (If this doesn't work run nixops reboot -d hydra)

nixops ssh -d hydra hydra -- partx /dev/sda

Expand the file system

nixops ssh -d hydra hydra -- resize2fs /dev/sda1
nixops ssh -d hydra hydra -- df -h
echo good job 👍