# This example shows how to copy a local disk image to S3 and then extract the image onto EC2

# ******************  ON THE LOCAL COMPUTER *******************

# create a virtual disk that is 1 MB x 1200 in size = 1,258,291,200 Bytes
sudo dd if=/dev/zero of=vhd.img bs=1M count=1200
# format the disk using the ext4 filesystem
sudo mkfs.ext4 vhd.img
# mount the disk at "/mnt"
sudo mount -t auto -o loop vhd.img /mnt
# check that the disk is mounted
df -h
# create a hello file on the new virtual disk
cd /mnt
sudo echo "hello world !" > hello.txt
ls -l
cd
# unmount the virtual disk
sudo umount /mnt
# compress the disk
bzip2 vhd.img
# push the disk image to S3
aws s3 cp vhd.img.bz2 s3://tcss562-f21-images

exit

# ******************  ON THE AWS EC2 VM *******************

# with the awscli installed and configured
# download the image from S3
aws s3 cp s3://tcss562-f21-images/vhd.img.bz2 vhd.img.bz2
# uncompress the image
bzip2 -d vhd.img.bz2

# we need to calculate the number of sectors for the partition
# disk sectors are 512 bytes each
# divide the disk size by 512 to determine sectors
# sectors = 1258291200 / 512 = 2459648

# create a disk partition for this disk that is 2459648 sectors in size using the ephemeral drive or a newly mounted EBS volume
# that is unformatted

sudo fdisk /dev/nvme1n1

# in the interactive user interface select the following:

#----------------------------------------------------------------------------------------------
#Welcome to fdisk (util-linux 2.34).
#Changes will remain in memory only, until you decide to write them.
#Be careful before using the write command.
#
#Device does not contain a recognized partition table.
#Created a new DOS disklabel with disk identifier 0xe871155c.
#
#Command (m for help): n
#Partition type
#   p   primary (0 primary, 0 extended, 4 free)
#   e   extended (container for logical partitions)
#Select (default p): p
#Partition number (1-4, default 1): 1
#First sector (2048-97656249, default 2048): 2048
#Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-97656249, default 97656249): 2459648
#
#Created a new partition 1 of type 'Linux' and of size 1.2 GiB.
#
#Command (m for help): t
#Selected partition 1
#Hex code (type L to list all codes): 83
#Changed type of partition 'Linux' to 'Linux'.
#
#Command (m for help): p
#Disk /dev/nvme1n1: 46.58 GiB, 50000000000 bytes, 97656250 sectors
#Disk model: Amazon EC2 NVMe Instance Storage        
#Units: sectors of 1 * 512 = 512 bytes
#Sector size (logical/physical): 512 bytes / 512 bytes
#I/O size (minimum/optimal): 512 bytes / 512 bytes
#Disklabel type: dos
#Disk identifier: 0xe871155c
#
#Device         Boot Start     End Sectors  Size Id Type
#/dev/nvme1n1p1       2048 2459648 2457601  1.2G 83 Linux
#
#Command (m for help): w
#The partition table has been altered.
#Calling ioctl() to re-read partition table.
#Syncing disks.
#----------------------------------------------------------------------------------------------
# now check if the partition has been created.  it should be listed as /dev/nvme1n1p1:
ls /dev/nvme1n1*

# now copy the data to the partition
sudo dd if=vhd.img of=/dev/nvme1n1p1

# mount the disk
sudo mount /dev/nvme1n1p1 /mnt

# and check if the hello file is there
cat /mnt/hello.txt

# notice that we were able to copy the disk image to the cloud
# and we never had to format the cloud disk
# we copied the file system from our local disk to the cloud disk








